Skip to content

Commit

Permalink
Some refactoring and cleanup of HS2_Fix_UpdateWet; Added a warning lo…
Browse files Browse the repository at this point in the history
…g message if nulls are found
  • Loading branch information
ManlyMarco committed Feb 28, 2021
1 parent 9bf7398 commit 1e5916c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 56 deletions.
77 changes: 36 additions & 41 deletions 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
Expand All @@ -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 ;)

/// <summary>
/// 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 ;)
/// </summary>
[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;
}
}
}
18 changes: 4 additions & 14 deletions src/HS2_Fix_UpdateWet/HS2_Fix_UpdateWet.csproj
Expand Up @@ -9,11 +9,12 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>IllusionFixes</RootNamespace>
<AssemblyName>HS2_Fix_UpdateWet</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -26,13 +27,14 @@
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>embedded</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\bin\BepInEx\plugins\IllusionFixes\</OutputPath>
<DefineConstants>TRACE;HS2</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony, Version=2.2.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand All @@ -43,10 +45,6 @@
<HintPath>..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp.2020.5.29.2\lib\net46\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.2020.5.29.2\lib\net46\Assembly-CSharp-firstpass.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="BepInEx, Version=5.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\IllusionLibs.BepInEx.5.4.4\lib\net35\BepInEx.dll</HintPath>
<Private>False</Private>
Expand All @@ -57,12 +55,6 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.2018.4.11.2\lib\net46\UnityEngine.dll</HintPath>
<Private>False</Private>
Expand All @@ -87,11 +79,9 @@
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp.2020.5.29.2\build\IllusionLibs.HoneySelect2.Assembly-CSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp.2020.5.29.2\build\IllusionLibs.HoneySelect2.Assembly-CSharp.targets'))" />
<Error Condition="!Exists('..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.2020.5.29.2\build\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.2020.5.29.2\build\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.targets'))" />
<Error Condition="!Exists('..\..\packages\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.2018.4.11.2\build\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.2018.4.11.2\build\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.targets'))" />
<Error Condition="!Exists('..\..\packages\IllusionLibs.BepInEx.Harmony.2.2.0.1\build\IllusionLibs.BepInEx.Harmony.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\IllusionLibs.BepInEx.Harmony.2.2.0.1\build\IllusionLibs.BepInEx.Harmony.targets'))" />
</Target>
<Import Project="..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.2020.5.29.2\build\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.targets" Condition="Exists('..\..\packages\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.2020.5.29.2\build\IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass.targets')" />
<Import Project="..\..\packages\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.2018.4.11.2\build\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.targets" Condition="Exists('..\..\packages\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.2018.4.11.2\build\IllusionLibs.HoneySelect2.UnityEngine.CoreModule.targets')" />
<Import Project="..\..\packages\IllusionLibs.BepInEx.Harmony.2.2.0.1\build\IllusionLibs.BepInEx.Harmony.targets" Condition="Exists('..\..\packages\IllusionLibs.BepInEx.Harmony.2.2.0.1\build\IllusionLibs.BepInEx.Harmony.targets')" />
</Project>
1 change: 0 additions & 1 deletion src/HS2_Fix_UpdateWet/packages.config
Expand Up @@ -3,6 +3,5 @@
<package id="IllusionLibs.BepInEx" version="5.4.4" targetFramework="net471" />
<package id="IllusionLibs.BepInEx.Harmony" version="2.2.0.1" targetFramework="net471" />
<package id="IllusionLibs.HoneySelect2.Assembly-CSharp" version="2020.5.29.2" targetFramework="net471" />
<package id="IllusionLibs.HoneySelect2.Assembly-CSharp-firstpass" version="2020.5.29.2" targetFramework="net471" />
<package id="IllusionLibs.HoneySelect2.UnityEngine.CoreModule" version="2018.4.11.2" targetFramework="net471" />
</packages>

0 comments on commit 1e5916c

Please sign in to comment.