From 1853233dd30e7e5748570a82ec637bda95ffe0c1 Mon Sep 17 00:00:00 2001 From: Tyfon <29051038+tyfon7@users.noreply.github.com> Date: Mon, 20 May 2024 02:14:36 -0700 Subject: [PATCH] Code cleanup --- Patches/ContainerStackPatch.cs | 4 +- Patches/DialogPatches.cs | 8 ++- Patches/DisabledActionsPatch.cs | 7 ++- Patches/EditBuildScreenPatch.cs | 12 ++-- Patches/ItemPanelPatches.cs | 92 +++++++++++++++---------------- Patches/ItemPanelResizePatches.cs | 16 +++--- Patches/MailReceiveAllPatch.cs | 6 +- Patches/ProductionPatch.cs | 29 +++++----- Patches/ScrollPatches.cs | 37 +++++-------- Patches/ScrollSyncPatches.cs | 12 ++-- Patches/SwapPatch.cs | 62 +++++++++------------ Patches/TooltipPatch.cs | 12 ++-- Patches/TransferConfirmPatch.cs | 9 ++- Patches/WeaponBindingPatch.cs | 7 ++- Patches/WeaponZoomPatch.cs | 29 +++------- Plugin.cs | 2 +- Settings.cs | 3 +- 17 files changed, 154 insertions(+), 193 deletions(-) diff --git a/Patches/ContainerStackPatch.cs b/Patches/ContainerStackPatch.cs index ab98260..dae5ae5 100644 --- a/Patches/ContainerStackPatch.cs +++ b/Patches/ContainerStackPatch.cs @@ -14,14 +14,14 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { - MethodInfo method = AccessTools.Method(typeof(InteractionsHandlerClass), "smethod_0"); + MethodInfo method = AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.smethod_0)); MergeableItemType = method.GetParameters()[2].ParameterType.GetElementType(); // parameter is a ref type, get underlying type return method; } // Reimplementing this entire method to ignore SpawnedInSession for certain types [PatchPrefix] - private static bool Prefix(IEnumerable gridsToPut, Item itemToMerge, ref object mergeableItem, int overrideCount, ref bool __result) + public static bool Prefix(IEnumerable gridsToPut, Item itemToMerge, ref object mergeableItem, int overrideCount, ref bool __result) { if (!MergeableItemType.IsInstanceOfType(itemToMerge)) { diff --git a/Patches/DialogPatches.cs b/Patches/DialogPatches.cs index dcb5c21..7b85b7c 100644 --- a/Patches/DialogPatches.cs +++ b/Patches/DialogPatches.cs @@ -32,7 +32,7 @@ namespace UIFixes } [PatchPostfix] - private static void Postfix(object __instance, bool ___bool_0) + public static void Postfix(object __instance, bool ___bool_0) { if (!___bool_0) { @@ -52,11 +52,13 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { // The parent has a Show() so need to be specific - return typeof(HandoverRagfairMoneyWindow).GetMethods().First(m => m.Name == "Show" && m.GetParameters()[0].ParameterType == typeof(Inventory)); + return AccessTools.GetDeclaredMethods(typeof(HandoverRagfairMoneyWindow)).First( + m => m.Name == nameof(HandoverRagfairMoneyWindow.Show) && + m.GetParameters()[0].ParameterType == typeof(Inventory)); } [PatchPostfix] - private static void Postfix(TMP_InputField ____inputField) + public static void Postfix(TMP_InputField ____inputField) { ____inputField.contentType = TMP_InputField.ContentType.IntegerNumber; ____inputField.ActivateInputField(); diff --git a/Patches/DisabledActionsPatch.cs b/Patches/DisabledActionsPatch.cs index 522b4b3..e7ef687 100644 --- a/Patches/DisabledActionsPatch.cs +++ b/Patches/DisabledActionsPatch.cs @@ -8,19 +8,20 @@ namespace UIFixes { public class DisabledActionsPatch : ModulePatch { - private static string[] UnimplementedActions = ["Bang & clear", "Flash & clear", "Move in"]; + private static readonly string[] UnimplementedActions = ["Bang & clear", "Flash & clear", "Move in"]; + protected override MethodBase GetTargetMethod() { Type type = typeof(GetActionsClass); return AccessTools.GetDeclaredMethods(type).FirstOrDefault(x => { var parameters = x.GetParameters(); - return x.Name == "GetAvailableActions" && parameters[0].Name == "owner"; + return x.Name == nameof(GetActionsClass.GetAvailableActions) && parameters[0].Name == "owner"; }); } [PatchPostfix] - private static void Postfix(ref ActionsReturnClass __result) + public static void Postfix(ref ActionsReturnClass __result) { if (Settings.RemoveDisabledActions.Value && __result != null) { diff --git a/Patches/EditBuildScreenPatch.cs b/Patches/EditBuildScreenPatch.cs index 1dbdc4a..addf8ec 100644 --- a/Patches/EditBuildScreenPatch.cs +++ b/Patches/EditBuildScreenPatch.cs @@ -1,5 +1,6 @@ using Aki.Reflection.Patching; using EFT.UI; +using HarmonyLib; using System; using System.Linq; using System.Reflection; @@ -25,11 +26,11 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { Type type = typeof(EditBuildScreen).GetNestedTypes().Single(x => x.GetMethod("CloseScreenInterruption") != null); // EditBuildScreen.GClass3126 - return type.GetMethod("CloseScreenInterruption"); + return AccessTools.Method(type, "CloseScreenInterruption"); } [PatchPrefix] - private static void Prefix(bool moveForward) + public static void Prefix(bool moveForward) { MoveForward = moveForward; } @@ -39,12 +40,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(EditBuildScreen); - return type.GetMethod("method_35"); + return AccessTools.Method(typeof(EditBuildScreen), nameof(EditBuildScreen.method_35)); } [PatchPrefix] - private static bool Prefix(ref Task __result) + public static bool Prefix(ref Task __result) { if (MoveForward && Settings.ShowPresetConfirmations.Value == WeaponPresetConfirmationOption.Always) { @@ -56,7 +56,7 @@ namespace UIFixes return true; } - __result = Task.FromResult(true); + __result = Task.FromResult(true); return false; } } diff --git a/Patches/ItemPanelPatches.cs b/Patches/ItemPanelPatches.cs index 50aca0e..0bec153 100644 --- a/Patches/ItemPanelPatches.cs +++ b/Patches/ItemPanelPatches.cs @@ -41,32 +41,30 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(ItemSpecificationPanel), "method_5"); + return AccessTools.Method(typeof(ItemSpecificationPanel), nameof(ItemSpecificationPanel.method_5)); } [PatchPostfix] - private static void Postfix( + public static void Postfix( ItemSpecificationPanel __instance, Item ___item_0, CompactCharacteristicPanel ____compactCharTemplate, Transform ____compactPanel, SimpleTooltip ___simpleTooltip_0) { - if (!Settings.ShowModStats.Value || !(___item_0 is Mod)) + if (!Settings.ShowModStats.Value || ___item_0 is not Mod) { return; } - bool changed; - var deepAttributes = GetDeepAttributes(___item_0, out changed); + var deepAttributes = GetDeepAttributes(___item_0, out bool changed); if (!changed) { return; } - var compactPanels = AttributeCompactPanelDictionaryField.GetValue(__instance) as IDisposable; // Clean up existing one - if (compactPanels != null) + if (AttributeCompactPanelDictionaryField.GetValue(__instance) is IDisposable compactPanels) { compactPanels.Dispose(); } @@ -110,13 +108,13 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { - RefreshStaticMethod = AccessTools.Method(typeof(ItemSpecificationPanel), "smethod_1", null, [typeof(CompactCharacteristicPanel)]); + RefreshStaticMethod = AccessTools.Method(typeof(ItemSpecificationPanel), nameof(ItemSpecificationPanel.smethod_1), null, [typeof(CompactCharacteristicPanel)]); - return AccessTools.Method(typeof(ItemSpecificationPanel), "method_6"); + return AccessTools.Method(typeof(ItemSpecificationPanel), nameof(ItemSpecificationPanel.method_6)); } [PatchPrefix] - private static void Prefix(Item compareItem) + public static void Prefix(Item compareItem) { if (compareItem == null) { @@ -129,26 +127,27 @@ namespace UIFixes { float maxDurability = armorComponents.Sum(c => c.Repairable.Durability); - ItemAttributeClass itemAttributeClass = new ItemAttributeClass(EItemAttributeId.ArmorPoints); - itemAttributeClass.Name = EItemAttributeId.ArmorPoints.GetName(); - itemAttributeClass.Base = () => maxDurability; - itemAttributeClass.StringValue = () => Math.Round(maxDurability, 1).ToString(CultureInfo.InvariantCulture); - itemAttributeClass.DisplayType = () => EItemAttributeDisplayType.Compact; + var itemAttributeClass = new ItemAttributeClass(EItemAttributeId.ArmorPoints) + { + Name = EItemAttributeId.ArmorPoints.GetName(), + Base = () => maxDurability, + StringValue = () => Math.Round(maxDurability, 1).ToString(CultureInfo.InvariantCulture), + DisplayType = () => EItemAttributeDisplayType.Compact + }; compareItem.Attributes.Insert(0, itemAttributeClass); } } [PatchPostfix] - private static void Postfix(ItemSpecificationPanel __instance, Item compareItem) + public static void Postfix(ItemSpecificationPanel __instance, Item compareItem) { - if (!Settings.ShowModStats.Value || !(compareItem is Mod)) + if (!Settings.ShowModStats.Value || compareItem is not Mod) { return; } - bool changed; - List deepAttributes = GetDeepAttributes(compareItem, out changed); + List deepAttributes = GetDeepAttributes(compareItem, out bool changed); if (!changed) { return; @@ -179,7 +178,7 @@ namespace UIFixes SimpleContextMenuButtonTextField = AccessTools.Field(typeof(ContextMenuButton), "_text"); - return AccessTools.Method(typeof(ItemSpecificationPanel), "method_4"); + return AccessTools.Method(typeof(ItemSpecificationPanel), nameof(ItemSpecificationPanel.method_4)); } private static string GetLabel() @@ -188,9 +187,9 @@ namespace UIFixes } [PatchPostfix] - private static void Postfix(ItemSpecificationPanel __instance, ItemInfoInteractionsAbstractClass contextInteractions, Item ___item_0, InteractionButtonsContainer ____interactionButtonsContainer) + public static void Postfix(ItemSpecificationPanel __instance, ItemInfoInteractionsAbstractClass contextInteractions, Item ___item_0, InteractionButtonsContainer ____interactionButtonsContainer) { - if (!(___item_0 is Mod)) + if (___item_0 is not Mod) { return; } @@ -200,25 +199,28 @@ namespace UIFixes SimpleContextMenuButton toggleButton = null; - Action onClick = () => Settings.ShowModStats.Value = !Settings.ShowModStats.Value; - // Listen to the setting and the work there to handle multiple windows open at once - EventHandler onSettingChanged = (sender, args) => + void onSettingChanged(object sender, EventArgs args) { var text = SimpleContextMenuButtonTextField.GetValue(toggleButton) as TextMeshProUGUI; text.text = GetLabel(); __instance.method_5(); // rebuild stat panels - }; + } Settings.ShowModStats.SettingChanged += onSettingChanged; - Action createButton = () => + static void onClick() + { + Settings.ShowModStats.Value = !Settings.ShowModStats.Value; + } + + void createButton() { Sprite sprite = CacheResourcesPopAbstractClass.Pop("Characteristics/Icons/Modding"); toggleButton = UnityEngine.Object.Instantiate(template, transform, false); toggleButton.Show(GetLabel(), null, sprite, onClick, null); ____interactionButtonsContainer.method_5(toggleButton); // add to disposable list - }; + } // Subscribe to redraws to recreate when mods get dropped in contextInteractions.OnRedrawRequired += createButton; @@ -238,11 +240,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(CompactCharacteristicPanel), "SetValues"); + return AccessTools.Method(typeof(CompactCharacteristicPanel), nameof(CompactCharacteristicPanel.SetValues)); } [PatchPostfix] - private static void Postfix(CompactCharacteristicPanel __instance, TextMeshProUGUI ___ValueText) + public static void Postfix(CompactCharacteristicPanel __instance, TextMeshProUGUI ___ValueText) { try { @@ -262,14 +264,14 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { - RoundToIntMethod = AccessTools.Method(typeof(Mathf), "RoundToInt"); - ToStringMethod = AccessTools.Method(typeof(float), "ToString", [typeof(string)]); + RoundToIntMethod = AccessTools.Method(typeof(Mathf), nameof(Mathf.RoundToInt)); + ToStringMethod = AccessTools.Method(typeof(float), nameof(float.ToString), [typeof(string)]); - return AccessTools.Method(typeof(CharacteristicPanel), "SetValues"); + return AccessTools.Method(typeof(CharacteristicPanel), nameof(CharacteristicPanel.SetValues)); } [PatchPostfix] - private static void Postfix(CharacteristicPanel __instance, TextMeshProUGUI ___ValueText) + public static void Postfix(CharacteristicPanel __instance, TextMeshProUGUI ___ValueText) { try { @@ -283,7 +285,7 @@ namespace UIFixes // This transpiler looks for where it rounds a float to an int, and skips that. Instead it calls ToString("0.0#") on it [PatchTranspiler] - private static IEnumerable Transpile(IEnumerable instructions) + public static IEnumerable Transpile(IEnumerable instructions) { int skip = 0; CodeInstruction lastInstruction = null; @@ -351,18 +353,15 @@ namespace UIFixes // Completely redo it if ((EItemAttributeId)attribute.Id == EItemAttributeId.CenterOfImpact) { - ItemAttributeClass compareAttribute = CompactCharacteristicPanelCompareItemAttributeField.GetValue(panel) as ItemAttributeClass; - if (compareAttribute != null) + if (CompactCharacteristicPanelCompareItemAttributeField.GetValue(panel) is ItemAttributeClass compareAttribute) { string currentStringValue = attribute.StringValue(); var moaMatch = Regex.Match(currentStringValue, @"^(\S+)"); - float moa; - if (float.TryParse(moaMatch.Groups[1].Value, out moa)) + if (float.TryParse(moaMatch.Groups[1].Value, out float moa)) { string compareStringValue = compareAttribute.StringValue(); moaMatch = Regex.Match(compareStringValue, @"^(\S+)"); - float compareMoa; - if (float.TryParse(moaMatch.Groups[1].Value, out compareMoa)) + if (float.TryParse(moaMatch.Groups[1].Value, out float compareMoa)) { float delta = compareMoa - moa; string final = currentStringValue; @@ -384,9 +383,8 @@ namespace UIFixes var match = Regex.Match(text, @" %\(([+-].*)\)"); if (match.Success) { - float value; // If this fails to parse, I don't know what it is, leave it be - if (float.TryParse(match.Groups[1].Value, out value)) + if (float.TryParse(match.Groups[1].Value, out float value)) { string sign = value > 0 ? "+" : ""; string color = (attribute.LessIsGood && value < 0) || (!attribute.LessIsGood && value > 0) ? IncreasingColorHex : DecreasingColorHex; @@ -408,9 +406,8 @@ namespace UIFixes match = Regex.Match(text, @"(\S)%\(([+-].*)\)"); if (match.Success) { - float value; // If this fails to parse, I don't know what it is, leave it be - if (float.TryParse(match.Groups[2].Value, out value)) + if (float.TryParse(match.Groups[2].Value, out float value)) { string sign = value > 0 ? "+" : ""; string color = (attribute.LessIsGood && value < 0) || (!attribute.LessIsGood && value > 0) ? IncreasingColorHex : DecreasingColorHex; @@ -423,9 +420,8 @@ namespace UIFixes match = Regex.Match(text, @"\(([+-].*)\)"); if (match.Success) { - float value; // If this fails to parse, I don't know what it is, leave it be - if (float.TryParse(match.Groups[1].Value, out value)) + if (float.TryParse(match.Groups[1].Value, out float value)) { string sign = value > 0 ? "+" : ""; string color = (attribute.LessIsGood && value < 0) || (!attribute.LessIsGood && value > 0) ? IncreasingColorHex : DecreasingColorHex; @@ -471,7 +467,7 @@ namespace UIFixes private static IEnumerable CombineAttributes(IList first, IList second) { - foreach (EItemAttributeId id in first.Select(a => a.Id).Union(second.Select(a => a.Id))) + foreach (EItemAttributeId id in first.Select(a => a.Id).Union(second.Select(a => a.Id)).Select(v => (EItemAttributeId)v)) { // Need to cast the id since it's of type Enum for some reason var attribute = first.FirstOrDefault(a => (EItemAttributeId)a.Id == id); diff --git a/Patches/ItemPanelResizePatches.cs b/Patches/ItemPanelResizePatches.cs index 1ff8af0..4765c69 100644 --- a/Patches/ItemPanelResizePatches.cs +++ b/Patches/ItemPanelResizePatches.cs @@ -35,11 +35,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(StretchArea), "OnDrag"); + return AccessTools.Method(typeof(StretchArea), nameof(StretchArea.OnDrag)); } [PatchPostfix] - private static void Postfix(LayoutElement ___layoutElement_0) + public static void Postfix(LayoutElement ___layoutElement_0) { if (!Settings.RememberInspectSize.Value || ___layoutElement_0.GetComponent() == null) { @@ -62,11 +62,11 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(ItemSpecificationPanel), "Show"); + return AccessTools.Method(typeof(ItemSpecificationPanel), nameof(ItemSpecificationPanel.Show)); } [PatchPrefix] - private static void Prefix(LayoutElement ___layoutElement_0) + public static void Prefix(LayoutElement ___layoutElement_0) { if (Settings.RememberInspectSize.Value) { @@ -75,7 +75,7 @@ namespace UIFixes } [PatchPostfix] - private static void Postfix(ItemSpecificationPanel __instance, LayoutElement ___layoutElement_0) + public static void Postfix(ItemSpecificationPanel __instance, LayoutElement ___layoutElement_0) { if (Settings.LockInspectPreviewSize.Value) { @@ -191,7 +191,7 @@ namespace UIFixes LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRect); - layoutRect.CorrectPositionResolution(default(MarginsStruct)); + layoutRect.CorrectPositionResolution(default); } } @@ -217,11 +217,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(ItemUiContext), "Inspect"); + return AccessTools.Method(typeof(ItemUiContext), nameof(ItemUiContext.Inspect)); } [PatchPostfix] - private static void Postfix(List ____children) + public static void Postfix(List ____children) { var inspectWindow = ____children.Last(); if (inspectWindow != null) diff --git a/Patches/MailReceiveAllPatch.cs b/Patches/MailReceiveAllPatch.cs index 14e8055..fe7b364 100644 --- a/Patches/MailReceiveAllPatch.cs +++ b/Patches/MailReceiveAllPatch.cs @@ -1,5 +1,6 @@ using Aki.Reflection.Patching; using EFT.UI.Chat; +using HarmonyLib; using System; using System.Reflection; @@ -9,12 +10,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(ChatMessageSendBlock); - return type.GetMethod("Show", BindingFlags.Public | BindingFlags.Instance); + return AccessTools.Method(typeof(ChatMessageSendBlock), nameof(ChatMessageSendBlock.Show)); } [PatchPrefix] - private static void Prefix(DialogueClass dialogue) + public static void Prefix(DialogueClass dialogue) { // Force this false will recalculate each time. This is less than ideal, but the way the code is structured makes it very difficult to do correctly. dialogue.HasMessagesWithRewards = false; diff --git a/Patches/ProductionPatch.cs b/Patches/ProductionPatch.cs index f9eb819..7ae96f4 100644 --- a/Patches/ProductionPatch.cs +++ b/Patches/ProductionPatch.cs @@ -15,7 +15,7 @@ namespace UIFixes private static FieldInfo ProductionPanelSearch; private static FieldInfo SubstrateContentLayoutField; - private static Dictionary LastSearches = []; + private static readonly Dictionary LastSearches = []; public static void Enable() { @@ -45,7 +45,7 @@ namespace UIFixes } [PatchPostfix] - private static void Postfix(ProductionPanel.Class1631 __instance, GClass1923 scheme, ProduceView view) + public static void Postfix(ProductionPanel.Class1631 __instance, GClass1923 scheme, ProduceView view) { var searchField = ProductionPanelSearch.GetValue(__instance.productionPanel_0) as ValidationInputField; if (searchField.text.Length > 0 && scheme.endProduct.LocalizedName().IndexOf(searchField.text, StringComparison.InvariantCultureIgnoreCase) < 0) @@ -61,21 +61,20 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(ProductionPanel), "ShowContents"); + return AccessTools.Method(typeof(ProductionPanel), nameof(ProductionPanel.ShowContents)); } [PatchPrefix] - private static void Prefix(ProductionPanel __instance, ValidationInputField ____searchInputField) + public static void Prefix(ProductionPanel __instance, ValidationInputField ____searchInputField) { - string lastSearch; - if (LastSearches.TryGetValue(__instance.AreaData.ToString(), out lastSearch)) + if (LastSearches.TryGetValue(__instance.AreaData.ToString(), out string lastSearch)) { ____searchInputField.text = lastSearch; } } [PatchPostfix] - private static void Postfix(ProductionPanel __instance, ValidationInputField ____searchInputField) + public static void Postfix(ProductionPanel __instance, ValidationInputField ____searchInputField) { // Force it to render immediately, at full height, even if the search filtering would reduce the number of children if (__instance.method_4().Count() > 2) @@ -96,13 +95,13 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(ProductionPanel), "method_4"); + return AccessTools.Method(typeof(ProductionPanel), nameof(ProductionPanel.method_4)); } // Working with GClasses directly here, because this would be a nightmare with reflection // Copied directly from method_4 [PatchPrefix] - private static bool Prefix(ref IEnumerable __result, ProductionPanel __instance, GClass1922[] ___gclass1922_0, ValidationInputField ____searchInputField) + public static bool Prefix(ref IEnumerable __result, ProductionPanel __instance, GClass1922[] ___gclass1922_0, ValidationInputField ____searchInputField) { __result = ___gclass1922_0.OfType().Where(scheme => !scheme.locked) .OrderBy(scheme => scheme.endProduct.LocalizedName().Contains(____searchInputField.text) ? 0 : 1) // search-matching items first @@ -119,11 +118,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(ProductionPanel), "method_9"); + return AccessTools.Method(typeof(ProductionPanel), nameof(ProductionPanel.method_9)); } [PatchPrefix] - private static void Prefix(ProductionPanel __instance) + public static void Prefix(ProductionPanel __instance) { __instance.method_8(); // update sort order } @@ -134,11 +133,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(ProductionPanel), "Close"); + return AccessTools.Method(typeof(ProductionPanel), nameof(ProductionPanel.Close)); } [PatchPrefix] - private static void Prefix(ProductionPanel __instance, ValidationInputField ____searchInputField) + public static void Prefix(ProductionPanel __instance, ValidationInputField ____searchInputField) { LastSearches[__instance.AreaData.ToString()] = ____searchInputField.text; @@ -154,11 +153,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(HideoutScreenOverlay), "ReturnToPreviousState"); + return AccessTools.Method(typeof(HideoutScreenOverlay), nameof(HideoutScreenOverlay.ReturnToPreviousState)); } [PatchPostfix] - private static void Postfix() + public static void Postfix() { LastSearches.Clear(); } diff --git a/Patches/ScrollPatches.cs b/Patches/ScrollPatches.cs index 89975e5..3ee50bb 100644 --- a/Patches/ScrollPatches.cs +++ b/Patches/ScrollPatches.cs @@ -126,23 +126,18 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(SimpleStashPanel); - return type.GetMethod("Update", BindingFlags.Public | BindingFlags.Instance); + return AccessTools.Method(typeof(SimpleStashPanel), nameof(SimpleStashPanel.Update)); } [PatchPrefix] - private static void Prefix(SimpleStashPanel __instance, ScrollRect ____stashScroll) + public static void Prefix(SimpleStashPanel __instance, ScrollRect ____stashScroll) { - if (____stashScroll == null) - { - // For some reason, sometimes SimpleStashPanel doesn't have a reference to its own ScrollRect? - ____stashScroll = __instance.GetComponentInChildren(); - } - HandleInput(____stashScroll); + // For some reason, sometimes SimpleStashPanel doesn't have a reference to its own ScrollRect? + HandleInput(____stashScroll ?? __instance.GetComponentInChildren()); } [PatchTranspiler] - private static IEnumerable Transpiler(IEnumerable instructions) + public static IEnumerable Transpile(IEnumerable instructions) { if (Settings.RebindPageUpDown.Value) { @@ -157,18 +152,17 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(TraderDealScreen); - return type.GetMethod("Update", BindingFlags.Public | BindingFlags.Instance); + return AccessTools.Method(typeof(TraderDealScreen), nameof(TraderDealScreen.Update)); } [PatchPrefix] - private static void Prefix(TraderDealScreen __instance, TraderDealScreen.ETraderMode ___etraderMode_0, ScrollRect ____traderScroll, ScrollRect ____stashScroll) + public static void Prefix(TraderDealScreen.ETraderMode ___etraderMode_0, ScrollRect ____traderScroll, ScrollRect ____stashScroll) { HandleInput(___etraderMode_0 == TraderDealScreen.ETraderMode.Purchase ? ____traderScroll : ____stashScroll); } [PatchTranspiler] - private static IEnumerable Transpiler(IEnumerable instructions) + public static IEnumerable Transpile(IEnumerable instructions) { if (Settings.RebindPageUpDown.Value) { @@ -183,12 +177,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(OfferViewList); - return type.GetMethod("Update", BindingFlags.Public | BindingFlags.Instance); + return AccessTools.Method(typeof(OfferViewList), nameof(OfferViewList.Update)); } [PatchPrefix] - private static void Prefix(OfferViewList __instance, LightScroller ____scroller) + public static void Prefix(LightScroller ____scroller) { HandleInput(____scroller); } @@ -209,12 +202,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(MessagesContainer); - return type.GetMethod("Update", BindingFlags.Public | BindingFlags.Instance); + return AccessTools.Method(typeof(MessagesContainer), nameof(MessagesContainer.Update)); } [PatchPrefix] - private static void Prefix(MessagesContainer __instance, LightScroller ____scroller) + public static void Prefix(LightScroller ____scroller) { HandleInput(____scroller); } @@ -235,12 +227,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(ScrollRectNoDrag); - return type.GetMethod("OnScroll"); + return AccessTools.Method(typeof(ScrollRectNoDrag), nameof(ScrollRectNoDrag.OnScroll)); //type.GetMethod("OnScroll"); } [PatchPrefix] - private static void Prefix(PointerEventData data) + public static void Prefix(PointerEventData data) { data.scrollDelta *= Settings.MouseScrollMulti.Value; } diff --git a/Patches/ScrollSyncPatches.cs b/Patches/ScrollSyncPatches.cs index a729dd4..501e9fa 100644 --- a/Patches/ScrollSyncPatches.cs +++ b/Patches/ScrollSyncPatches.cs @@ -39,11 +39,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(SimpleStashPanel), "Show"); + return AccessTools.Method(typeof(SimpleStashPanel), nameof(SimpleStashPanel.Show)); } [PatchPostfix] - private static void Postfix(SimpleStashPanel __instance) + public static void Postfix(SimpleStashPanel __instance) { SynchronizeScrollRect(__instance); } @@ -53,13 +53,13 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(TraderDealScreen), "method_3"); + return AccessTools.Method(typeof(TraderDealScreen), nameof(TraderDealScreen.method_3)); } // TraderDealScreen is a monstrosity that loads multiple times and isn't done loading when Show() is done // method_3 shows the stash grid, if method_5() returned true [PatchPostfix] - private static void Postfix(TraderDealScreen __instance, ScrollRect ____stashScroll) + public static void Postfix(TraderDealScreen __instance, ScrollRect ____stashScroll) { if (__instance.method_5()) { @@ -72,11 +72,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(AddOfferWindow), "Show"); + return AccessTools.Method(typeof(AddOfferWindow), nameof(AddOfferWindow.Show)); } [PatchPostfix] - private static void Postfix(AddOfferWindow __instance) + public static void Postfix(AddOfferWindow __instance) { SynchronizeScrollRect(__instance); } diff --git a/Patches/SwapPatch.cs b/Patches/SwapPatch.cs index 71fe946..9893309 100644 --- a/Patches/SwapPatch.cs +++ b/Patches/SwapPatch.cs @@ -46,7 +46,7 @@ namespace UIFixes // The most recent GridItemView that was hovered - needed to forcibly update hover state after swap private static GridItemView LastHoveredGridItemView; - private static EOwnerType[] BannedOwnerTypes = [EOwnerType.Mail, EOwnerType.Trader]; + private static readonly EOwnerType[] BannedOwnerTypes = [EOwnerType.Mail, EOwnerType.Trader]; public static void Enable() { @@ -61,7 +61,7 @@ namespace UIFixes CanAcceptOperationSucceededProperty = AccessTools.Property(CanAcceptOperationType, "Succeeded"); CanAcceptOperationErrorProperty = AccessTools.Property(CanAcceptOperationType, "Error"); - SwapOperationType = AccessTools.Method(typeof(InteractionsHandlerClass), "Swap").ReturnType; // GStruct414 + SwapOperationType = AccessTools.Method(typeof(InteractionsHandlerClass), nameof(InteractionsHandlerClass.Swap)).ReturnType; // GStruct414 SwapOperationToCanAcceptOperationOperator = SwapOperationType.GetMethods().First(m => m.Name == "op_Implicit" && m.ReturnType == CanAcceptOperationType); GridViewNonInteractableField = AccessTools.Field(typeof(GridView), "_nonInteractable"); @@ -146,8 +146,7 @@ namespace UIFixes private static bool CouldEverFit(ItemContextClass itemContext, ItemContextAbstractClass containerItemContext) { Item item = itemContext.Item; - LootItemClass container = containerItemContext.Item as LootItemClass; - if (container == null) + if (containerItemContext.Item is not LootItemClass container) { return false; } @@ -171,12 +170,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(ItemView); - return type.GetMethod("OnDrag"); + return AccessTools.Method(typeof(ItemView), nameof(ItemView.OnDrag)); } [PatchPrefix] - private static void Prefix(ItemView __instance) + public static void Prefix(ItemView __instance) { SourceContainer = __instance.Container; } @@ -188,9 +186,8 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { - Type type = typeof(GridView); - GridViewTraderControllerClassField = AccessTools.GetDeclaredFields(type).First(f => f.FieldType == typeof(TraderControllerClass)); - return type.GetMethod("CanAccept"); + GridViewTraderControllerClassField = AccessTools.GetDeclaredFields(typeof(GridView)).First(f => f.FieldType == typeof(TraderControllerClass)); + return AccessTools.Method(typeof(GridView), nameof(GridView.CanAccept)); } // Essentially doing what happens in StashGridClass.method_6, which checks if any of the squares are already taken @@ -236,7 +233,7 @@ namespace UIFixes } [PatchPostfix] - private static void Postfix(GridView __instance, ItemContextClass itemContext, ItemContextAbstractClass targetItemContext, ref object operation, ref bool __result, Dictionary ___dictionary_0) + public static void Postfix(GridView __instance, ItemContextClass itemContext, ItemContextAbstractClass targetItemContext, ref object operation, ref bool __result, Dictionary ___dictionary_0) { if (!ValidPrerequisites(itemContext, targetItemContext, operation)) { @@ -254,8 +251,7 @@ namespace UIFixes } // Repair kits are special - ItemView targetItemView; - if (___dictionary_0.TryGetValue(targetItem.Id, out targetItemView)) + if (___dictionary_0.TryGetValue(targetItem.Id, out ItemView targetItemView)) { if (targetItemView.CanInteract(itemContext)) { @@ -328,21 +324,15 @@ namespace UIFixes // Swap does not do that, because spaghetti, so do it here. public class SwapOperationRaiseEventsPatch : ModulePatch { - private static MethodInfo RaiseUnbindItemEvent; - private static Type RaiseUnbindItemEventArgs; // GEventArgs13 - protected override MethodBase GetTargetMethod() { - RaiseUnbindItemEvent = AccessTools.Method(typeof(InventoryControllerClass), "RaiseUnbindItemEvent"); - RaiseUnbindItemEventArgs = RaiseUnbindItemEvent.GetParameters()[0].ParameterType; return AccessTools.Method(SwapOperationType.GenericTypeArguments[0], "RaiseEvents"); // GClass2787 } [PatchPostfix] - private static void Postfix(TraderControllerClass controller, CommandStatus status, Item ___Item, Item ___Item1) + public static void Postfix(TraderControllerClass controller, CommandStatus status, Item ___Item, Item ___Item1) { - InventoryControllerClass inventoryController = controller as InventoryControllerClass; - if (status != CommandStatus.Succeed || inventoryController == null || ___Item == null || ___Item1 == null) + if (status != CommandStatus.Succeed || ___Item == null || ___Item1 == null || controller is not InventoryControllerClass inventoryController) { return; } @@ -378,11 +368,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(GridItemView), "OnPointerEnter"); + return AccessTools.Method(typeof(GridItemView), nameof(GridItemView.OnPointerEnter)); } [PatchPostfix] - private static void Postfix(GridItemView __instance) + public static void Postfix(GridItemView __instance) { LastHoveredGridItemView = __instance; } @@ -394,12 +384,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(ItemContextClass); - return type.GetMethod("CanAccept"); + return AccessTools.Method(typeof(ItemContextClass), nameof(ItemContextClass.CanAccept)); } [PatchPostfix] - private static void Postfix(ItemContextClass __instance, Slot slot, ItemContextAbstractClass targetItemContext, ref object operation, TraderControllerClass itemController, bool simulate, ref bool __result) + public static void Postfix(ItemContextClass __instance, Slot slot, ItemContextAbstractClass targetItemContext, ref object operation, TraderControllerClass itemController, bool simulate, ref bool __result) { // targetItemContext here is not the target item, it's the *parent* context, i.e. the owner of the slot // Do a few more checks @@ -437,18 +426,17 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(GridItemView); - return type.GetMethod("method_12"); + return AccessTools.Method(typeof(GridItemView), nameof(GridItemView.method_12)); } [PatchPrefix] - private static void Prefix() + public static void Prefix() { InHighlight = true; } [PatchPostfix] - private static void Postfix() + public static void Postfix() { InHighlight = false; } @@ -460,18 +448,18 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(SlotView); - return type.GetMethod("method_2"); + return AccessTools.Method(typeof(SlotView), nameof(SlotView.method_2)); + } [PatchPrefix] - private static void Prefix() + public static void Prefix() { InHighlight = true; } [PatchPostfix] - private static void Postfix() + public static void Postfix() { InHighlight = false; } @@ -488,7 +476,7 @@ namespace UIFixes } [PatchPostfix] - private static void Postfix(Item item, ref bool __result) + public static void Postfix(Item item, ref bool __result) { LastCheckItemFilterId = item.Id; LastCheckItemFilterResult = __result; @@ -501,11 +489,11 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(DraggedItemView), "UpdateTargetUnderCursor"); + return AccessTools.Method(typeof(DraggedItemView), nameof(DraggedItemView.UpdateTargetUnderCursor)); } [PatchPostfix] - private static void Postfix(DraggedItemView __instance, ItemContextAbstractClass itemUnderCursor) + public static void Postfix(DraggedItemView __instance, ItemContextAbstractClass itemUnderCursor) { if (SourceContainer is Component sourceComponent) { diff --git a/Patches/TooltipPatch.cs b/Patches/TooltipPatch.cs index b72875e..2b55b10 100644 --- a/Patches/TooltipPatch.cs +++ b/Patches/TooltipPatch.cs @@ -1,6 +1,6 @@ using Aki.Reflection.Patching; using EFT.UI.DragAndDrop; -using System; +using HarmonyLib; using System.Reflection; namespace UIFixes @@ -9,18 +9,14 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(QuestItemViewPanel); - return type.GetMethod("method_2"); + return AccessTools.Method(typeof(QuestItemViewPanel), nameof(QuestItemViewPanel.method_2)); } [PatchPostfix] - private static void Postfix(QuestItemViewPanel __instance) + public static void Postfix(QuestItemViewPanel __instance) { GridItemView parent = __instance.GetComponentInParent(); - if (parent != null) - { - parent.ShowTooltip(); - } + parent?.ShowTooltip(); } } } diff --git a/Patches/TransferConfirmPatch.cs b/Patches/TransferConfirmPatch.cs index ce630d1..9b103bb 100644 --- a/Patches/TransferConfirmPatch.cs +++ b/Patches/TransferConfirmPatch.cs @@ -1,6 +1,6 @@ using Aki.Reflection.Patching; using EFT.UI; -using System; +using HarmonyLib; using System.Reflection; using System.Threading.Tasks; @@ -10,19 +10,18 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - Type type = typeof(TransferItemsScreen); - return type.GetMethod("method_4", BindingFlags.Public | BindingFlags.Instance); + return AccessTools.Method(typeof(TransferItemsScreen), nameof(TransferItemsScreen.method_4)); } [PatchPrefix] - private static bool Prefix(ref Task __result) + public static bool Prefix(ref Task __result) { if (Settings.ShowTransferConfirmations.Value == TransferConfirmationOption.Always) { return true; } - __result = Task.FromResult(true); + __result = Task.FromResult(true); return false; } } diff --git a/Patches/WeaponBindingPatch.cs b/Patches/WeaponBindingPatch.cs index c21f02b..71c4d48 100644 --- a/Patches/WeaponBindingPatch.cs +++ b/Patches/WeaponBindingPatch.cs @@ -2,6 +2,7 @@ using Aki.Reflection.Utils; using EFT.InputSystem; using EFT.InventoryLogic; +using HarmonyLib; using System; using System.Linq; using System.Reflection; @@ -16,12 +17,12 @@ namespace UIFixes protected override MethodBase GetTargetMethod() { ControlSettingsClass = PatchConstants.EftTypes.Single(x => x.GetMethod("GetBoundItemNames") != null); // GClass960 - GetKeyNameMethod = ControlSettingsClass.GetMethod("GetKeyName"); - return ControlSettingsClass.GetMethod("GetBoundItemNames", BindingFlags.Public | BindingFlags.Instance); + GetKeyNameMethod = AccessTools.Method(ControlSettingsClass, "GetKeyName"); + return AccessTools.Method(ControlSettingsClass, "GetBoundItemNames"); } [PatchPostfix] - private static void Postfix(object __instance, EBoundItem boundItem, ref string __result) + public static void Postfix(object __instance, EBoundItem boundItem, ref string __result) { switch(boundItem) { diff --git a/Patches/WeaponZoomPatch.cs b/Patches/WeaponZoomPatch.cs index 6ea817b..09d3ce1 100644 --- a/Patches/WeaponZoomPatch.cs +++ b/Patches/WeaponZoomPatch.cs @@ -2,6 +2,7 @@ using EFT.InventoryLogic; using EFT.UI; using EFT.UI.WeaponModding; +using HarmonyLib; using System; using System.Reflection; using UnityEngine.EventSystems; @@ -18,22 +19,16 @@ namespace UIFixes public class EditBuildScreenZoomPatch : ModulePatch { - private static ScrollTrigger ScrollTrigger; protected override MethodBase GetTargetMethod() { - Type type = typeof(EditBuildScreen); - return type.GetMethod("Show", [typeof(Item), typeof(Item), typeof(InventoryControllerClass), typeof(ISession)]); + return AccessTools.Method(typeof(EditBuildScreen), nameof(EditBuildScreen.Show), [typeof(Item), typeof(Item), typeof(InventoryControllerClass), typeof(ISession)]); } [PatchPrefix] - private static void Prefix(EditBuildScreen __instance, WeaponPreview ____weaponPreview) + public static void Prefix(EditBuildScreen __instance, WeaponPreview ____weaponPreview) { - if (ScrollTrigger == null) - { - ScrollTrigger = __instance.gameObject.AddComponent(); - } - - ScrollTrigger.OnOnScroll += (PointerEventData eventData) => + var scrollTrigger = __instance.gameObject.AddComponent(); + scrollTrigger.OnOnScroll += (PointerEventData eventData) => { if (____weaponPreview != null && __instance != null) { @@ -46,22 +41,16 @@ namespace UIFixes public class WeaponModdingScreenZoomPatch : ModulePatch { - private static ScrollTrigger ScrollTrigger; protected override MethodBase GetTargetMethod() { - Type type = typeof(WeaponModdingScreen); - return type.GetMethod("Show", [typeof(Item), typeof(InventoryControllerClass), typeof(LootItemClass[])]); + return AccessTools.Method(typeof(WeaponModdingScreen), nameof(WeaponModdingScreen.Show), [typeof(Item), typeof(InventoryControllerClass), typeof(LootItemClass[])]); } [PatchPrefix] - private static void Prefix(WeaponModdingScreen __instance, WeaponPreview ____weaponPreview) + public static void Prefix(WeaponModdingScreen __instance, WeaponPreview ____weaponPreview) { - if (ScrollTrigger == null) - { - ScrollTrigger = __instance.gameObject.AddComponent(); - } - - ScrollTrigger.OnOnScroll += (PointerEventData eventData) => + var scrollTrigger = __instance.gameObject.AddComponent(); + scrollTrigger.OnOnScroll += (PointerEventData eventData) => { if (____weaponPreview != null && __instance != null) { diff --git a/Plugin.cs b/Plugin.cs index bcca119..854b0f1 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -5,7 +5,7 @@ namespace UIFixes [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] public class Plugin : BaseUnityPlugin { - private void Awake() + public void Awake() { Settings.Init(Config); diff --git a/Settings.cs b/Settings.cs index 9520beb..bdf2f4d 100644 --- a/Settings.cs +++ b/Settings.cs @@ -228,8 +228,7 @@ namespace UIFixes int settingOrder = configEntries.Count; foreach (var entry in configEntries) { - ConfigurationManagerAttributes attributes = entry.Description.Tags[0] as ConfigurationManagerAttributes; - if (attributes != null) + if (entry.Description.Tags[0] is ConfigurationManagerAttributes attributes) { attributes.Order = settingOrder; }