【发布时间】:2018-12-06 01:03:28
【问题描述】:
我正在尝试为游戏编写模组引擎。
为了实现这一点,我决定在 Unity 的 Assembly-CSharp 游戏代码文件中添加对我的引擎 dll 的引用,然后使用 Mono.cecil 将引擎调用写入游戏加载时调用的函数之一.
但是,这个(函数调用)不起作用,代码正常执行,但我的引擎没有被调用。我也试过用 dnSpy 来做,但也没有用。我能够在其中添加代码,如果调用该函数,该代码将写入文件,该函数有效,因此要么引擎被调用(或引用)的方式出现问题,要么统一做了一些事情来阻止这种行为。
使用 mono.cecil 添加引用:
private void AddRef(string path)
{
var _Module = ModuleDefinition.ReadModule(Path.Combine(Application.StartupPath, @"Assembly-CSharp.dll"));
var nameReference = new AssemblyNameReference("ModEngine", new Version(1, 0, 0, 0));
_Module.AssemblyReferences.Add(nameReference);
}
添加对函数的调用(可能不是一种非常有效的方法):
private void ReWrite()
{
var path = Path.Combine(Application.StartupPath, "Assembly-CSharp.dll"); //Get path to asm
var assembly = AssemblyDefinition.ReadAssembly(path); //Load asm
//Get types that match criteria
var toInspect = assembly.MainModule.GetTypes().SelectMany(t => t.Methods.Select(m => new { t, m })).Where(x => x.m.HasBody);
toInspect = toInspect.Where(x => x.t.Name.EndsWith("GlobalStats") && x.m.Name == "LoadStatData");
foreach (var method in toInspect) //Get the type
{
var processor = method.m.Body.GetILProcessor(); //Get IL processor
var call = processor.Create(OpCodes.Call, method.m.Module.Import(typeof(WeNeedToModDeeperEngine.ModEngine).GetMethod("Main"))); //Create a call opcode to the engine
var lastInstruction = method.m.Body.Instructions[method.m.Body.Instructions.Count - 1]; //Get the last command
processor.InsertBefore(lastInstruction, call); //Write the call before the first command
}
assembly.Write("Assembly-CSharp-patched.dll"); //Write the assembly
}
关于如何让这个(调用 mod 引擎)工作有什么想法吗?
【问题讨论】:
标签: c# unity3d mono.cecil