Skip to content

Commit

Permalink
Added Feature: "SmartFavoritesSaving"
Browse files Browse the repository at this point in the history
  • Loading branch information
AuriRex committed Oct 14, 2023
1 parent b7d19e5 commit 644d4bd
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
108 changes: 108 additions & 0 deletions TheArchive.IL2CPP/Features/Dev/SmartFavoritesSaving.cs
Original file line number Diff line number Diff line change
@@ -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<eGameStateName>(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<GearManager> _A_SaveFavoritesData;

public static void Init()
{
_A_SaveFavoritesData = MethodAccessor<GearManager>.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
}
}
1 change: 1 addition & 0 deletions TheArchive.MONO/TheArchive.MONO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
<Compile Include="..\TheArchive.IL2CPP\Features\Dev\ModSettings.SubMenu.cs" Link="Features\Dev\ModSettings.SubMenu.cs" />
<Compile Include="..\TheArchive.IL2CPP\Features\Dev\PlayerDialogFilter.cs" Link="Features\Dev\PlayerDialogFilter.cs" />
<Compile Include="..\TheArchive.IL2CPP\Features\Dev\SettingsDebug.cs" Link="Features\Dev\SettingsDebug.cs" />
<Compile Include="..\TheArchive.IL2CPP\Features\Dev\SmartFavoritesSaving.cs" Link="Features\Dev\SmartFavoritesSaving.cs" />
<Compile Include="..\TheArchive.IL2CPP\Features\Dev\StartupscreenOverride.cs" Link="Features\Dev\StartupscreenOverride.cs" />
<Compile Include="..\TheArchive.IL2CPP\Features\Dev\UnityRandomOverrider.cs" Link="Features\Dev\UnityRandomOverrider.cs" />
<Compile Include="..\TheArchive.IL2CPP\Features\Fixes\BioTrackerStuckSoundFix.cs" Link="Features\Fixes\BioTrackerStuckSoundFix.cs" />
Expand Down

0 comments on commit 644d4bd

Please sign in to comment.