Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of SaveGuardian v1.0.0
SaveGuardianMod.dll
Decompiled 21 minutes agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using Photon.Pun; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: IgnoresAccessChecksTo("")] [assembly: AssemblyCompany("sticot19")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0+2e2f6e4949524566b1997ff5e00b383a0e37d6e0")] [assembly: AssemblyProduct("SaveGuardianMod")] [assembly: AssemblyTitle("SaveGuardianMod")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.0.0.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace SaveGuardianMod { internal static class LevelSnapshot { private static Dictionary<string, Dictionary<string, int>>? _stats; internal static void Capture() { StatsManager instance = StatsManager.instance; if (Object.op_Implicit((Object)(object)instance)) { _stats = instance.dictionaryOfDictionaries.ToDictionary((KeyValuePair<string, Dictionary<string, int>> kv) => kv.Key, (KeyValuePair<string, Dictionary<string, int>> kv) => new Dictionary<string, int>(kv.Value)); } } internal static void Clear() { _stats = null; } internal static void RestoreHealth() { StatsManager instance = StatsManager.instance; if (!Object.op_Implicit((Object)(object)instance) || _stats == null || !_stats.TryGetValue("playerHealth", out Dictionary<string, int> value)) { return; } instance.playerHealth.Clear(); foreach (KeyValuePair<string, int> item in value) { instance.playerHealth[item.Key] = item.Value; } } internal static int GetStartHealth(string steamID) { if (_stats != null && _stats.TryGetValue("playerHealth", out Dictionary<string, int> value) && value.TryGetValue(steamID, out var value2)) { return value2; } return 100; } internal static void Restore() { StatsManager instance = StatsManager.instance; if (!Object.op_Implicit((Object)(object)instance) || _stats == null) { SaveGuardianMod.Logger.LogWarning((object)"No level-start snapshot: restarting with the current stats."); return; } Dictionary<string, int> dictionary = new Dictionary<string, int>(instance.itemStatBattery); foreach (KeyValuePair<string, Dictionary<string, int>> stat in _stats) { if (!instance.dictionaryOfDictionaries.TryGetValue(stat.Key, out var value)) { continue; } value.Clear(); foreach (KeyValuePair<string, int> item in stat.Value) { value[item.Key] = item.Value; } } foreach (KeyValuePair<string, int> item2 in dictionary) { if (instance.item.ContainsKey(item2.Key)) { instance.itemStatBattery[item2.Key] = item2.Value; } } } } [HarmonyPatch(typeof(DataDirector), "SaveDeleteCheck")] internal static class BlockSaveDeleteCheckPatch { private static bool Prefix(bool _leaveGame) { if (!SaveGuardianMod.BlockSaveDeletion.Value) { return true; } SaveGuardianMod.Logger.LogDebug((object)$"Skipped automatic save deletion check (leaveGame={_leaveGame})."); return false; } } [HarmonyPatch(typeof(RunManager), "ResetProgress")] internal static class KeepProgressOnGameOverPatch { private static bool Prefix() { if (!SaveGuardianMod.BlockSaveDeletion.Value || !SemiFunc.RunIsArena()) { return true; } SaveGuardianMod.Logger.LogInfo((object)"Game over: run progress kept."); return false; } } [HarmonyPatch(typeof(RunManager), "ChangeLevel")] internal static class RestartLevelOnDeathPatch { private static bool Prefix(RunManager __instance, bool _levelFailed) { if (!_levelFailed || !SaveGuardianMod.RestartLevelOnDeath.Value || __instance.restarting || !SemiFunc.IsMasterClientOrSingleplayer() || !SaveGuardianMod.IsRestartableScene()) { return true; } SaveGuardianMod.RestartCurrentLevel(); return false; } } [HarmonyPatch(typeof(SemiFunc), "OnSceneSwitch")] internal static class CaptureLevelStartPatch { private static void Postfix(bool _gameOver, bool _leaveGame) { if (_leaveGame) { LevelSnapshot.Clear(); } else if (!_gameOver && SaveGuardianMod.IsRestartableScene()) { LevelSnapshot.Capture(); } } } [BepInPlugin("sticot19.SaveGuardianMod", "SaveGuardianMod", "1.0.0")] public class SaveGuardianMod : BaseUnityPlugin { internal static ConfigEntry<bool> BlockSaveDeletion = null; internal static ConfigEntry<bool> RestartLevelOnDeath = null; internal static ConfigEntry<KeyboardShortcut> RestartLevelKey = null; private static bool _pendingHealthRestore; private static readonly HashSet<PlayerHealth> _healthRestored = new HashSet<PlayerHealth>(); private static float _healthDeadline; private const float HealthTimeoutSeconds = 20f; internal static SaveGuardianMod Instance { get; private set; } = null; internal static ManualLogSource Logger => Instance._logger; private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger; internal Harmony? Harmony { get; set; } private void Awake() { //IL_007e: Unknown result type (might be due to invalid IL or missing references) Instance = this; ((Component)this).gameObject.transform.parent = null; ((Object)((Component)this).gameObject).hideFlags = (HideFlags)61; BlockSaveDeletion = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "BlockSaveDeletion", true, "Empêche la suppression automatique de la sauvegarde (mort, game over, quitter avant la fin du premier niveau) et conserve la progression après un game over. La suppression manuelle depuis le menu reste possible."); RestartLevelOnDeath = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RestartLevelOnDeath", true, "Quand toute l'équipe est morte (solo ou multijoueur), le niveau ou la boutique est relancé depuis son début sur une nouvelle map : chaque joueur retrouve ses PV d'entrée de niveau, batteries des objets conservées, achats en boutique préservés."); RestartLevelKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "RestartLevelKey", new KeyboardShortcut((KeyCode)290, Array.Empty<KeyCode>()), "Relance manuellement le niveau ou la boutique depuis son début sur une nouvelle map, comme après une mort (hôte uniquement)."); Patch(); Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!"); } internal void Patch() { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Expected O, but got Unknown //IL_0025: Expected O, but got Unknown if (Harmony == null) { Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID); Harmony val2 = val; Harmony = val; } Harmony.PatchAll(); } internal void Unpatch() { Harmony? harmony = Harmony; if (harmony != null) { harmony.UnpatchSelf(); } } private void Update() { //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) if (_pendingHealthRestore) { TickHealthRestore(); } KeyboardShortcut value = RestartLevelKey.Value; if (((KeyboardShortcut)(ref value)).IsDown() && CanRestartManually()) { Logger.LogInfo((object)"Manual restart requested."); RestartCurrentLevel(); } } private static bool CanRestartManually() { RunManager instance = RunManager.instance; if (Object.op_Implicit((Object)(object)instance) && !instance.restarting && SemiFunc.IsMasterClientOrSingleplayer() && SemiFunc.NoTextInputsActive() && IsRestartableScene() && Object.op_Implicit((Object)(object)LevelGenerator.Instance)) { return LevelGenerator.Instance.Generated; } return false; } internal static bool IsRestartableScene() { if (!SemiFunc.RunIsLevel()) { return SemiFunc.RunIsShop(); } return true; } internal static void RestartCurrentLevel() { Logger.LogInfo((object)"Restarting the current level from its start."); LevelSnapshot.Restore(); _pendingHealthRestore = true; RunManager instance = RunManager.instance; if (SemiFunc.RunIsShop()) { instance.ChangeLevel(false, false, (ChangeLevelType)5); return; } Level previousRunLevel = instance.previousRunLevel; instance.previousRunLevel = instance.levelCurrent; try { instance.ChangeLevel(false, false, (ChangeLevelType)1); } finally { instance.previousRunLevel = previousRunLevel; } } private static void TickHealthRestore() { RunManager instance = RunManager.instance; if (!Object.op_Implicit((Object)(object)instance) || instance.restarting || !Object.op_Implicit((Object)(object)LevelGenerator.Instance) || !LevelGenerator.Instance.Generated) { LevelSnapshot.RestoreHealth(); _healthDeadline = Time.time + 20f; return; } if (!SemiFunc.IsMultiplayer()) { _pendingHealthRestore = false; return; } bool flag = true; foreach (PlayerAvatar item in Object.op_Implicit((Object)(object)GameDirector.instance) ? GameDirector.instance.PlayerList : new List<PlayerAvatar>()) { PlayerHealth val = (Object.op_Implicit((Object)(object)item) ? item.playerHealth : null); if (!Object.op_Implicit((Object)(object)val) || _healthRestored.Contains(val)) { continue; } if (!val.healthSet) { flag = false; continue; } int num = Mathf.Clamp(LevelSnapshot.GetStartHealth(SemiFunc.PlayerGetSteamID(item)), 1, val.maxHealth); if (val.health != num) { val.photonView.RPC("UpdateHealthRPC", (RpcTarget)0, new object[4] { num, val.maxHealth, false, false }); } _healthRestored.Add(val); } if (flag || Time.time > _healthDeadline) { _pendingHealthRestore = false; _healthRestored.Clear(); } } } }