Decompiled source of PapysLand Menu v0.4.0

BepInEx/plugins/PapysLandMenu/PapysLandMenu.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("papydiablo.papysland.menu", "PapysLand Menu", "0.4.0")]
public sealed class PapysLandMenu : BaseUnityPlugin
{
	private Type startupType;

	private Object startup;

	private float nextSearch;

	private bool mainVisible;

	private bool characterVisible;

	private bool scanned;

	private readonly Dictionary<Image, bool> logos = new Dictionary<Image, bool>();

	private Texture2D papysLandLogo;

	private Texture2D characterLogo;

	private void Awake()
	{
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			startupType = assembly.GetType("FejdStartup", throwOnError: false);
			if (startupType != null)
			{
				break;
			}
		}
		papysLandLogo = LoadEmbeddedPng("PapysLandLogo.png", "PapysLandLogo");
		characterLogo = LoadEmbeddedPng("PapysLandCharacterLogo.png", "PapysLandCharacterLogo");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PapysLand Menu 0.4.0 charge. Logos accueil + selection personnage actifs.");
	}

	private Texture2D LoadEmbeddedPng(string fileName, string textureName)
	{
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Expected O, but got Unknown
		try
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			string text = null;
			string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
			foreach (string text2 in manifestResourceNames)
			{
				if (text2.EndsWith(fileName, StringComparison.OrdinalIgnoreCase))
				{
					text = text2;
					break;
				}
			}
			if (text == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Ressource " + fileName + " introuvable dans la DLL."));
				return null;
			}
			using Stream stream = executingAssembly.GetManifestResourceStream(text);
			if (stream == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Impossible d'ouvrir la ressource " + fileName + "."));
				return null;
			}
			byte[] array = new byte[stream.Length];
			int num;
			for (int j = 0; j < array.Length; j += num)
			{
				num = stream.Read(array, j, array.Length - j);
				if (num <= 0)
				{
					break;
				}
			}
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			((Object)val).name = textureName;
			((Texture)val).wrapMode = (TextureWrapMode)1;
			((Texture)val).filterMode = (FilterMode)1;
			if (!ImageConversion.LoadImage(val, array, false))
			{
				Object.Destroy((Object)(object)val);
				((BaseUnityPlugin)this).Logger.LogError((object)("Echec du chargement PNG " + fileName + "."));
				return null;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)(fileName + " charge : " + ((Texture)val).width + "x" + ((Texture)val).height));
			return val;
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Erreur chargement " + fileName + " : " + ex));
			return null;
		}
	}

	private object Field(string name)
	{
		if (startup == (Object)null || startupType == null)
		{
			return null;
		}
		FieldInfo field = startupType.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (!(field == null))
		{
			return field.GetValue(startup);
		}
		return null;
	}

	private bool Visible(string name)
	{
		object obj = Field(name);
		GameObject val = (GameObject)((obj is GameObject) ? obj : null);
		if ((Object)(object)val != (Object)null)
		{
			return val.activeInHierarchy;
		}
		return false;
	}

	private void Update()
	{
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		if (startup == (Object)null && startupType != null && Time.unscaledTime >= nextSearch)
		{
			nextSearch = Time.unscaledTime + 2f;
			Object[] array = Resources.FindObjectsOfTypeAll(startupType);
			Object[] array2 = array;
			foreach (Object val in array2)
			{
				Component val2 = (Component)(object)((val is Component) ? val : null);
				if ((Object)(object)val2 != (Object)null)
				{
					Scene scene = val2.gameObject.scene;
					if (((Scene)(ref scene)).IsValid())
					{
						startup = val;
						scanned = false;
						break;
					}
				}
			}
		}
		mainVisible = Visible("m_mainMenu");
		characterVisible = Visible("m_characterSelectScreen");
		if (mainVisible && !scanned)
		{
			ScanLogos();
		}
		foreach (KeyValuePair<Image, bool> logo in logos)
		{
			if ((Object)(object)logo.Key != (Object)null)
			{
				((Behaviour)logo.Key).enabled = !mainVisible && logo.Value;
			}
		}
	}

	private void ScanLogos()
	{
		scanned = true;
		Object obj = startup;
		Component val = (Component)(object)((obj is Component) ? obj : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Canvas val2 = val.GetComponentInChildren<Canvas>(true);
		if ((Object)(object)val2 == (Object)null)
		{
			val2 = val.GetComponentInParent<Canvas>();
		}
		Transform val3 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).transform : val.transform);
		Image[] componentsInChildren = ((Component)val3).GetComponentsInChildren<Image>(true);
		foreach (Image val4 in componentsInChildren)
		{
			string text = (((Object)val4).name + " " + (((Object)(object)val4.sprite == (Object)null) ? "" : ((Object)val4.sprite).name)).ToLowerInvariant();
			if (text.Contains("logo") || text.Contains("valheim"))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Image candidate titre: " + text));
				if (!((Object)(object)((Component)val4).GetComponent<Button>() != (Object)null) && ((Component)val4).GetComponentsInChildren<Button>(true).Length <= 0 && !logos.ContainsKey(val4))
				{
					logos.Add(val4, ((Behaviour)val4).enabled);
				}
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Habillage accueil initialise. Images de logo reperees: " + logos.Count));
	}

	private void OnGUI()
	{
		if (mainVisible || characterVisible)
		{
			int depth = GUI.depth;
			GUI.depth = -20;
			if (mainVisible)
			{
				DrawAdaptiveMainLogo();
			}
			else if (characterVisible)
			{
				DrawAdaptiveCharacterLogo();
			}
			GUI.depth = depth;
		}
	}

	private void DrawAdaptiveMainLogo()
	{
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)papysLandLogo == (Object)null))
		{
			float num = Screen.width;
			float num2 = Screen.height;
			float num3 = Mathf.Clamp(num * 0.34f, num2 * 0.46f, num * 0.46f);
			float num4 = (float)((Texture)papysLandLogo).width / (float)((Texture)papysLandLogo).height;
			float num5 = num3 / num4;
			float num6 = num * 0.5f;
			float num7 = num2 * 0.495f;
			float num8 = num2 * 0.245f;
			if (num5 > num8)
			{
				num5 = num8;
				num3 = num5 * num4;
			}
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(num6 - num3 * 0.5f, num7 - num5 * 0.5f, num3, num5);
			Color color = GUI.color;
			GUI.color = Color.white;
			GUI.DrawTexture(val, (Texture)(object)papysLandLogo, (ScaleMode)2, true);
			GUI.color = color;
		}
	}

	private void DrawAdaptiveCharacterLogo()
	{
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)characterLogo == (Object)null))
		{
			float num = Screen.width;
			float num2 = Screen.height;
			float num3 = (float)((Texture)characterLogo).width / (float)((Texture)characterLogo).height;
			float num4 = Mathf.Min(num * 0.28f, num2 * 0.48f);
			float num5 = num4 / num3;
			float num6 = num2 * 0.245f;
			if (num5 > num6)
			{
				num5 = num6;
				num4 = num5 * num3;
			}
			float num7 = num * 0.012f;
			float num8 = num2 * 0.015f;
			Rect val = default(Rect);
			((Rect)(ref val))..ctor(num7, num8, num4, num5);
			Color color = GUI.color;
			GUI.color = Color.white;
			GUI.DrawTexture(val, (Texture)(object)characterLogo, (ScaleMode)2, true);
			GUI.color = color;
		}
	}

	private void OnDestroy()
	{
		foreach (KeyValuePair<Image, bool> logo in logos)
		{
			if ((Object)(object)logo.Key != (Object)null)
			{
				((Behaviour)logo.Key).enabled = logo.Value;
			}
		}
		if ((Object)(object)papysLandLogo != (Object)null)
		{
			Object.Destroy((Object)(object)papysLandLogo);
		}
		if ((Object)(object)characterLogo != (Object)null)
		{
			Object.Destroy((Object)(object)characterLogo);
		}
	}
}