Skip to content

Commit

Permalink
undo caching
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Dec 21, 2023
1 parent b6edd30 commit c941261
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions src/SpaceWarp.Core/Patching/PartOwnerComponentOnFixedUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,27 @@ namespace SpaceWarp.Patching;
[HarmonyPatch]
internal class PartOwnerComponentOnFixedUpdate
{
private class ReferenceBoolean
{
internal static ReferenceBoolean TRUE = new();
internal static ReferenceBoolean FALSE = new();
public static implicit operator ReferenceBoolean(bool b) => b ? TRUE : FALSE;
public static implicit operator bool(ReferenceBoolean b) => b == TRUE;
}

private static ConditionalWeakTable<PartOwnerComponent, ReferenceBoolean> AlreadyCachedOwners = new();
[HarmonyPatch(typeof(PartOwnerComponent), "OnFixedUpdate"), HarmonyPrefix]
private static bool PerformBackgroundCalculationsForRegisteredModules(double universalTime,
double deltaUniversalTime,
PartOwnerComponent __instance)
{
if (!AlreadyCachedOwners.TryGetValue(__instance, out var isModulePresent))
{
isModulePresent = false;
var hasPartModuleMethod = __instance.GetType().GetMethod("HasPartModule");
var isModulePresent = false;
var hasPartModuleMethod = __instance.GetType().GetMethod("HasPartModule");

if (hasPartModuleMethod != null)
if (hasPartModuleMethod != null)
{
// Go through each registered module and check if it's present in PartOwnerComponent modules
foreach (var moduleType in PartComponentModuleOverride.RegisteredPartComponentOverrides)
{
// Go through each registered module and check if it's present in PartOwnerComponent modules
foreach (var moduleType in PartComponentModuleOverride.RegisteredPartComponentOverrides)
var genericMethod = hasPartModuleMethod.MakeGenericMethod(moduleType);
if ((bool)genericMethod.Invoke(__instance, null))
{
var genericMethod = hasPartModuleMethod.MakeGenericMethod(moduleType);
if ((bool)genericMethod.Invoke(__instance, null))
{
isModulePresent = true;
break;
}
isModulePresent = true;
break;
}
}

AlreadyCachedOwners.Add(__instance, isModulePresent);
}

// If registered module is present, run the original 0.1.5 method that runs background resource checks
if (isModulePresent)
{
Expand Down

0 comments on commit c941261

Please sign in to comment.