Decompiled source of SaveManager v0.3.2

BepInEx/plugins/SaveManager/SaveManager.dll

Decompiled 5 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyFileVersion("0.3.1.0")]
[assembly: AssemblyInformationalVersion("0.3.1")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.3.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 SaveManager
{
	internal static class Hotkeys
	{
		public static void PollInGame()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.CfgKeyPanel != null && Input.GetKeyDown(Plugin.CfgKeyPanel.Value))
			{
				SavePage.Open();
			}
			if (Plugin.CfgKeySave != null && Input.GetKeyDown(Plugin.CfgKeySave.Value))
			{
				Plugin.DoSave(null, out var _);
			}
			if (Plugin.CfgKeyLoad != null && Input.GetKeyDown(Plugin.CfgKeyLoad.Value))
			{
				if (Plugin.CfgInGameLoad != null && Plugin.CfgInGameLoad.Value)
				{
					Plugin.DoQuickLoad(out var message2);
					Ui.Notify(message2);
				}
				else
				{
					SavePage.Open();
					Ui.Notify("游戏内读档没开:在「存档」分页里选档,或打开 ExperimentalInGameLoad");
				}
			}
		}

		public static void PollMenu()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.CfgKeyPanel != null && Input.GetKeyDown(Plugin.CfgKeyPanel.Value))
			{
				SavePage.Open();
			}
		}
	}
	[HarmonyPatch(typeof(PlayerCamera), "HandleInput")]
	internal static class Patch_PlayerCamera_HandleInput
	{
		private static void Prefix()
		{
			try
			{
				Hotkeys.PollInGame();
			}
			catch (Exception ex)
			{
				Log.Error("热键处理异常:" + ex);
			}
		}
	}
	[HarmonyPatch(typeof(PreRunScript), "Update")]
	internal static class Patch_PreRunScript_Update
	{
		private static void Prefix()
		{
			try
			{
				Hotkeys.PollMenu();
			}
			catch (Exception ex)
			{
				Log.Error("主菜单热键异常:" + ex);
			}
		}
	}
	[HarmonyPatch(typeof(ConsoleScript), "RegisterAllCommands")]
	internal static class Patch_ConsoleCommands
	{
		private static void Postfix(ConsoleScript __instance)
		{
			try
			{
				ConsoleCommands.Register(__instance);
			}
			catch (Exception ex)
			{
				Log.Error("注册控制台命令失败:" + ex);
			}
		}
	}
	internal static class ConsoleCommands
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static Func<object, bool> <>9__2_0;

			public static Action <>9__2_1;

			public static Func<SlotMeta, string> <>9__2_10;

			public static Action <>9__2_2;

			public static Action <>9__2_3;

			public static Action <>9__2_4;

			public static Action <>9__2_5;

			public static Action <>9__2_6;

			public static Action <>9__2_7;

			public static Action <>9__2_8;

			public static Action <>9__2_9;

			internal bool <Register>b__2_0(object c)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				return ((Command)c).name == "cssave";
			}

			internal void <Register>b__2_1(string[] a)
			{
				Ui.Notify(Plugin.DoSave((a.Length > 1) ? string.Join(" ", a.Skip(1)) : null, out var message) ? message : ("失败:" + message));
			}

			internal void <Register>b__2_2(string[] a)
			{
				Ui.Notify((SlotStore.List().Count == 0) ? "还没有任何存档槽" : string.Join("\n", from s in SlotStore.List()
					select string.Format("{0}  D{1}  {2}  {3}{4}{5}", s.name, s.biome, s.DurationText(), (s.mode == "mp") ? "联机" : "单人", s.sandbox ? " 沙盒" : "", s.cheated ? " 作弊" : "")));
			}

			internal string <Register>b__2_10(SlotMeta s)
			{
				return string.Format("{0}  D{1}  {2}  {3}{4}{5}", s.name, s.biome, s.DurationText(), (s.mode == "mp") ? "联机" : "单人", s.sandbox ? " 沙盒" : "", s.cheated ? " 作弊" : "");
			}

			internal void <Register>b__2_3(string[] a)
			{
				if (a.Length < 2)
				{
					throw new Exception("用法:csload <槽名>");
				}
				Ui.Notify(Plugin.DoLoad(a[1], out var message) ? message : ("失败:" + message));
			}

			internal void <Register>b__2_4(string[] a)
			{
				if (!Directory.Exists(Paths.SlotRoot))
				{
					Directory.CreateDirectory(Paths.SlotRoot);
				}
				PreRunScript.OpenInFileBrowser(Paths.SlotRoot);
				Ui.Notify("已打开:" + Paths.SlotRoot);
			}

			internal void <Register>b__2_5(string[] a)
			{
				if (a.Length < 2)
				{
					throw new Exception("用法:csdel <槽名>");
				}
				Ui.Notify(SlotStore.Delete(a[1], out var error) ? ("已删除:" + a[1]) : ("失败:" + error));
			}

			internal void <Register>b__2_6(string[] a)
			{
				if (a.Length < 3)
				{
					throw new Exception("用法:csrename <旧名> <新名>");
				}
				Ui.Notify(SlotStore.Rename(a[1], a[2], out var error) ? "已改名" : ("失败:" + error));
			}

			internal void <Register>b__2_7(string[] a)
			{
				if (a.Length < 2)
				{
					throw new Exception("用法:csrollback <槽名>");
				}
				Ui.Notify(SlotStore.RestoreBackup(a[1], out var error) ? ("已回滚:" + a[1]) : ("失败:" + error));
			}

			internal void <Register>b__2_8(string[] a)
			{
				if (a.Length < 2)
				{
					throw new Exception("用法:csexport <槽名>");
				}
				Ui.Notify(SlotStore.ExportToGameSave(a[1], out var error) ? ("已铺成 save.sv:" + a[1]) : ("失败:" + error));
			}

			internal void <Register>b__2_9(string[] a)
			{
				SavePage.Open();
			}
		}

		private static readonly FieldInfo CommandsField = typeof(ConsoleScript).GetField("Commands", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

		private static IList _list;

		public static void Register(ConsoleScript instance)
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Expected O, but got Unknown
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Expected O, but got Unknown
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Expected O, but got Unknown
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Expected O, but got Unknown
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Expected O, but got Unknown
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Expected O, but got Unknown
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_023e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Expected O, but got Unknown
			//IL_0283: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_028e: Expected O, but got Unknown
			//IL_02cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d8: Expected O, but got Unknown
			if (CommandsField == null)
			{
				Log.Warn("找不到 ConsoleScript.Commands 字段");
				return;
			}
			_list = CommandsField.GetValue(CommandsField.IsStatic ? null : instance) as IList;
			if (_list == null)
			{
				Log.Warn("ConsoleScript.Commands 不是 IList");
			}
			else
			{
				if (_list.Cast<object>().Any((object c) => ((Command)c).name == "cssave"))
				{
					return;
				}
				object obj = <>c.<>9__2_1;
				if (obj == null)
				{
					Action val = delegate(string[] a)
					{
						Ui.Notify(Plugin.DoSave((a.Length > 1) ? string.Join(" ", a.Skip(1)) : null, out var message) ? message : ("失败:" + message));
					};
					<>c.<>9__2_1 = val;
					obj = (object)val;
				}
				Add("cssave", "[存档] 把当前局面存成一个新槽(不传名字=自动命名)", (Action)obj, ("name", "可选,存档名(可含中文)"));
				object obj2 = <>c.<>9__2_2;
				if (obj2 == null)
				{
					Action val2 = delegate
					{
						Ui.Notify((SlotStore.List().Count == 0) ? "还没有任何存档槽" : string.Join("\n", from s in SlotStore.List()
							select string.Format("{0}  D{1}  {2}  {3}{4}{5}", s.name, s.biome, s.DurationText(), (s.mode == "mp") ? "联机" : "单人", s.sandbox ? " 沙盒" : "", s.cheated ? " 作弊" : "")));
					};
					<>c.<>9__2_2 = val2;
					obj2 = (object)val2;
				}
				Add("cslots", "[存档] 列出所有存档槽", (Action)obj2, null);
				object obj3 = <>c.<>9__2_3;
				if (obj3 == null)
				{
					Action val3 = delegate(string[] a)
					{
						if (a.Length < 2)
						{
							throw new Exception("用法:csload <槽名>");
						}
						Ui.Notify(Plugin.DoLoad(a[1], out var message) ? message : ("失败:" + message));
					};
					<>c.<>9__2_3 = val3;
					obj3 = (object)val3;
				}
				Add("csload", "[存档] 读取指定槽(回主菜单流程)", (Action)obj3, ("name", "槽名(cslots 里看到的那个)"));
				object obj4 = <>c.<>9__2_4;
				if (obj4 == null)
				{
					Action val4 = delegate
					{
						if (!Directory.Exists(Paths.SlotRoot))
						{
							Directory.CreateDirectory(Paths.SlotRoot);
						}
						PreRunScript.OpenInFileBrowser(Paths.SlotRoot);
						Ui.Notify("已打开:" + Paths.SlotRoot);
					};
					<>c.<>9__2_4 = val4;
					obj4 = (object)val4;
				}
				Add("csdir", "[存档] 打开存档目录", (Action)obj4, null);
				object obj5 = <>c.<>9__2_5;
				if (obj5 == null)
				{
					Action val5 = delegate(string[] a)
					{
						if (a.Length < 2)
						{
							throw new Exception("用法:csdel <槽名>");
						}
						Ui.Notify(SlotStore.Delete(a[1], out var error) ? ("已删除:" + a[1]) : ("失败:" + error));
					};
					<>c.<>9__2_5 = val5;
					obj5 = (object)val5;
				}
				Add("csdel", "[存档] 删除一个槽", (Action)obj5, ("name", "槽名"));
				object obj6 = <>c.<>9__2_6;
				if (obj6 == null)
				{
					Action val6 = delegate(string[] a)
					{
						if (a.Length < 3)
						{
							throw new Exception("用法:csrename <旧名> <新名>");
						}
						Ui.Notify(SlotStore.Rename(a[1], a[2], out var error) ? "已改名" : ("失败:" + error));
					};
					<>c.<>9__2_6 = val6;
					obj6 = (object)val6;
				}
				Add("csrename", "[存档] 给槽改名", (Action)obj6, ("old", "旧名"), ("new", "新名"));
				object obj7 = <>c.<>9__2_7;
				if (obj7 == null)
				{
					Action val7 = delegate(string[] a)
					{
						if (a.Length < 2)
						{
							throw new Exception("用法:csrollback <槽名>");
						}
						Ui.Notify(SlotStore.RestoreBackup(a[1], out var error) ? ("已回滚:" + a[1]) : ("失败:" + error));
					};
					<>c.<>9__2_7 = val7;
					obj7 = (object)val7;
				}
				Add("csrollback", "[存档] 回滚到该槽的上一档备份", (Action)obj7, ("name", "槽名"));
				object obj8 = <>c.<>9__2_8;
				if (obj8 == null)
				{
					Action val8 = delegate(string[] a)
					{
						if (a.Length < 2)
						{
							throw new Exception("用法:csexport <槽名>");
						}
						Ui.Notify(SlotStore.ExportToGameSave(a[1], out var error) ? ("已铺成 save.sv:" + a[1]) : ("失败:" + error));
					};
					<>c.<>9__2_8 = val8;
					obj8 = (object)val8;
				}
				Add("csexport", "[存档] 把槽铺成游戏当前存档(游戏外换档同效)", (Action)obj8, ("name", "槽名"));
				object obj9 = <>c.<>9__2_9;
				if (obj9 == null)
				{
					Action val9 = delegate
					{
						SavePage.Open();
					};
					<>c.<>9__2_9 = val9;
					obj9 = (object)val9;
				}
				Add("csslots", "[存档] 打开设置里的「存档」分页", (Action)obj9, null);
				Log.Info("控制台命令已注册(cssave / cslots / csload / csdir / csdel / csrename / csrollback / csexport / csslots)");
			}
		}

		private static void Add(string name, string desc, Action action, params (string, string)[] argDesc)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected O, but got Unknown
			try
			{
				_list.Add((object?)new Command(name, desc, action, (Dictionary<int, List<string>>)null, argDesc));
			}
			catch (Exception ex)
			{
				Log.Warn("加命令失败 " + name + ":" + ex.Message);
			}
		}
	}
	internal static class Paths
	{
		public static string DataPath => Application.persistentDataPath;

		public static string VanillaSave => Path.Combine(DataPath, "save.sv");

		public static string MpSaveDir => Path.Combine(DataPath, "mp_save");

		public static string SlotRoot
		{
			get
			{
				string text = ((Plugin.CfgRootDir != null) ? Plugin.CfgRootDir.Value : string.Empty);
				if (!string.IsNullOrWhiteSpace(text))
				{
					try
					{
						return Path.GetFullPath(text.Trim());
					}
					catch
					{
					}
				}
				return Path.Combine(DataPath, "SaveSlots");
			}
		}

		public static bool MpPresent => Directory.Exists(MpSaveDir);

		public static void EnsureRoot()
		{
			Directory.CreateDirectory(SlotRoot);
		}

		public static string SlotDir(string slotName)
		{
			return Path.Combine(SlotRoot, slotName);
		}

		public static string SlotSave(string slotName)
		{
			return Path.Combine(SlotDir(slotName), "save.sv");
		}

		public static string SlotMp(string slotName)
		{
			return Path.Combine(SlotDir(slotName), "mp");
		}

		public static string SlotMeta(string slotName)
		{
			return Path.Combine(SlotDir(slotName), "meta.json");
		}

		public static string SlotBackup(string slotName)
		{
			return Path.Combine(SlotDir(slotName), "backup");
		}

		public static string SafeName(string raw)
		{
			if (string.IsNullOrWhiteSpace(raw))
			{
				return null;
			}
			StringBuilder stringBuilder = new StringBuilder(raw.Length);
			string text = raw.Trim();
			foreach (char c in text)
			{
				if (Array.IndexOf(Path.GetInvalidFileNameChars(), c) >= 0 || c == '\\' || c == '/')
				{
					stringBuilder.Append('_');
				}
				else
				{
					stringBuilder.Append(c);
				}
			}
			string text2 = stringBuilder.ToString().Trim('.', ' ');
			if (text2.Length > 80)
			{
				text2 = text2.Substring(0, 80);
			}
			if (text2.Length != 0)
			{
				return text2;
			}
			return null;
		}
	}
	[BepInPlugin("suzuay.savemanager", "SM (Save Manager)", "0.3.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "suzuay.savemanager";

		public const string VersionStr = "0.3.1";

		internal static Plugin Instance;

		internal static ConfigEntry<KeyCode> CfgKeySave;

		internal static ConfigEntry<KeyCode> CfgKeyPanel;

		internal static ConfigEntry<KeyCode> CfgKeyLoad;

		internal static ConfigEntry<bool> CfgCheatMark;

		internal static ConfigEntry<bool> CfgSandboxNoCheat;

		internal static ConfigEntry<bool> CfgInGameLoad;

		internal static ConfigEntry<bool> CfgAllowMp;

		internal static ConfigEntry<bool> CfgKeepBackup;

		internal static ConfigEntry<bool> CfgBypassGate;

		internal static ConfigEntry<bool> CfgAutoSave;

		internal static ConfigEntry<int> CfgAutoSaveMinutes;

		internal static ConfigEntry<string> CfgRootDir;

		private float _autoTimer;

		internal static bool MpAllowed
		{
			get
			{
				if (CfgAllowMp != null)
				{
					return CfgAllowMp.Value;
				}
				return true;
			}
		}

		private void Awake()
		{
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Log.Init(((BaseUnityPlugin)this).Logger);
			CfgKeySave = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("键位 Keybinds", "QuickSave", (KeyCode)286, "游戏内随时存一个新槽");
			CfgKeyLoad = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("键位 Keybinds", "QuickLoad", (KeyCode)290, "读最近一次存档(需启用 ExperimentalInGameLoad)");
			CfgKeyPanel = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("键位 Keybinds", "Panel", (KeyCode)289, "打开设置菜单里的「存档」分页");
			CfgCheatMark = ((BaseUnityPlugin)this).Config.Bind<bool>("存档 Slot", "MarkCheatedSave", false, "存档时把本局标记为 cheated(原版语义:成就关闭到重启)。默认关。");
			CfgSandboxNoCheat = ((BaseUnityPlugin)this).Config.Bind<bool>("存档 Slot", "SandboxNoCheatMark", true, "沙盒模式里不标记作弊(沙盒本身就允许随便用控制台)。");
			CfgAllowMp = ((BaseUnityPlugin)this).Config.Bind<bool>("存档 Slot", "AllowInMultiplayer", true, "联机时也允许存/读档(会连同 mp_save 一起收进槽里)。");
			CfgKeepBackup = ((BaseUnityPlugin)this).Config.Bind<bool>("存档 Slot", "KeepLastBackup", true, "覆盖同名槽前,把上一份挪进 backup/。");
			CfgRootDir = ((BaseUnityPlugin)this).Config.Bind<string>("存档 Slot", "SlotRootOverride", "", "留空 = 游戏存档目录下的 SaveSlots\\。");
			CfgBypassGate = ((BaseUnityPlugin)this).Config.Bind<bool>("兼容 Compatibility", "BypassTutorialGate", true, "没做完基础教程(didbasiccourse)时也允许读档:跳过原版「先做教程」的拦截,直接铺档读。沙盒/教程玩家需要。");
			CfgInGameLoad = ((BaseUnityPlugin)this).Config.Bind<bool>("兼容 Compatibility", "ExperimentalInGameLoad", true, "允许游戏内直接读档:存档页点「读取此档」/ F9 会自动回主菜单→铺档→继续。默认开。");
			CfgAutoSave = ((BaseUnityPlugin)this).Config.Bind<bool>("自动存档 AutoSave", "Enabled", false, "定时自动存一个新槽(默认关)");
			CfgAutoSaveMinutes = ((BaseUnityPlugin)this).Config.Bind<int>("自动存档 AutoSave", "Minutes", 5, "自动存档间隔(分钟)");
			try
			{
				new Harmony("suzuay.savemanager").PatchAll();
				Log.Info("Harmony 补丁已打上");
			}
			catch (Exception ex)
			{
				Log.Error("Harmony 补丁失败:" + ex);
			}
			Log.Info("存档根目录:" + Paths.SlotRoot);
			Log.Info("联机存档目录存在:" + Paths.MpPresent);
		}

		private void Update()
		{
			if (CfgAutoSave == null || !CfgAutoSave.Value)
			{
				return;
			}
			if (!SlotStore.InWorld)
			{
				_autoTimer = 0f;
				return;
			}
			_autoTimer += Time.unscaledDeltaTime;
			float num = (float)Mathf.Max(1, CfgAutoSaveMinutes.Value) * 60f;
			if (!(_autoTimer < num))
			{
				_autoTimer = 0f;
				if (!SlotStore.Capture(null, out var slotName, out var error))
				{
					Log.Warn("自动存档失败:" + error);
				}
				else
				{
					Ui.Notify("已自动存档:" + slotName);
				}
			}
		}

		public static void SaveConfig()
		{
			try
			{
				if ((Object)(object)Instance != (Object)null && ((BaseUnityPlugin)Instance).Config != null)
				{
					((BaseUnityPlugin)Instance).Config.Save();
				}
			}
			catch (Exception ex)
			{
				Log.Warn("保存配置失败:" + ex.Message);
			}
		}

		public static bool DoSave(string name, out string message)
		{
			if (!SlotStore.Capture(name, out var slotName, out var error))
			{
				message = error;
				return false;
			}
			message = "已存档:" + slotName;
			Ui.Notify(message);
			return true;
		}

		public static bool DoLoad(string slotName, out string message)
		{
			if (string.IsNullOrEmpty(slotName))
			{
				message = "没有选中存档槽";
				return false;
			}
			if (!Directory.Exists(Paths.SlotDir(slotName)))
			{
				message = "槽不存在:" + slotName;
				return false;
			}
			bool flag = SlotStore.ReadMeta(slotName)?.sandbox ?? false;
			if (flag != Sandbox.IsActive)
			{
				message = (flag ? "这是沙盒存档,只能在沙盒场地里读" : "这是实机存档,沙盒场地里读不了");
				return false;
			}
			if (SlotStore.InWorld && (CfgInGameLoad == null || !CfgInGameLoad.Value))
			{
				message = "游戏内读档没开(ExperimentalInGameLoad=false):先在暂停菜单里选档";
				return false;
			}
			message = "正在读档:" + slotName;
			((MonoBehaviour)Instance).StartCoroutine(LoadRoutine(slotName));
			return true;
		}

		public static bool DoQuickLoad(out string message)
		{
			bool sandbox = Sandbox.IsActive;
			List<SlotMeta> list = (from s in SlotStore.List()
				where s.sandbox == sandbox
				select s).ToList();
			if (list.Count == 0)
			{
				message = "当前模式还没有任何存档槽";
				return false;
			}
			return DoLoad(list[0].name, out message);
		}

		public static void OpenSavePage()
		{
			SavePage.Open();
		}

		private static IEnumerator LoadRoutine(string slotName)
		{
			if (SlotStore.InWorld)
			{
				try
				{
					if ((Object)(object)PauseHandler.main != (Object)null && PauseHandler.paused)
					{
						PauseHandler.TogglePause();
					}
				}
				catch (Exception ex)
				{
					Log.Warn("解除暂停失败:" + ex.Message);
				}
				PreRunScript oldPre = PreRunScript.instance;
				try
				{
					PlayerCamera.main.ToMainMenu();
				}
				catch (Exception ex2)
				{
					Log.Error("回主菜单失败:" + ex2.Message);
				}
				float waited = 0f;
				while (waited < 12f)
				{
					PreRunScript instance = PreRunScript.instance;
					if ((Object)(object)instance != (Object)null && (Object)(object)instance != (Object)(object)oldPre)
					{
						break;
					}
					waited += Time.unscaledDeltaTime;
					yield return null;
				}
				Log.Info("等主菜单结束,等待 " + waited.ToString("0.0") + "s");
				yield return null;
				yield return null;
			}
			if (!SlotStore.PrepareLoad(slotName, out var error))
			{
				Log.Error(error);
				Ui.Notify("读档失败:" + error);
				yield break;
			}
			SaveSystem.loadedRun = true;
			PreRunScript instance2 = PreRunScript.instance;
			if ((Object)(object)instance2 == (Object)null)
			{
				Log.Error("找不到 PreRunScript.instance");
				Ui.Notify("读档失败:找不到主菜单脚本");
				yield break;
			}
			if (PlayerPrefs.GetInt("didbasiccourse") == 0)
			{
				if (CfgBypassGate == null || !CfgBypassGate.Value)
				{
					Ui.Notify("原版要求先做完基础教程才能读档(BypassTutorialGate 可关掉这条限制)");
					yield break;
				}
				Ui.Notify("没做完基础教程 → 走直连读档");
				RawLoad(instance2);
				yield break;
			}
			try
			{
				instance2.LoadRun();
			}
			catch (Exception ex3)
			{
				Log.Error("LoadRun 抛异常:" + ex3);
			}
			Log.Info("已触发读档:" + slotName);
		}

		private static void RawLoad(PreRunScript pre)
		{
			Type typeFromHandle = typeof(PreRunScript);
			try
			{
				FieldInfo field = typeFromHandle.GetField("savedCharacter", BindingFlags.Instance | BindingFlags.NonPublic);
				FieldInfo field2 = typeFromHandle.GetField("characterSelected", BindingFlags.Static | BindingFlags.Public);
				if (field != null && field2 != null)
				{
					field2.SetValue(null, field.GetValue(pre));
				}
				FieldInfo field3 = typeFromHandle.GetField("startedLoading", BindingFlags.Instance | BindingFlags.NonPublic);
				if (field3 != null)
				{
					field3.SetValue(pre, true);
				}
				if ((Object)(object)pre.tutorialWarning != (Object)null)
				{
					pre.tutorialWarning.SetActive(false);
				}
				if ((Object)(object)pre.runSettingsScreen != (Object)null)
				{
					pre.runSettingsScreen.SetActive(false);
				}
			}
			catch (Exception ex)
			{
				Log.Warn("直连读档准备失败:" + ex.Message);
			}
			((MonoBehaviour)Instance).StartCoroutine(SceneLoadRoutine());
		}

		private static IEnumerator SceneLoadRoutine()
		{
			try
			{
				if ((Object)(object)GlobalDark.main != (Object)null)
				{
					GlobalDark.main.Darken();
				}
			}
			catch
			{
			}
			float t = 0f;
			while (t < 6f)
			{
				bool flag = false;
				try
				{
					flag = (Object)(object)GlobalDark.main != (Object)null && GlobalDark.main.IsDarkening();
				}
				catch
				{
				}
				if (!flag)
				{
					break;
				}
				t += Time.unscaledDeltaTime;
				yield return null;
			}
			SceneManager.LoadScene("SampleScene");
		}
	}
	internal static class Log
	{
		private static ManualLogSource _src;

		public static void Init(ManualLogSource src)
		{
			_src = src;
		}

		public static void Info(string m)
		{
			if (_src != null)
			{
				_src.LogInfo((object)m);
			}
			else
			{
				Debug.Log((object)("[SM] " + m));
			}
		}

		public static void Warn(string m)
		{
			if (_src != null)
			{
				_src.LogWarning((object)m);
			}
			else
			{
				Debug.LogWarning((object)("[SM] " + m));
			}
		}

		public static void Error(string m)
		{
			if (_src != null)
			{
				_src.LogError((object)m);
			}
			else
			{
				Debug.LogError((object)("[SM] " + m));
			}
		}
	}
	internal static class Sandbox
	{
		public static bool IsActive
		{
			get
			{
				try
				{
					Type typeFromHandle = typeof(TutorialHandler);
					FieldInfo field = typeFromHandle.GetField("main", BindingFlags.Static | BindingFlags.Public);
					Object val = (Object)((field != null) ? /*isinst with value type is only supported in some contexts*/: null);
					if (val == (Object)null)
					{
						return false;
					}
					FieldInfo field2 = typeFromHandle.GetField("activeCourse", BindingFlags.Instance | BindingFlags.Public);
					if (field2 == null)
					{
						return false;
					}
					object? value = field2.GetValue(val);
					TutorialCourse val2 = (TutorialCourse)((value is TutorialCourse) ? value : null);
					if ((Object)(object)val2 == (Object)null)
					{
						return false;
					}
					string text = val2.LocaleName();
					return !string.IsNullOrEmpty(text) && text.IndexOf("sandbox", StringComparison.OrdinalIgnoreCase) >= 0;
				}
				catch (Exception ex)
				{
					Log.Warn("沙盒检测失败:" + ex.Message);
					return false;
				}
			}
		}
	}
	internal static class Ui
	{
		public static void Notify(string msg)
		{
			Log.Info(msg);
			try
			{
				Type typeFromHandle = typeof(ConsoleScript);
				object obj = typeFromHandle.GetField("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(null);
				if (obj != null)
				{
					typeFromHandle.GetMethod("LogToConsole", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[1] { typeof(string) }, null)?.Invoke(obj, new object[1] { msg });
				}
			}
			catch
			{
			}
		}

		public static void Alert(string msg, bool important = false)
		{
			Log.Info(msg);
			try
			{
				if ((Object)(object)PlayerCamera.main != (Object)null)
				{
					PlayerCamera.main.DoAlert(msg, important);
				}
			}
			catch
			{
			}
		}
	}
	internal static class SavePage
	{
		private enum Screen
		{
			Main,
			Options
		}

		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static UnityAction <>9__21_0;

			public static Action <>9__32_0;

			public static Action <>9__32_1;

			public static Action <>9__32_2;

			public static Action <>9__32_3;

			public static Action <>9__32_4;

			public static Action <>9__32_5;

			public static Action <>9__32_6;

			public static Action <>9__33_0;

			public static Action <>9__33_1;

			public static Func<SlotMeta, bool> <>9__34_1;

			public static Func<SlotMeta, bool> <>9__34_2;

			public static UnityAction<string> <>9__34_3;

			internal void <InstallTab>b__21_0()
			{
				if ((Object)(object)SettingsMenu.instance != (Object)null)
				{
					SettingsMenu.instance.SelectTab(5);
				}
			}

			internal void <BuildLeft>b__32_0()
			{
				Ui.Notify(Plugin.DoSave(null, out var message) ? message : ("存档失败:" + message));
				Rebuild();
			}

			internal void <BuildLeft>b__32_1()
			{
				Ui.Notify(Plugin.DoQuickLoad(out var message) ? message : ("读档失败:" + message));
			}

			internal void <BuildLeft>b__32_2()
			{
				try
				{
					if (!Directory.Exists(Paths.SlotRoot))
					{
						Directory.CreateDirectory(Paths.SlotRoot);
					}
					PreRunScript.OpenInFileBrowser(Paths.SlotRoot);
				}
				catch (Exception ex)
				{
					Ui.Notify("打开目录失败:" + ex.Message);
				}
			}

			internal void <BuildLeft>b__32_3()
			{
				Ui.Notify(Plugin.DoLoad(_selected, out var message) ? message : ("读档失败:" + message));
			}

			internal void <BuildLeft>b__32_4()
			{
				Ui.Notify(SlotStore.RestoreBackup(_selected, out var error) ? ("已回滚:" + _selected) : ("回滚失败:" + error));
				Rebuild();
			}

			internal void <BuildLeft>b__32_5()
			{
				Ui.Notify(SlotStore.Delete(_selected, out var error) ? ("已删除:" + _selected) : ("删除失败:" + error));
				_selected = null;
				Rebuild();
			}

			internal void <BuildLeft>b__32_6()
			{
				_screen = Screen.Options;
				Rebuild();
			}

			internal void <BuildOptions>b__33_0()
			{
				_screen = Screen.Main;
				Rebuild();
			}

			internal void <BuildOptions>b__33_1()
			{
				int[] array = new int[7] { 1, 2, 5, 10, 15, 30, 60 };
				int value = Plugin.CfgAutoSaveMinutes.Value;
				int num = Array.IndexOf(array, value);
				Plugin.CfgAutoSaveMinutes.Value = array[(num + 1) % array.Length];
				Plugin.SaveConfig();
				Rebuild();
			}

			internal bool <BuildRight>b__34_1(SlotMeta s)
			{
				return s.name != _selected;
			}

			internal bool <BuildRight>b__34_2(SlotMeta s)
			{
				return s.name == _selected;
			}

			internal void <BuildRight>b__34_3(string v)
			{
				try
				{
					if (!SlotStore.Rename(_selected, v, out var error))
					{
						Ui.Notify("改名失败:" + error);
						return;
					}
					Ui.Notify("已改名:" + v);
					_selected = Paths.SafeName(v);
					Rebuild();
				}
				catch (Exception ex)
				{
					Log.Error("改名异常:" + ex);
				}
			}
		}

		public const int TabIndex = 5;

		private const string RowPrefab = "Special/GameSettingLanguage";

		private const string InputPrefab = "Special/GameSettingInt";

		private const float LeftWidth = 196f;

		private const float Gap = 8f;

		private const float TabGap = 4f;

		private static readonly List<GameObject> _rows = new List<GameObject>();

		private static RectTransform _content;

		private static RectTransform _leftContent;

		private static RectTransform _rightContent;

		private static ScrollRect _leftScroll;

		private static ScrollRect _rightScroll;

		private static SettingsMenu _menu;

		private static Button _tabButton;

		private static string _selected;

		private static float _pageHeight = 430f;

		private static float _keepLeft = 1f;

		private static float _keepRight = 1f;

		private static Screen _screen = Screen.Main;

		private static int _lastCat = -1;

		private static TMP_FontAsset _font;

		private static float _colWidth = 196f;

		public static void InstallTab(SettingsMenu menu)
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Expected O, but got Unknown
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Expected O, but got Unknown
			_menu = menu;
			if ((Object)(object)menu == (Object)null || menu.buttons == null || menu.buttons.Count == 0 || ((Object)(object)_tabButton != (Object)null && (Object)(object)((Component)_tabButton).gameObject != (Object)null))
			{
				return;
			}
			Button val = menu.buttons[menu.buttons.Count - 1];
			if ((Object)(object)val == (Object)null || (Object)(object)((Component)val).transform.parent == (Object)null)
			{
				return;
			}
			GameObject val2 = Object.Instantiate<GameObject>(((Component)val).gameObject, ((Component)val).transform.parent);
			((Object)val2).name = "SaveManagerTab";
			val2.SetActive(true);
			RectTransform component = ((Component)val).GetComponent<RectTransform>();
			RectTransform component2 = val2.GetComponent<RectTransform>();
			if ((Object)(object)component != (Object)null && (Object)(object)component2 != (Object)null)
			{
				component2.anchorMin = component.anchorMin;
				component2.anchorMax = component.anchorMax;
				component2.pivot = component.pivot;
				component2.sizeDelta = component.sizeDelta;
				component2.anchoredPosition = component.anchoredPosition;
			}
			TextMeshProUGUI[] componentsInChildren = val2.GetComponentsInChildren<TextMeshProUGUI>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				((Behaviour)componentsInChildren[i]).enabled = false;
			}
			Image[] componentsInChildren2 = val2.GetComponentsInChildren<Image>(true);
			foreach (Image val3 in componentsInChildren2)
			{
				if ((Object)(object)((Component)val3).gameObject != (Object)(object)val2)
				{
					((Behaviour)val3).enabled = false;
				}
			}
			AddLabel(val2);
			Button component3 = val2.GetComponent<Button>();
			if ((Object)(object)component3 == (Object)null)
			{
				Object.Destroy((Object)(object)val2);
				return;
			}
			component3.onClick = new ButtonClickedEvent();
			ButtonClickedEvent onClick = component3.onClick;
			object obj = <>c.<>9__21_0;
			if (obj == null)
			{
				UnityAction val4 = delegate
				{
					if ((Object)(object)SettingsMenu.instance != (Object)null)
					{
						SettingsMenu.instance.SelectTab(5);
					}
				};
				<>c.<>9__21_0 = val4;
				obj = (object)val4;
			}
			((UnityEvent)onClick).AddListener((UnityAction)obj);
			menu.buttons.Add(component3);
			_tabButton = component3;
			RelayoutTabRow();
			Log.Info("设置页已注册「存档」分页(buttons index " + (menu.buttons.Count - 1) + ")");
		}

		private static void RelayoutTabRow()
		{
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_035e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0368: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)_menu == (Object)null || _menu.buttons == null)
				{
					return;
				}
				int count = _menu.buttons.Count;
				int num = count - 1;
				if (num < 1)
				{
					return;
				}
				RectTransform val = FindLayoutGroup(((Object)(object)_tabButton != (Object)null) ? ((Component)_tabButton).transform : null);
				HorizontalLayoutGroup val2 = (((Object)(object)val != (Object)null) ? ((Component)val).GetComponent<HorizontalLayoutGroup>() : null);
				GridLayoutGroup val3 = (((Object)(object)val != (Object)null) ? ((Component)val).GetComponent<GridLayoutGroup>() : null);
				float num2 = 0f;
				Rect rect;
				for (int i = 0; i < num; i++)
				{
					RectTransform val4 = (((Object)(object)_menu.buttons[i] != (Object)null) ? ((Component)_menu.buttons[i]).GetComponent<RectTransform>() : null);
					if (!((Object)(object)val4 == (Object)null))
					{
						rect = val4.rect;
						float num3;
						if (!(((Rect)(ref rect)).width > 1f))
						{
							num3 = Mathf.Abs(val4.sizeDelta.x);
						}
						else
						{
							rect = val4.rect;
							num3 = ((Rect)(ref rect)).width;
						}
						float num4 = num3;
						if (num4 > num2)
						{
							num2 = num4;
						}
					}
				}
				float num5 = 4f;
				float num6 = 0f;
				if ((Object)(object)val2 != (Object)null)
				{
					num5 = ((HorizontalOrVerticalLayoutGroup)val2).spacing;
					rect = val.rect;
					num6 = ((Rect)(ref rect)).width - (float)((LayoutGroup)val2).padding.left - (float)((LayoutGroup)val2).padding.right;
				}
				else if ((Object)(object)val3 != (Object)null)
				{
					num5 = val3.spacing.x;
					rect = val.rect;
					num6 = ((Rect)(ref rect)).width - (float)((LayoutGroup)val3).padding.left - (float)((LayoutGroup)val3).padding.right;
				}
				float num7;
				if (num6 > 1f)
				{
					num7 = Mathf.Floor((num6 - (float)num * num5) / (float)(num + 1)) - 1f;
				}
				else
				{
					if (!(num2 > 1f))
					{
						return;
					}
					num7 = Mathf.Floor(((float)num * num2 - 4f) / (float)(num + 1)) - 1f;
				}
				num7 = Mathf.Max(24f, num7);
				for (int j = 0; j < count; j++)
				{
					RectTransform val5 = (((Object)(object)_menu.buttons[j] != (Object)null) ? ((Component)_menu.buttons[j]).GetComponent<RectTransform>() : null);
					if ((Object)(object)val5 == (Object)null)
					{
						continue;
					}
					val5.sizeDelta = new Vector2(num7, val5.sizeDelta.y);
					LayoutElement component = ((Component)val5).GetComponent<LayoutElement>();
					if ((Object)(object)component != (Object)null)
					{
						component.preferredWidth = num7;
						if (component.minWidth > num7)
						{
							component.minWidth = num7;
						}
					}
				}
				if ((Object)(object)val != (Object)null)
				{
					LayoutRebuilder.MarkLayoutForRebuild(val);
					Canvas.ForceUpdateCanvases();
					return;
				}
				float num8 = float.MaxValue;
				for (int k = 0; k < num; k++)
				{
					RectTransform component2 = ((Component)_menu.buttons[k]).GetComponent<RectTransform>();
					if ((Object)(object)component2 != (Object)null)
					{
						num8 = Mathf.Min(num8, component2.anchoredPosition.x);
					}
				}
				if (num8 > 1000000f)
				{
					return;
				}
				for (int l = 0; l < count; l++)
				{
					RectTransform component3 = ((Component)_menu.buttons[l]).GetComponent<RectTransform>();
					if (!((Object)(object)component3 == (Object)null))
					{
						component3.anchoredPosition = new Vector2(num8 + (float)l * (num7 + 4f), component3.anchoredPosition.y);
					}
				}
			}
			catch (Exception ex)
			{
				Log.Warn("分页排版修正失败:" + ex.Message);
			}
		}

		private static void AddLabel(GameObject go)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				GameObject val = new GameObject("Label", new Type[1] { typeof(RectTransform) });
				val.transform.SetParent(go.transform, false);
				RectTransform component = val.GetComponent<RectTransform>();
				component.anchorMin = Vector2.zero;
				component.anchorMax = Vector2.one;
				component.offsetMin = new Vector2(2f, 2f);
				component.offsetMax = new Vector2(-2f, -2f);
				TextMeshProUGUI val2 = val.AddComponent<TextMeshProUGUI>();
				TMP_FontAsset val3 = BorrowFont();
				if ((Object)(object)val3 != (Object)null)
				{
					((TMP_Text)val2).font = val3;
				}
				((TMP_Text)val2).text = "存档";
				((TMP_Text)val2).fontSize = 15f;
				((TMP_Text)val2).alignment = (TextAlignmentOptions)514;
				((Graphic)val2).color = new Color(0.92f, 0.92f, 0.88f, 1f);
				((Graphic)val2).raycastTarget = false;
			}
			catch (Exception ex)
			{
				Log.Warn("分页文字创建失败:" + ex.Message);
			}
		}

		private static TMP_FontAsset BorrowFont()
		{
			if ((Object)(object)_font != (Object)null)
			{
				return _font;
			}
			try
			{
				if ((Object)(object)_menu != (Object)null)
				{
					TextMeshProUGUI[] componentsInChildren = ((Component)_menu).GetComponentsInChildren<TextMeshProUGUI>(true);
					foreach (TextMeshProUGUI val in componentsInChildren)
					{
						if ((Object)(object)((TMP_Text)val).font != (Object)null)
						{
							_font = ((TMP_Text)val).font;
							return _font;
						}
					}
				}
				TMP_FontAsset[] array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
				if (array != null && array.Length != 0)
				{
					_font = array[0];
				}
			}
			catch
			{
			}
			return _font;
		}

		private static RectTransform FindLayoutGroup(Transform from)
		{
			Transform val = from;
			for (int i = 0; i < 6; i++)
			{
				if (!((Object)(object)val != (Object)null))
				{
					break;
				}
				LayoutGroup component = ((Component)val).GetComponent<LayoutGroup>();
				if ((Object)(object)component != (Object)null)
				{
					Transform transform = ((Component)component).transform;
					return (RectTransform)(object)((transform is RectTransform) ? transform : null);
				}
				val = val.parent;
			}
			return null;
		}

		public static void OnTabSelected(SettingsMenu menu, SettingCategory category)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Invalid comparison between Unknown and I4
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Invalid comparison between I4 and Unknown
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected I4, but got Unknown
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected I4, but got Unknown
			_menu = menu;
			ClearRows();
			if ((int)category != 5)
			{
				_lastCat = (int)category;
				return;
			}
			if (_lastCat != (int)category)
			{
				_screen = Screen.Main;
				_lastCat = (int)category;
			}
			try
			{
				BuildPage();
			}
			catch (Exception ex)
			{
				Log.Error("渲染存档页失败:" + ex);
			}
		}

		private static void ClearRows()
		{
			if ((Object)(object)_leftScroll != (Object)null)
			{
				_keepLeft = _leftScroll.verticalNormalizedPosition;
			}
			if ((Object)(object)_rightScroll != (Object)null)
			{
				_keepRight = _rightScroll.verticalNormalizedPosition;
			}
			for (int num = _rows.Count - 1; num >= 0; num--)
			{
				if ((Object)(object)_rows[num] != (Object)null)
				{
					Object.Destroy((Object)(object)_rows[num]);
				}
			}
			_rows.Clear();
			_leftScroll = null;
			_rightScroll = null;
			_leftContent = null;
			_rightContent = null;
		}

		private static void BuildPage()
		{
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_menu == (Object)null) && !((Object)(object)_menu.content == (Object)null))
			{
				_content = _menu.content;
				float num = PageWidth();
				_pageHeight = PageHeight();
				RectTransform val = NewRect("SaveManager.Page", (Transform)(object)_content);
				Vector2 val2 = default(Vector2);
				((Vector2)(ref val2))..ctor(0.5f, 1f);
				val.anchorMax = val2;
				val.anchorMin = val2;
				val.pivot = new Vector2(0.5f, 1f);
				val.sizeDelta = new Vector2(num, _pageHeight);
				val.anchoredPosition = Vector2.zero;
				_rows.Add(((Component)val).gameObject);
				if (_screen == Screen.Options)
				{
					BuildOptions(val, num);
				}
				else
				{
					BuildLeft(NewColumn("Left", val, 0f, 196f, out _leftScroll, out _leftContent));
					BuildRight(NewColumn("Right", val, 204f, num - 196f - 8f, out _rightScroll, out _rightContent));
				}
				_content.sizeDelta = new Vector2(_content.sizeDelta.x, _pageHeight);
				if ((Object)(object)Plugin.Instance != (Object)null)
				{
					((MonoBehaviour)Plugin.Instance).StartCoroutine(RestoreScroll());
				}
			}
		}

		private static RectTransform NewColumn(string name, RectTransform parent, float x, float width, out ScrollRect scroll, out RectTransform content)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			RectTransform val = NewRect(name, (Transform)(object)parent);
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(0f, 1f);
			val.anchorMax = val2;
			val.anchorMin = val2;
			val.pivot = new Vector2(0f, 1f);
			val.sizeDelta = new Vector2(width, _pageHeight);
			val.anchoredPosition = new Vector2(x, 0f);
			_colWidth = width;
			((Component)val).gameObject.AddComponent<RectMask2D>();
			((Graphic)((Component)val).gameObject.AddComponent<Image>()).color = new Color(0f, 0f, 0f, 0.001f);
			content = NewRect(name + ".Content", (Transform)(object)val);
			content.anchorMin = new Vector2(0f, 1f);
			content.anchorMax = new Vector2(1f, 1f);
			content.pivot = new Vector2(0.5f, 1f);
			content.anchoredPosition = Vector2.zero;
			content.sizeDelta = new Vector2(0f, 0f);
			scroll = ((Component)val).gameObject.AddComponent<ScrollRect>();
			scroll.viewport = val;
			scroll.content = content;
			scroll.horizontal = false;
			scroll.vertical = true;
			scroll.movementType = (MovementType)2;
			scroll.scrollSensitivity = 40f;
			return val;
		}

		private static IEnumerator RestoreScroll()
		{
			yield return null;
			if ((Object)(object)_leftScroll != (Object)null)
			{
				_leftScroll.verticalNormalizedPosition = Mathf.Clamp01(_keepLeft);
			}
			if ((Object)(object)_rightScroll != (Object)null)
			{
				_rightScroll.verticalNormalizedPosition = Mathf.Clamp01(_keepRight);
			}
		}

		private static void BuildLeft(RectTransform column)
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			float y = 0f;
			bool isActive = Sandbox.IsActive;
			Section("操作", ref y);
			LRow("+ 新建存档", ref y, delegate
			{
				Ui.Notify(Plugin.DoSave(null, out var message) ? message : ("存档失败:" + message));
				Rebuild();
			}, isActive ? new Color?(new Color(0.98f, 0.86f, 0.52f, 1f)) : ((Color?)null));
			LRow("读取最近一档", ref y, delegate
			{
				Ui.Notify(Plugin.DoQuickLoad(out var message) ? message : ("读档失败:" + message));
			});
			LRow("打开文件夹", ref y, delegate
			{
				try
				{
					if (!Directory.Exists(Paths.SlotRoot))
					{
						Directory.CreateDirectory(Paths.SlotRoot);
					}
					PreRunScript.OpenInFileBrowser(Paths.SlotRoot);
				}
				catch (Exception ex)
				{
					Ui.Notify("打开目录失败:" + ex.Message);
				}
			});
			if (_selected != null)
			{
				Section("已选存档", ref y);
				LRow("读取此档", ref y, delegate
				{
					Ui.Notify(Plugin.DoLoad(_selected, out var message) ? message : ("读档失败:" + message));
				});
				LRow("回滚上一档", ref y, delegate
				{
					Ui.Notify(SlotStore.RestoreBackup(_selected, out var error) ? ("已回滚:" + _selected) : ("回滚失败:" + error));
					Rebuild();
				});
				LRow("删除此档", ref y, delegate
				{
					Ui.Notify(SlotStore.Delete(_selected, out var error) ? ("已删除:" + _selected) : ("删除失败:" + error));
					_selected = null;
					Rebuild();
				});
			}
			Section("存档设置", ref y);
			LRow("存档选项与设置 ▸", ref y, (Action)delegate
			{
				_screen = Screen.Options;
				Rebuild();
			}, (Color?)new Color(0.86f, 0.9f, 0.98f, 1f));
			_leftContent.sizeDelta = new Vector2(_leftContent.sizeDelta.x, y + 8f);
		}

		private static void BuildOptions(RectTransform root, float width)
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			NewColumn("Options", root, 0f, width, out _leftScroll, out _leftContent);
			float y = 0f;
			LRow("← 返回存档列表", ref y, (Action)delegate
			{
				_screen = Screen.Main;
				Rebuild();
			}, (Color?)new Color(0.98f, 0.86f, 0.52f, 1f));
			Section("行为", ref y);
			ToggleRow("游戏内读档", ref y, Plugin.CfgInGameLoad);
			ToggleRow("自动存档", ref y, Plugin.CfgAutoSave);
			LRow("自动存档间隔:" + Plugin.CfgAutoSaveMinutes.Value + " 分钟", ref y, delegate
			{
				int[] array = new int[7] { 1, 2, 5, 10, 15, 30, 60 };
				int value = Plugin.CfgAutoSaveMinutes.Value;
				int num = Array.IndexOf(array, value);
				Plugin.CfgAutoSaveMinutes.Value = array[(num + 1) % array.Length];
				Plugin.SaveConfig();
				Rebuild();
			});
			Section("存档标记", ref y);
			ToggleRow("标记作弊(成就关闭)", ref y, Plugin.CfgCheatMark);
			ToggleRow("沙盒不标作弊", ref y, Plugin.CfgSandboxNoCheat);
			Section("其它", ref y);
			ToggleRow("联机允许存读档", ref y, Plugin.CfgAllowMp);
			ToggleRow("覆盖前保留上一档", ref y, Plugin.CfgKeepBackup);
			ToggleRow("绕过教程门禁", ref y, Plugin.CfgBypassGate);
			_leftContent.sizeDelta = new Vector2(_leftContent.sizeDelta.x, y + 8f);
		}

		private static void BuildRight(RectTransform column)
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0355: Unknown result type (might be due to invalid IL or missing references)
			List<SlotMeta> source = SlotStore.List();
			bool sandboxNow = Sandbox.IsActive;
			source = source.Where((SlotMeta s) => s.sandbox == sandboxNow).ToList();
			if (_selected != null && source.All((SlotMeta s) => s.name != _selected))
			{
				_selected = null;
			}
			float x = column.sizeDelta.x;
			float y = 0f;
			if (_selected != null)
			{
				SlotMeta slotMeta = source.FirstOrDefault((SlotMeta s) => s.name == _selected);
				RRow("已选:" + _selected, ref y, x, (Action)null, (Color?)new Color(0.98f, 0.86f, 0.52f, 1f));
				GameObject val = NewRow("Special/GameSettingInt", _rightContent, x, ref y);
				if ((Object)(object)val != (Object)null)
				{
					try
					{
						TextMeshProUGUI component = ((Component)val.transform.GetChild(0)).GetComponent<TextMeshProUGUI>();
						if ((Object)(object)component != (Object)null)
						{
							((TMP_Text)component).text = "新名字";
						}
						TMP_InputField component2 = ((Component)val.transform.GetChild(1)).GetComponent<TMP_InputField>();
						if ((Object)(object)component2 != (Object)null)
						{
							component2.SetTextWithoutNotify((slotMeta != null) ? slotMeta.name : _selected);
							((UnityEvent<string>)(object)component2.onEndEdit).AddListener((UnityAction<string>)delegate(string v)
							{
								try
								{
									if (!SlotStore.Rename(_selected, v, out var error))
									{
										Ui.Notify("改名失败:" + error);
									}
									else
									{
										Ui.Notify("已改名:" + v);
										_selected = Paths.SafeName(v);
										Rebuild();
									}
								}
								catch (Exception ex2)
								{
									Log.Error("改名异常:" + ex2);
								}
							});
						}
					}
					catch (Exception ex)
					{
						Log.Warn("改名输入行失败:" + ex.Message);
					}
				}
			}
			RRow("共 " + source.Count + " 档(无上限,按时间倒序)", ref y, x, (Action)null, (Color?)new Color(0.72f, 0.72f, 0.68f, 1f));
			if (source.Count == 0)
			{
				RRow("(还没有存档:进游戏按 F5,或点左边「新建存档」)", ref y, x, (Action)null, (Color?)new Color(0.72f, 0.72f, 0.68f, 1f));
			}
			else
			{
				foreach (SlotMeta item in source)
				{
					SlotMeta mm = item;
					string text = ((item.name == _selected) ? "▶ " : "\u2003");
					string text2 = ((item.mode == "mp") ? "联机" : "单人");
					if (item.sandbox)
					{
						text2 += "·沙盒";
					}
					if (item.cheated)
					{
						text2 += "·作弊";
					}
					RRow($"{text}{item.name}\u2003D{item.biome} · {item.DurationText()} · {item.itemCount}件 · {text2}", ref y, x, delegate
					{
						_selected = mm.name;
						Rebuild();
					});
				}
			}
			_rightContent.sizeDelta = new Vector2(0f, Mathf.Max(y, _pageHeight));
		}

		private static void Rebuild()
		{
			OnTabSelected(_menu, (SettingCategory)5);
		}

		private static GameObject NewRow(string prefab, RectTransform parent, float width, ref float y)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			GameObject val;
			try
			{
				val = Utils.Create(prefab, (Transform)(object)parent);
			}
			catch (Exception ex)
			{
				Log.Warn("生成行失败 [" + prefab + "]:" + ex.Message);
				return null;
			}
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			RectTransform component = val.GetComponent<RectTransform>();
			if ((Object)(object)component != (Object)null)
			{
				float num = ((component.sizeDelta.y > 1f) ? component.sizeDelta.y : 44f);
				Vector2 val2 = default(Vector2);
				((Vector2)(ref val2))..ctor(0.5f, 1f);
				component.anchorMax = val2;
				component.anchorMin = val2;
				component.pivot = new Vector2(0.5f, 0.5f);
				component.sizeDelta = new Vector2(width, num);
				component.anchoredPosition = new Vector2(0f, 0f - (y + num * 0.5f));
				y += num + 2f;
			}
			_rows.Add(val);
			return val;
		}

		private static void Section(string title, ref float y)
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			GameObject val = NewRow("Special/GameSettingLanguage", _leftContent, _colWidth, ref y);
			if (!((Object)(object)val == (Object)null))
			{
				TextMeshProUGUI component = ((Component)val.transform.GetChild(0)).GetComponent<TextMeshProUGUI>();
				if ((Object)(object)component != (Object)null)
				{
					((TMP_Text)component).text = title;
					((Graphic)component).color = new Color(0.98f, 0.86f, 0.52f, 1f);
				}
				Button component2 = val.GetComponent<Button>();
				if ((Object)(object)component2 != (Object)null)
				{
					component2.onClick = new ButtonClickedEvent();
				}
			}
		}

		private static void LRow(string label, ref float y, Action onClick, Color? color = null)
		{
			GameObject val = NewRow("Special/GameSettingLanguage", _leftContent, _colWidth, ref y);
			if (!((Object)(object)val == (Object)null))
			{
				Bind(val, label, onClick, color);
			}
		}

		private static void ToggleRow(string label, ref float y, ConfigEntry<bool> entry, string tip = null)
		{
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			bool on = entry != null && entry.Value;
			LRow(label + ":" + (on ? "开" : "关"), ref y, delegate
			{
				if (entry != null)
				{
					entry.Value = !on;
				}
				Plugin.SaveConfig();
				Rebuild();
			}, on ? new Color?(new Color(0.72f, 0.92f, 0.72f, 1f)) : ((Color?)null));
		}

		private static void RRow(string label, ref float y, float width, Action onClick, Color? color = null)
		{
			GameObject val = NewRow("Special/GameSettingLanguage", _rightContent, width, ref y);
			if (!((Object)(object)val == (Object)null))
			{
				Bind(val, label, onClick, color);
			}
		}

		private static void Bind(GameObject go, string label, Action onClick, Color? color, bool dragGuard = true)
		{
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			TextMeshProUGUI component = ((Component)go.transform.GetChild(0)).GetComponent<TextMeshProUGUI>();
			if ((Object)(object)component != (Object)null)
			{
				((TMP_Text)component).text = label;
				if (color.HasValue)
				{
					((Graphic)component).color = color.Value;
				}
			}
			Button component2 = go.GetComponent<Button>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.onClick = new ButtonClickedEvent();
			}
			if (onClick == null)
			{
				return;
			}
			RowClick rowClick = go.GetComponent<RowClick>();
			if ((Object)(object)rowClick == (Object)null)
			{
				rowClick = go.AddComponent<RowClick>();
			}
			rowClick.dragGuard = dragGuard;
			rowClick.Tap = delegate
			{
				try
				{
					onClick();
				}
				catch (Exception ex)
				{
					Log.Error("行点击异常:" + ex);
				}
			};
		}

		private static RectTransform NewRect(string name, Transform parent)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			RectTransform component = new GameObject(name, new Type[1] { typeof(RectTransform) }).GetComponent<RectTransform>();
			((Transform)component).SetParent(parent, false);
			return component;
		}

		private static float PageWidth()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			Rect rect = _content.rect;
			float num = ((Rect)(ref rect)).width;
			if (num <= 1f)
			{
				num = _content.sizeDelta.x;
			}
			if (num <= 1f)
			{
				Transform parent = ((Transform)_content).parent;
				RectTransform val = (RectTransform)(object)((parent is RectTransform) ? parent : null);
				if (val != null)
				{
					rect = val.rect;
					num = ((Rect)(ref rect)).width;
				}
			}
			if (num <= 1f)
			{
				num = 760f;
			}
			return num;
		}

		private static float PageHeight()
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			Transform parent = ((Transform)_content).parent;
			RectTransform val = (RectTransform)(object)((parent is RectTransform) ? parent : null);
			Rect rect;
			if (val != null)
			{
				rect = val.rect;
				if (((Rect)(ref rect)).height > 1f)
				{
					rect = val.rect;
					return ((Rect)(ref rect)).height;
				}
			}
			rect = _content.rect;
			float num = ((Rect)(ref rect)).height;
			if (num <= 1f)
			{
				num = _content.sizeDelta.y;
			}
			if (num <= 1f)
			{
				num = 430f;
			}
			return num;
		}

		public static void CommitLayout()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_content != (Object)null)
			{
				_content.sizeDelta = new Vector2(_content.sizeDelta.x, _pageHeight);
			}
		}

		public static bool Open()
		{
			if ((Object)(object)Plugin.Instance == (Object)null)
			{
				return false;
			}
			((MonoBehaviour)Plugin.Instance).StartCoroutine(OpenRoutine());
			return true;
		}

		private static IEnumerator OpenRoutine()
		{
			if ((Object)(object)PauseHandler.main != (Object)null)
			{
				if (!PauseHandler.paused)
				{
					PauseHandler.TogglePause();
				}
				yield return null;
				yield return null;
				if ((Object)(object)PauseHandler.main != (Object)null)
				{
					PauseHandler.main.Settings();
				}
			}
			else
			{
				Transform val = null;
				try
				{
					if ((Object)(object)PreRunScript.instance != (Object)null && (Object)(object)PreRunScript.instance.mainCanvas != (Object)null)
					{
						val = ((Component)PreRunScript.instance.mainCanvas).transform;
					}
					else if ((Object)(object)PlayerCamera.main != (Object)null && (Object)(object)PlayerCamera.main.mainCanvas != (Object)null)
					{
						val = ((Component)PlayerCamera.main.mainCanvas).transform;
					}
				}
				catch
				{
				}
				if ((Object)(object)val == (Object)null)
				{
					Ui.Notify("找不到 UI,无法打开存档页");
					yield break;
				}
				SettingsMenu.OpenMenu(val);
			}
			yield return null;
			yield return null;
			if ((Object)(object)SettingsMenu.instance != (Object)null)
			{
				try
				{
					SettingsMenu.instance.SelectTab(5);
				}
				catch (Exception ex)
				{
					Log.Error("跳转存档页失败:" + ex);
				}
			}
		}
	}
	internal class RowClick : MonoBehaviour, IPointerDownHandler, IEventSystemHandler, IPointerClickHandler
	{
		public Action Tap;

		public bool dragGuard = true;

		private Vector2 _down;

		public void OnPointerDown(PointerEventData e)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			_down = e.position;
		}

		public void OnPointerClick(PointerEventData e)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			if (dragGuard)
			{
				Vector2 val = e.position - _down;
				if (((Vector2)(ref val)).sqrMagnitude > 144f)
				{
					return;
				}
			}
			try
			{
				Tap?.Invoke();
			}
			catch (Exception ex)
			{
				Log.Error("行点击异常:" + ex);
			}
		}
	}
	[HarmonyPatch(typeof(SettingsMenu), "Start")]
	internal static class Patch_SettingsMenu_Start
	{
		private static void Postfix(SettingsMenu __instance)
		{
			try
			{
				SavePage.InstallTab(__instance);
			}
			catch (Exception ex)
			{
				Log.Error("装存档分页失败:" + ex);
			}
		}
	}
	[HarmonyPatch(typeof(SettingsMenu), "SelectTab", new Type[] { typeof(SettingCategory) })]
	internal static class Patch_SettingsMenu_SelectTab
	{
		private static void Postfix(SettingsMenu __instance, SettingCategory category)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				SavePage.OnTabSelected(__instance, category);
			}
			catch (Exception ex)
			{
				Log.Error("渲染存档分页失败:" + ex);
			}
		}
	}
	[Serializable]
	internal class SlotMeta
	{
		public int format = 1;

		public string name = "";

		public string createdAt = "";

		public string mode = "sp";

		public bool sandbox;

		public int biome = 1;

		public float runTime;

		public string character = "";

		public int itemCount;

		public bool cheated;

		public bool hasBackup;

		public long bytes;

		public List<string> players = new List<string>();

		public string note = "";

		public string TimeText()
		{
			try
			{
				if (DateTime.TryParse(createdAt, out var result))
				{
					return result.ToString("MM-dd HH:mm");
				}
			}
			catch
			{
			}
			return createdAt;
		}

		public string DurationText()
		{
			int num = (int)Math.Floor(runTime);
			int num2 = num / 3600;
			int num3 = num % 3600 / 60;
			if (num2 > 0)
			{
				return num2 + "h" + num3 + "m";
			}
			return num3 + "m" + num % 60 + "s";
		}
	}
	internal static class SlotStore
	{
		public static bool InWorld
		{
			get
			{
				if ((Object)(object)PlayerCamera.main != (Object)null)
				{
					return (Object)(object)PlayerCamera.main.body != (Object)null;
				}
				return false;
			}
		}

		public static bool Capture(string requestedName, out string slotName, out string error)
		{
			slotName = null;
			error = null;
			if (!InWorld)
			{
				error = "当前不在游戏世界内,无法存档";
				return false;
			}
			if (!Plugin.MpAllowed && Paths.MpPresent)
			{
				error = "联机存档被配置禁用(AllowInMultiplayer=false)";
				return false;
			}
			bool isActive = Sandbox.IsActive;
			try
			{
				bool num = Plugin.CfgCheatMark != null && Plugin.CfgCheatMark.Value;
				bool flag = isActive && (Plugin.CfgSandboxNoCheat == null || Plugin.CfgSandboxNoCheat.Value);
				if (num && !flag && !SteamManager.disableAchievements)
				{
					SteamManager.disableAchievements = true;
				}
				SaveSystem.SaveGame();
			}
			catch (Exception ex)
			{
				error = "调用游戏 SaveGame 失败:" + ex.Message;
				Log.Error(error + "\n" + ex.StackTrace);
				return false;
			}
			bool mpPresent = Paths.MpPresent;
			string text = (File.Exists(Paths.VanillaSave) ? Paths.VanillaSave : (mpPresent ? Path.Combine(Paths.MpSaveDir, "save.sv") : null));
			if (text == null || !File.Exists(text))
			{
				error = "游戏没有产出 save.sv(可能处于不允许存档的层)";
				return false;
			}
			SlotMeta slotMeta = Probe(text);
			slotMeta.mode = (mpPresent ? "mp" : "sp");
			slotMeta.sandbox = isActive;
			slotMeta.createdAt = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
			if (mpPresent)
			{
				slotMeta.players = ListMpPlayers();
			}
			string text2 = Paths.SafeName(requestedName);
			if (text2 == null)
			{
				text2 = UniqueAutoName(slotMeta);
			}
			try
			{
				Paths.EnsureRoot();
				Directory.CreateDirectory(Paths.SlotDir(text2));
				if (Plugin.CfgKeepBackup != null && Plugin.CfgKeepBackup.Value && (File.Exists(Paths.SlotSave(text2)) || Directory.Exists(Paths.SlotMp(text2))))
				{
					RotateBackup(text2);
					slotMeta.hasBackup = true;
				}
				try
				{
					File.Copy(Paths.VanillaSave, Paths.SlotSave(text2), overwrite: true);
				}
				catch (FileNotFoundException)
				{
				}
				if (Directory.Exists(Paths.SlotMp(text2)))
				{
					Directory.Delete(Paths.SlotMp(text2), recursive: true);
				}
				if (mpPresent)
				{
					CopyDir(Paths.MpSaveDir, Paths.SlotMp(text2));
				}
				slotMeta.name = text2;
				slotMeta.bytes = new FileInfo(text).Length;
				WriteMeta(text2, slotMeta);
				slotName = text2;
				Log.Info($"已存档 -> {text2}({slotMeta.mode},{FormatBytes(slotMeta.bytes)})");
				return true;
			}
			catch (Exception ex3)
			{
				error = "写入存档槽失败:" + ex3.Message;
				Log.Error(error + "\n" + ex3.StackTrace);
				return false;
			}
		}

		public static bool PrepareLoad(string slotName, out string error)
		{
			error = null;
			if (!Directory.Exists(Paths.SlotDir(slotName)))
			{
				error = "槽不存在:" + slotName;
				return false;
			}
			try
			{
				string text = Paths.SlotSave(slotName);
				string text2 = Paths.SlotMp(slotName);
				if (Directory.Exists(text2))
				{
					if (Directory.Exists(Paths.MpSaveDir))
					{
						string destDirName = Paths.MpSaveDir + ".old_" + DateTime.Now.ToString("HHmmss");
						try
						{
							Directory.Move(Paths.MpSaveDir, destDirName);
						}
						catch
						{
							Directory.Delete(Paths.MpSaveDir, recursive: true);
						}
					}
					CopyDir(text2, Paths.MpSaveDir);
				}
				if (File.Exists(text))
				{
					File.Copy(text, Paths.VanillaSave, overwrite: true);
				}
				else if (!Directory.Exists(text2))
				{
					error = "槽里没有 save.sv:" + slotName;
					return false;
				}
				Log.Info("已铺档 -> " + slotName);
				return true;
			}
			catch (Exception ex)
			{
				error = "铺档失败:" + ex.Message;
				Log.Error(error + "\n" + ex.StackTrace);
				return false;
			}
		}

		public static bool ExportToGameSave(string slotName, out string error)
		{
			return PrepareLoad(slotName, out error);
		}

		public static List<SlotMeta> List()
		{
			List<SlotMeta> list = new List<SlotMeta>();
			try
			{
				if (!Directory.Exists(Paths.SlotRoot))
				{
					return list;
				}
				string[] directories = Directory.GetDirectories(Paths.SlotRoot);
				foreach (string text in directories)
				{
					string fileName = Path.GetFileName(text);
					SlotMeta slotMeta = ReadMeta(fileName);
					if (slotMeta == null)
					{
						if (!File.Exists(Path.Combine(text, "save.sv")) && !Directory.Exists(Path.Combine(text, "mp")))
						{
							continue;
						}
						slotMeta = new SlotMeta
						{
							name = fileName
						};
					}
					slotMeta.name = fileName;
					if (string.IsNullOrEmpty(slotMeta.createdAt))
					{
						try
						{
							slotMeta.createdAt = File.GetLastWriteTime(text).ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
						}
						catch
						{
						}
					}
					slotMeta.hasBackup = Directory.Exists(Paths.SlotBackup(fileName));
					list.Add(slotMeta);
				}
			}
			catch (Exception ex)
			{
				Log.Error("列存档失败:" + ex.Message);
			}
			return list.OrderByDescending<SlotMeta, string>((SlotMeta m) => m.createdAt, StringComparer.Ordinal).ToList();
		}

		public static bool Delete(string slotName, out string error)
		{
			error = null;
			try
			{
				if (!Directory.Exists(Paths.SlotDir(slotName)))
				{
					error = "槽不存在";
					return false;
				}
				Directory.Delete(Paths.SlotDir(slotName), recursive: true);
				Log.Info("已删除槽:" + slotName);
				return true;
			}
			catch (Exception ex)
			{
				error = "删除失败:" + ex.Message;
				return false;
			}
		}

		public static bool Rename(string oldName, string newNameRaw, out string error)
		{
			error = null;
			string text = Paths.SafeName(newNameRaw);
			if (text == null)
			{
				error = "名字不合法";
				return false;
			}
			try
			{
				if (!Directory.Exists(Paths.SlotDir(oldName)))
				{
					error = "槽不存在";
					return false;
				}
				if (Directory.Exists(Paths.SlotDir(text)))
				{
					error = "目标名字已存在";
					return false;
				}
				Directory.Move(Paths.SlotDir(oldName), Paths.SlotDir(text));
				SlotMeta slotMeta = ReadMeta(text) ?? new SlotMeta();
				slotMeta.name = text;
				WriteMeta(text, slotMeta);
				Log.Info("已改名:" + oldName + " -> " + text);
				return true;
			}
			catch (Exception ex)
			{
				error = "改名失败:" + ex.Message;
				return false;
			}
		}

		public static bool RestoreBackup(string slotName, out string error)
		{
			error = null;
			string text = Paths.SlotBackup(slotName);
			try
			{
				if (!Directory.Exists(text))
				{
					error = "这个槽没有上一档备份";
					return false;
				}
				if (File.Exists(Path.Combine(text, "save.sv")))
				{
					File.Copy(Path.Combine(text, "save.sv"), Paths.SlotSave(slotName), overwrite: true);
				}
				string text2 = Path.Combine(text, "mp");
				if (Directory.Exists(text2))
				{
					if (Directory.Exists(Paths.SlotMp(slotName)))
					{
						Directory.Delete(Paths.SlotMp(slotName), recursive: true);
					}
					CopyDir(text2, Paths.SlotMp(slotName));
				}
				Log.Info("已回滚上一档:" + slotName);
				return true;
			}
			catch (Exception ex)
			{
				error = "回滚失败:" + ex.Message;
				return false;
			}
		}

		private static void RotateBackup(string slotName)
		{
			string text = Paths.SlotBackup(slotName);
			try
			{
				if (Directory.Exists(text))
				{
					Directory.Delete(text, recursive: true);
				}
				Directory.CreateDirectory(text);
				if (File.Exists(Paths.SlotSave(slotName)))
				{
					File.Copy(Paths.SlotSave(slotName), Path.Combine(text, "save.sv"), overwrite: true);
				}
				if (Directory.Exists(Paths.SlotMp(slotName)))
				{
					CopyDir(Paths.SlotMp(slotName), Path.Combine(text, "mp"));
				}
			}
			catch (Exception ex)
			{
				Log.Warn("备份上一档失败:" + ex.Message);
			}
		}

		private static string UniqueAutoName(SlotMeta meta)
		{
			string text = string.Format("{0}_D{1}", DateTime.Now.ToString("yyyy-MM-dd_HHmm"), Math.Max(1, meta.biome));
			string text2 = text;
			int num = 2;
			while (Directory.Exists(Paths.SlotDir(text2)))
			{
				text2 = text + "_" + num++;
			}
			return text2;
		}

		private static List<string> ListMpPlayers()
		{
			List<string> list = new List<string>();
			try
			{
				string[] directories = Directory.GetDirectories(Paths.MpSaveDir);
				for (int i = 0; i < directories.Length; i++)
				{
					string fileName = Path.GetFileName(directories[i]);
					if (fileName.StartsWith("STEAM_"))
					{
						list.Add(fileName);
					}
				}
			}
			catch
			{
			}
			return list;
		}

		public static SlotMeta Probe(string path)
		{
			SlotMeta slotMeta = new SlotMeta();
			try
			{
				byte[] array = File.ReadAllBytes(path);
				slotMeta.bytes = array.Length;
				JObject val = JObject.Parse(SaveSystem.Unzip(array));
				slotMeta.biome = I(val, "biome", 1);
				slotMeta.runTime = F(val, "runTime", 0f);
				slotMeta.character = S(val, "character");
				slotMeta.cheated = B(val, "cheatedSave");
				JToken val2 = val["items"];
				JArray val3 = (JArray)(object)((val2 is JArray) ? val2 : null);
				slotMeta.itemCount = ((val3 != null) ? ((JContainer)val3).Count : 0);
			}
			catch (Exception ex)
			{
				Log.Warn("解析存档元信息失败(" + path + "):" + ex.Message);
			}
			return slotMeta;
		}

		private static int I(JObject o, string k, int d)
		{
			try
			{
				return (o[k] != null) ? Extensions.Value<int>((IEnumerable<JToken>)o[k]) : d;
			}
			catch
			{
				return d;
			}
		}

		private static float F(JObject o, string k, float d)
		{
			try
			{
				return (o[k] != null) ? Extensions.Value<float>((IEnumerable<JToken>)o[k]) : d;
			}
			catch
			{
				return d;
			}
		}

		private static bool B(JObject o, string k)
		{
			try
			{
				return o[k] != null && Extensions.Value<bool>((IEnumerable<JToken>)o[k]);
			}
			catch
			{
				return false;
			}
		}

		private static string S(JObject o, string k)
		{
			try
			{
				return (o[k] != null) ? Extensions.Value<string>((IEnumerable<JToken>)o[k]) : "";
			}
			catch
			{
				return "";
			}
		}

		public static SlotMeta ReadMeta(string slotName)
		{
			try
			{
				string path = Paths.SlotMeta(slotName);
				if (!File.Exists(path))
				{
					return null;
				}
				return JsonConvert.DeserializeObject<SlotMeta>(File.ReadAllText(path));
			}
			catch
			{
				return null;
			}
		}

		public static void WriteMeta(string slotName, SlotMeta meta)
		{
			try
			{
				Directory.CreateDirectory(Paths.SlotDir(slotName));
				File.WriteAllText(Paths.SlotMeta(slotName), JsonConvert.SerializeObject((object)meta, (Formatting)1));
			}
			catch (Exception ex)
			{
				Log.Warn("写 meta 失败:" + ex.Message);
			}
		}

		public static void CopyDir(string from, string to)
		{
			Directory.CreateDirectory(to);
			string[] files = Directory.GetFiles(from);
			foreach (string text in files)
			{
				File.Copy(text, Path.Combine(to, Path.GetFileName(text)), overwrite: true);
			}
			files = Directory.GetDirectories(from);
			foreach (string text2 in files)
			{
				CopyDir(text2, Path.Combine(to, Path.GetFileName(text2)));
			}
		}

		public static string FormatBytes(long b)
		{
			if (b < 1024)
			{
				return b + "B";
			}
			if (b < 1048576)
			{
				return ((double)b / 1024.0).ToString("0.0") + "KB";
			}
			return ((double)b / 1024.0 / 1024.0).ToString("0.0") + "MB";
		}
	}
}