using System.Threading; using Global.Shared.Events; using HarmonyLib; using Sandbox.Definitions; using Sandbox.Game.Entities; using VRageMath; namespace Global.Shared.Patches { [HarmonyPatch(typeof(MyCubeGrid))] public class CubeGridPatch { private static readonly ThreadLocal CallDepth = new ThreadLocal(); public static ThreadLocal PlayerId = new ThreadLocal(); public static bool IsInMergeGridInternal => CallDepth.Value > 0; [HarmonyPrefix] [HarmonyPatch("MergeGridInternal")] private static bool MergeGridInternalPrefix() { // if (Config.Patches.DisableAllPatches || !Config.Patches.EnableGridMerge) return true; CallDepth.Value++; return true; } [HarmonyPostfix] [HarmonyPatch("MergeGridInternal")] private static void MergeGridInternalPostfix(MyCubeGrid __instance) { if (!IsInMergeGridInternal) return; if (--CallDepth.Value > 0) return; __instance.GridSystems.ConveyorSystem.FlagForRecomputation(); } [HarmonyPatch("CanPlaceBlock", typeof(Vector3I), typeof(Vector3I), typeof(MyBlockOrientation), typeof(MyCubeBlockDefinition), typeof(ulong), typeof(int?), typeof(bool), typeof(bool))] [HarmonyPostfix] private static void CanPlaceBlockPostfix(Vector3I min, Vector3I max, MyBlockOrientation orientation, MyCubeBlockDefinition definition, ulong placingPlayer = 0, int? ignoreMultiblockId = null, bool ignoreFracturedPieces = false, bool isProjection = false) { PlayerId.Value = placingPlayer; } [HarmonyPatch("CanAddCubes", typeof(Vector3I), typeof(Vector3I), typeof(MyBlockOrientation), typeof(MyCubeBlockDefinition))] [HarmonyPostfix] private static bool CanAddCubesPostfix(bool canPlace, MyCubeGrid __instance, Vector3I min, Vector3I max, MyBlockOrientation? orientation, MyCubeBlockDefinition definition) { BlockEvents.OnCanPlaceBlockEvent(__instance, orientation, definition, PlayerId.Value, ref canPlace); PlayerId.Value = 0; return canPlace; } } }