Skip to content

Commit

Permalink
[ME][KK/KKS] Remove coordinate properties when that coordinate is rem…
Browse files Browse the repository at this point in the history
…oved (#249)

When a coordinate was removed, the associated properties were never purged. This could cause some very bad card bloating, primarily when textures were involved.

This now purges those properties. It will also purge them on save, in case a card made before this purging has properties from slots that no longer exist. If these slots still exist however, it could still contain dead properties (e.g. you removed a slot and then added it back).
  • Loading branch information
RikkiBalboa authored Apr 30, 2024
1 parent 6a40091 commit 15e7606
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public class MaterialEditorCharaController : CharaCustomFunctionController
/// <param name="currentGameMode"></param>
protected override void OnCardBeingSaved(GameMode currentGameMode)
{
#if KK || KKS
//Always run on save to also purge them for cards made before this purging was implemented
PurgeUnusedCoordinates();
#endif
PurgeUnusedTextures();

if (RendererPropertyList.Count == 0 && MaterialFloatPropertyList.Count == 0 && MaterialKeywordPropertyList.Count == 0 && MaterialColorPropertyList.Count == 0 && MaterialTexturePropertyList.Count == 0 && MaterialShaderList.Count == 0 && MaterialCopyList.Count == 0)
Expand Down Expand Up @@ -2376,6 +2380,24 @@ protected void PurgeUnusedTextures()
}
}

#if KK || KKS

/// <summary>
/// Purge coordinate properties that reference a coordinate that no longer exists
/// </summary>
internal void PurgeUnusedCoordinates()
{
RendererPropertyList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
ProjectorPropertyList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
MaterialFloatPropertyList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
MaterialColorPropertyList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
MaterialKeywordPropertyList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
MaterialTexturePropertyList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
MaterialShaderList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
MaterialCopyList.RemoveAll(x => ChaControl.chaFile.coordinate.ElementAtOrDefault(x.CoordinateIndex) == null);
}
#endif

/// <summary>
/// Type of object, used for saving MaterialEditor data.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/MaterialEditor.Core/Core.MaterialEditor.Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,5 +591,16 @@ internal static void UncensorSelectorHookStudio(ChaControl chaControl)
{
chaControl.StartCoroutine(MaterialEditorPlugin.GetCharaController(chaControl).LoadData(false, false, false));
}

#if KK || KKS
internal static void RemoveCoordinateSlotHook()
{
if (MakerAPI.InsideAndLoaded)
{
var controller = MaterialEditorPlugin.GetCharaController(MakerAPI.GetCharacterControl());
controller.PurgeUnusedCoordinates();
}
}
#endif
}
}
15 changes: 14 additions & 1 deletion src/MaterialEditor.Core/Core.MaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace KK_Plugins.MaterialEditor
[BepInDependency(ExtendedSave.GUID, ExtendedSave.Version)]
#if !PH
[BepInDependency(Sideloader.Sideloader.GUID, Sideloader.Sideloader.Version)]
#elif KK || KKS
[BepInDependency("com.deathweasel.bepinex.moreoutfits", BepInDependency.DependencyFlags.SoftDependency)]
#endif
[BepInPlugin(PluginGUID, PluginName, PluginVersion)]
public partial class MaterialEditorPlugin : MaterialEditorAPI.MaterialEditorPluginBase
Expand Down Expand Up @@ -217,6 +219,17 @@ internal void Main()
harmony.Patch(method, new HarmonyMethod(typeof(Hooks).GetMethod(nameof(Hooks.UncensorSelectorHookStudio), AccessTools.all)));
}

#if KK || KKS
//Hook to delete properties of an outfit that gets removed
var moreOutfitsType = Type.GetType($"KK_Plugins.MoreOutfits.Plugin, {Constants.Prefix}_MoreOutfits");
if(moreOutfitsType != null)
{
var method = moreOutfitsType.GetMethod("RemoveCoordinateSlot", AccessTools.all);
if (method != null)
harmony.Patch(method, postfix: new HarmonyMethod(typeof(Hooks).GetMethod(nameof(Hooks.RemoveCoordinateSlotHook), AccessTools.all)));
}
#endif

StartCoroutine(LoadXML());
StartCoroutine(GetUncensorSelectorParts());

Expand Down Expand Up @@ -1020,4 +1033,4 @@ public static void ClearCache(GameObject gameObject)
Hooks.ClearCache(gameObject);
}
}
}
}

0 comments on commit 15e7606

Please sign in to comment.