Decompiled source of HistoryExtension v0.1.0

HistoryExtension.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.Encirclepedia;
using PeglinUI.MainMenu;
using PeglinUI.RunSummary;
using Saving;
using Stats;
using ToolBox.Serialization;
using UnityEngine;
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("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+182620c0cc24c6014870e21a89ef591ee444214a")]
[assembly: AssemblyProduct("HistoryExtension")]
[assembly: AssemblyTitle("HistoryExtension")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.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);

		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
			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_0062: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(self);
			RunSummaryLine[] componentsInChildren = ((Component)self.runSummaryLineContainer).gameObject.GetComponentsInChildren<RunSummaryLine>();
			Image val3 = default(Image);
			foreach (RunSummaryLine val in componentsInChildren)
			{
				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;
					}
				}
			}
		}
	}
	[BepInPlugin("HistoryExtension", "HistoryExtension", "0.1.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 = "0.1.0";
	}
}