Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions StarMap.API/OnFrameAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Reflection;

namespace StarMap.API
{
/// <summary>
/// Methods marked with this attribute will be called after KSA Program.OnFrame is called.
/// </summary>
/// <remarks>
/// Methods using this attribute must match the following signature:
///
/// <code>
/// public void MethodName(double currentPlayerTime, double dtPlayer);
/// </code>
///
/// Parameter requirements:
/// <list type="bullet">
/// <item>
/// <description>
/// <paramref name="currentPlayerTime"/>
/// </description>
/// </item>
/// <item>
/// <description>
/// <paramref name="dtPlayer"/>
/// </description>
/// </item>
/// </list>
///
/// Requirements:
/// <list type="bullet">
/// <item><description>Return type must be <see cref="void"/>.</description></item>
/// <item><description>Method must be an instance method (non-static).</description></item>
/// </list>
/// </remarks>
public sealed class StarMapAfterOnFrameAttribute : StarMapMethodAttribute
{
public override bool IsValidSignature(MethodInfo method)
{
return method.ReturnType == typeof(void) &&
method.GetParameters().Length == 2 &&
method.GetParameters()[0].ParameterType == typeof(double) &&
method.GetParameters()[1].ParameterType == typeof(double);

}
}
}
1 change: 1 addition & 0 deletions StarMap.API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Any method within this class that has any of the attributes will used, so if two
- StarMapUnload: Called when KSA is unloaded.
- StarMapBeforeGui: Called just before KSA starts drawing its Ui.
- StarMapAfterGui: Called after KSA has drawn its Ui.
- StarMapAfterOnFrame: Called after KSA calls Program.OnFrame
5 changes: 0 additions & 5 deletions StarMap.Core/ModRepository/LoadedModRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ public void Dispose()
method.Invoke(@object, []);
}

foreach (var modLoadContext in _modLoadContexts.Values)
{
modLoadContext.Unload();
}

_mods.Dispose();
}
}
Expand Down
13 changes: 13 additions & 0 deletions StarMap.Core/Patches/ProgramPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace StarMap.Core.Patches
internal static class ProgramPatcher
{
private const string OnDrawUiMethodName = "OnDrawUi";
private const string OnFrameMethodName = "OnFrame";

[HarmonyPatch(OnDrawUiMethodName)]
[HarmonyPrefix]
Expand All @@ -32,5 +33,17 @@ public static void AfterOnDrawUi(double dt)
method.Invoke(@object, [dt]);
}
}

[HarmonyPatch(OnFrameMethodName)]
[HarmonyPostfix]
public static void AfterOnFrame(double currentPlayerTime, double dtPlayer)
{
var methods = StarMapCore.Instance?.LoadedMods.Mods.Get<StarMapAfterOnFrameAttribute>() ?? [];

foreach (var (_, @object, method) in methods)
{
method.Invoke(@object, new object[] { currentPlayerTime, dtPlayer });
}
}
}
}
1 change: 0 additions & 1 deletion StarMap/GameSurveyer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void RunGame()

public void Dispose()
{
_gameAssemblyContext.Unload();
_core?.DeInit();
}
}
Expand Down
Loading