Skip to content

Commit

Permalink
Add API for mods to register their custom module PartComponentModule …
Browse files Browse the repository at this point in the history
…for background resource processing
  • Loading branch information
Falki-git committed Dec 20, 2023
1 parent c4090f8 commit 0705222
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/SpaceWarp.Core/API/Parts/PartComponentModuleOverride.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using JetBrains.Annotations;
using KSP.Sim.impl;
using SpaceWarp.API.Logging;

namespace SpaceWarp.API.Parts;

[PublicAPI]
public static class PartComponentModuleOverride
{
private static readonly ILogger _LOGGER = new UnityLogSource("SpaceWarp.PartComponentModuleOverride");

internal static List<Type> RegisteredPartComponentOverrides = new();

/// <summary>
/// Registers your custom module for background resource processing.
/// </summary>
/// <typeparam name="T">Your Custom Module class that inherits from PartComponentModule</typeparam>
public static void RegisterModuleForBackgroundResourceProcessing<T>() where T : PartComponentModule
{
var moduleName = typeof(T).Name;

// Check if this Module is already registered
if (RegisteredPartComponentOverrides.Contains(typeof(T)))
{
throw new ArgumentException($"Module '{moduleName}' is already registered. Skipping.", nameof(T));
}

RegisteredPartComponentOverrides.Add(typeof(T));
_LOGGER.LogInfo($"Registered '{moduleName}' for background resources processing.");
}

/// <summary>
/// Unregisters your custom module from background resource processing.
/// </summary>
/// <typeparam name="T">Your Custom Module class that inherits from PartComponentModule</typeparam>
public static void UnRegisterModuleForBackgroundResourceProcessing<T>() where T : PartComponentModule
{
if (!RegisteredPartComponentOverrides.Contains(typeof(T))) return;

RegisteredPartComponentOverrides.Remove(typeof(T));
_LOGGER.LogInfo($"Unregistered '{typeof(T).Name}' from background resources processing.");
}
}
55 changes: 55 additions & 0 deletions src/SpaceWarp.Core/Patching/PartOwnerComponentOnFixedUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using HarmonyLib;
using KSP.Sim.impl;
using SpaceWarp.API.Parts;

namespace SpaceWarp.Patching;

[HarmonyPatch]
internal class PartOwnerComponentOnFixedUpdate
{
[HarmonyPatch(typeof(PartOwnerComponent), "OnFixedUpdate"), HarmonyPrefix]
private static bool PerformBackgroundCalculationsForRegisteredModules(double universalTime,
double deltaUniversalTime,
PartOwnerComponent __instance)
{
var isModulePresent = false;

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

if (hasPartModuleMethod != null)
{
var genericMethod = hasPartModuleMethod.MakeGenericMethod(moduleType);

if ((bool)genericMethod.Invoke(__instance, null))
{
isModulePresent = true;
break;
}
}
}

// If registered module is present, run the original 0.1.5 method that runs background resource checks
if (isModulePresent)
{
__instance.RecalculatePhysicsStats(false);
if (__instance.PartAttachmentsDirty)
{
__instance.UpdatePartRelationships();
}
if (__instance.ResourceFlowRequestManager != null && __instance.FlowGraph != null)
{
__instance.ResourceFlowRequestManager.UpdateFlowRequests(universalTime, deltaUniversalTime);
}
__instance.UpdateInsolation();
__instance.UpdateHasPanelsStellarExposure();

return false;
}

// No registered modules are present, resume with the current method
return true;
}
}

0 comments on commit 0705222

Please sign in to comment.