Files
QuartZ-dump/GlobalShared/Patches/CubeGridPatch.cs
2024-12-29 21:15:58 +01:00

66 lines
2.3 KiB
C#

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<int> CallDepth = new ThreadLocal<int>();
public static ThreadLocal<ulong> PlayerId = new ThreadLocal<ulong>();
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;
}
}
}