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 fc4ea7c commit 82dc128
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<SpaceWarpVersion>1.5.4</SpaceWarpVersion>
<SpaceWarpVersion>1.6.0</SpaceWarpVersion>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>SpaceWarp</RootNamespace>
<LangVersion>11</LangVersion>
Expand Down
45 changes: 45 additions & 0 deletions SpaceWarp.Core/API/Parts/PartComponentModuleOverride.cs
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 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 82dc128

Please sign in to comment.