From 1e5916caa6c30e5a104d98bff6fb8f30192fb78c Mon Sep 17 00:00:00 2001 From: ManlyMarco Date: Sun, 28 Feb 2021 17:42:37 +0100 Subject: [PATCH] Some refactoring and cleanup of HS2_Fix_UpdateWet; Added a warning log message if nulls are found --- src/HS2_Fix_UpdateWet/FixUpdateWet.cs | 77 +++++++++---------- .../HS2_Fix_UpdateWet.csproj | 18 +---- src/HS2_Fix_UpdateWet/packages.config | 1 - 3 files changed, 40 insertions(+), 56 deletions(-) diff --git a/src/HS2_Fix_UpdateWet/FixUpdateWet.cs b/src/HS2_Fix_UpdateWet/FixUpdateWet.cs index a32f75f..6e28e0f 100644 --- a/src/HS2_Fix_UpdateWet/FixUpdateWet.cs +++ b/src/HS2_Fix_UpdateWet/FixUpdateWet.cs @@ -1,9 +1,9 @@ using AIChara; using BepInEx; -using BepInEx.Logging; using Common; using HarmonyLib; using System.Linq; +using BepInEx.Logging; using UnityEngine; namespace IllusionFixes @@ -14,59 +14,54 @@ public class FixUpdateWet : BaseUnityPlugin public const string GUID = "HS2_Fix_UpdateWet"; public const string PluginName = "Fix UpdateWet Exceptions"; - private static FixUpdateWet Instance; + private static new ManualLogSource Logger; - private ManualLogSource Log => Logger; - - public void Start() + private void Awake() { - Instance = this; - PatchMe(); - } + Logger = base.Logger; - private static void PatchMe() - { - Harmony harmony = new Harmony(GUID); - harmony.PatchAll(typeof(FixUpdateWet)); + Harmony.CreateAndPatchAll(typeof(FixUpdateWet), GUID); } - // Some modded items have null renderers in the CmpHair or CmpClothes MB setup - // This is harmless everywhere but throws an error in ChaControl.UpdateWet which causes wet effects to not apply properly and a major logging - // driven performance hit - - // This fixes it by simply removing the non-existent renderer from the array which *I think* is entirely safe...doesn't seem to cause an issue - // I mean, the renderer isn't there anyway...hence it being null ;) - + /// + /// Some modded items have null renderers in the CmpHair or CmpClothes MB setup + /// This is harmless everywhere but throws an error in ChaControl.UpdateWet which causes wet effects to not apply properly and a major logging + /// driven performance hit + /// + /// This fixes it by simply removing the non-existent renderer from the array which *I think* is entirely safe...doesn't seem to cause an issue + /// I mean, the renderer isn't there anyway...hence it being null ;) + /// [HarmonyPrefix, HarmonyPatch(typeof(ChaControl), "UpdateWet")] - static void UpdateWetPreHook(ChaControl __instance) + private static void UpdateWetPreHook(ChaControl __instance) { // Cleanup broken renderers foreach (CmpHair hair in __instance.cmpHair) - { - if (hair != null && hair.rendHair != null && hair.rendHair.Contains(null)) - { - hair.rendHair = hair.rendHair.Where(r => r != null).ToArray(); - } - if (hair != null && hair.rendAccessory != null && hair.rendAccessory.Contains(null)) - { - hair.rendAccessory = hair.rendAccessory.Where(r => r != null).ToArray(); - } + { + if (hair == null) continue; + + if (ContainsNulls(hair.rendHair)) hair.rendHair = hair.rendHair.RemoveNulls(); + if (ContainsNulls(hair.rendAccessory)) hair.rendAccessory = hair.rendAccessory.RemoveNulls(); } foreach (CmpClothes clothes in __instance.cmpClothes) { - if (clothes != null && clothes.rendNormal01 != null && clothes.rendNormal01.Contains(null)) - { - clothes.rendNormal01 = clothes.rendNormal01.Where(r => r != null).ToArray(); - } - if (clothes != null && clothes.rendNormal02 != null && clothes.rendNormal02.Contains(null)) - { - clothes.rendNormal02 = clothes.rendNormal02.Where(r => r != null).ToArray(); - } - if (clothes != null && clothes.rendNormal03 != null && clothes.rendNormal03.Contains(null)) - { - clothes.rendNormal03 = clothes.rendNormal03.Where(r => r != null).ToArray(); - } + if (clothes == null) continue; + + if (ContainsNulls(clothes.rendNormal01)) clothes.rendNormal01 = clothes.rendNormal01.RemoveNulls(); + if (ContainsNulls(clothes.rendNormal02)) clothes.rendNormal02 = clothes.rendNormal02.RemoveNulls(); + if (ContainsNulls(clothes.rendNormal03)) clothes.rendNormal03 = clothes.rendNormal03.RemoveNulls(); + } + } + + private static bool ContainsNulls(Renderer[] renderers) + { + var containsNulls = renderers != null && renderers.Contains(null); + if (containsNulls) + { + Logger.LogWarning("Found null renderers in the CmpHair or CmpClothes MBs. " + + "This is most likely an issue with the last modded clothes you tried to use. " + + "The nulls will be removed but the mod might have issues anyways and should be fixed by the author."); } + return containsNulls; } } } diff --git a/src/HS2_Fix_UpdateWet/HS2_Fix_UpdateWet.csproj b/src/HS2_Fix_UpdateWet/HS2_Fix_UpdateWet.csproj index 8e71d4b..e9ed814 100644 --- a/src/HS2_Fix_UpdateWet/HS2_Fix_UpdateWet.csproj +++ b/src/HS2_Fix_UpdateWet/HS2_Fix_UpdateWet.csproj @@ -9,11 +9,12 @@ Properties IllusionFixes HS2_Fix_UpdateWet - v4.7.1 + v4.6 512 true + true @@ -26,13 +27,14 @@ false - pdbonly + embedded true ..\..\bin\BepInEx\plugins\IllusionFixes\ TRACE;HS2 prompt 4 true + true @@ -43,10 +45,6 @@ ..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp.2020.5.29.2\lib\net46\Assembly-CSharp.dll False - - ..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.2020.5.29.2\lib\net46\Assembly-CSharp-firstpass.dll - False - ..\..\packages\IllusionLibs.BepInEx.5.4.4\lib\net35\BepInEx.dll False @@ -57,12 +55,6 @@ - - - - - - ..\..\packages\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.2018.4.11.2\lib\net46\UnityEngine.dll False @@ -87,11 +79,9 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - \ No newline at end of file diff --git a/src/HS2_Fix_UpdateWet/packages.config b/src/HS2_Fix_UpdateWet/packages.config index f23e2ac..67a6b1c 100644 --- a/src/HS2_Fix_UpdateWet/packages.config +++ b/src/HS2_Fix_UpdateWet/packages.config @@ -3,6 +3,5 @@ - \ No newline at end of file