private static void InstanceTests() { MethodBase[] methods = { typeof(InstanceClassA).GetMethod("A", BindingFlags.Instance | BindingFlags.Public), typeof(InstanceClassA).GetMethod("B", BindingFlags.Instance | BindingFlags.Public), typeof(InstanceClassB).GetMethod("A", BindingFlags.Instance | BindingFlags.Public), typeof(InstanceClassB).GetMethod("B", BindingFlags.Instance | BindingFlags.Public) }; // Jit TestStaticReplaceJited RuntimeHelpers.PrepareMethod( typeof(Program).GetMethod("TestInstanceReplaceJited", BindingFlags.Static | BindingFlags.NonPublic).MethodHandle); // Replace StaticClassA.A() with StaticClassB.A() Console.WriteLine("Replacing InstanceClassA.A() with InstanceClassB.A()"); MethodUtil.ReplaceMethod(methods[2], methods[0]); // Call StaticClassA.A() from a method that has already been jited Console.WriteLine("Call InstanceClassA.A() from a method that has already been jited"); TestInstanceReplaceJited(); // Call StaticClassA.A() from a method that has not been jited Console.WriteLine("Call InstanceClassA.A() from a method that has not been jited"); TestInstanceReplace(); }
private void KillProfiler() { var keenStart = typeof(MySimpleProfiler).GetMethod("Begin"); var keenEnd = typeof(MySimpleProfiler).GetMethod("End"); var ourStart = typeof(Server).GetMethod("Begin"); var ourEnd = typeof(Server).GetMethod("End"); MethodUtil.ReplaceMethod(ourStart, keenStart); MethodUtil.ReplaceMethod(ourEnd, keenEnd); }
public static void Run() { // I think at a lower level this call uses the one below so may not be necessary MethodBase checkMethod = typeof(GraphicsAdapter).GetMethod("IsProfileSupported", BindingFlags.Instance | BindingFlags.Public); MethodBase replaceCheckMethod = typeof(GraphicsAdapterReplacement).GetMethod("IsProfileSupported", BindingFlags.Instance | BindingFlags.Public); MethodUtil.ReplaceMethod(replaceCheckMethod, checkMethod); // Disable the profile capabilities check MethodBase profileMethod = typeof(GraphicsAdapter).GetMethod("IsProfileSupported", BindingFlags.Instance | BindingFlags.NonPublic); MethodBase replaceProfileMethod = typeof(GraphicsAdapterReplacement).GetMethod("IsProfileSupported", BindingFlags.Instance | BindingFlags.NonPublic); MethodUtil.ReplaceMethod(replaceProfileMethod, profileMethod); }
public static void Init() { //ApplicationLog.BaseLog.Warn("Skipping profiler init"); //return; var entType = typeof(MyEntities); m_entitiesForUpdate = (MyDistributedUpdater <CachingList <MyEntity>, MyEntity>)entType.GetField("m_entitiesForUpdate", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); m_entitiesForUpdate10 = (MyDistributedUpdater <CachingList <MyEntity>, MyEntity>)entType.GetField("m_entitiesForUpdate10", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); m_entitiesForUpdate100 = (MyDistributedUpdater <CachingList <MyEntity>, MyEntity>)entType.GetField("m_entitiesForUpdate100", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); m_creationThread = (MyEntityCreationThread)entType.GetField("m_creationThread", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); var keenBefore = entType.GetMethod("UpdateBeforeSimulation"); var ourBefore = typeof(ProfilerInjection).GetMethod("UpdateBeforeSimulation"); var keenAfter = entType.GetMethod("UpdateAfterSimulation"); var ourAfter = typeof(ProfilerInjection).GetMethod("UpdateAfterSimulation"); //using (var md5 = MD5.Create()) //{ // var body = keenBefore.GetMethodBody().GetILAsByteArray(); // var hash = md5.ComputeHash(body); // string compStr = Convert.ToBase64String(hash).ToUpper(); // if (compStr!= "YXXKWK+W+OHP+LNOGGM32A==" || body.Length != 238) // { // BaseLog.Warn("UpdateBeforeSimulation signature has changed! Cannot load profiler injector!"); // BaseLog.Error($"{compStr}:{body.Length}"); // return; // } // body = keenAfter.GetMethodBody().GetILAsByteArray(); // hash = md5.ComputeHash(body); // compStr = Convert.ToBase64String(hash).ToUpper(); // if (compStr!= "7ZKFYWZRON9U3G43AV4QRA==" || body.Length != 220) // { // BaseLog.Warn("UpdateAfterSimulation signature has changed! Cannot load profiler injector!"); // BaseLog.Error($"{compStr}:{body.Length}"); // return; // } //} BaseLog.Info("Replacing UpdateBeforeSimulation"); MethodUtil.ReplaceMethod(ourBefore, keenBefore); BaseLog.Info("Replacing UpdateAfterSimulation"); MethodUtil.ReplaceMethod(ourAfter, keenAfter); }
private static void DynamicTests() { // Create dynamic method C in StaticClassA var dynamicMethod = CreateTestMethod(typeof(StaticClassA), "C"); // Get methodbase for class MethodBase method = typeof(StaticClassA).GetMethod("B", BindingFlags.Static | BindingFlags.Public); // Jit TestStaticReplaceJited RuntimeHelpers.PrepareMethod(typeof(Program) .GetMethod("TestDynamicReplaceJited", BindingFlags.Static | BindingFlags.NonPublic).MethodHandle); // Replace StaticClassA.B() with dynamic StaticClassA.C() Console.WriteLine("Replacing StaticClassA.B() with dynamic StaticClassA.C()"); MethodUtil.ReplaceMethod(dynamicMethod, method); // Call StaticClassA.B() from a method that has already been jited Console.WriteLine("Call StaticClassA.B() from a method that has already been jited"); TestDynamicReplaceJited(); // Call StaticClassA.B() from a method that has not been jited Console.WriteLine("Call StaticClassA.B() from a method that has not been jited"); TestDynamicReplace(); }