Decompiled source of SaveProtectSimple v1.1.0

plugins/SaveProtectSimple.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
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("SaveProtectSimple")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SaveProtectSimple")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("2dd15ec1-d817-423a-91ec-a3a70a7d5c2a")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace cn_xc.SaveProtectSimple;

[BepInPlugin("cn_xc.SaveProtectSimple", "Save Protect Simple", "1.1.0")]
public class SaveProtectSimplePlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	public static ConfigEntry<bool> PreventSaveDelete;

	public static ConfigEntry<bool> AllowDelete;

	public static ConfigEntry<bool> RemoveSaveLimit;

	public static ConfigEntry<int> MaxSaveSlots;

	public static ConfigEntry<bool> EnableAutoBackup;

	private readonly Harmony harmony = new Harmony("cn_xc.SaveProtectSimple");

	private void Awake()
	{
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Expected O, but got Unknown
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Expected O, but got Unknown
		//IL_017c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		PreventSaveDelete = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "PreventSaveDelete", true, "死亡时不删除存档。");
		AllowDelete = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowDelete", false, "存档清理模式。");
		RemoveSaveLimit = ((BaseUnityPlugin)this).Config.Bind<bool>("SaveLimit", "RemoveSaveLimit", true, "移除存档数量上限。");
		MaxSaveSlots = ((BaseUnityPlugin)this).Config.Bind<int>("SaveLimit", "MaxSaveSlots", 999, "最大存档数量。");
		EnableAutoBackup = ((BaseUnityPlugin)this).Config.Bind<bool>("AutoBackup", "EnableAutoBackup", true, "退出房间时自动创建存档。");
		MethodInfo methodInfo = AccessTools.Method(typeof(StatsManager), "SaveFileDelete", (Type[])null, (Type[])null);
		if (methodInfo != null)
		{
			harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(SaveProtectSimplePlugin), "SaveFileDeletePrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		MethodInfo methodInfo2 = AccessTools.Method(typeof(MenuPageSaves), "OnNewGame", (Type[])null, (Type[])null);
		if (methodInfo2 != null)
		{
			harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(SaveProtectSimplePlugin), "OnNewGamePrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		MethodInfo methodInfo3 = AccessTools.Method(typeof(RunManager), "LeaveToMainMenu", (Type[])null, (Type[])null);
		if (methodInfo3 != null)
		{
			harmony.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(typeof(SaveProtectSimplePlugin), "LeaveToMainMenuPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
		}
		Log.LogInfo((object)"Save Protect Simple v1.1.0 已加载!");
	}

	public static bool SaveFileDeletePrefix(string saveFileName)
	{
		if (AllowDelete.Value)
		{
			return true;
		}
		if (!PreventSaveDelete.Value)
		{
			return true;
		}
		Log.LogInfo((object)("\ud83d\udee1\ufe0f 保留: " + saveFileName));
		return false;
	}

	public static void OnNewGamePrefix(MenuPageSaves __instance)
	{
		if (RemoveSaveLimit.Value)
		{
			FieldInfo fieldInfo = AccessTools.Field(typeof(MenuPageSaves), "maxSaveFiles");
			if (fieldInfo != null)
			{
				fieldInfo.SetValue(__instance, MaxSaveSlots.Value);
			}
		}
	}

	public static void LeaveToMainMenuPostfix()
	{
		if (!EnableAutoBackup.Value)
		{
			return;
		}
		StatsManager instance = StatsManager.instance;
		if ((Object)(object)instance == (Object)null)
		{
			return;
		}
		FieldInfo fieldInfo = AccessTools.Field(typeof(StatsManager), "saveFileReady");
		if (fieldInfo == null || !(bool)fieldInfo.GetValue(instance))
		{
			return;
		}
		bool flag = SemiFunc.IsMultiplayer();
		bool flag2 = false;
		if (flag)
		{
			Type type = AccessTools.TypeByName("PhotonNetwork");
			if (type != null)
			{
				PropertyInfo propertyInfo = AccessTools.Property(type, "IsMasterClient");
				if (propertyInfo != null)
				{
					flag2 = (bool)propertyInfo.GetValue(null);
				}
			}
		}
		if (!(!flag || flag2))
		{
			return;
		}
		string text = "REPO_SAVE_AUTO_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
		try
		{
			instance.SaveFileCreate(text, false);
			instance.SaveGame(text);
			Log.LogInfo((object)("\ud83d\udcbe 自动存档: " + text));
		}
		catch (Exception ex)
		{
			Log.LogError((object)("自动存档失败: " + ex.Message));
		}
	}
}