Decompiled source of NoPetRides ModUtils v1.2.0

NPR_Valheim_ModUtils.dll

Decompiled 6 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: AssemblyCompany("NPR_Valheim_ModUtils")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("BepInEx 5")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d156f2a212104760baf64470c6c7a2efdfcaa7bb")]
[assembly: AssemblyProduct("NPR_Valheim_ModUtils")]
[assembly: AssemblyTitle("NPR_Valheim_ModUtils")]
[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 NPR_Valheim_ModUtils
{
	public static class Extensions
	{
		public static void SafeInvoke(this Action events)
		{
			if (events == null)
			{
				return;
			}
			Delegate[] invocationList = events.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				Action action = (Action)invocationList[i];
				try
				{
					action();
				}
				catch (Exception ex)
				{
					ModUtilsManager.Log.LogWarning((object)$"Exception thrown at event {new StackFrame(1).GetMethod().Name} in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{ex}");
				}
			}
		}
	}
	[BepInPlugin("com.nopetrides.valheim.mod-utils", "NPR_ValheimModUtils", "1.2.0")]
	[BepInProcess("valheim.exe")]
	public class ModUtilsManager : BaseUnityPlugin
	{
		public const string PLUGIN_GUID = "com.nopetrides.valheim.mod-utils";

		public const string PLUGIN_NAME = "NPR_ValheimModUtils";

		public const string PLUGIN_VERSION = "1.2.0";

		public const string VALHEIM_EXE_NAME = "valheim.exe";

		internal const string LoggerName = "ModUtilsManagerLog";

		public const string ROOT_GO_NAME = "_ModUtilsRoot";

		public readonly Color ValheimOrange = new Color(1f, 0.631f, 0.235f, 1f);

		private static ModUtilsManager _instance;

		internal static Harmony Harmony;

		internal static ManualLogSource Log;

		private bool GUIInStart;

		private static bool Headless;

		internal static GameObject RootObject;

		public Font AveriaSerif { get; private set; }

		public Font AveriaSerifBold { get; private set; }

		public static ModUtilsManager Instance => _instance;

		public static GameObject CustomGUIFront { get; private set; }

		public static GameObject CustomGUIBack { get; private set; }

		public static event Action OnCustomGUIAvailable;

		internal void FejdStartup_SetupGui(FejdStartup self)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			Log.LogInfo((object)"Building GUI in startup");
			Scene activeScene = SceneManager.GetActiveScene();
			GameObject? obj = ((IEnumerable<GameObject>)((Scene)(ref activeScene)).GetRootGameObjects()).FirstOrDefault((Func<GameObject, bool>)((GameObject x) => ((Object)x).name == "GuiRoot"));
			Transform val = ((obj != null) ? obj.transform.Find("GUI") : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				Log.LogError((object)"GuiRoot GUI not found, not creating custom GUI");
				return;
			}
			GUIInStart = true;
			CreateCustomGUI(val);
		}

		internal void Game_Start(Game self)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			Log.LogInfo((object)"Building GUI in game");
			Scene activeScene = SceneManager.GetActiveScene();
			GameObject? obj = ((IEnumerable<GameObject>)((Scene)(ref activeScene)).GetRootGameObjects()).FirstOrDefault((Func<GameObject, bool>)((GameObject x) => ((Object)x).name == "_GameMain"));
			Transform val = ((obj != null) ? obj.transform.Find("LoadingGUI") : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				Log.LogError((object)"_GameMain LoadingGUI not found, not creating custom GUI");
				return;
			}
			GUIInStart = false;
			CreateCustomGUI(val);
		}

		private void CreateCustomGUI(Transform parent)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			CustomGUIFront = new GameObject("CustomGUIFront", new Type[2]
			{
				typeof(RectTransform),
				typeof(GuiPixelFix)
			});
			CustomGUIFront.layer = 5;
			CustomGUIFront.transform.SetParent(((Component)parent).transform, false);
			CustomGUIFront.transform.SetAsLastSibling();
			CustomGUIFront.GetComponent<RectTransform>().anchorMin = Vector2.zero;
			CustomGUIFront.GetComponent<RectTransform>().anchorMax = Vector2.one;
			CustomGUIBack = new GameObject("CustomGUIBack", new Type[2]
			{
				typeof(RectTransform),
				typeof(GuiPixelFix)
			});
			CustomGUIBack.layer = 5;
			CustomGUIBack.transform.SetParent(((Component)parent).transform, false);
			CustomGUIBack.transform.SetAsFirstSibling();
			CustomGUIBack.GetComponent<RectTransform>().anchorMin = Vector2.zero;
			CustomGUIBack.GetComponent<RectTransform>().anchorMax = Vector2.one;
			InvokeOnCustomGUIAvailable();
		}

		private void InvokeOnCustomGUIAvailable()
		{
			Log.LogInfo((object)"Custom GUI Front & Back built and ready");
			ModUtilsManager.OnCustomGUIAvailable?.SafeInvoke();
		}

		public void Init()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			Headless = (int)SystemInfo.graphicsDeviceType == 4;
			if (!Headless)
			{
				Harmony.PatchAll(typeof(Patches));
				SceneManager.sceneLoaded += InitialLoad;
			}
		}

		private void InitialLoad(Scene scene, LoadSceneMode loadMode)
		{
			Log.LogInfo((object)"Processing Initial Load");
			if (!(((Scene)(ref scene)).name == "start") && !(((Scene)(ref scene)).name == "main"))
			{
				return;
			}
			try
			{
				Font[] source = Resources.FindObjectsOfTypeAll<Font>();
				AveriaSerif = ((IEnumerable<Font>)source).FirstOrDefault((Func<Font, bool>)((Font x) => ((Object)x).name == "AveriaSerifLibre-Regular"));
				AveriaSerifBold = ((IEnumerable<Font>)source).FirstOrDefault((Func<Font, bool>)((Font x) => ((Object)x).name == "AveriaSerifLibre-Bold"));
				if ((Object)(object)AveriaSerifBold == (Object)null || (Object)(object)AveriaSerif == (Object)null)
				{
					throw new Exception("Fonts not found");
				}
				Log.LogInfo((object)"Fonts loaded");
			}
			catch (Exception ex)
			{
				Log.LogError((object)ex);
			}
			finally
			{
				SceneManager.sceneLoaded -= InitialLoad;
			}
		}

		private void Awake()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Expected O, but got Unknown
			if ((Object)(object)_instance != (Object)null)
			{
				Object.Destroy((Object)(object)this);
				return;
			}
			_instance = this;
			Log = new ManualLogSource("ModUtilsManagerLog");
			Logger.Sources.Add((ILogSource)(object)Log);
			Log.LogInfo((object)"Plugin com.nopetrides.valheim.mod-utils is loading...");
			Harmony = new Harmony("com.nopetrides.valheim.mod-utils");
			RootObject = new GameObject("_ModUtilsRoot");
			Object.DontDestroyOnLoad((Object)(object)RootObject);
			Log.LogInfo((object)"Initializing...");
			Init();
			Game.isModded = true;
			Log.LogInfo((object)"Plugin com.nopetrides.valheim.mod-utils Loaded successfully");
		}
	}
	public static class Patches
	{
		[HarmonyPatch(typeof(FejdStartup), "SetupGui")]
		[HarmonyPostfix]
		private static void FejdStartup_SetupGui(FejdStartup __instance)
		{
			ModUtilsManager.Instance.FejdStartup_SetupGui(__instance);
		}

		[HarmonyPatch(typeof(Game), "Start")]
		[HarmonyPostfix]
		private static void Game_Start(Game __instance)
		{
			ModUtilsManager.Instance.Game_Start(__instance);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "NPR_Valheim_ModUtils";

		public const string PLUGIN_NAME = "NPR_Valheim_ModUtils";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}