Skip to content

Commit

Permalink
Fix StockGenerator_Clothes patch performance
Browse files Browse the repository at this point in the history
The current StockGenerator_Clothes_HandlesThingDef_Patch implementation
imposes a prohibitive performance overhead when a trader is active on
the map due to it iterating across the entire def database every time
in a high call frequency patch. As a fix, cache the set of defs to be
matched at the patching stage.

This patch was actually a no-op because __result was not declared as a
reference parameter, so the original return value wouldn't have been
modified, so fix that as well.

Also make the build more portable by converting some reference path to
relative ones so that they do not assume a particular install directory
for the game.
  • Loading branch information
mszabo-wikia committed Dec 3, 2021
1 parent 6032f36 commit 10342a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ namespace PokeWorld
[HarmonyPatch("HandlesThingDef")]
public class StockGenerator_Clothes_HandlesThingDef_Patch
{
public static void Postfix(ThingDef __0, bool __result)
private static ISet<ThingDef> pwBelts;

public static void Prepare()
{
// Cache matching defs during patching to avoid prohibitive performance overhead when a trader is on the map (#23)
pwBelts = DefDatabase<ThingDef>.AllDefs.Where((ThingDef x) => x.thingCategories != null && x.thingCategories.Contains(DefDatabase<ThingCategoryDef>.GetNamed("PW_Belts"))).ToHashSet();

}

public static void Postfix(ThingDef __0, ref bool __result)
{
if(__result == true)
if(__result == true && pwBelts.Contains(__0))
{
if (DefDatabase<ThingDef>.AllDefs.Where((ThingDef x) => x.thingCategories != null && x.thingCategories.Contains(DefDatabase<ThingCategoryDef>.GetNamed("PW_Belts"))).Contains(__0))
{
__result = false;
}
__result = false;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/PokeWorld/PokeWorld/PokeWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<HintPath>..\..\..\Assemblies\1SettingsHelper.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>D:\Program Files (x86)\Steam\steamapps\common\RimWorld\RimWorldWin64_Data\Managed\Assembly-CSharp.dll</HintPath>
<HintPath>..\..\..\..\..\RimWorldWin64_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -51,7 +51,7 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>D:\Program Files (x86)\Steam\steamapps\common\RimWorld\RimWorldWin64_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>..\..\..\..\..\RimWorldWin64_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
Expand Down

0 comments on commit 10342a4

Please sign in to comment.