GenerateCreateNewCachedIsland() { var clientDataType = Type.GetType(ClientData.InternalTypeName); if (clientDataType == null) { throw new InvalidOperationException("Couldn't fine " + ClientData.InternalTypeName); } var type = Type.GetType(_clientDataIsland); if (type == null) { throw new InvalidOperationException("Couldn't find " + _clientDataIsland); } var backing = clientDataType.GetMethod("CreateNewCachedIsland"); if (backing == null) { throw new InvalidOperationException("Couldn't find CreateNewCachedIsland"); } var meth = new DynamicMethod(nameof(CreateNewCachedIsland), typeof(IslandData), new[] { typeof(object), typeof(IMyReplicable), typeof(HashSet <IMyReplicable>), typeof(MyTimeSpan) }, true); var gen = new LoggingIlGenerator(meth.GetILGenerator(), _level); gen.EmitComment("// Start " + meth.Name); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Castclass, clientDataType); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldarg_2); gen.Emit(OpCodes.Ldarg_3); gen.Emit(OpCodes.Callvirt, backing); var islandInternal = gen.DeclareLocal(type); var islandExternal = gen.DeclareLocal(typeof(IslandData)); gen.Emit(OpCodes.Stloc, islandInternal); EmitCopyAs(gen, islandInternal, islandExternal); gen.Emit(OpCodes.Ldloc, islandExternal); gen.Emit(OpCodes.Ret); gen.EmitComment("// End " + meth.Name); Check(meth); return(meth.CreateDelegate <Func <object, IMyReplicable, HashSet <IMyReplicable>, MyTimeSpan, IslandData> >()); }
GenerateClientDataReplicableToIslandTryGetValue() { var type = Type.GetType(_clientDataIsland); if (type == null) { throw new InvalidOperationException("Couldn't find " + _clientDataIsland); } var cacheType = typeof(Dictionary <,>).MakeGenericType(typeof(IMyStreamableReplicable), type); var backing = cacheType.GetMethod("TryGetValue", BindingFlags.Instance | BindingFlags.Public); if (backing == null) { throw new InvalidOperationException("Couldn't find TryGetValue"); } var meth = new DynamicMethod(nameof(ClientDataReplicableToIslandTryGetValue), typeof(bool), new[] { typeof(object), typeof(IMyStreamableReplicable), typeof(IslandData).MakeByRefType() }, true); var gen = new LoggingIlGenerator(meth.GetILGenerator(), _level); gen.EmitComment("// Start " + meth.Name); var islandInternal = gen.DeclareLocal(type); var islandExternal = gen.DeclareLocal(typeof(IslandData)); var tmpResult = gen.DeclareLocal(typeof(bool)); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Castclass, cacheType); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldloca, islandInternal); gen.Emit(OpCodes.Callvirt, backing); gen.Emit(OpCodes.Stloc, tmpResult); EmitCopyAs(gen, islandInternal, islandExternal); gen.Emit(OpCodes.Ldarg_2); gen.Emit(OpCodes.Ldloc, islandExternal); gen.Emit(OpCodes.Stobj, typeof(IslandData)); gen.Emit(OpCodes.Ldloc, tmpResult); gen.Emit(OpCodes.Ret); gen.EmitComment("// End " + meth.Name); Check(meth); return((TryGetValueTemplate <IMyStreamableReplicable, IslandData>)meth.CreateDelegate( typeof(TryGetValueTemplate <IMyStreamableReplicable, IslandData>))); }
private static Action <object, IslandData> GenerateRemoveCachedIsland() { var clientDataType = Type.GetType(ClientData.InternalTypeName); if (clientDataType == null) { throw new InvalidOperationException("Couldn't fine " + ClientData.InternalTypeName); } var type = Type.GetType(_clientDataIsland); if (type == null) { throw new InvalidOperationException("Couldn't find " + _clientDataIsland); } var backing = clientDataType.GetMethod("RemoveCachedIsland"); if (backing == null) { throw new InvalidOperationException("Couldn't find RemoveCachedIsland"); } var meth = new DynamicMethod(nameof(RemoveCachedIsland), typeof(void), new[] { typeof(object), typeof(IslandData) }, true); var gen = new LoggingIlGenerator(meth.GetILGenerator(), _level); gen.EmitComment("// Start " + meth.Name); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Castclass, clientDataType); var islandExternal = gen.DeclareLocal(typeof(IslandData)); var islandInternal = gen.DeclareLocal(type); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Stloc, islandExternal); EmitCopyAs(gen, islandExternal, islandInternal); gen.Emit(OpCodes.Ldloc, islandInternal); gen.Emit(OpCodes.Callvirt, backing); gen.Emit(OpCodes.Ret); gen.EmitComment("// End " + meth.Name); Check(meth); return(meth.CreateDelegate <Action <object, IslandData> >()); }
private static DynamicProxyEnumerable <IslandData> GenerateClientDataIslandsEnumerator() { var type = Type.GetType(_clientDataIsland); if (type == null) { throw new InvalidOperationException("Couldn't find " + _clientDataIsland); } var meth = new DynamicMethod(nameof(ClientDataIslandsEnumerator) + "_Mapper", typeof(IslandData), new[] { typeof(IEnumerator) }, true); var genericEnumerator = typeof(IEnumerator <>).MakeGenericType(type); var currentProp = genericEnumerator.GetProperty("Current"); if (currentProp == null) { throw new InvalidOperationException("Couldn't find Current property on " + genericEnumerator.FullName); } var gen = new LoggingIlGenerator(meth.GetILGenerator(), _level); var islandInternal = gen.DeclareLocal(type); var islandExternal = gen.DeclareLocal(typeof(IslandData)); gen.EmitComment("// Start " + meth.Name); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Castclass, genericEnumerator); gen.Emit(OpCodes.Callvirt, currentProp.GetMethod); gen.Emit(OpCodes.Stloc, islandInternal); EmitCopyAs(gen, islandInternal, islandExternal); gen.Emit(OpCodes.Ldloc, islandExternal); gen.Emit(OpCodes.Ret); gen.EmitComment("// End " + meth.Name); Check(meth); return(new DynamicProxyEnumerable <IslandData>((o) => ((IEnumerable)o).GetEnumerator(), meth.CreateDelegate <Func <IEnumerator, IslandData> >())); }
private void SavePatchedMethod(string target) { var asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("SomeName"), AssemblyBuilderAccess.RunAndSave, Path.GetDirectoryName(target)); var moduleBuilder = asmBuilder.DefineDynamicModule(Path.GetFileNameWithoutExtension(target), Path.GetFileName(target)); var typeBuilder = moduleBuilder.DefineType("Test", TypeAttributes.Public); var methodName = _method.Name + $"_{_patchSalt}"; var returnType = _method is MethodInfo meth ? meth.ReturnType : typeof(void); var parameters = _method.GetParameters(); var parameterTypes = (_method.IsStatic ? Enumerable.Empty <Type>() : new[] { _method.DeclaringType }) .Concat(parameters.Select(x => x.ParameterType)).ToArray(); var patchMethod = typeBuilder.DefineMethod(methodName, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes); if (!_method.IsStatic) { patchMethod.DefineParameter(0, ParameterAttributes.None, INSTANCE_PARAMETER); } for (var i = 0; i < parameters.Length; i++) { patchMethod.DefineParameter((patchMethod.IsStatic ? 0 : 1) + i, parameters[i].Attributes, parameters[i].Name); } var generator = new LoggingIlGenerator(patchMethod.GetILGenerator(), LogLevel.Trace); List <MsilInstruction> il = EmitPatched((type, pinned) => new MsilLocal(generator.DeclareLocal(type, pinned))).ToList(); MethodTranspiler.EmitMethod(il, generator); Type res = typeBuilder.CreateType(); asmBuilder.Save(Path.GetFileName(target)); foreach (var method in res.GetMethods(BindingFlags.Public | BindingFlags.Static)) { _log.Info($"Information " + method); } }
public DynamicMethod ComposePatchedMethod() { DynamicMethod method = AllocatePatchMethod(); var generator = new LoggingIlGenerator(method.GetILGenerator(), PrintMsil ? LogLevel.Info : LogLevel.Trace); List <MsilInstruction> il = EmitPatched((type, pinned) => new MsilLocal(generator.DeclareLocal(type, pinned))).ToList(); if (PrintMsil) { lock (_log) { MethodTranspiler.IntegrityAnalysis(LogLevel.Info, il); } } MethodTranspiler.EmitMethod(il, generator); try { PatchUtilities.Compile(method); } catch { lock (_log) { var ctx = new MethodContext(method); ctx.Read(); MethodTranspiler.IntegrityAnalysis(LogLevel.Warn, ctx.Instructions); } throw; } return(method); }
public DynamicMethod ComposePatchedMethod() { DynamicMethod method = AllocatePatchMethod(); var generator = new LoggingIlGenerator(method.GetILGenerator(), PrintMode.HasFlag(PrintModeEnum.EmittedReflection) ? LogLevel.Info : LogLevel.Trace); List <MsilInstruction> il = EmitPatched((type, pinned) => new MsilLocal(generator.DeclareLocal(type, pinned))).ToList(); var dumpTarget = DumpTarget != null?File.CreateText(DumpTarget) : null; try { const string gap = "\n\n\n\n\n"; void LogTarget(PrintModeEnum mode, bool err, string msg) { if (DumpMode.HasFlag(mode)) { dumpTarget?.WriteLine((err ? "ERROR " : "") + msg); } if (!PrintMode.HasFlag(mode)) { return; } if (err) { _log.Error(msg); } else { _log.Info(msg); } } if (PrintMsil || DumpTarget != null) { lock (_log) { var ctx = new MethodContext(_method); ctx.Read(); LogTarget(PrintModeEnum.Original, false, "========== Original method =========="); MethodTranspiler.IntegrityAnalysis((a, b) => LogTarget(PrintModeEnum.Original, a, b), ctx.Instructions, true); LogTarget(PrintModeEnum.Original, false, gap); LogTarget(PrintModeEnum.Emitted, false, "========== Desired method =========="); MethodTranspiler.IntegrityAnalysis((a, b) => LogTarget(PrintModeEnum.Emitted, a, b), il); LogTarget(PrintModeEnum.Emitted, false, gap); } } MethodTranspiler.EmitMethod(il, generator); try { PatchUtilities.Compile(method); } catch { lock (_log) { var ctx = new MethodContext(method); ctx.Read(); MethodTranspiler.IntegrityAnalysis((err, msg) => _log.Warn(msg), ctx.Instructions); } throw; } if (PrintMsil || DumpTarget != null) { lock (_log) { var ctx = new MethodContext(method); ctx.Read(); LogTarget(PrintModeEnum.Patched, false, "========== Patched method =========="); MethodTranspiler.IntegrityAnalysis((a, b) => LogTarget(PrintModeEnum.Patched, a, b), ctx.Instructions, true); LogTarget(PrintModeEnum.Patched, false, gap); } } } finally { dumpTarget?.Close(); } return(method); }