Decompiled source of GuidebookPageFix v0.1.0

BepInEx/plugins/GuidebookPageFix/GuidebookPageFix.dll

Decompiled 4 days ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Peak;
using UnityEngine;
using UnityEngine.SceneManagement;
using Zorro.Core;

[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("TactiKot")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Restores missing guidebook data after scene changes and prevents consuming a scroll before selecting its page.")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("GuidebookPageFix")]
[assembly: AssemblyTitle("GuidebookPageFix")]
[assembly: AssemblyVersion("0.1.0.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 GuidebookPageFix
{
	[BepInProcess("PEAK.exe")]
	[BepInPlugin("com.tactikot.peak.guidebookpagefix", "GuidebookPageFix", "0.1.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Id = "com.tactikot.peak.guidebookpagefix";

		public const string Name = "GuidebookPageFix";

		public const string Version = "0.1.0";

		private static ManualLogSource? log;

		private static float nextWarning;

		private Harmony? harmony;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Expected O, but got Unknown
			log = ((BaseUnityPlugin)this).Logger;
			harmony = new Harmony("com.tactikot.peak.guidebookpagefix");
			try
			{
				harmony.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(AchievementManager), "Start", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "AfterManagerStart", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				harmony.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(Action_SpawnGuidebookPage), "RunAction", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "OpenScroll", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Enabled: restores missing guidebook data from saved stats; selects a page before consuming its scroll. Install on each reading player's client.");
			}
			catch (Exception arg)
			{
				harmony.UnpatchSelf();
				((BaseUnityPlugin)this).Logger.LogError((object)$"Fix could not be enabled; its patches were removed: {arg}");
			}
		}

		private void OnDestroy()
		{
			Harmony? obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}

		private static void AfterManagerStart(AchievementManager __instance)
		{
			((MonoBehaviour)__instance).StartCoroutine(InitializeWhenReady(__instance));
		}

		private static IEnumerator InitializeWhenReady(AchievementManager manager)
		{
			while (Object.op_Implicit((Object)(object)manager) && !AchievementManager.GotStats)
			{
				yield return (object)new WaitForSeconds(0.25f);
			}
			if (Object.op_Implicit((Object)(object)manager) && (Object)(object)Singleton<AchievementManager>.Instance == (Object)(object)manager)
			{
				EnsureData(manager);
			}
		}

		private static bool EnsureData(AchievementManager? manager)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)manager) || !AchievementManager.GotStats)
			{
				return false;
			}
			if (manager.guidebookData != null)
			{
				return true;
			}
			try
			{
				int num = default(int);
				if (!manager.GetSteamStatInt((STEAMSTATTYPE)16, ref num))
				{
					return false;
				}
				manager.guidebookData = new GuidebookData
				{
					seenGuidebookPages = (GUIDEBOOKPAGEFLAGS)num
				};
				ManualLogSource? obj = log;
				if (obj != null)
				{
					Scene scene = ((Component)manager).gameObject.scene;
					obj.LogInfo((object)$"Restored guidebook data in {((Scene)(ref scene)).name}; saved page flags: {num}.");
				}
				return true;
			}
			catch (Exception arg)
			{
				Warn($"Could not load guidebook data; scroll use will wait: {arg}");
				return false;
			}
		}

		private static bool OpenScroll(Action_SpawnGuidebookPage __instance)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			Item component = ((Component)__instance).GetComponent<Item>();
			if (!Object.op_Implicit((Object)(object)component) || !Object.op_Implicit((Object)(object)component.holderCharacter) || component.consuming)
			{
				return false;
			}
			GameObject val = default(GameObject);
			try
			{
				if (!EnsureData(Singleton<AchievementManager>.Instance))
				{
					Warn("Guidebook data is not ready; the scroll was kept. Try reading it again after stats finish loading.");
					return false;
				}
				GuidebookPageCollection instance = SingletonAsset<GuidebookPageCollection>.Instance;
				if (!Object.op_Implicit((Object)(object)instance) || !instance.TryPickPageToSpawn(((Component)__instance).transform.position, ref val) || !Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)val.GetComponent<Guidebook>()))
				{
					Warn("No valid guidebook page was selected; the scroll was kept.");
					return false;
				}
			}
			catch (Exception arg)
			{
				Warn($"Guidebook page selection failed; the scroll was kept: {arg}");
				return false;
			}
			Character holderCharacter = component.holderCharacter;
			((MonoBehaviour)component).StartCoroutine(component.ConsumeDelayed(false));
			((MonoBehaviour)holderCharacter).StartCoroutine(__instance.SpawnPageDelayed(val));
			return false;
		}

		private static void Warn(string message)
		{
			if (!(Time.realtimeSinceStartup < nextWarning))
			{
				nextWarning = Time.realtimeSinceStartup + 5f;
				ManualLogSource? obj = log;
				if (obj != null)
				{
					obj.LogWarning((object)message);
				}
			}
		}
	}
}