Decompiled source of MainloadTool v2.0.0

BepInEx/plugins/MainloadTool.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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 UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using YuanAPI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("MainloadTool")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+612d1a14f07fd3aee94eea7a05ddcffd25ae1a64")]
[assembly: AssemblyProduct("MainloadTool")]
[assembly: AssemblyTitle("MainloadTool")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 MainloadTool
{
	internal class CsvWriter
	{
		private readonly List<string> _lines;

		private readonly List<string> _currentLine;

		private readonly string _filePath;

		public int LineCount => _lines.Count;

		public int CurrentFieldCount => _currentLine.Count;

		public string FilePath => _filePath;

		public CsvWriter(string fileName)
		{
			if (string.IsNullOrWhiteSpace(fileName))
			{
				throw new ArgumentException("文件名不能为空", "fileName");
			}
			_filePath = Path.Combine("Output", fileName.Trim() + ".csv");
			_lines = new List<string>();
			_currentLine = new List<string>();
		}

		public void WriteLine(List<string> fields)
		{
			if (fields == null)
			{
				throw new ArgumentNullException("fields");
			}
			string item = FormatCsvLine(fields);
			_lines.Add(item);
		}

		public void WriteLine(params string[] fields)
		{
			WriteLine(fields.ToList());
		}

		public void WriteField(string field)
		{
			_currentLine.Add(field ?? string.Empty);
		}

		public void WriteField(List<string> fields)
		{
			if (fields == null)
			{
				throw new ArgumentNullException("fields");
			}
			_currentLine.AddRange(fields);
		}

		public void WriteField(params string[] fields)
		{
			WriteField(fields.ToList());
		}

		public void EndRow()
		{
			if (_currentLine.Count > 0)
			{
				string item = FormatCsvLine(_currentLine);
				_lines.Add(item);
				_currentLine.Clear();
			}
		}

		public void Save()
		{
			string directoryName = Path.GetDirectoryName(_filePath);
			if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(directoryName))
			{
				Directory.CreateDirectory(directoryName);
			}
			try
			{
				File.WriteAllLines(_filePath, _lines, Encoding.UTF8);
				MainloadTool.Logger.LogInfo((object)("CSV文件已保存到: " + _filePath));
			}
			catch (Exception ex)
			{
				throw new IOException("保存CSV文件失败: " + ex.Message, ex);
			}
		}

		private string FormatCsvLine(List<string> fields)
		{
			IEnumerable<string> values = fields.Select(delegate(string field)
			{
				if (field == null)
				{
					return string.Empty;
				}
				return (field.Contains(",") || field.Contains("\"") || field.Contains("\n") || field.Contains("\r")) ? ("\"" + field.Replace("\"", "\"\"") + "\"") : field;
			});
			return string.Join(",", values);
		}
	}
	internal class DumpAllText
	{
		internal static List<string> _languages;

		private static void DumpText(string name, List<List<string>> texts)
		{
			CsvWriter csvWriter = new CsvWriter(name + MainloadTool.GameVersion);
			List<string> list = new List<string> { "序号" };
			list.AddRange(_languages);
			csvWriter.WriteLine(list);
			int count = texts.Count;
			for (int i = 0; i < count; i++)
			{
				csvWriter.WriteField(i.ToString());
				csvWriter.WriteField(texts[i]);
				csvWriter.EndRow();
			}
			csvWriter.Save();
		}

		public static void Text_AllProp()
		{
			DumpText("Text_AllProp", AllText.Text_AllProp);
		}

		public static void Text_AllBuild()
		{
			CsvWriter csvWriter = new CsvWriter("Text_AllBuild" + MainloadTool.GameVersion);
			List<string> list = new List<string> { "序号" };
			foreach (string language in _languages)
			{
				list.Add(language + "建筑");
				list.Add(language + "简介");
			}
			csvWriter.WriteLine(list);
			int count = AllText.Text_AllBuild.Count;
			for (int i = 0; i < count; i++)
			{
				csvWriter.WriteField(i.ToString());
				foreach (string item in AllText.Text_AllBuild[i])
				{
					csvWriter.WriteField(item.Split(new char[1] { '|' }));
				}
				csvWriter.EndRow();
			}
			csvWriter.Save();
		}

		public static void Text_AllPropClass()
		{
			DumpText("Text_AllPropClass", AllText.Text_AllPropClass);
		}

		public static void Text_TipShow()
		{
			DumpText("Text_TipShow", AllText.Text_TipShow);
		}
	}
	internal static class DumpMainload
	{
		public static void AllPropdata()
		{
			CsvWriter csvWriter = new CsvWriter("AllPropdata" + MainloadTool.GameVersion);
			csvWriter.WriteLine(new List<string>
			{
				"序号", "价格", "分类", "文", "武", "商", "艺", "健康", "心情", "魅力",
				"幸运", "寿命", "计谋", "巫", "医", "相", "卜", "魅", "工"
			});
			int count = Mainload.AllPropdata.Count;
			for (int i = 0; i < count; i++)
			{
				csvWriter.WriteField(i.ToString());
				csvWriter.WriteField(Mainload.AllPropdata[i][0]);
				csvWriter.WriteField(Mainload.AllPropdata[i][1]);
				csvWriter.WriteField(Mainload.AllPropdata[i][2].Split(new char[1] { '|' }));
				csvWriter.EndRow();
			}
			csvWriter.Save();
		}

		public static void AllBuilddata()
		{
			CsvWriter csvWriter = new CsvWriter("AllBuilddata" + MainloadTool.GameVersion);
			CsvWriter csvWriter2 = new CsvWriter("AllBuilddata[7]" + MainloadTool.GameVersion);
			csvWriter.WriteLine(new List<string>
			{
				"序号", "null_1", "价格", "升级基准", "列3", "解锁等级", "分类", "null_2", "属性", "生活品质",
				"列9"
			});
			List<string> list = new List<string> { "序号" };
			Dictionary<int, string> dictionary = new Dictionary<int, string>
			{
				{ 0, "文" },
				{ 1, "武" },
				{ 2, "商" },
				{ 3, "艺" },
				{ 4, "长寿" },
				{ 5, "爱情" },
				{ 6, "幸运" },
				{ 7, "多子" },
				{ 8, "魅力" },
				{ 10, "繁荣" },
				{ 11, "声望" },
				{ 12, "环境" },
				{ 13, "安全" },
				{ 14, "便捷" },
				{ 16, "教化" },
				{ 17, "厢房" },
				{ 18, "农舍" },
				{ 19, "仓库" },
				{ 20, "马厩" }
			};
			Dictionary<int, string> dictionary2 = new Dictionary<int, string>
			{
				{ 1, "刚正" },
				{ 3, "善良" },
				{ 4, "真诚" },
				{ 6, "高洁" }
			};
			int num = Mainload.AllBuilddata[0][7].Split(new char[1] { '|' }).Length;
			for (int i = 0; i < num; i++)
			{
				if (i != 9)
				{
					list.Add(dictionary.TryGetValue(i, out var value) ? value : $"{i}");
					continue;
				}
				int num2 = Mainload.AllBuilddata[0][7].Split(new char[1] { '|' })[9].Split(new char[1] { '@' }).Length;
				for (int j = 0; j < num2; j++)
				{
					list.Add(dictionary2.TryGetValue(j, out var value2) ? value2 : $"{i}_{j}");
				}
			}
			csvWriter2.WriteLine(list);
			int count = Mainload.AllBuilddata.Count;
			for (int k = 0; k < count; k++)
			{
				csvWriter.WriteField(k.ToString());
				csvWriter2.WriteField(k.ToString());
				if (k != 161)
				{
					csvWriter.WriteField(Mainload.AllBuilddata[k]);
					string[] array = Mainload.AllBuilddata[k][7].Split(new char[1] { '|' });
					for (int l = 0; l < num; l++)
					{
						if (l != 9)
						{
							csvWriter2.WriteField(array[l]);
							continue;
						}
						csvWriter2.WriteField(array[9].Split(new char[1] { '@' }));
					}
				}
				csvWriter.EndRow();
				csvWriter2.EndRow();
			}
			csvWriter.Save();
			csvWriter2.Save();
		}
	}
	[BepInPlugin("cc.lymone.HoL.MainloadTool", "MainloadTool", "2.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class MainloadTool : BaseUnityPlugin
	{
		public const string MODNAME = "MainloadTool";

		public const string MODGUID = "cc.lymone.HoL.MainloadTool";

		public const string VERSION = "2.0.0";

		internal static ManualLogSource Logger;

		internal static string GameVersion;

		internal static ConfigEntry<KeyCode> OpenMenuKey;

		internal static ConfigEntry<string> AutoLoadSaveName;

		internal static ConfigEntry<bool> IsPreventRemoveSave;

		private Rect _windowRect = new Rect(150f, 100f, 800f, 600f);

		internal bool ShowMenu { get; private set; }

		private void Awake()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			GameVersion = "_v" + Mainload.Vision_now.Substring(2);
			BindConfig();
			Harmony val = new Harmony("cc.lymone.HoL.MainloadTool");
			val.PatchAll(typeof(PerDangBTPatch));
			val.PatchAll(typeof(StartGameUIPatch));
			Localization.Initialize();
		}

		private void Start()
		{
			DumpAllText._languages = Localization.GetAllLocales();
		}

		private void BindConfig()
		{
			OpenMenuKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("加载工具 Mainload Tool", "菜单按键 Open Menu Key", (KeyCode)278, "");
			AutoLoadSaveName = ((BaseUnityPlugin)this).Config.Bind<string>("存档工具 Save Tool", "自动加载存档 Auto Load Save Name", "", "打开游戏自动加载存档,0-5为原版支持的存档,留空关闭\n可以自定义其他存档名,需要自行确保文件存在");
			IsPreventRemoveSave = ((BaseUnityPlugin)this).Config.Bind<bool>("存档工具 Save Tool", "阻止删除存档 Prevent Remove Save", true, "当加载存档出错时,是否阻止删除存档文件");
		}

		private void Update()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			if (Input.GetKeyDown(OpenMenuKey.Value))
			{
				ShowMenu = !ShowMenu;
				if (ShowMenu)
				{
					Mainload.isMapPanelOpen = false;
				}
				Logger.LogInfo((object)("MainloadTool窗口已" + (ShowMenu ? "打开" : "关闭")));
			}
			if (ShowMenu && (Input.mouseScrollDelta.y != 0f || Input.GetMouseButton(0) || Input.GetMouseButton(1) || (Input.GetMouseButton(2) && Input.anyKeyDown && !Input.GetKeyDown((KeyCode)283))))
			{
				Input.ResetInputAxes();
			}
		}

		private void OnGUI()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: 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_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Expected O, but got Unknown
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Expected O, but got Unknown
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: 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)
			if (ShowMenu)
			{
				Color backgroundColor = GUI.backgroundColor;
				GUI.backgroundColor = new Color(0.9f, 0.9f, 0.9f, 0.95f);
				GUI.BeginGroup(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height));
				GUI.color = new Color(0f, 0f, 0f, 0.1f);
				GUI.DrawTexture(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), (Texture)(object)Texture2D.whiteTexture);
				GUI.color = Color.white;
				GUI.EndGroup();
				GUI.skin.window.padding = new RectOffset(Mathf.RoundToInt(20f), Mathf.RoundToInt(20f), Mathf.RoundToInt(10f), Mathf.RoundToInt(10f));
				int fontSize = Mathf.RoundToInt(18f);
				GUI.skin.textField.fontSize = fontSize;
				GUI.skin.window.fontSize = fontSize;
				GUI.skin.label.fontSize = fontSize;
				GUI.skin.button.fontSize = fontSize;
				GUI.skin.button.alignment = (TextAnchor)4;
				_windowRect = GUI.Window(0, _windowRect, new WindowFunction(DrawWindow), "", GUI.skin.window);
				GUI.backgroundColor = backgroundColor;
			}
		}

		private void DrawWindow(int windowID)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			//IL_02e8: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.BeginVertical((GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.ExpandHeight(true)
			});
			GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
			GUILayout.FlexibleSpace();
			GUIStyle val = new GUIStyle(GUI.skin.label);
			GUILayout.Label("Mainload Tool v2.0.0 for Game " + GameVersion.Substring(1), val, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(false) });
			GUILayout.FlexibleSpace();
			GUILayout.EndHorizontal();
			GUILayout.Space(20f);
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			GUILayout.BeginVertical(GUI.skin.box, (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.Width(0f),
				GUILayout.ExpandWidth(true)
			});
			GUILayout.Label("Dump AllText", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(350f) });
			GUILayout.Space(5f);
			if (GUILayout.Button("Text_AllProp", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.Height(30f)
			}))
			{
				DumpAllText.Text_AllProp();
			}
			GUILayout.Space(5f);
			if (GUILayout.Button("Text_AllBuild", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.Height(30f)
			}))
			{
				DumpAllText.Text_AllBuild();
			}
			GUILayout.Space(5f);
			if (GUILayout.Button("Text_AllPropClass", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.Height(30f)
			}))
			{
				DumpAllText.Text_AllPropClass();
			}
			GUILayout.Space(5f);
			if (GUILayout.Button("Text_TipShow", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.Height(30f)
			}))
			{
				DumpAllText.Text_TipShow();
			}
			GUILayout.EndVertical();
			GUILayout.Space(10f);
			GUILayout.BeginVertical(GUI.skin.box, (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.Width(0f),
				GUILayout.ExpandWidth(true)
			});
			GUILayout.Label("Dump Mainload", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(350f) });
			GUILayout.Space(5f);
			if (GUILayout.Button("AllPropData", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.Height(30f)
			}))
			{
				DumpMainload.AllPropdata();
			}
			GUILayout.Space(5f);
			if (GUILayout.Button("AllBuilddata", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.Height(30f)
			}))
			{
				DumpMainload.AllBuilddata();
			}
			GUILayout.EndVertical();
			GUILayout.EndHorizontal();
			GUILayout.EndVertical();
			GUI.DragWindow(new Rect(0f, 0f, ((Rect)(ref _windowRect)).width, ((Rect)(ref _windowRect)).height));
		}
	}
	[HarmonyPatch]
	public class PerDangBTPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch(typeof(PerDangBT), "OnEnableData")]
		public static bool PreventRemoveSaveWhenCheckError(PerDangBT __instance, ref bool ___isHaveData)
		{
			if (!MainloadTool.IsPreventRemoveSave.Value)
			{
				return true;
			}
			bool flag = true;
			try
			{
				if (ES3.FileExists("FW/" + ((Object)__instance).name + "/GameData.es3"))
				{
					ES3.Load<List<string>>("FamilyData", "FW/" + ((Object)__instance).name + "/GameData.es3");
				}
				else
				{
					flag = false;
				}
			}
			catch (Exception ex)
			{
				flag = false;
				MainloadTool.Logger.LogError((object)("Error when load save " + ((Object)__instance).name + ", file remove has been prevented: " + ex.Message));
			}
			___isHaveData = flag;
			return false;
		}
	}
	[HarmonyPatch]
	public class StartGameUIPatch
	{
		[CompilerGenerated]
		private sealed class <LoadSave>d__1 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public string saveName;

			private GameObject <mainUI>5__1;

			private Transform <cunDangUI>5__2;

			private Transform <saveTag>5__3;

			private GameObject <thisSaveTag>5__4;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <LoadSave>d__1(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<mainUI>5__1 = null;
				<cunDangUI>5__2 = null;
				<saveTag>5__3 = null;
				<thisSaveTag>5__4 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<mainUI>5__1 = GameObject.Find("MainUI");
					<cunDangUI>5__2 = <mainUI>5__1.transform.Find("CunDangUI");
					<saveTag>5__3 = <cunDangUI>5__2.Find("0");
					<thisSaveTag>5__4 = Object.Instantiate<GameObject>(((Component)<saveTag>5__3).gameObject, <cunDangUI>5__2);
					((Object)<thisSaveTag>5__4).name = saveName;
					((Component)<cunDangUI>5__2).gameObject.SetActive(true);
					<>2__current = null;
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					((UnityEvent)((Component)<thisSaveTag>5__4.transform.Find("BackBT")).GetComponent<Button>().onClick).Invoke();
					return false;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartGameUI), "Start")]
		public static void AutoLoadSave(StartGameUI __instance)
		{
			string value = MainloadTool.AutoLoadSaveName.Value;
			if (value == "")
			{
				return;
			}
			MainloadTool.Logger.LogInfo((object)("Auto load save FW/" + value + "."));
			try
			{
				((MonoBehaviour)__instance).StartCoroutine(LoadSave(value));
			}
			catch (Exception ex)
			{
				MainloadTool.Logger.LogWarning((object)("Auto load save failed: " + ex.Message));
			}
		}

		[IteratorStateMachine(typeof(<LoadSave>d__1))]
		private static IEnumerator LoadSave(string saveName)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LoadSave>d__1(0)
			{
				saveName = saveName
			};
		}
	}
}