Decompiled source of SaveFileRenamer v1.0.0

SaveFileRenamer.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[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.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SaveFileRenamer")]
[assembly: AssemblyTitle("SaveFileRenamer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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.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]
	[Microsoft.CodeAnalysis.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]
	[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;
		}
	}
}
[HarmonyPatch]
public class SaveFileNameEditorPatch
{
	[Serializable]
	public class SerializableStringDictionary
	{
		public string[] keys;

		public string[] values;

		public SerializableStringDictionary(Dictionary<string, string> dictionary)
		{
			keys = new string[dictionary.Count];
			values = new string[dictionary.Count];
			int num = 0;
			foreach (KeyValuePair<string, string> item in dictionary)
			{
				keys[num] = item.Key;
				values[num] = item.Value;
				num++;
			}
		}

		public Dictionary<string, string> ToDictionary()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			if (keys != null && values != null)
			{
				for (int i = 0; i < keys.Length && i < values.Length; i++)
				{
					if (!string.IsNullOrEmpty(keys[i]))
					{
						dictionary[keys[i]] = values[i];
					}
				}
			}
			return dictionary;
		}
	}

	private static Dictionary<string, string> customSaveNames = new Dictionary<string, string>();

	private static MenuElementSaveFile currentlyEditing = null;

	private static TMP_InputField activeInputField = null;

	private static bool initialized = false;

	[HarmonyPatch(typeof(MenuElementSaveFile), "Start")]
	[HarmonyPostfix]
	private static void AddEditFunctionality(MenuElementSaveFile __instance)
	{
		if (!initialized)
		{
			LoadCustomNames();
			initialized = true;
		}
		CreateEditInputField(__instance);
		UpdateSaveFileDisplay(__instance);
	}

	[HarmonyPatch(typeof(MenuElementSaveFile), "Update")]
	[HarmonyPostfix]
	private static void AddEditControls(MenuElementSaveFile __instance)
	{
		MenuElementHover component = ((Component)__instance).GetComponent<MenuElementHover>();
		if (component != null && component.isHovering && (Input.GetMouseButtonDown(1) || Input.GetKeyDown((KeyCode)283)) && (Object)(object)currentlyEditing == (Object)null)
		{
			StartEditingName(__instance);
		}
		if ((Object)(object)currentlyEditing == (Object)(object)__instance && Input.GetKeyDown((KeyCode)27))
		{
			CancelEditing();
		}
	}

	private static void CreateEditInputField(MenuElementSaveFile saveFile)
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Expected O, but got Unknown
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: 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_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: 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_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Expected O, but got Unknown
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Expected O, but got Unknown
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: 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_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_020c: Unknown result type (might be due to invalid IL or missing references)
		MenuElementSaveFile saveFile2 = saveFile;
		if (!((Object)(object)((Component)saveFile2).transform.Find("SaveNameInputField") != (Object)null))
		{
			GameObject val = new GameObject("SaveNameInputField");
			val.transform.SetParent(((Component)saveFile2).transform, false);
			RectTransform val2 = val.AddComponent<RectTransform>();
			RectTransform component = ((Component)saveFile2.saveFileHeader).GetComponent<RectTransform>();
			val2.anchorMin = component.anchorMin;
			val2.anchorMax = component.anchorMax;
			val2.anchoredPosition = component.anchoredPosition;
			val2.sizeDelta = new Vector2(component.sizeDelta.x + 20f, component.sizeDelta.y + 10f);
			Image val3 = val.AddComponent<Image>();
			((Graphic)val3).color = new Color(0.1f, 0.1f, 0.1f, 0.9f);
			TMP_InputField val4 = val.AddComponent<TMP_InputField>();
			GameObject val5 = new GameObject("Text Area");
			val5.transform.SetParent(val.transform, false);
			RectTransform val6 = val5.AddComponent<RectTransform>();
			val6.anchorMin = Vector2.zero;
			val6.anchorMax = Vector2.one;
			val6.offsetMin = new Vector2(5f, 2f);
			val6.offsetMax = new Vector2(-5f, -2f);
			GameObject val7 = new GameObject("Text");
			val7.transform.SetParent(val5.transform, false);
			RectTransform val8 = val7.AddComponent<RectTransform>();
			val8.anchorMin = Vector2.zero;
			val8.anchorMax = Vector2.one;
			val8.offsetMin = Vector2.zero;
			val8.offsetMax = Vector2.zero;
			TextMeshProUGUI val9 = val7.AddComponent<TextMeshProUGUI>();
			((TMP_Text)val9).text = "";
			((TMP_Text)val9).fontSize = ((TMP_Text)saveFile2.saveFileHeader).fontSize;
			((Graphic)val9).color = Color.white;
			((TMP_Text)val9).alignment = ((TMP_Text)saveFile2.saveFileHeader).alignment;
			val4.textComponent = (TMP_Text)(object)val9;
			((Selectable)val4).targetGraphic = (Graphic)(object)val3;
			val4.characterLimit = 50;
			((UnityEvent<string>)(object)val4.onSubmit).AddListener((UnityAction<string>)delegate(string value)
			{
				ConfirmEdit(saveFile2, value);
			});
			((UnityEvent<string>)(object)val4.onDeselect).AddListener((UnityAction<string>)delegate(string value)
			{
				ConfirmEdit(saveFile2, value);
			});
			val.SetActive(false);
		}
	}

	private static void StartEditingName(MenuElementSaveFile saveFile)
	{
		if ((Object)(object)currentlyEditing != (Object)null)
		{
			return;
		}
		currentlyEditing = saveFile;
		Transform val = ((Component)saveFile).transform.Find("SaveNameInputField");
		if ((Object)(object)val != (Object)null)
		{
			GameObject gameObject = ((Component)val).gameObject;
			activeInputField = gameObject.GetComponent<TMP_InputField>();
			string text = GetDisplayName(saveFile.saveFileName);
			if (text.EndsWith(".save"))
			{
				text = text.Substring(0, text.Length - 5);
			}
			activeInputField.text = text;
			gameObject.SetActive(true);
			((Selectable)activeInputField).Select();
			activeInputField.ActivateInputField();
			((Component)saveFile.saveFileHeader).gameObject.SetActive(false);
			MenuManager.instance.MenuEffectClick((MenuClickEffectType)4, (MenuPage)null, -1f, -1f, false);
		}
	}

	private static void ConfirmEdit(MenuElementSaveFile saveFile, string newName)
	{
		if (!((Object)(object)currentlyEditing != (Object)(object)saveFile))
		{
			if (!string.IsNullOrWhiteSpace(newName) && newName.Trim().Length > 0)
			{
				string value = newName.Trim();
				customSaveNames[saveFile.saveFileName] = value;
				UpdateSaveFileDisplay(saveFile);
				SaveCustomNames();
				MenuManager.instance.MenuEffectClick((MenuClickEffectType)1, (MenuPage)null, -1f, -1f, false);
			}
			else
			{
				MenuManager.instance.MenuEffectClick((MenuClickEffectType)2, (MenuPage)null, -1f, -1f, false);
			}
			EndEditing();
		}
	}

	private static void CancelEditing()
	{
		if (!((Object)(object)currentlyEditing == (Object)null))
		{
			MenuManager.instance.MenuEffectClick((MenuClickEffectType)2, (MenuPage)null, -1f, -1f, false);
			EndEditing();
		}
	}

	private static void EndEditing()
	{
		if ((Object)(object)currentlyEditing != (Object)null)
		{
			if ((Object)(object)activeInputField != (Object)null)
			{
				((Component)activeInputField).gameObject.SetActive(false);
			}
			((Component)currentlyEditing.saveFileHeader).gameObject.SetActive(true);
			currentlyEditing = null;
			activeInputField = null;
		}
	}

	private static void UpdateSaveFileDisplay(MenuElementSaveFile saveFile)
	{
		if ((Object)(object)saveFile.saveFileHeader != (Object)null)
		{
			string displayName = GetDisplayName(saveFile.saveFileName);
			((TMP_Text)saveFile.saveFileHeader).text = displayName;
		}
	}

	private static string GetDisplayName(string fileName)
	{
		if (customSaveNames.ContainsKey(fileName))
		{
			return customSaveNames[fileName];
		}
		string text = fileName;
		if (text.EndsWith(".save"))
		{
			text = text.Substring(0, text.Length - 5);
		}
		return text;
	}

	private static void SaveCustomNames()
	{
		try
		{
			string text = JsonUtility.ToJson((object)new SerializableStringDictionary(customSaveNames));
			PlayerPrefs.SetString("CustomSaveNames", text);
			PlayerPrefs.Save();
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("Failed to save custom save names: " + ex.Message));
		}
	}

	private static void LoadCustomNames()
	{
		try
		{
			if (!PlayerPrefs.HasKey("CustomSaveNames"))
			{
				return;
			}
			string @string = PlayerPrefs.GetString("CustomSaveNames");
			if (!string.IsNullOrEmpty(@string))
			{
				SerializableStringDictionary serializableStringDictionary = JsonUtility.FromJson<SerializableStringDictionary>(@string);
				if (serializableStringDictionary != null && serializableStringDictionary.keys != null && serializableStringDictionary.values != null)
				{
					customSaveNames = serializableStringDictionary.ToDictionary();
				}
			}
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("Failed to load custom save names: " + ex.Message));
			customSaveNames = new Dictionary<string, string>();
		}
	}
}
namespace SaveFileRenamer
{
	[BepInPlugin("Omniscye.SaveFileRenamer", "SaveFileRenamer", "1.0")]
	public class SaveFileRenamer : BaseUnityPlugin
	{
		internal static SaveFileRenamer Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		internal void Patch()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0026: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
		}
	}
}