Decompiled source of QuickSave v0.2.1

QuickSave.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("QuickSave")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Quick-save and quick-load hotkeys")]
[assembly: AssemblyFileVersion("0.2.1.0")]
[assembly: AssemblyInformationalVersion("0.2.1+a2afa3d962886bb25a72d1d0ae45167e1d694804")]
[assembly: AssemblyProduct("QuickSave")]
[assembly: AssemblyTitle("QuickSave")]
[assembly: AssemblyVersion("0.2.1.0")]
[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 QuickSave
{
	[BepInPlugin("com.geostyx.dsp.quicksave", "QuickSave", "0.2.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private const string SaveName = "quicksave";

		private const string CompressSaveGuid = "org.soardev.compresssave";

		private ConfigEntry<KeyboardShortcut> _quickSaveKey;

		private ConfigEntry<KeyboardShortcut> _quickLoadKey;

		private ConfigEntry<int> _quickSaveCount;

		private FieldInfo? _useCompressSave;

		private FieldInfo? _enableCompress;

		private MethodInfo? _useCommonSaveCompressionType;

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			_quickSaveKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "QuickSaveKey", new KeyboardShortcut((KeyCode)286, Array.Empty<KeyCode>()), "Saves the game, replacing the oldest quicksave.");
			_quickLoadKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "QuickLoadKey", new KeyboardShortcut((KeyCode)290, Array.Empty<KeyCode>()), "Loads the newest quicksave.");
			_quickSaveCount = ((BaseUnityPlugin)this).Config.Bind<int>("Saves", "QuickSaveCount", 5, new ConfigDescription("Number of quicksaves to keep.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 20), Array.Empty<object>()));
			if (Chainloader.PluginInfos.TryGetValue("org.soardev.compresssave", out var value))
			{
				Type type = ((object)value.Instance).GetType().Assembly.GetType("CompressSave.PatchSave");
				_useCompressSave = type?.GetField("UseCompressSave");
				_enableCompress = type?.GetField("EnableCompress");
				_useCommonSaveCompressionType = type?.GetMethod("UseCommonSaveCompressionType");
				if (_useCompressSave == null || _enableCompress == null || _useCommonSaveCompressionType == null)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"CompressSave.PatchSave members not found; quicksaves will not be compressed");
					_useCompressSave = null;
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"QuickSave 0.2.1 loaded");
		}

		private void Update()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			if (VFInput.inputing || !GameMain.isRunning || (!GameMain.instance.isMenuDemo && (GameMain.inOtherScene || GameMain.mainPlayer.respawning)) || UIRoot.instance.uiGame.buildMenu.currentCategory != 0)
			{
				return;
			}
			KeyboardShortcut value = _quickSaveKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				QuickSave();
				return;
			}
			value = _quickLoadKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				QuickLoad();
			}
		}

		private void QuickSave()
		{
			if (!GameMain.instance.isMenuDemo && !GameMain.data.guideRunning)
			{
				bool flag = (GameMain.localStar == null || GameMain.localStar.loaded) && (GameMain.localPlanet == null || (GameMain.localPlanet.loaded && GameMain.localPlanet.factoryLoaded));
				bool flag2 = flag && SaveGame(SlotName(OldestSlot()));
				if (flag2)
				{
					UIAutoSave.lastSaveTick = GameMain.gameTick;
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)(flag ? "Quicksave failed" : "Quicksave skipped while the local star or planet loads"));
				}
				UIRealtimeTip.Popup(Localization.Translate(flag2 ? "保存成功" : "保存失败"), !flag2, 0);
			}
		}

		private bool SaveGame(string name)
		{
			if (_useCompressSave == null || !(bool)_enableCompress.GetValue(null))
			{
				return GameSave.SaveCurrentGame(name);
			}
			_useCompressSave.SetValue(null, true);
			_useCommonSaveCompressionType.Invoke(null, null);
			try
			{
				return GameSave.SaveCurrentGame(name);
			}
			finally
			{
				_useCompressSave.SetValue(null, false);
			}
		}

		private int OldestSlot()
		{
			int num = 1;
			for (int i = 2; i <= _quickSaveCount.Value; i++)
			{
				if (SaveTime(i) < SaveTime(num))
				{
					num = i;
				}
			}
			return num;
		}

		private int NewestSlot()
		{
			int num = 1;
			for (int i = 2; i <= _quickSaveCount.Value; i++)
			{
				if (SaveTime(i) > SaveTime(num))
				{
					num = i;
				}
			}
			return num;
		}

		private static string SlotName(int slot)
		{
			return string.Format("{0}_{1}", "quicksave", slot);
		}

		private static DateTime SaveTime(int slot)
		{
			string path = GameSave.SavePath(SlotName(slot));
			if (!File.Exists(path))
			{
				return DateTime.MinValue;
			}
			return File.GetLastWriteTimeUtc(path);
		}

		private void QuickLoad()
		{
			//IL_004f: 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)
			string text = SlotName(NewestSlot());
			if (!GameSave.SaveExist(text))
			{
				UIRealtimeTip.Popup("No quicksave", true, 0);
				return;
			}
			GameDesc val = GameSave.LoadGameDesc(text);
			if (val == null)
			{
				UIRealtimeTip.Popup(Localization.Translate("无效存档"), true, 0);
				return;
			}
			DSPGame.StartGame(text);
			DSPGame.WillEnableSandboxTools = val.isSandboxMode;
			DSPGame.LoadWithGoalLevel = val.goalLevel;
			DSPGame.RecordPlayVersion();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.geostyx.dsp.quicksave";

		public const string PLUGIN_NAME = "QuickSave";

		public const string PLUGIN_VERSION = "0.2.1";
	}
}