-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API for mods to register their custom module PartComponentModule …
…for background resource processing
- Loading branch information
Showing
3 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
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
55
SpaceWarp.Core/Patching/PartOwnerComponentOnFixedUpdate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |