Skip to content

Commit

Permalink
Cache part component owners we already checked.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Dec 21, 2023
1 parent 2f9458e commit ca4b2cf
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/SpaceWarp.Core/Patching/PartOwnerComponentOnFixedUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@ namespace SpaceWarp.Patching;
[HarmonyPatch]
internal class PartOwnerComponentOnFixedUpdate
{
private static Dictionary<PartOwnerComponent, bool> AlreadyCachedOwners = new();
[HarmonyPatch(typeof(PartOwnerComponent), "OnFixedUpdate"), HarmonyPrefix]
private static bool PerformBackgroundCalculationsForRegisteredModules(double universalTime,
double deltaUniversalTime,
PartOwnerComponent __instance)
{
var isModulePresent = false;
var hasPartModuleMethod = __instance.GetType().GetMethod("HasPartModule");

if (hasPartModuleMethod != null)
if (!AlreadyCachedOwners.TryGetValue(__instance, out var isModulePresent))
{
// Go through each registered module and check if it's present in PartOwnerComponent modules
foreach (var moduleType in PartComponentModuleOverride.RegisteredPartComponentOverrides)
isModulePresent = false;
var hasPartModuleMethod = __instance.GetType().GetMethod("HasPartModule");

if (hasPartModuleMethod != null)
{
// 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))
{
isModulePresent = true;
break;
}
}
}

AlreadyCachedOwners.Add(__instance, isModulePresent);
}

// If registered module is present, run the original 0.1.5 method that runs background resource checks
Expand Down

0 comments on commit ca4b2cf

Please sign in to comment.