/// <summary>
        /// Primitive patching. Inserts a jump to 'target' at 'site'. Works even if both methods'
        /// callers have already been compiled.
        /// </summary>
        /// <param name="site"></param>
        /// <param name="target"></param>
        public static RedirectCallsState PatchJumpTo(IntPtr site, IntPtr target)
        {
            RedirectCallsState state = new RedirectCallsState();

            // R11 is volatile.
            unsafe
            {
                byte* sitePtr = (byte*)site.ToPointer();
                state.a = *sitePtr;
                state.b = *(sitePtr + 1);
                state.c = *(sitePtr + 10);
                state.d = *(sitePtr + 11);
                state.e = *(sitePtr + 12);
                state.f = *((ulong*)(sitePtr + 2));

                *sitePtr = 0x49; // mov r11, target
                *(sitePtr + 1) = 0xBB;
                *((ulong*)(sitePtr + 2)) = (ulong)target.ToInt64();
                *(sitePtr + 10) = 0x41; // jmp r11
                *(sitePtr + 11) = 0xFF;
                *(sitePtr + 12) = 0xE3;
            }

            return state;
        }
        public static void Deploy()
        {
            if (!deployed)
            {
                _InitializePrefab_original = typeof(BuildingInfo).GetMethod("InitializePrefab", BindingFlags.Instance | BindingFlags.Public);
                _InitializePrefab_detour = typeof(BuildingInfoDetour).GetMethod("InitializePrefab", BindingFlags.Instance | BindingFlags.Public);
                _InitializePrefab_state = RedirectionHelper.RedirectCalls(_InitializePrefab_original, _InitializePrefab_detour);

                deployed = true;

                BetterUpgradeMod.debugLog.Add("Better Upgrade: BuildingInfo Methods detoured!");
            }
        }
        public static void Deploy()
        {
            if (!deployed)
            {
                try {
                _PrivateBuildingAI_StartUpgrading_original = typeof(PrivateBuildingAI).GetMethod("StartUpgrading", BindingFlags.Instance | BindingFlags.NonPublic);
                _PrivateBuildingAI_StartUpgrading_detour = typeof(PrivateBuildingAIDetour).GetMethod("StartUpgrading", BindingFlags.Instance | BindingFlags.NonPublic);
                _PrivateBuildingAI_StartUpgrading_state = RedirectionHelper.RedirectCalls(_PrivateBuildingAI_StartUpgrading_original, _PrivateBuildingAI_StartUpgrading_detour);

                }
                catch (Exception e)
                {
                    BetterUpgradeMod.debugLog.Add("Detour 0 failed: " + e.Message + ":" + e.StackTrace);
                }

                deployed = true;

                BetterUpgradeMod.debugLog.Add("Better Upgrade: PrivateBuildingAI Methods detoured!");
            }
        }
 public static void RevertRedirect(MethodInfo from, RedirectCallsState state)
 {
     var fptr1 = from.MethodHandle.GetFunctionPointer();
     RevertJumpTo(fptr1, state);
 }
 public static void RevertJumpTo(IntPtr site, RedirectCallsState state)
 {
     unsafe
     {
         byte* sitePtr = (byte*)site.ToPointer();
         *sitePtr = state.a; // mov r11, target
         *(sitePtr + 1) = state.b;
         *((ulong*)(sitePtr + 2)) = state.f;
         *(sitePtr + 10) = state.c; // jmp r11
         *(sitePtr + 11) = state.d;
         *(sitePtr + 12) = state.e;
     }
 }