public void Patch() { HelperClass.SetLogger(logger); MethodDefinition setBlocksMethod = HelperClass.findMember <MethodDefinition>(module, "GameManager", false, HelperClass.MemberNameComparer <MethodDefinition>("SetBlocksRPC")); PropertyDefinition worldProperty = HelperClass.findMember <PropertyDefinition>(module, "GameManager", false, HelperClass.MemberNameComparer <PropertyDefinition>("World")); if (setBlocksMethod != null && worldProperty != null) { MethodBody body = setBlocksMethod.Body; ILProcessor proc = body.GetILProcessor(); List <Instruction> hook = HookHelper.Instance.prepareEventHook(setBlocksMethod, "SetBlocksEvent", new Instruction[][] { new Instruction[] { proc.Create(OpCodes.Ldc_I4_0), proc.Create(OpCodes.Box, module.Import(mscorlibModule.GetType("System.Boolean"))) }, new Instruction[] { proc.Create(OpCodes.Ldarg_1) }, new Instruction[] { proc.Create(OpCodes.Ldarg_0), proc.Create(OpCodes.Call, module.Import(worldProperty.GetMethod)) }, }); hook.Add(proc.Create(OpCodes.Dup)); hook.Add(proc.Create(OpCodes.Ldc_I4_0)); hook.Add(proc.Create(OpCodes.Ldelem_Ref)); hook.Add(proc.Create(OpCodes.Unbox_Any, module.Import(mscorlibModule.GetType("System.Boolean")))); int jmp1_sindex = hook.Count; hook.Add(null); //Brfalse hook.Add(proc.Create(OpCodes.Pop)); hook.Add(proc.Create(OpCodes.Ret)); int jmp1_tindex = hook.Count; hook.Add(proc.Create(OpCodes.Ldc_I4_1)); hook.Add(proc.Create(OpCodes.Ldelem_Ref)); hook.Add(proc.Create(OpCodes.Starg, 1)); hook.Insert(jmp1_sindex, proc.Create(OpCodes.Brfalse, hook[jmp1_tindex])); hook.RemoveAt(jmp1_sindex + 1); HookHelper.insertAt(body, 0, hook.ToArray()); } }
public void Patch() { HelperClass.SetLogger(logger); MethodDefinition initChunkCluster = HelperClass.findMember <MethodDefinition>(module, "ChunkCluster", false, HelperClass.MemberNameComparer <MethodDefinition>("Init"), HelperClass.MethodParametersComparer("EnumChunkProviderId")); if (initChunkCluster == null) { return; } { int[] jumpsToDefault = HelperClass.FindOPCodePattern(initChunkCluster, new OpCode[] { OpCodes.Switch, OpCodes.Br }, 1); if (jumpsToDefault.Length == 0) { logger.Error("ChunkCluster.Init has no switch (am I outdated?)!"); return; } if (jumpsToDefault.Length > 1) { logger.Error("ChunkCluster.Init has multiple switches (am I outdated?)!"); return; } MethodBody body = initChunkCluster.Body; int targetIndex = body.Instructions.IndexOf((Instruction)body.Instructions[jumpsToDefault[0]].Operand); if (!HelperClass.MethodOPCodeComparer(new int[] { targetIndex, targetIndex + 1, targetIndex + 2 }, new OpCode[] { OpCodes.Ldarg_0, OpCodes.Ldnull, OpCodes.Stfld }, null).Execute(initChunkCluster)) { logger.Error("ChunkCluster.Init's default case is unknown (I am outdated!)!"); return; } ILProcessor proc = body.GetILProcessor(); List <Instruction> hook = HookHelper.Instance.prepareEventHook(initChunkCluster, "UnknownChunkProviderEvent", new Instruction[][] { new Instruction[] { proc.Create(OpCodes.Ldarg_0) }, new Instruction[] { proc.Create(OpCodes.Ldc_I4_0), proc.Create(OpCodes.Box, module.Import(mscorlibModule.GetType("System.Boolean"))) }, new Instruction[] { proc.Create(OpCodes.Ldarg_1), proc.Create(OpCodes.Box, module.Import(mscorlibModule.GetType("System.Int32"))) }, new Instruction[] { proc.Create(OpCodes.Ldnull) }, }); hook.Insert(0, proc.Create(OpCodes.Ldarg_0)); hook.Add(proc.Create(OpCodes.Dup)); hook.Add(proc.Create(OpCodes.Ldc_I4_1)); hook.Add(proc.Create(OpCodes.Ldelem_Ref)); hook.Add(proc.Create(OpCodes.Unbox_Any, module.Import(mscorlibModule.GetType("System.Boolean")))); int jmp1_sindex = hook.Count; hook.Add(null); //brtrue hook.Add(proc.Create(OpCodes.Pop)); hook.Add(proc.Create(OpCodes.Br, body.Instructions[targetIndex + 1])); int jmp1_tindex = hook.Count; hook.Add(proc.Create(OpCodes.Ldc_I4_2)); hook.Add(proc.Create(OpCodes.Ldelem_Ref)); hook.Add(proc.Create(OpCodes.Br, body.Instructions[targetIndex + 2])); hook.RemoveAt(jmp1_sindex); hook.Insert(jmp1_sindex, proc.Create(OpCodes.Brtrue, hook[jmp1_tindex])); HookHelper.insertAt(body, targetIndex, hook.ToArray()); body.Instructions[jumpsToDefault[0]].Operand = hook[0]; } }
public void Patch() { HelperClass.SetLogger(null); //foreach RPC method in ConnectionManager foreach (MethodDefinition mdef in HelperClass.findMembers<MethodDefinition>(module, "ConnectionManager", HelperClass.MemberCustomAttributeComparer<MethodDefinition>("UnityEngine.RPC"))) { if (mdef.HasBody) { Mono.Collections.Generic.Collection<ParameterDefinition> _params = mdef.Parameters; if (_params.Count == 0 || !_params[_params.Count - 1].ParameterType.FullName.Equals("UnityEngine.NetworkMessageInfo")) { ParameterDefinition messageInfoPardef = new ParameterDefinition("_messageInfo", ParameterAttributes.None, mdef.DeclaringType.Module.Import(unityModule.GetType("UnityEngine.NetworkMessageInfo"))); mdef.Parameters.Add(messageInfoPardef); } ILProcessor proc = mdef.Body.GetILProcessor(); List<Instruction[]> argLoaders = new List<Instruction[]>(); argLoaders.Add(new Instruction[] { proc.Create(OpCodes.Ldc_I4_0), proc.Create(OpCodes.Box, module.Import(mscorlibModule.GetType("System.Boolean"))), }); argLoaders.Add(new Instruction[] { proc.Create(OpCodes.Ldstr, mdef.Name), }); List<Instruction> instrTmp = new List<Instruction>(); for (int i = 0; i < _params.Count; i++) { ParameterDefinition param = _params[i]; instrTmp.Clear(); instrTmp.Add(proc.Create(OpCodes.Ldarg, param)); if (param.ParameterType.IsValueType) { instrTmp.Add(proc.Create(OpCodes.Box, module.Import(param.ParameterType))); } argLoaders.Add(instrTmp.ToArray()); } List<Instruction> hook = HookHelper.Instance.prepareEventHook(mdef, "RPCEvent", argLoaders.ToArray()); for (int i = 0; i < _params.Count; i++) { ParameterDefinition param = _params[i]; hook.Add(proc.Create(OpCodes.Dup)); hook.Add(proc.Create(OpCodes.Ldc_I4, i + 2)); hook.Add(proc.Create(OpCodes.Ldelem_Ref)); if (param.ParameterType.IsValueType) { hook.Add(proc.Create(OpCodes.Unbox_Any, module.Import(param.ParameterType))); } else { hook.Add(proc.Create(OpCodes.Castclass, module.Import(param.ParameterType))); } hook.Add(proc.Create(OpCodes.Starg, param)); } hook.Add(proc.Create(OpCodes.Ldc_I4_0)); hook.Add(proc.Create(OpCodes.Ldelem_Ref)); hook.Add(proc.Create(OpCodes.Unbox_Any, module.Import(mscorlibModule.GetType("System.Boolean")))); hook.Add(proc.Create(OpCodes.Brfalse, mdef.Body.Instructions[0])); hook.Add(proc.Create(OpCodes.Ret)); HookHelper.insertAt(mdef.Body, 0, hook.ToArray()); } } }
public void Patch() { System.Reflection.Assembly ownAssembly = System.Reflection.Assembly.GetCallingAssembly(); Stream xmlStream = ownAssembly.GetManifestResourceStream("SKPatcher.xmlpatchers.xml"); XmlDocument xml = new XmlDocument(); xml.Load(xmlStream); XmlElement document = xml.DocumentElement; XmlNode[] hookNodes = document.ChildNodes.Items("hook"); foreach (XmlNode hookNode in hookNodes) { if (hookNode.Attributes["type"] == null || hookNode.Attributes["name"] == null || hookNode.Attributes["insertAt"] == null) { logger.Error("Error in xmlpatchers.xml : Unable to find one of the necessary attributes!"); break; } if (hookNode.Attributes["type"].Value.ToLower().Equals("event")) { string eventName = hookNode.Attributes["name"].Value; bool isCancellable = (hookNode.Attributes["cancellable"] != null) && (hookNode.Attributes["cancellable"].Value.ToLower().Equals("true")); int insertAt; if (!Int32.TryParse(hookNode.Attributes["insertAt"].Value, out insertAt)) { logger.Error("Error in xmlpatchers.xml : Unable to parse the insertAt value!"); continue; } XmlNode typeNode = hookNode.ChildNodes.Item("type"); XmlNode methodNode = hookNode.ChildNodes.Item("method"); XmlNode parametersNode = hookNode.ChildNodes.Item("parameters"); if (typeNode == null || methodNode == null || parametersNode == null) { logger.Error("Error in xmlpatchers.xml : ChildNodes missing for event (name = \"" + eventName + "\")!"); continue; } string typeName = typeNode.Attributes["name"].Value; XmlAttribute methodName = methodNode.Attributes["name"]; XmlAttribute methodParameters = methodNode.Attributes["parameters"]; XmlAttribute methodReturn = methodNode.Attributes["return"]; TypeDefinition targetType = module.GetType(typeName); if (targetType == null) { logger.Error("Error in xmlpatchers.xml : Unable to find type \"" + typeName + "\"!"); continue; } //I don't always use 'var' but when I do, it would be odd not to. var methodComparers = new List <HelperClass.GenericFuncContainer <MethodDefinition, bool> >(3); if (methodName != null) { methodComparers.Add(HelperClass.MemberNameComparer <MethodDefinition>(methodName.Value)); } if (methodParameters != null) { methodComparers.Add(HelperClass.MethodParametersComparer(methodParameters.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))); } if (methodReturn != null) { methodComparers.Add(HelperClass.MethodReturnTypeComparer(methodReturn.Value)); } methodComparers.Add(new HelperClass.GenericFuncContainer <MethodDefinition, bool>(method => Matches(method, methodNode))); MethodDefinition targetMethod = HelperClass.findMember <MethodDefinition>(module, targetType, false, methodComparers.ToArray()); int parameterId = (isCancellable ? 0 : -1); if (targetMethod != null) { MethodBody body = targetMethod.Body; ILProcessor proc = body.GetILProcessor(); List <Instruction[]> argLoaders = new List <Instruction[]>(parametersNode.ChildNodes.Count + (isCancellable ? 1 : 0)); if (isCancellable) { argLoaders.Add(new Instruction[] { proc.Create(OpCodes.Ldc_I4_0), proc.Create(OpCodes.Box, module.Import(mscorlibModule.GetType("System.Boolean"))) }); } List <Instruction[]> argWriters = new List <Instruction[]>(parametersNode.ChildNodes.Count); if (isCancellable) { Instruction[] cancelInstr = new Instruction[8]; cancelInstr[0] = proc.Create(OpCodes.Dup); cancelInstr[1] = proc.Create(OpCodes.Ldc_I4_0); cancelInstr[2] = proc.Create(OpCodes.Ldelem_Ref); cancelInstr[3] = proc.Create(OpCodes.Unbox_Any, module.Import(mscorlibModule.GetType("System.Boolean"))); cancelInstr[5] = proc.Create(OpCodes.Pop); cancelInstr[6] = proc.Create(OpCodes.Ret); cancelInstr[7] = proc.Create(OpCodes.Nop); cancelInstr[4] = proc.Create(OpCodes.Brfalse, cancelInstr[7]); argWriters.Add(cancelInstr); } foreach (XmlNode parNode in parametersNode.ChildNodes.Items("parameter")) { parameterId++; XmlAttribute _typeAttr = parNode.Attributes["type"]; string parType = (_typeAttr == null) ? "System.Object" : _typeAttr.Value; TypeDefinition parTypeDef = findType(parType); if (parTypeDef == null) { logger.Error("Error in xmlpatchers.xml : Unable to find type \"" + parType + "\"!"); continue; } bool isCustom = parNode.Attributes.ValueOf("isCustom").ToLower().Equals("true"); XmlNode loadNode = parNode.ChildNodes.Item("load"); XmlNode writeNode = parNode.ChildNodes.Item("write"); if (loadNode != null) { List <Instruction> loadInstr = new List <Instruction>(ParseInstructions(loadNode, proc)); if (parTypeDef.IsValueType) { loadInstr.Add(proc.Create(OpCodes.Box, module.Import(parTypeDef))); } argLoaders.Add(loadInstr.ToArray()); } if (writeNode != null) { List <Instruction> writeInstr = new List <Instruction> (); if (!isCustom) { writeInstr.Add(proc.Create(OpCodes.Dup)); writeInstr.Add(proc.Create(OpCodes.Ldc_I4, parameterId)); writeInstr.Add(proc.Create(OpCodes.Ldelem_Ref)); if (parTypeDef.IsValueType) { writeInstr.Add(proc.Create(OpCodes.Unbox_Any, module.Import(parTypeDef))); } else { writeInstr.Add(proc.Create(OpCodes.Castclass, module.Import(parTypeDef))); } } writeInstr.AddRange(ParseInstructions(writeNode, proc)); argWriters.Add(writeInstr.ToArray()); } } List <Instruction> hook = HookHelper.Instance.prepareEventHook(targetMethod, eventName, argLoaders.ToArray()); for (int i = 0; i < argWriters.Count; i++) { hook.AddRange(argWriters[i]); } hook.Add(proc.Create(OpCodes.Pop)); HookHelper.insertAt(body, insertAt, hook.ToArray()); } } } }
public void Patch() { HelperClass.SetLogger(null); //HelperClass.OnError would otherwise show errors if a NetPackage class doesn't override Process,Read or Write TypeDefinition[] netPackageTypes = HelperClass.findTypes(module, HelperClass.CombinedComparer <MethodDefinition>( HelperClass.MemberNameComparer <MethodDefinition>("GetPackageType"), HelperClass.MethodReturnTypeComparer("PackageType"), HelperClass.MethodNegAttributeComparer(MethodAttributes.Abstract)) ); if (netPackageTypes == null || netPackageTypes.Length == 0) { logger.Error("Unable to find any NetPackage classes!"); return; } foreach (TypeDefinition curPackageType in netPackageTypes) { MethodDefinition processMethod = HelperClass.findMember <MethodDefinition>(module, curPackageType, false, HelperClass.MemberNameComparer <MethodDefinition>("Process"), HelperClass.MethodReturnTypeComparer("System.Void"), HelperClass.MethodParametersComparer("World", "INetConnectionCallbacks")); if (processMethod != null) { MethodBody body = processMethod.Body; if (body != null) { ILProcessor proc = body.GetILProcessor(); List <Instruction> eventHook = HookHelper.Instance.prepareEventHook(processMethod, "ProcessPacketEvent", new Instruction[][] { new Instruction[] { proc.Create(OpCodes.Ldarg_0), }, new Instruction[] { proc.Create(OpCodes.Ldc_I4_0), proc.Create(OpCodes.Box, module.Import(mscorlibModule.GetType("System.Boolean"))), }, new Instruction[] { proc.Create(OpCodes.Ldarg_1), }, new Instruction[] { proc.Create(OpCodes.Ldarg_2), }, } ); eventHook.Add(proc.Create(OpCodes.Ldc_I4_1)); eventHook.Add(proc.Create(OpCodes.Ldelem_Ref)); eventHook.Add(proc.Create(OpCodes.Unbox_Any, module.Import(mscorlibModule.GetType("System.Boolean")))); eventHook.Add(proc.Create(OpCodes.Brfalse, body.Instructions[0])); eventHook.Add(proc.Create(OpCodes.Ret)); HookHelper.insertAt(body, 0, eventHook.ToArray()); } } MethodDefinition readMethod = HelperClass.findMember <MethodDefinition>(module, curPackageType, false, HelperClass.MemberNameComparer <MethodDefinition>("Read"), HelperClass.MethodReturnTypeComparer("System.Void"), HelperClass.MethodParametersComparer("System.IO.BinaryReader")); if (readMethod != null) { MethodBody body = readMethod.Body; if (body != null) { ILProcessor proc = body.GetILProcessor(); List <Instruction> eventHook = HookHelper.Instance.prepareEventHook(readMethod, "ReadPacketFromBufEvent", new Instruction[][] { new Instruction[] { proc.Create(OpCodes.Ldarg_0), }, } ); eventHook.Add(proc.Create(OpCodes.Pop)); HookHelper.insertAt(body, body.Instructions.Count - 1, eventHook.ToArray()); } } MethodDefinition writeMethod = HelperClass.findMember <MethodDefinition>(module, curPackageType, false, HelperClass.MemberNameComparer <MethodDefinition>("Write"), HelperClass.MethodReturnTypeComparer("System.Void"), HelperClass.MethodParametersComparer("System.IO.BinaryWriter")); if (writeMethod != null) { MethodBody body = writeMethod.Body; if (body != null) { ILProcessor proc = body.GetILProcessor(); List <Instruction> eventHook = HookHelper.Instance.prepareEventHook(writeMethod, "WritePacketToBufEvent", new Instruction[][] { new Instruction[] { proc.Create(OpCodes.Ldarg_0), }, new Instruction[] { proc.Create(OpCodes.Ldc_I4_0), proc.Create(OpCodes.Box, module.Import(mscorlibModule.GetType("System.Boolean"))), }, } ); eventHook.Add(proc.Create(OpCodes.Ldc_I4_1)); eventHook.Add(proc.Create(OpCodes.Ldelem_Ref)); eventHook.Add(proc.Create(OpCodes.Unbox_Any, module.Import(mscorlibModule.GetType("System.Boolean")))); eventHook.Add(proc.Create(OpCodes.Brfalse, body.Instructions[0])); eventHook.Add(proc.Create(OpCodes.Ret)); HookHelper.insertAt(body, 0, eventHook.ToArray()); } } } }