From b51696fb4537bfb847bf7a0458929970e89ec8de Mon Sep 17 00:00:00 2001 From: Jonathan Riedel Date: Sun, 8 Jun 2025 21:39:23 +0200 Subject: [PATCH] Code updates --- DisableShelves.csproj | 8 +- Extensions.cs | 90 +++++++++++++++++ ...nager_GetLabeledEmptyDisplaySlots_Patch.cs | 18 ++++ Patches/Label_ClearLabel_Patch.cs | 47 +++++++++ ...r_IsDisplaySlotAvailableToRestock_Patch.cs | 17 ++++ Plugin.cs | 99 +++++-------------- 6 files changed, 203 insertions(+), 76 deletions(-) create mode 100644 Extensions.cs create mode 100644 Patches/DisplayManager_GetLabeledEmptyDisplaySlots_Patch.cs create mode 100644 Patches/Label_ClearLabel_Patch.cs create mode 100644 Patches/Restocker_IsDisplaySlotAvailableToRestock_Patch.cs diff --git a/DisableShelves.csproj b/DisableShelves.csproj index a12ba8a..8f08de1 100644 --- a/DisableShelves.csproj +++ b/DisableShelves.csproj @@ -1,16 +1,20 @@ - + netstandard2.1 DisableShelves My first plugin - 1.0.0 + 1.1.0 true latest + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Extensions.cs b/Extensions.cs new file mode 100644 index 0000000..62513ce --- /dev/null +++ b/Extensions.cs @@ -0,0 +1,90 @@ +using MyBox; +using System.Collections.Generic; +using UnityEngine; + +namespace DisableShelves +{ + public static class Extensions + { + public static string GetColor(this Label label) + { + return label.gameObject.transform.GetChild(0).GetChild(0).GetComponent().material.color.ToHex(); + } + public static object GetField(this object obj, string field) + { + return obj.GetType().GetField(field, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(obj); + } + public static Label GetLabel(this DisplaySlot slot) + { + return ((Label)typeof(DisplaySlot).GetField("m_Label", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(slot)); + } + public static string ToGUID(this Label label) + { + return label.transform.position.x.ToString("0.0000") + ":" + label.transform.position.y.ToString("0.0000") + ":" + label.transform.position.z.ToString("0.0000"); + } + public static void Disable(this Label __instance, bool all = false) + { + if (all) + { + ((DisplaySlot[])__instance.DisplaySlot.Display.GetField("m_DisplaySlots")).ForEach(x => x.GetLabel().Disable(false)); + return; + } + + Plugin.StaticLogger.LogInfo("Disabling " + __instance.ToGUID()); + var cube = __instance.gameObject.transform.GetChild(0).GetChild(0); + var canvasbg = __instance.gameObject.transform.GetChild(1).GetChild(0); + + var mat = cube.GetComponent().material; + mat.color = Plugin.RedColor.ToUnityColor(); + + List disabled = Plugin.DisabledLabelsGUIDs; + if (!disabled.Contains(__instance.ToGUID())) + disabled.Add(__instance.ToGUID()); + Plugin.DisabledLabelsGUIDs = disabled; + } + public static void Enable(this Label __instance, bool all = false) + { + if (all) + { + ((DisplaySlot[])__instance.DisplaySlot.Display.GetField("m_DisplaySlots")).ForEach(x => x.GetLabel().Enable(false)); + return; + } + + Plugin.StaticLogger.LogInfo("Enabling " + __instance.ToGUID()); + var cube = __instance.gameObject.transform.GetChild(0).GetChild(0); + var canvasbg = __instance.gameObject.transform.GetChild(1).GetChild(0); + + var mat = cube.GetComponent().material; + mat.color = Plugin.YellowColor.ToUnityColor(); + + List disabled = Plugin.DisabledLabelsGUIDs; + disabled.Remove(__instance.ToGUID()); + Plugin.DisabledLabelsGUIDs = disabled; + } + public static bool IsEnabled(this Label __instance) + { + var fi_m_DisplaySlot = typeof(Label).GetField("m_DisplaySlot", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + var displaySlot = ((DisplaySlot)fi_m_DisplaySlot.GetValue(__instance)); + if (displaySlot != null) + { + if (displaySlot.HasProduct) + { + var cube = __instance.gameObject.transform.GetChild(0).GetChild(0); + var canvasbg = __instance.gameObject.transform.GetChild(1).GetChild(0); + + var mat = cube.GetComponent().material; + var color = mat.color.ToHex(); + Plugin.StaticLogger.LogInfo(__instance.ToGUID() + ": Color is " + color); + var enabled = color != Plugin.RedColor; + if (enabled && Plugin.DisabledLabelsGUIDs.Contains(__instance.ToGUID())) + { + __instance.Disable(); + enabled = false; + } + return enabled; + } + } + return true; + } + } +} diff --git a/Patches/DisplayManager_GetLabeledEmptyDisplaySlots_Patch.cs b/Patches/DisplayManager_GetLabeledEmptyDisplaySlots_Patch.cs new file mode 100644 index 0000000..3bf6197 --- /dev/null +++ b/Patches/DisplayManager_GetLabeledEmptyDisplaySlots_Patch.cs @@ -0,0 +1,18 @@ +using HarmonyLib; +using System.Collections.Generic; +using System.Linq; + +namespace DisableShelves +{ + public static partial class ShelfLabelPatch + { + [HarmonyPatch(typeof(DisplayManager), "GetLabeledEmptyDisplaySlots")] + public static class DisplayManager_GetLabeledEmptyDisplaySlots_Patch + { + public static void Postfix(ref List __result) + { + __result = __result.Where(x => x.GetLabel().IsEnabled()).ToList(); + } + } + } +} diff --git a/Patches/Label_ClearLabel_Patch.cs b/Patches/Label_ClearLabel_Patch.cs new file mode 100644 index 0000000..9507ba2 --- /dev/null +++ b/Patches/Label_ClearLabel_Patch.cs @@ -0,0 +1,47 @@ +using BepInEx.Configuration; +using HarmonyLib; +using UnityEngine; + +namespace DisableShelves +{ + public static partial class ShelfLabelPatch + { + [HarmonyPatch(typeof(Label), "SetProductIcon")] + public static class Label_SetProductIcon_Patch + { + public static void Prefix(Label __instance, int productID) + { + __instance.IsEnabled(); + } + } + [HarmonyPatch(typeof(Label), "ClearLabel")] + [HarmonyPriority(10000)] + public static class Label_ClearLabel_Patch + { + public static bool Prefix(Label __instance) + { + Plugin.StaticLogger.LogInfo("Label: " + __instance.ToGUID()); + Plugin.StaticLogger.LogInfo("Running prefix patcher"); + var fi_m_DisplaySlot = typeof(Label).GetField("m_DisplaySlot", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + var displaySlot = ((DisplaySlot)fi_m_DisplaySlot.GetValue(__instance)); + if (displaySlot != null) + { + if (displaySlot.HasProduct) + { + bool all = Plugin.MultiModeShortcut.Value.IsPressed(); + Plugin.StaticLogger.LogInfo("Multi Mode Shortcut pressed: " + all); + Plugin.StaticLogger.LogInfo("Label is shelf"); + if (__instance.IsEnabled()) + __instance.Disable(all); + else + __instance.Enable(all); + + Plugin.StaticLogger.LogWarning("Returning false"); + return false; + } + } + return true; + } + } + } +} diff --git a/Patches/Restocker_IsDisplaySlotAvailableToRestock_Patch.cs b/Patches/Restocker_IsDisplaySlotAvailableToRestock_Patch.cs new file mode 100644 index 0000000..f664566 --- /dev/null +++ b/Patches/Restocker_IsDisplaySlotAvailableToRestock_Patch.cs @@ -0,0 +1,17 @@ +using HarmonyLib; + +namespace DisableShelves +{ + public static partial class ShelfLabelPatch + { + [HarmonyPatch(typeof(Restocker), "IsDisplaySlotAvailableToRestock")] + public static class Restocker_IsDisplaySlotAvailableToRestock_Patch + { + public static void Postfix(DisplaySlot displaySlot, ref bool __result) + { + if (!__result) return; + if (!displaySlot.GetLabel().IsEnabled()) __result = false; + } + } + } +} diff --git a/Plugin.cs b/Plugin.cs index b0fa2c0..83f546c 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -1,4 +1,5 @@ using BepInEx; +using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using MyBox; @@ -6,32 +7,43 @@ using System; using System.Collections.Generic; using System.Linq; using UnityEngine; +using UnityEngine.SceneManagement; using UnityEngine.UI; namespace DisableShelves { - public static class Extensions - { - public static string GetColor(this Label label) - { - return label.gameObject.transform.GetChild(0).GetChild(0).GetComponent().material.color.ToHex(); - } - public static Label GetLabel(this DisplaySlot slot) - { - return ((Label)typeof(DisplaySlot).GetField("m_Label", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(slot)); - } - } [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] public class Plugin : BaseUnityPlugin { + public const string YellowColor = "#FFDF00"; + public const string RedColor = "#FF0000"; public static List