using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ModSaveBackups")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ModSaveBackups")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("144ea274-5a8d-4bce-917f-b42e2d203bc2")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.1.0")]
namespace ModSaveBackups;
public static class ModSave
{
public static void Save(PluginInfo pluginInfo, object data)
{
if (GameState.playing)
{
Directory.CreateDirectory(GetSaveDirectory(SaveSlots.currentSlot));
FileStream fileStream = File.Create(GetSaveModFile(SaveSlots.currentSlot, pluginInfo));
BinaryFormatter binaryFormatter = new BinaryFormatter();
try
{
binaryFormatter.Serialize(fileStream, data);
}
catch (Exception ex)
{
Plugin.logger.LogError((object)("Could not serialize data '" + pluginInfo.Metadata.GUID + "'"));
Plugin.logger.LogError((object)(pluginInfo.Metadata.GUID + ": " + ex.Message));
}
fileStream.Close();
}
}
public static bool Load(PluginInfo pluginInfo, out object loadedObject)
{
loadedObject = null;
if (!GameState.playing && !GameState.currentlyLoading)
{
return false;
}
if (!File.Exists(GetSaveModFile(SaveSlots.currentSlot, pluginInfo)))
{
Plugin.logger.LogError((object)("Could not find mod save file for '" + pluginInfo.Metadata.GUID + "'"));
return false;
}
FileStream fileStream = File.OpenRead(GetSaveModFile(SaveSlots.currentSlot, pluginInfo));
if (fileStream.Length <= 0)
{
fileStream.Close();
Plugin.logger.LogError((object)("File stream length is 0 '" + pluginInfo.Metadata.GUID + "'"));
return false;
}
try
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
object obj = binaryFormatter.Deserialize(fileStream);
fileStream.Close();
loadedObject = obj;
return true;
}
catch (Exception ex)
{
fileStream.Close();
Plugin.logger.LogError((object)("Could not deserialize mod save for '" + pluginInfo.Metadata.GUID + "'"));
Plugin.logger.LogError((object)(pluginInfo.Metadata.GUID + ": " + ex.Message));
return false;
}
}
public static bool Load<T>(PluginInfo pluginInfo, out T loadedObject)
{
loadedObject = default(T);
object loadedObject2;
bool flag = Load(pluginInfo, out loadedObject2);
if (flag)
{
loadedObject = (T)loadedObject2;
}
return flag;
}
internal static string GetSaveModFile(int slot, PluginInfo pluginInfo)
{
return Path.Combine(GetSaveDirectory(slot), pluginInfo.Metadata.GUID + ".save");
}
public static string GetSaveDirectory(int slot)
{
return Path.Combine(Application.persistentDataPath, $"slot{slot}");
}
}
internal class ModSaveSlots
{
public static void PushBackups(PluginInfo pluginInfo)
{
int num = 5;
string backupPath = GetBackupPath(pluginInfo, SaveSlots.currentSlot, num);
if (File.Exists(backupPath))
{
File.Delete(backupPath);
}
for (int num2 = num - 1; num2 > 0; num2--)
{
string backupPath2 = GetBackupPath(pluginInfo, SaveSlots.currentSlot, num2);
if (File.Exists(backupPath2))
{
File.Move(backupPath2, GetBackupPath(pluginInfo, SaveSlots.currentSlot, num2 + 1));
}
}
string currentSaveModFile = GetCurrentSaveModFile(pluginInfo);
if (File.Exists(currentSaveModFile))
{
File.Move(currentSaveModFile, GetBackupPath(pluginInfo, SaveSlots.currentSlot, 1));
}
}
private static string GetBackupPath(PluginInfo pluginInfo, int slot, int backupIndex)
{
return Path.Combine(ModSave.GetSaveDirectory(slot), pluginInfo.Metadata.GUID + "_backup" + backupIndex + ".save");
}
public static string GetCurrentSaveModFile(PluginInfo pluginInfo)
{
return Path.Combine(ModSave.GetSaveDirectory(SaveSlots.currentSlot), pluginInfo.Metadata.GUID + ".save");
}
public static string GetBackupSaveModFile(PluginInfo pluginInfo, int slot, int backupIndex)
{
for (int num = backupIndex; num > 0; num--)
{
string backupPath = GetBackupPath(pluginInfo, slot, num);
if (File.Exists(backupPath))
{
Plugin.logger.LogDebug((object)("Loading SaveMod backup file path: " + backupPath));
return backupPath;
}
}
Plugin.logger.LogDebug((object)$"Loading current {pluginInfo} SaveMod file");
return GetCurrentSaveModFile(pluginInfo);
}
}
[BepInPlugin("com.raddude82.modsavebackups", "ModSaveBackups", "1.1.1")]
public class Plugin : BaseUnityPlugin
{
public const string PLUGIN_GUID = "com.raddude82.modsavebackups";
public const string PLUGIN_NAME = "ModSaveBackups";
public const string PLUGIN_VERSION = "1.1.1";
internal static Plugin instance;
internal static ManualLogSource logger;
private void Awake()
{
instance = this;
logger = ((BaseUnityPlugin)this).Logger;
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.raddude82.modsavebackups");
}
}
internal class SaveLoadPatches
{
[HarmonyPatch(typeof(ModSave))]
private class ModSavePatches
{
[HarmonyPrefix]
[HarmonyPatch("Save")]
public static void SavePatch(PluginInfo pluginInfo)
{
ModSaveSlots.PushBackups(pluginInfo);
backupIndex = 0;
}
[HarmonyPrefix]
[HarmonyPatch("GetSaveModFile")]
public static bool GetSaveModFilePatch(ref string __result, int slot, PluginInfo pluginInfo)
{
if (backupIndex == 0)
{
return true;
}
__result = ModSaveSlots.GetBackupSaveModFile(pluginInfo, slot, backupIndex);
return false;
}
}
[HarmonyPatch(typeof(SaveLoadManager))]
private class SaveLoadManagerPatches
{
[HarmonyPostfix]
[HarmonyPatch("Awake")]
public static void AwakePatch()
{
backupIndex = 0;
}
[HarmonyPrefix]
[HarmonyPatch("LoadGame")]
public static void LoadGamePatch(int backupIndex)
{
SaveLoadPatches.backupIndex = backupIndex;
}
}
private static int backupIndex;
}