From 750de96d35c567d382f012037b1670f835cf1dfb Mon Sep 17 00:00:00 2001 From: Tyfon <29051038+tyfon7@users.noreply.github.com> Date: Wed, 29 May 2024 12:27:20 -0700 Subject: [PATCH] Remember autoselect similar --- Patches/AddOfferRememberAutoselectPatches.cs | 62 ++++++++++++++++++++ Plugin.cs | 1 + Settings.cs | 10 ++++ 3 files changed, 73 insertions(+) create mode 100644 Patches/AddOfferRememberAutoselectPatches.cs diff --git a/Patches/AddOfferRememberAutoselectPatches.cs b/Patches/AddOfferRememberAutoselectPatches.cs new file mode 100644 index 0000000..a6b4771 --- /dev/null +++ b/Patches/AddOfferRememberAutoselectPatches.cs @@ -0,0 +1,62 @@ +using Aki.Reflection.Patching; +using EFT.UI; +using EFT.UI.Ragfair; +using HarmonyLib; +using System.Reflection; +using UnityEngine; + +namespace UIFixes +{ + public static class AddOfferRememberAutoselectPatches + { + private static readonly string PlayerPrefKey = "UIFixes.AddOffer.AutoselectSimilar"; + + public static void Enable() + { + new RememberAutoselectPatch().Enable(); + new RestoreAutoselectPatch().Enable(); + + Settings.RememberAutoselectSimilar.SettingChanged += (sender, args) => + { + if (!Settings.RememberAutoselectSimilar.Value) + { + PlayerPrefs.DeleteKey(PlayerPrefKey); + } + }; + } + + public class RememberAutoselectPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(AddOfferWindow), nameof(AddOfferWindow.method_8)); + } + + [PatchPostfix] + public static void Postfix(bool value) + { + if (Settings.RememberAutoselectSimilar.Value) + { + PlayerPrefs.SetInt(PlayerPrefKey, value ? 1 : 0); + } + } + } + + public class RestoreAutoselectPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(AddOfferWindow), nameof(AddOfferWindow.Awake)); + } + + [PatchPrefix] + public static void Prefix(UpdatableToggle ____autoSelectSimilar) + { + if (Settings.RememberAutoselectSimilar.Value && PlayerPrefs.HasKey(PlayerPrefKey)) + { + ____autoSelectSimilar.UpdateValue(PlayerPrefs.GetInt(PlayerPrefKey) == 1); + } + } + } + } +} diff --git a/Plugin.cs b/Plugin.cs index f082f0a..a7f2f3c 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -39,6 +39,7 @@ namespace UIFixes new AutofillQuestItemsPatch().Enable(); InsureContextMenuPatches.Enable(); TradingAutoSwitchPatches.Enable(); + AddOfferRememberAutoselectPatches.Enable(); } public static bool InRaid() diff --git a/Settings.cs b/Settings.cs index f858d8e..b94b9d9 100644 --- a/Settings.cs +++ b/Settings.cs @@ -65,6 +65,7 @@ namespace UIFixes public static ConfigEntry AutoExpandCategories { get; set; } public static ConfigEntry KeepAddOfferOpen { get; set; } public static ConfigEntry KeepAddOfferOpenIgnoreMaxOffers { get; set; } // Advanced + public static ConfigEntry RememberAutoselectSimilar { get; set; } // Advanced public static void Init(ConfigFile config) { @@ -310,6 +311,15 @@ namespace UIFixes null, new ConfigurationManagerAttributes { IsAdvanced = true }))); + configEntries.Add(RememberAutoselectSimilar = config.Bind( + FleaMarketSection, + "Remember Add Offer Autoselect Similar", + true, + new ConfigDescription( + "Remember the state of the Autoselect Similar checkbox in the Add Offer window", + null, + new ConfigurationManagerAttributes { IsAdvanced = true }))); + RecalcOrder(configEntries); } private static void RecalcOrder(List configEntries)