Decompiled source of MAF Loading Screens v1.0.2

CustomLoadingScreens.dll

Decompiled a day 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 BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
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(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d92191e3a6a5dffc93a6d44bc4fe11316edf0b83")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CustomLoadingScreens;

[BepInPlugin("aedenthorn.CustomLoadingScreens", "Custom Loading Screens", "0.7.1")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(FejdStartup), "LoadMainScene")]
	public static class LoadMainScene_Patch
	{
		public static void Prefix(FejdStartup __instance)
		{
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			Dbgl("loading main scene");
			Dbgl("getting new random images");
			loadingSprite = GetRandomLoadingScreen();
			if (differentSpawnScreen.Value)
			{
				loadingSprite2 = GetRandomLoadingScreen();
			}
			Dbgl("getting new random tips");
			if (loadingTips.Any())
			{
				loadingTip = loadingTips[Random.Range(0, loadingTips.Length)];
				if (differentSpawnTip.Value)
				{
					loadingTip2 = loadingTips[Random.Range(0, loadingTips.Length)];
				}
			}
			Image val = Object.Instantiate<Image>(((Component)__instance.m_loading.transform.Find("Bkg")).GetComponent<Image>(), __instance.m_loading.transform);
			if ((Object)(object)val == (Object)null)
			{
				Dbgl("missed bkg");
				return;
			}
			Dbgl("setting sprite to loading screen");
			val.sprite = loadingSprite;
			((Graphic)val).color = loadingColorMask.Value;
			val.type = (Type)0;
			val.preserveAspect = true;
		}
	}

	[HarmonyPriority(800)]
	[HarmonyPatch(typeof(ZNet), "RPC_ClientHandshake")]
	public static class ZNet_RPC_ClientHandshake_Patch
	{
		public static bool Prefix(ZNet __instance, ZRpc rpc, bool needPassword)
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			Dbgl("RPC_ClientHandshake");
			if (!__instance.IsServer())
			{
				Image val = Object.Instantiate<Image>(((Component)((Component)Hud.instance).transform.Find("LoadingBlack").Find("Bkg")).GetComponent<Image>(), ((Component)((Component)Hud.instance).transform.Find("LoadingBlack")).transform);
				if ((Object)(object)val == (Object)null)
				{
					Dbgl("missed bkg");
					return true;
				}
				Dbgl("setting sprite to loading screen");
				val.sprite = loadingSprite;
				((Graphic)val).color = loadingColorMask.Value;
				val.type = (Type)0;
				val.preserveAspect = true;
				if (loadingTips.Any())
				{
					Object.Instantiate<Transform>(Hud.instance.m_loadingTip.transform.parent.Find("panel_separator"), ((Component)((Component)Hud.instance).transform.Find("LoadingBlack")).transform);
					TMP_Text component = Object.Instantiate<GameObject>(((Component)Hud.instance.m_loadingTip).gameObject, ((Component)((Component)Hud.instance).transform.Find("LoadingBlack")).transform).GetComponent<TMP_Text>();
					if ((Object)(object)component != (Object)null)
					{
						component.text = loadingTip;
						((Graphic)component).color = tipTextColor.Value;
					}
				}
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Hud), "UpdateBlackScreen")]
	public static class UpdateBlackScreen_Patch
	{
		public static void Prefix(Hud __instance, bool ___m_haveSetupLoadScreen, ref bool __state)
		{
			__state = !___m_haveSetupLoadScreen;
		}

		public static void Postfix(Hud __instance, bool ___m_haveSetupLoadScreen, ref bool __state)
		{
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			if (__state && ___m_haveSetupLoadScreen)
			{
				if (differentSpawnScreen.Value)
				{
					loadingSprite2 = GetRandomLoadingScreen();
				}
				if (loadingTips.Any() && differentSpawnTip.Value)
				{
					loadingTip2 = loadingTips[Random.Range(0, loadingTips.Length)];
				}
				Dbgl("setting sprite to loading screen");
				__instance.m_loadingImage.sprite = (differentSpawnScreen.Value ? loadingSprite2 : loadingSprite);
				((Graphic)__instance.m_loadingImage).color = spawnColorMask.Value;
				if (loadingTips.Any())
				{
					__instance.m_loadingTip.text = (differentSpawnTip.Value ? loadingTip2 : loadingTip);
				}
				((Graphic)__instance.m_loadingTip).color = tipTextColor.Value;
				if (removeVignette.Value)
				{
					((Component)__instance.m_loadingProgress.transform.Find("TopFade")).gameObject.SetActive(false);
					((Component)__instance.m_loadingProgress.transform.Find("BottomFade")).gameObject.SetActive(false);
					((Component)__instance.m_loadingProgress.transform.Find("text_darken")).gameObject.SetActive(false);
				}
			}
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	public static class InputText_Patch
	{
		public static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				__instance.AddString(text);
				__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded");
				return false;
			}
			return true;
		}
	}

	public static readonly bool isDebug = true;

	public static BepInExPlugin context;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<string> loadingText;

	public static ConfigEntry<bool> differentSpawnScreen;

	public static ConfigEntry<bool> differentSpawnTip;

	public static ConfigEntry<bool> showTipsOnLoadingScreen;

	public static ConfigEntry<bool> removeVignette;

	public static ConfigEntry<Color> spawnColorMask;

	public static ConfigEntry<Color> loadingColorMask;

	public static ConfigEntry<Color> loadingTextColor;

	public static ConfigEntry<Color> tipTextColor;

	public static List<string> loadingScreens = new List<string>();

	public static Dictionary<string, string> loadingScreens2 = new Dictionary<string, string>();

	public static Dictionary<string, DateTime> fileWriteTimes = new Dictionary<string, DateTime>();

	public static List<string> screensToLoad = new List<string>();

	public static string[] loadingTips = new string[0];

	public static Dictionary<string, Texture2D> cachedScreens = new Dictionary<string, Texture2D>();

	public static Sprite loadingSprite;

	public static Sprite loadingSprite2;

	public static string loadingTip;

	public static string loadingTip2;

	private static Dictionary<string, List<string>> screensByBiome;

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	public void Awake()
	{
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		differentSpawnScreen = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DifferentSpawnScreen", true, "Use a different screen for the spawn part");
		differentSpawnTip = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DifferentSpawnTip", true, "Use a different tip for the spawn part");
		spawnColorMask = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "SpawnColorMask", new Color(0.532f, 0.588f, 0.853f, 1f), "Change the color mask of the spawn screen (set last number to 0 to disable)");
		loadingColorMask = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "LoadingColorMask", Color.white, "Change the color mask of the initial loading screen (set to white to disable)");
		removeVignette = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RemoveMask", true, "Remove dark edges for the spawn part");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 0, "Nexus mod ID for updates");
		loadingText = ((BaseUnityPlugin)this).Config.Bind<string>("General", "LoadingText", "Loading...", "Custom Loading... text");
		loadingTextColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "LoadingTextColor", new Color(1f, 0.641f, 0f, 1f), "Custom Loading... text color");
		tipTextColor = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "TipTextColor", Color.white, "Custom tip text color");
		if (modEnabled.Value)
		{
			LoadCustomLoadingScreens();
			LoadTips();
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}

	public void LoadTips()
	{
		string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "CustomLoadingScreens", "tips.txt");
		if (!File.Exists(path))
		{
			Dbgl("No tips, creating empty file");
			File.Create(path);
		}
		else
		{
			loadingTips = File.ReadAllLines(path);
		}
	}

	public static void LoadCustomLoadingScreens()
	{
		LoadCustomLoadingScreensImpl(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "CustomLoadingScreens"));
	}

	public static Sprite GetRandomLoadingScreen()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Expected O, but got Unknown
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		List<string> unlockedScreenPool = GetUnlockedScreenPool();
		if (!unlockedScreenPool.Any())
		{
			return null;
		}
		Texture2D val = new Texture2D(2, 2);
		byte[] array = File.ReadAllBytes(unlockedScreenPool[Random.Range(0, unlockedScreenPool.Count)]);
		ImageConversion.LoadImage(val, array);
		return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), Vector2.zero, 1f);
	}

	private static string[] GetBiomeOrder()
	{
		return new string[7] { "Meadows", "BlackForest", "Swamp", "Mountain", "Plains", "Mistlands", "Ashlands" };
	}

	private static Dictionary<string, string> GetBiomeGlobalKeys()
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
		dictionary["Meadows"] = "defeated_eikthyr";
		dictionary["BlackForest"] = "defeated_gdking";
		dictionary["Swamp"] = "defeated_bonemass";
		dictionary["Mountain"] = "defeated_dragon";
		dictionary["Plains"] = "defeated_goblinking";
		dictionary["Mistlands"] = "defeated_queen";
		dictionary["Ashlands"] = "defeated_fader";
		return dictionary;
	}

	private static bool GetGlobalKeyReflective(string key)
	{
		try
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			for (int i = 0; i < assemblies.Length; i++)
			{
				Type type = assemblies[i].GetType("ZoneSystem");
				if (type == null)
				{
					continue;
				}
				object obj = null;
				FieldInfo field = type.GetField("instance", BindingFlags.Static | BindingFlags.Public);
				if (field != null)
				{
					obj = field.GetValue(null);
				}
				else
				{
					PropertyInfo property = type.GetProperty("instance", BindingFlags.Static | BindingFlags.Public);
					if (property != null)
					{
						obj = property.GetValue(null, null);
					}
				}
				if (obj == null)
				{
					return false;
				}
				MethodInfo method = type.GetMethod("GetGlobalKey", new Type[1] { typeof(string) });
				if (method == null)
				{
					return false;
				}
				object obj2 = method.Invoke(obj, new object[1] { key });
				return obj2 is bool && (bool)obj2;
			}
		}
		catch
		{
			return false;
		}
		return false;
	}

	private static List<string> GetUnlockedScreenPool()
	{
		List<string> list = new List<string>();
		if (screensByBiome == null)
		{
			return list;
		}
		if (screensByBiome.TryGetValue("base", out var value))
		{
			list.AddRange(value);
		}
		Dictionary<string, string> biomeGlobalKeys = GetBiomeGlobalKeys();
		string[] biomeOrder = GetBiomeOrder();
		string[] array = biomeOrder;
		foreach (string key in array)
		{
			if (GetGlobalKeyReflective(biomeGlobalKeys[key]) && screensByBiome.TryGetValue(key, out var value2))
			{
				list.AddRange(value2);
			}
		}
		return list;
	}

	private static void LoadCustomLoadingScreensImpl(string path)
	{
		string[] biomeOrder = GetBiomeOrder();
		screensByBiome = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
		screensByBiome["base"] = new List<string>();
		string[] array = biomeOrder;
		foreach (string key in array)
		{
			screensByBiome[key] = new List<string>();
		}
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
			return;
		}
		array = Directory.GetFiles(path, "*.png", SearchOption.AllDirectories);
		foreach (string text in array)
		{
			string fileName = Path.GetFileName(Path.GetDirectoryName(text));
			string key2 = "base";
			string[] array2 = biomeOrder;
			foreach (string key in array2)
			{
				if (string.Equals(fileName, key, StringComparison.OrdinalIgnoreCase))
				{
					key2 = key;
					break;
				}
			}
			screensByBiome[key2].Add(text);
		}
	}
}