Skip to content

Commit 574bc28

Browse files
Copilotpardeike
andcommitted
Add public API for MonoMod switches with tests
Co-authored-by: pardeike <[email protected]>
1 parent c33fa7e commit 574bc28

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Harmony/Public/Harmony.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,56 @@ public static MethodBase GetOriginalMethodFromStackframe(StackFrame frame)
333333
///
334334
public static Dictionary<string, Version> VersionInfo(out Version currentVersion)
335335
=> PatchProcessor.VersionInfo(out currentVersion);
336+
337+
/// <summary>Sets a MonoMod switch value (e.g., "DMDDebug", "DMDDumpTo")</summary>
338+
/// <param name="name">The switch name</param>
339+
/// <param name="value">The value to set (bool, string, etc.)</param>
340+
///
341+
public static void SetSwitch(string name, object value)
342+
{
343+
var switchesType = AccessTools.TypeByName("MonoMod.Switches");
344+
var method = AccessTools.Method(switchesType, "SetSwitchValue");
345+
method.Invoke(null, [name, value]);
346+
}
347+
348+
/// <summary>Clears a MonoMod switch value</summary>
349+
/// <param name="name">The switch name</param>
350+
///
351+
public static void ClearSwitch(string name)
352+
{
353+
var switchesType = AccessTools.TypeByName("MonoMod.Switches");
354+
var method = AccessTools.Method(switchesType, "ClearSwitchValue");
355+
method.Invoke(null, [name]);
356+
}
357+
358+
/// <summary>Tries to get a MonoMod switch value</summary>
359+
/// <param name="name">The switch name</param>
360+
/// <param name="value">The switch value if found</param>
361+
/// <returns>True if the switch was found, false otherwise</returns>
362+
///
363+
public static bool TryGetSwitch(string name, out object value)
364+
{
365+
var switchesType = AccessTools.TypeByName("MonoMod.Switches");
366+
var method = AccessTools.Method(switchesType, "TryGetSwitchValue");
367+
var args = new object[] { name, null };
368+
var result = (bool)method.Invoke(null, args);
369+
value = args[1];
370+
return result;
371+
}
372+
373+
/// <summary>Tries to determine if a MonoMod switch is enabled</summary>
374+
/// <param name="name">The switch name</param>
375+
/// <param name="isEnabled">True if the switch is enabled, false otherwise</param>
376+
/// <returns>True if the switch enablement state could be determined, false otherwise</returns>
377+
///
378+
public static bool TryIsSwitchEnabled(string name, out bool isEnabled)
379+
{
380+
var switchesType = AccessTools.TypeByName("MonoMod.Switches");
381+
var method = AccessTools.Method(switchesType, "TryGetSwitchEnabled");
382+
var args = new object[] { name, false };
383+
var result = (bool)method.Invoke(null, args);
384+
isEnabled = (bool)args[1];
385+
return result;
386+
}
336387
}
337388
}

HarmonyTests/Tools/TestSwitches.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using HarmonyLib;
2+
using NUnit.Framework;
3+
4+
namespace HarmonyLibTests.Tools
5+
{
6+
[TestFixture, NonParallelizable]
7+
public class Test_Switches : TestLogger
8+
{
9+
[Test]
10+
public void Test_SetAndGetSwitch()
11+
{
12+
Harmony.SetSwitch("DMDDebug", true);
13+
var result = Harmony.TryGetSwitch("DMDDebug", out var value);
14+
Assert.IsTrue(result);
15+
Assert.IsTrue(value is bool);
16+
Assert.IsTrue((bool)value);
17+
}
18+
19+
[Test]
20+
public void Test_SetAndGetStringSwitch()
21+
{
22+
var testPath = "Path\\To\\Dir\\Where\\Should\\Go";
23+
Harmony.SetSwitch("DMDDumpTo", testPath);
24+
var result = Harmony.TryGetSwitch("DMDDumpTo", out var value);
25+
Assert.IsTrue(result);
26+
Assert.IsTrue(value is string);
27+
Assert.AreEqual(testPath, (string)value);
28+
}
29+
30+
[Test]
31+
public void Test_ClearSwitch()
32+
{
33+
Harmony.SetSwitch("DMDDebug", true);
34+
var resultBefore = Harmony.TryGetSwitch("DMDDebug", out var valueBefore);
35+
Assert.IsTrue(resultBefore);
36+
Assert.IsNotNull(valueBefore);
37+
38+
Harmony.ClearSwitch("DMDDebug");
39+
var resultAfter = Harmony.TryGetSwitch("DMDDebug", out var valueAfter);
40+
Assert.IsFalse(resultAfter);
41+
}
42+
43+
[Test]
44+
public void Test_TryIsSwitchEnabled()
45+
{
46+
Harmony.SetSwitch("DMDDebug", true);
47+
var result = Harmony.TryIsSwitchEnabled("DMDDebug", out var isEnabled);
48+
Assert.IsTrue(result);
49+
Assert.IsTrue(isEnabled);
50+
51+
Harmony.SetSwitch("DMDDebug", false);
52+
result = Harmony.TryIsSwitchEnabled("DMDDebug", out isEnabled);
53+
Assert.IsTrue(result);
54+
Assert.IsFalse(isEnabled);
55+
}
56+
57+
[Test]
58+
public void Test_GetNonExistentSwitch()
59+
{
60+
var result = Harmony.TryGetSwitch("NonExistentSwitch", out var value);
61+
Assert.IsFalse(result);
62+
}
63+
64+
[Test]
65+
public void Test_IsSwitchEnabledNonExistent()
66+
{
67+
var result = Harmony.TryIsSwitchEnabled("NonExistentSwitch", out var isEnabled);
68+
Assert.IsFalse(result);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)