From 644d4bd5474e8df4ebe9931a242d79a855260f2b Mon Sep 17 00:00:00 2001 From: AuriRex Date: Sat, 14 Oct 2023 18:06:27 +0200 Subject: [PATCH] Added Feature: "SmartFavoritesSaving" --- .../Features/Dev/SmartFavoritesSaving.cs | 108 ++++++++++++++++++ TheArchive.MONO/TheArchive.MONO.csproj | 1 + 2 files changed, 109 insertions(+) create mode 100644 TheArchive.IL2CPP/Features/Dev/SmartFavoritesSaving.cs diff --git a/TheArchive.IL2CPP/Features/Dev/SmartFavoritesSaving.cs b/TheArchive.IL2CPP/Features/Dev/SmartFavoritesSaving.cs new file mode 100644 index 00000000..10e9ad20 --- /dev/null +++ b/TheArchive.IL2CPP/Features/Dev/SmartFavoritesSaving.cs @@ -0,0 +1,108 @@ +using Gear; +using System.Runtime.CompilerServices; +using TheArchive.Core.Attributes; +using TheArchive.Core.FeaturesAPI; +using TheArchive.Interfaces; +using TheArchive.Utilities; +using static TheArchive.Utilities.Utils; + +namespace TheArchive.Features.Dev +{ + [EnableFeatureByDefault] + internal class SmartFavoritesSaving : Feature + { + public override string Name => "Smart Favorites Saving"; + + public override string Group => FeatureGroups.Dev; + + public override string Description => "Only save selected weapons/vanity on drop or game quit."; + + public static new IArchiveLogger FeatureLogger { get; set; } + + private static readonly eGameStateName _eGameStateName_Generating = GetEnumFromName(nameof(eGameStateName.Generating)); + + public void OnGameStateChanged(eGameStateName state) + { + if(state == _eGameStateName_Generating) + { + SaveFavoritesFiles(); + } + } + + public override void OnQuit() + { + SaveFavoritesFiles(); + } + + private static void SaveFavoritesFiles() + { + FeatureLogger.Notice("Saving Favorites file(s)!"); + GearManager_SaveFavoritesData_Patch.InvokeOriginal(); +#if IL2CPP + if (Is.R6OrLater) + GearManager_SaveBotFavoritesData_Patch.InvokeOriginal(); +#endif + } + +#if MONO + [ArchivePatch(typeof(GearManager), "SaveFavoritesData")] +#else + [ArchivePatch(typeof(GearManager), nameof(GearManager.SaveFavoritesData))] +#endif + internal static class GearManager_SaveFavoritesData_Patch + { + private static bool _shouldRun = false; + + private static MethodAccessor _A_SaveFavoritesData; + + public static void Init() + { + _A_SaveFavoritesData = MethodAccessor.GetAccessor("SaveFavoritesData"); + } + + public static bool Prefix() + { + if (_shouldRun) + return ArchivePatch.RUN_OG; + + if (DevMode) + FeatureLogger.Debug("Skipping SaveFavoritesData!"); + return ArchivePatch.SKIP_OG; + } + + public static void InvokeOriginal() + { + _shouldRun = true; + _A_SaveFavoritesData.Invoke(null); + _shouldRun = false; + } + } + +#if IL2CPP + [RundownConstraint(RundownFlags.RundownSix, RundownFlags.Latest)] + [ArchivePatch(typeof(GearManager), nameof(GearManager.SaveBotFavoritesData))] + internal static class GearManager_SaveBotFavoritesData_Patch + { + private static bool _shouldRun = false; + + public static bool Prefix() + { + if (_shouldRun) + return ArchivePatch.RUN_OG; + + if (DevMode) + FeatureLogger.Debug("Skipping SaveBotFavoritesData!"); + return ArchivePatch.SKIP_OG; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static void InvokeOriginal() + { + _shouldRun = true; + GearManager.SaveBotFavoritesData(); + _shouldRun = false; + } + } +#endif + } +} diff --git a/TheArchive.MONO/TheArchive.MONO.csproj b/TheArchive.MONO/TheArchive.MONO.csproj index 1c1cb116..04e26527 100644 --- a/TheArchive.MONO/TheArchive.MONO.csproj +++ b/TheArchive.MONO/TheArchive.MONO.csproj @@ -227,6 +227,7 @@ +