private static IEnumerable <CodeInstruction> Transpile(ILGenerator gen, MethodBase original, IEnumerable <CodeInstruction> insns)
        {
            var stage1 = CarpenterMenuPatcher.Transpile(gen, original, insns);

            var ret = new List <CodeInstruction>();

            foreach (var insn in stage1)
            {
                if (insn.opcode == OpCodes.Isinst && (Type)insn.operand == typeof(Farm))
                {
                    insn.operand = typeof(BuildableGameLocation);
                }
                ret.Add(insn);
            }

            return(ret);
        }
        /// <summary>The method to call before <see cref="CarpenterMenu.receiveLeftClick"/>.</summary>
        public static bool Before_ReceiveLeftClick(CarpenterMenu __instance, int x, int y, bool playSound)
        {
            //
            // Transpiling is difficult because of the lambda and some internal compiler nonsense
            //

            try
            {
                var __instance_freeze                = Mod.Instance.Helper.Reflection.GetField <bool>(__instance, "freeze");
                var __instance_onFarm                = Mod.Instance.Helper.Reflection.GetField <bool>(__instance, "onFarm");
                var __instance_moving                = Mod.Instance.Helper.Reflection.GetField <bool>(__instance, "moving");
                var __instance_upgrading             = Mod.Instance.Helper.Reflection.GetField <bool>(__instance, "upgrading");
                var __instance_demolishing           = Mod.Instance.Helper.Reflection.GetField <bool>(__instance, "demolishing");
                var __instance_buildingToMove        = Mod.Instance.Helper.Reflection.GetField <Building>(__instance, "buildingToMove");
                var __instance_currentBlueprintIndex = Mod.Instance.Helper.Reflection.GetField <int>(__instance, "currentBlueprintIndex");
                var __instance_price      = Mod.Instance.Helper.Reflection.GetField <int>(__instance, "price");
                var __instance_blueprints = Mod.Instance.Helper.Reflection.GetField <List <BluePrint> >(__instance, "blueprints");

                if (__instance_freeze.GetValue())
                {
                    goto ret;
                }
                if (!__instance_onFarm.GetValue())
                {
                    CarpenterMenuPatcher.base_receiveLeftClick(__instance, x, y, playSound);
                }
                if (__instance.cancelButton.containsPoint(x, y))
                {
                    if (!__instance_onFarm.GetValue())
                    {
                        __instance.exitThisMenu();
                        Game1.player.forceCanMove();
                        Game1.playSound("bigDeSelect");
                    }
                    else
                    {
                        if (__instance_moving.GetValue() && __instance_buildingToMove.GetValue() != null)
                        {
                            Game1.playSound("cancel");
                            goto ret;
                        }
                        __instance.returnToCarpentryMenu();
                        Game1.playSound("smallSelect");
                        goto ret;
                    }
                }
                if (!__instance_onFarm.GetValue() && __instance.backButton.containsPoint(x, y))
                {
                    __instance_currentBlueprintIndex.SetValue(__instance_currentBlueprintIndex.GetValue() - 1);
                    if (__instance_currentBlueprintIndex.GetValue() < 0)
                    {
                        __instance_currentBlueprintIndex.SetValue(__instance_blueprints.GetValue().Count - 1);
                    }
                    __instance.setNewActiveBlueprint();
                    Game1.playSound("shwip");
                    __instance.backButton.scale = __instance.backButton.baseScale;
                }
                if (!__instance_onFarm.GetValue() && __instance.forwardButton.containsPoint(x, y))
                {
                    __instance_currentBlueprintIndex.SetValue((__instance_currentBlueprintIndex.GetValue() + 1) % __instance_blueprints.GetValue().Count);
                    __instance.setNewActiveBlueprint();
                    __instance.backButton.scale = __instance.backButton.baseScale;
                    Game1.playSound("shwip");
                }
                if (!__instance_onFarm.GetValue() && __instance.demolishButton.containsPoint(x, y) && __instance.demolishButton.visible)
                {
                    Game1.globalFadeToBlack(__instance.setUpForBuildingPlacement);
                    Game1.playSound("smallSelect");
                    __instance_onFarm.SetValue(true);
                    __instance_demolishing.SetValue(true);
                }
                if (!__instance_onFarm.GetValue() && __instance.moveButton.containsPoint(x, y) && __instance.moveButton.visible)
                {
                    Game1.globalFadeToBlack(__instance.setUpForBuildingPlacement);
                    Game1.playSound("smallSelect");
                    __instance_onFarm.SetValue(true);
                    __instance_moving.SetValue(true);
                }
                if (__instance.okButton.containsPoint(x, y) && !__instance_onFarm.GetValue() && (Game1.player.Money >= __instance_price.GetValue() && __instance_blueprints.GetValue()[__instance_currentBlueprintIndex.GetValue()].doesFarmerHaveEnoughResourcesToBuild()))
                {
                    Game1.globalFadeToBlack(__instance.setUpForBuildingPlacement);
                    Game1.playSound("smallSelect");
                    __instance_onFarm.SetValue(true);
                }
                if (!__instance_onFarm.GetValue() || __instance_freeze.GetValue() || Game1.globalFade)
                {
                    goto ret;
                }
                if (__instance_demolishing.GetValue())
                {
                    // MINE - Farm -> BuildableGameLocation
                    BuildableGameLocation farm = CarpenterMenuPatcher.ReturnCurrentLocationAnyways("Farm") as BuildableGameLocation;
                    Building destroyed         = farm.getBuildingAt(new Vector2((Game1.viewport.X + Game1.getOldMouseX()) / 64, (Game1.viewport.Y + Game1.getOldMouseY()) / 64));

                    void BuildingLockFailed()
                    {
                        if (!__instance_demolishing.GetValue())
                        {
                            return;
                        }
                        Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_LockFailed"), Color.Red, 3500f));
                    }

                    void ContinueDemolish()
                    {
                        if (!__instance_demolishing.GetValue() || destroyed == null || !farm.buildings.Contains(destroyed))
                        {
                            return;
                        }
                        if (destroyed.daysOfConstructionLeft.Value > 0 || destroyed.daysUntilUpgrade.Value > 0)
                        {
                            Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_DuringConstruction"), Color.Red, 3500f));
                        }
                        else if (destroyed.indoors.Value is AnimalHouse house && house.animalsThatLiveHere.Count > 0)
                        {
                            Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_AnimalsHere"), Color.Red, 3500f));
                        }
                        else if (destroyed.indoors.Value != null && destroyed.indoors.Value.farmers.Any())
                        {
                            Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_PlayerHere"), Color.Red, 3500f));
                        }
                        else
                        {
                            if (destroyed.indoors.Value is Cabin cabin)
                            {
                                foreach (Farmer allFarmer in Game1.getAllFarmers())
                                {
                                    if (allFarmer.currentLocation.Name == cabin.GetCellarName())
                                    {
                                        Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_PlayerHere"), Color.Red, 3500f));
                                        return;
                                    }
                                }
                            }

                            if (destroyed.indoors.Value is Cabin cabinA && cabinA.farmhand.Value.isActive())
                            {
                                Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_FarmhandOnline"), Color.Red, 3500f));
                            }
                            else
                            {
                                Chest chest = null;
                                if (destroyed.indoors.Value is Cabin cabinB)
                                {
                                    List <Item> objList = cabinB.demolish();
                                    if (objList.Count > 0)
                                    {
                                        chest = new Chest(true);
                                        chest.fixLidFrame();
                                        chest.items.Set(objList);
                                    }
                                }

                                if (!farm.destroyStructure(destroyed))
                                {
                                    return;
                                }
                                Game1.flashAlpha = 1f;
                                destroyed.showDestroyedAnimation(Game1.getFarm());
                                Game1.playSound("explosion");
                                // function does nothing
                                //Utility.spreadAnimalsAround(destroyed, farm);
                                DelayedAction.functionAfterDelay(__instance.returnToCarpentryMenu, 1500);
                                __instance_freeze.SetValue(true);
                                if (chest == null)
                                {
                                    return;
                                }
                                farm.objects[new Vector2(destroyed.tileX.Value + destroyed.tilesWide.Value / 2, destroyed.tileY.Value + destroyed.tilesHigh.Value / 2)] = chest;
                            }
                        }
 /// <summary>The method which transpiles <see cref="CarpenterMenu.tryToBuild"/>.</summary>
 private static IEnumerable <CodeInstruction> Transpile_TryToBuild(ILGenerator gen, MethodBase original, IEnumerable <CodeInstruction> insns)
 {
     return(CarpenterMenuPatcher.Transpile(gen, original, insns));
 }