private void button1_Click(object sender, EventArgs e) { //backup string appOrigPath = appPath + ".orig"; File.Copy(appPath, appOrigPath, true); File.Copy(injPath, Path.GetDirectoryName(appOrigPath) + @"\" + injPath, true); //get Assemblies var app = AssemblyDefinition.ReadAssembly(appOrigPath); var inj = AssemblyDefinition.ReadAssembly(injPath); var injType = inj.MainModule.Types.Single(t => t.Name == injTypeName); var injMethod = injType.Methods.Single(t => t.Name == injMethodName); //MethodDefinition myHook = app.MainModule.GetType("HookNamespace.MyHookClass").GetMethod("MyHook"); // assembiy.MainModule.Import(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })))); var appType = app.MainModule.Types.Single(t => t.Name == appTypeName); //var appMethod = appType.Methods.Single(t => t.Name == appMethodName); var appMethod = appType.Methods.Single(m => { return(m.Name == appMethodName && m.Parameters.Count == 3); }); var ipl = appMethod.Body.GetILProcessor(); var firstInstruction = ipl.Body.Instructions[0]; var instruction = ipl.Create(OpCodes.Call, app.MainModule.Import(injMethod.Resolve())); // This instruction is our instruction call ipl.InsertBefore(firstInstruction, ipl.Create(OpCodes.Nop)); ipl.InsertBefore(firstInstruction, ipl.Create(OpCodes.Ldarg_1)); ipl.InsertBefore(firstInstruction, ipl.Create(OpCodes.Ldarga_S, appMethod.Parameters[1])); ipl.InsertBefore(firstInstruction, ipl.Create(OpCodes.Ldarga_S, appMethod.Parameters[2])); ipl.InsertBefore(firstInstruction, instruction); app.Write(appPath); Msg_tb.AppendText(string.Format("{0}.{1} 在线翻译注入完成!!-------\r\n", appTypeName, appMethodName)); //Instruction ins = appMethod.Body.Instructions[0]; //var worker = appMethod.Body.GetILProcessor(); //MethodInfo m = typeof(Program).GetMethod("InjectMethod", BindingFlags.Static | BindingFlags.Public); //MethodReference refernce = app.MainModule.Import(m); //Instruction insCall = worker.Create(OpCodes.Call, refernce); //worker.InsertBefore(ins, insCall); //Instruction insNop = worker.Create(OpCodes.Nop); //worker.InsertAfter(insCall, insNop); }
public void inthh(string appTypeName, string appMethodName, String InCode, int py = 0) { try { if (checkBox1.Checked) { string appOrigPath = appPath + ".orig"; File.Copy(appPath, appOrigPath, true); File.Copy(HHPath, Path.GetDirectoryName(appOrigPath) + @"\" + HHPath, false); } } catch { } //get Assemblies var app = AssemblyDefinition.ReadAssembly(appPath); var inj = AssemblyDefinition.ReadAssembly(HHPath); var injType = inj.MainModule.Types.Single(t => t.Name == HHTypeName); var injMethod = injType.Methods.Single(t => t.Name == HHMethodName); var appType = app.MainModule.Types.Single(t => t.Name == appTypeName); var appMethod = appType.Methods.Single(m => { return(m.Name == appMethodName && m.Parameters.Count == 1); }); var ipl = appMethod.Body.GetILProcessor(); //var firstInstruction = ipl.Body.Instructions.Single(m => { return m.OpCode == OpCodes.Stfld && ((Mono.Cecil.FieldDefinition)m.Operand).Name.ToLower() == InCode.ToLower(); }); var firstInstruction = ipl.Body.Instructions.Single(m => { return(m.Operand != null && m.Operand.GetType() == typeof(FieldDefinition) && ((FieldDefinition)m.Operand).Name.ToLower() == InCode.ToLower()); }); if (py != 0) { firstInstruction = ipl.Body.Instructions[ipl.Body.Instructions.IndexOf(firstInstruction) + py]; } ipl.InsertBefore(firstInstruction, ipl.Create(OpCodes.Call, app.MainModule.Import(injMethod.Resolve()))); app.Write(appPath); Console.WriteLine(string.Format("{0}.{1} {2} 注入完成!!-------\r\n", appTypeName, appMethodName, InCode)); Msg_tb.AppendText(string.Format("{0}.{1} {2} 注入完成!!-------\r\n", appTypeName, appMethodName, InCode)); }
private void Init_BuffSct_Click(object sender, EventArgs e) { try { string appOrigPath = appPath + ".orig"; //File.Copy(appPath, appOrigPath, true); //File.Copy(HHPath, Path.GetDirectoryName(appOrigPath) + @"\" + HHPath, false); } catch { } //get Assemblies var app = AssemblyDefinition.ReadAssembly(appPath); var inj = AssemblyDefinition.ReadAssembly(HHPath); var 注入方法 = inj.MainModule.Types.Single(t => t.Name == HHTypeName).Methods.Single(t => t.Name == HHMethodName); string appTypeName = "ClientTableMgr"; string appMethodName = "Init_BuffSct";// Init_BuffSct(string text) var appMethod = app.MainModule.Types.Single(t => t.Name == appTypeName).Methods.Single(m => { return(m.Name == appMethodName && m.Parameters.Count == 1); }); var ipl = appMethod.Body.GetILProcessor(); string InCode = ""; InCode = "szTooltip"; var firstInstruction = ipl.Body.Instructions.Single(m => { return(m.OpCode == OpCodes.Stfld && ((Mono.Cecil.FieldDefinition)m.Operand).Name == InCode); }); ipl.InsertBefore(firstInstruction, ipl.Create(OpCodes.Call, app.MainModule.Import(注入方法.Resolve()))); InCode = "szBuffName"; firstInstruction = ipl.Body.Instructions.Single(m => { return(m.OpCode == OpCodes.Stfld && ((Mono.Cecil.FieldDefinition)m.Operand).Name == InCode); }); ipl.InsertBefore(firstInstruction, ipl.Create(OpCodes.Call, app.MainModule.Import(注入方法.Resolve()))); app.Write(appPath); Console.WriteLine("注入完成!!-------"); Msg_tb.AppendText("注入完成!!-------"); }