mod联动
baselib添加了一个mod联动的功能,可实现可选依赖的功能。
另一个mod
假如一个modid为test的mod在Entry里这么写:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| namespace Test.Scripts;
[ModInitializer(nameof(Init))] public class Entry { public static void Init() {}
public static List<string> TestIds = ["test1", "test2", "test3"];
public static void Register(string id) { Log.Info($"Register called with id: {id}"); TestIds.Add(id); } }
|
如果你不依赖这个mod的dll,无法直接调用Register函数。
你的mod
你可以新建这么一个类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| using BaseLib.Utils.ModInterop;
namespace JustAnotherTest.Scripts;
[ModInterop("test", "Test.Scripts.Entry")] public static class TestInterop { public static void Register(string id) { }
[InteropTarget("TestIds")] public static List<string> Ids { get; set; } }
|
然后在合适的时机调用即可:
1 2 3 4
| if (ModManager.GetLoadedMods().Any(m => string.Equals(m.manifest?.id, "test"))) { TestInterop.Register("JustAnotherModId"); }
|