Skip to content

Commit 7679c9b

Browse files
KlaasWhitemihemihe
andauthored
Add StarMap hook for after onFrame
* Fix package release * Onframepatch clean (#47) * Do not try to unload the assembly load contexts (#44) * Program.OnFrame Harmony patch * Updated the API readme --------- Co-authored-by: KlaasWhite <45828001+KlaasWhite@users.noreply.github.com> --------- Co-authored-by: mihe <mihemihe@hotmail.com>
1 parent d2adba3 commit 7679c9b

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

StarMap.API/OnFrameAttributes.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Reflection;
2+
3+
namespace StarMap.API
4+
{
5+
/// <summary>
6+
/// Methods marked with this attribute will be called after KSA Program.OnFrame is called.
7+
/// </summary>
8+
/// <remarks>
9+
/// Methods using this attribute must match the following signature:
10+
///
11+
/// <code>
12+
/// public void MethodName(double currentPlayerTime, double dtPlayer);
13+
/// </code>
14+
///
15+
/// Parameter requirements:
16+
/// <list type="bullet">
17+
/// <item>
18+
/// <description>
19+
/// <paramref name="currentPlayerTime"/>
20+
/// </description>
21+
/// </item>
22+
/// <item>
23+
/// <description>
24+
/// <paramref name="dtPlayer"/>
25+
/// </description>
26+
/// </item>
27+
/// </list>
28+
///
29+
/// Requirements:
30+
/// <list type="bullet">
31+
/// <item><description>Return type must be <see cref="void"/>.</description></item>
32+
/// <item><description>Method must be an instance method (non-static).</description></item>
33+
/// </list>
34+
/// </remarks>
35+
public sealed class StarMapAfterOnFrameAttribute : StarMapMethodAttribute
36+
{
37+
public override bool IsValidSignature(MethodInfo method)
38+
{
39+
return method.ReturnType == typeof(void) &&
40+
method.GetParameters().Length == 2 &&
41+
method.GetParameters()[0].ParameterType == typeof(double) &&
42+
method.GetParameters()[1].ParameterType == typeof(double);
43+
44+
}
45+
}
46+
}

StarMap.API/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Any method within this class that has any of the attributes will used, so if two
1414
- StarMapUnload: Called when KSA is unloaded.
1515
- StarMapBeforeGui: Called just before KSA starts drawing its Ui.
1616
- StarMapAfterGui: Called after KSA has drawn its Ui.
17+
- StarMapAfterOnFrame: Called after KSA calls Program.OnFrame

StarMap.Core/Patches/ProgramPatcher.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StarMap.Core.Patches
88
internal static class ProgramPatcher
99
{
1010
private const string OnDrawUiMethodName = "OnDrawUi";
11+
private const string OnFrameMethodName = "OnFrame";
1112

1213
[HarmonyPatch(OnDrawUiMethodName)]
1314
[HarmonyPrefix]
@@ -32,5 +33,17 @@ public static void AfterOnDrawUi(double dt)
3233
method.Invoke(@object, [dt]);
3334
}
3435
}
36+
37+
[HarmonyPatch(OnFrameMethodName)]
38+
[HarmonyPostfix]
39+
public static void AfterOnFrame(double currentPlayerTime, double dtPlayer)
40+
{
41+
var methods = StarMapCore.Instance?.LoadedMods.Mods.Get<StarMapAfterOnFrameAttribute>() ?? [];
42+
43+
foreach (var (_, @object, method) in methods)
44+
{
45+
method.Invoke(@object, new object[] { currentPlayerTime, dtPlayer });
46+
}
47+
}
3548
}
3649
}

0 commit comments

Comments
 (0)