Decompiled source of HistoryExtension v1.0.0

HistoryExtension.dll

Decompiled 2 months ago
using System;
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.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Loading;
using Map;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using On;
using On.Loading;
using On.Map;
using On.PeglinUI.Encirclepedia;
using On.PeglinUI.MainMenu;
using On.PeglinUI.RunSummary;
using PeglinUI.ControllerSupport;
using PeglinUI.Encirclepedia;
using PeglinUI.MainMenu;
using PeglinUI.RunSummary;
using Saving;
using Stats;
using TMPro;
using ToolBox.Serialization;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("HistoryExtension")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Store more than the past 20 runs along with other run history improvements")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c29cd2ad54827f53ad366158b5716ed2549f3fd9")]
[assembly: AssemblyProduct("HistoryExtension")]
[assembly: AssemblyTitle("HistoryExtension")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace HistoryExtension
{
	public class AuxiliaryRunStatsBehavior : MonoBehaviour
	{
		public class AuxiliaryRunStats
		{
			[JsonProperty("runs")]
			private Dictionary<Guid, AuxiliaryRunStatsData> _runs = new Dictionary<Guid, AuxiliaryRunStatsData>();

			public int Count()
			{
				return _runs.Count;
			}

			public void StartRun(RunStats runStats)
			{
				_runs.Add(runStats.runId, new AuxiliaryRunStatsData
				{
					RunStats = runStats
				});
			}

			public void ContinueRun(Guid guid)
			{
				AuxiliaryRunStatsData run;
				AuxiliaryRunStatsData auxiliaryRunStatsData = (run = GetRun(guid));
				if (run.Continues == null)
				{
					List<int> list2 = (run.Continues = new List<int>());
				}
				auxiliaryRunStatsData.Continues.Add(StaticGameData.chosenNextNodeIndex);
			}

			public bool IsRunContinued(Guid guid)
			{
				if (!_runs.ContainsKey(guid))
				{
					return true;
				}
				if (_runs.TryGetValue(guid, out var value))
				{
					List<int> continues = value.Continues;
					if (continues == null)
					{
						return false;
					}
					return continues.Count > 0;
				}
				return false;
			}

			private AuxiliaryRunStatsData GetRun(Guid guid)
			{
				AuxiliaryRunStatsData valueOrDefault = _runs.GetValueOrDefault(guid, new AuxiliaryRunStatsData());
				_runs[guid] = valueOrDefault;
				return valueOrDefault;
			}
		}

		public class AuxiliaryRunStatsData
		{
			public List<int> Continues { get; set; }

			public RunStats RunStats { get; set; }
		}

		public static AuxiliaryRunStats RunStats { get; private set; }

		public void Awake()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			RunStats = LoadRunStats();
			PlayButton.ConfirmRunConfigAndStartGame += new hook_ConfirmRunConfigAndStartGame(PlayButtonOnConfirmRunConfigAndStartGame);
			MapController.LoadRunStats += new hook_LoadRunStats(MapControllerOnLoadRunStats);
			SaveManager.OnSaveRequested = (SaveRequested)Delegate.Combine((Delegate?)(object)SaveManager.OnSaveRequested, (Delegate?)new SaveRequested(Save));
			PauseMenu.QuitToMenu += new hook_QuitToMenu(PauseMenuOnQuitToMenu);
		}

		public void OnDestroy()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			PlayButton.ConfirmRunConfigAndStartGame -= new hook_ConfirmRunConfigAndStartGame(PlayButtonOnConfirmRunConfigAndStartGame);
			MapController.LoadRunStats -= new hook_LoadRunStats(MapControllerOnLoadRunStats);
			SaveManager.OnSaveRequested = (SaveRequested)Delegate.Remove((Delegate?)(object)SaveManager.OnSaveRequested, (Delegate?)new SaveRequested(Save));
			PauseMenu.QuitToMenu -= new hook_QuitToMenu(PauseMenuOnQuitToMenu);
		}

		public void OnApplicationQuit()
		{
			Save();
		}

		private void PauseMenuOnQuitToMenu(orig_QuitToMenu orig, PauseMenu self)
		{
			orig.Invoke(self);
			Save();
		}

		private void PlayButtonOnConfirmRunConfigAndStartGame(orig_ConfirmRunConfigAndStartGame orig, PlayButton self)
		{
			orig.Invoke(self);
			RunStats.StartRun(StaticGameData.CurrentRunStats);
		}

		private RunStats MapControllerOnLoadRunStats(orig_LoadRunStats orig, MapController self)
		{
			RunStats obj = orig.Invoke(self);
			Guid runId = obj.runId;
			RunStats.ContinueRun(runId);
			return obj;
		}

		private AuxiliaryRunStats LoadRunStats()
		{
			AuxiliaryRunStats auxiliaryRunStats = new AuxiliaryRunStats();
			try
			{
				Debug.Log((object)"Loading auxiliary run stats");
				JsonConvert.PopulateObject(File.ReadAllText(GetFilePath()), (object)auxiliaryRunStats);
				Debug.Log((object)$"Populated {auxiliaryRunStats.Count()} runs from auxiliary run stats file");
			}
			catch (Exception arg)
			{
				Debug.LogWarning((object)$"Couldn't populate stats from existing file: {arg}");
			}
			return auxiliaryRunStats;
		}

		public void Save()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Debug.Log((object)"Saving...");
				string filePath = GetFilePath();
				Debug.Log((object)("Saving Run Stats to: " + filePath));
				using StreamWriter streamWriter = File.CreateText(filePath);
				new JsonSerializer().Serialize((TextWriter)streamWriter, (object)RunStats);
			}
			catch (Exception arg)
			{
				Debug.LogWarning((object)$"Couldn't save aux stats: {arg}");
			}
		}

		private string GetFilePath()
		{
			return Path.Combine(Application.persistentDataPath, $"auxiliaryRunStats-{DataSerializer.CurrentProfileIndex}.json");
		}
	}
	public class EncirclepediaHighlight : MonoBehaviour
	{
		public Color highlightColor = new Color(0.35f, 0.35f, 1f);

		private List<RunSummaryLine> _allItems = new List<RunSummaryLine>();

		private int _itemsPerPage = 20;

		private int _currentPage;

		private Button _prevButton;

		private Button _nextButton;

		public void Awake()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			RunStatisticsDetails.Initialize += new hook_Initialize(RunStatisticsDetailsOnInitialize);
			RunSummaryList.Awake += new hook_Awake(RunSummaryListOnAwake);
			EncirclepediaManager.OnEnable += new hook_OnEnable(EncirclepediaManagerOnOnEnable);
		}

		private void RunStatisticsDetailsOnInitialize(orig_Initialize orig, RunStatisticsDetails self, RunStats stats)
		{
			//IL_0048: 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)
			orig.Invoke(self, stats);
			bool flag = AuxiliaryRunStatsBehavior.RunStats.IsRunContinued(stats.runId);
			Transform val = self.headerParent.Find("PeglinContainer/StickerBackground");
			Image val2 = default(Image);
			if (Object.op_Implicit((Object)(object)val) && ((Component)val).TryGetComponent<Image>(ref val2))
			{
				((Graphic)val2).color = (flag ? Color.white : highlightColor);
			}
		}

		public void Destroy()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			RunStatisticsDetails.Initialize -= new hook_Initialize(RunStatisticsDetailsOnInitialize);
			RunSummaryList.Awake -= new hook_Awake(RunSummaryListOnAwake);
			EncirclepediaManager.OnEnable -= new hook_OnEnable(EncirclepediaManagerOnOnEnable);
		}

		private void EncirclepediaManagerOnOnEnable(orig_OnEnable orig, EncirclepediaManager self)
		{
			orig.Invoke(self);
			Canvas val = default(Canvas);
			if (((Component)self).TryGetComponent<Canvas>(ref val))
			{
				val.pixelPerfect = false;
			}
			else
			{
				Debug.Log((object)"Couldn't find encirclepedia manager canvas to disable pixel perfect.");
			}
		}

		private void RunSummaryListOnAwake(orig_Awake orig, RunSummaryList self)
		{
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Expected O, but got Unknown
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Expected O, but got Unknown
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Expected O, but got Unknown
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Expected O, but got Unknown
			//IL_0231: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Unknown result type (might be due to invalid IL or missing references)
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self);
			RunSummaryLine[] componentsInChildren = ((Component)self.runSummaryLineContainer).gameObject.GetComponentsInChildren<RunSummaryLine>();
			RunSummaryLine[] array = componentsInChildren;
			Image val3 = default(Image);
			foreach (RunSummaryLine val in array)
			{
				Guid runId = val._stats.runId;
				if (!AuxiliaryRunStatsBehavior.RunStats.IsRunContinued(runId))
				{
					Transform val2 = ((Component)val).transform.Find("Parent/PeglinContainer/StickerBackground");
					if (Object.op_Implicit((Object)(object)val2) && ((Component)val2).TryGetComponent<Image>(ref val3))
					{
						((Graphic)val3).color = highlightColor;
					}
				}
			}
			_currentPage = 0;
			_allItems = componentsInChildren.ToList();
			RectTransform component = ((Component)self).GetComponent<RectTransform>();
			_prevButton = Object.Instantiate<Button>(self.moveCameraButton, (Transform)(object)component);
			((Object)_prevButton).name = "Previous Runs";
			MapButtonToInput val4 = default(MapButtonToInput);
			if (((Component)_prevButton).TryGetComponent<MapButtonToInput>(ref val4))
			{
				Object.Destroy((Object)(object)val4);
				Transform obj = ((Component)_prevButton).transform.Find("ControllerButton");
				Object.Destroy((Object)(object)((obj != null) ? ((Component)obj).gameObject : null));
			}
			_prevButton.onClick = new ButtonClickedEvent();
			((UnityEvent)_prevButton.onClick).AddListener((UnityAction)delegate
			{
				_currentPage--;
				ShowPage();
			});
			RectTransform component2 = ((Component)_prevButton).GetComponent<RectTransform>();
			component2.anchorMin = new Vector2(0f, 0f);
			component2.anchorMax = new Vector2(0f, 0f);
			component2.anchoredPosition = new Vector2(30f, 0f);
			TextMeshProUGUI componentInChildren = ((Component)_prevButton).GetComponentInChildren<TextMeshProUGUI>();
			if ((Object)(object)componentInChildren != (Object)null)
			{
				((TMP_Text)componentInChildren).text = self.MOVE_LEFT_ARROW;
			}
			_nextButton = Object.Instantiate<Button>(self.moveCameraButton, (Transform)(object)component);
			((Object)_nextButton).name = "Next Runs";
			if (((Component)_nextButton).TryGetComponent<MapButtonToInput>(ref val4))
			{
				Object.Destroy((Object)(object)val4);
				Transform obj2 = ((Component)_nextButton).transform.Find("ControllerButton");
				Object.Destroy((Object)(object)((obj2 != null) ? ((Component)obj2).gameObject : null));
			}
			_nextButton.onClick = new ButtonClickedEvent();
			((UnityEvent)_nextButton.onClick).AddListener((UnityAction)delegate
			{
				_currentPage++;
				ShowPage();
			});
			RectTransform component3 = ((Component)_nextButton).GetComponent<RectTransform>();
			component3.anchorMin = new Vector2(1f, 0f);
			component3.anchorMax = new Vector2(1f, 0f);
			component3.anchoredPosition = new Vector2(-30f, 0f);
			ShowPage();
		}

		private void ShowPage()
		{
			int num = _currentPage * _itemsPerPage;
			int num2 = Mathf.Min(num + _itemsPerPage, _allItems.Count);
			for (int i = 0; i < _allItems.Count; i++)
			{
				RunSummaryLine obj = _allItems[i];
				GameObject val = ((obj != null) ? ((Component)obj).gameObject : null);
				if ((Object)(object)val != (Object)null)
				{
					val.SetActive(i >= num && i < num2);
				}
			}
			((Component)_prevButton).gameObject.SetActive(_currentPage > 0);
			((Component)_nextButton).gameObject.SetActive(num2 < _allItems.Count);
		}
	}
	[BepInPlugin("HistoryExtension", "HistoryExtension", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private ConfigEntry<int> _historyLimit;

		public void Awake()
		{
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Expected O, but got Unknown
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin HistoryExtension is loaded!");
			try
			{
				_historyLimit = ((BaseUnityPlugin)this).Config.Bind<int>("General", "History Limit", -1, "Number of history entries to store, -1 for infinite.");
				ConfigEntry<bool> val = ((BaseUnityPlugin)this).Config.Bind<bool>("Internal", "FirstRun", true, (ConfigDescription)null);
				if (val.Value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Initial Run: Backing up stats file");
					string filePath = DataSerializer.GetFilePath(DataSerializer.CurrentProfileIndex, (SaveType)4);
					File.Copy(filePath, filePath + ".bak", overwrite: true);
					val.Value = false;
				}
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)$"Couldn't read config file: {arg}");
			}
			AssetLoading.AddRunStats += new hook_AddRunStats(AssetLoadingOnAddRunStats);
			((Component)this).gameObject.AddComponent<AuxiliaryRunStatsBehavior>();
			EncirclepediaHighlight encirclepediaHighlight = ((Component)this).gameObject.AddComponent<EncirclepediaHighlight>();
			try
			{
				ConfigEntry<Color> val2 = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "Encirclepedia Highlight", encirclepediaHighlight.highlightColor, "Color to highlight non-continued runs in the encirclepedia.");
				encirclepediaHighlight.highlightColor = val2.Value;
			}
			catch (Exception arg2)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)$"Couldn't read encirclepedia highlight color from config: {arg2}");
			}
		}

		private void OnDestroy()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			AssetLoading.AddRunStats -= new hook_AddRunStats(AssetLoadingOnAddRunStats);
		}

		private void AssetLoadingOnAddRunStats(orig_AddRunStats orig, AssetLoading self, RunStats stats)
		{
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Expected O, but got Unknown
			foreach (RunStatsSaveData runStat in self.RunStats)
			{
				if (runStat.runId.Equals(stats.runId.ToString()))
				{
					return;
				}
			}
			int value = _historyLimit.Value;
			((BaseUnityPlugin)this).Logger.LogDebug((object)"Checking history limit");
			((BaseUnityPlugin)this).Logger.LogDebug((object)$"Run Stats Count: {self.RunStats.Count}");
			((BaseUnityPlugin)this).Logger.LogDebug((object)$"Limit: {value}");
			while (value >= 0 && self.RunStats.Count >= value && self.RunStats.Count > 0)
			{
				((BaseUnityPlugin)this).Logger.LogDebug((object)"Run stats count >= history limit. Dequeueing");
				self.RunStats.Dequeue();
			}
			PersistentPlayerData.Instance.PermanentStats.AddStatsToPermanent(stats);
			self.RunStats.Enqueue(new RunStatsSaveData(stats));
			self.SaveRunStats();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "HistoryExtension";

		public const string PLUGIN_NAME = "HistoryExtension";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}