c# - 在统一游戏中编写对函数的函数调用不起作用
问题描述
我正在尝试为游戏编写一个 mod 引擎。
为了实现这一点,我决定在 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 引擎被调用)工作的任何想法?
解决方案
看来你不能因为统一而弄乱 Assembly-CSharp 文件,而是我使用 UnityEngine.CoreModule.dll 并注入到 Display 或 Input
推荐阅读
- sql - 我应该如何优化此行级安全策略以读取事件?
- configuration - 按下前缀时,tmux 将“\”更改为空格键
- python - Excel 中的 XLSXwriter 和短日期
- android - 如果可能(双显示),如何安全快速地初始化演示片段?
- java - ComboAnalyzer - 在 AttributeSource 中找不到 AttributeImpl
- sql - SQL:在 WHERE 子句中输入变量字符串?
- javascript - 内部子子菜单不显示
- c# - 如何在 C# 中通过注释符号拆分文本文件
- javascript - React:为什么 onChange 事件没有触发?
- azure-active-directory - msonline 模块 - where 语句在 azure 函数 powershell 中不起作用