Decompiled source of PatronLogo v2.0.0

PatronLogo.dll

Decompiled 44 minutes ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;
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("PatronLogo")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PatronLogo")]
[assembly: AssemblyTitle("PatronLogo")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 PatronLogo
{
	public class FallingBackground : MonoBehaviour
	{
		private class Item
		{
			public RectTransform rt;

			public float speed;

			public float spin;

			public float size;
		}

		private readonly List<Item> items = new List<Item>();

		private ConfigEntry<string> sceneKeywords;

		private ConfigEntry<float> minSize;

		private ConfigEntry<float> maxSize;

		private ConfigEntry<float> minSpeed;

		private ConfigEntry<float> maxSpeed;

		private ManualLogSource log;

		private GameObject root;

		private RectTransform canvasRt;

		private bool visible;

		private Canvas canvas;

		private Camera cam;

		private ConfigEntry<string> mode;

		private ConfigEntry<int> overlayOrder;

		private string lastContext = "";

		private ConfigEntry<bool> forceAlways;

		private Type runManagerType;

		private float nextCheck;

		public static void Create(ConfigFile cfg, string imagePath)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("PatronRain");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<FallingBackground>().Setup(cfg, imagePath);
		}

		private void Setup(ConfigFile cfg, string imagePath)
		{
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Expected O, but got Unknown
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Expected O, but got Unknown
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Unknown result type (might be due to invalid IL or missing references)
			//IL_024b: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02be: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02dc: Unknown result type (might be due to invalid IL or missing references)
			log = Logger.CreateLogSource("PatronRain");
			sceneKeywords = cfg.Bind<string>("Rain", "SceneKeywords", "menu", "Через запятую: фон включается в сценах, в имени которых есть одно из слов (menu = Main Menu и Lobby Menu).");
			forceAlways = cfg.Bind<bool>("Rain", "ForceAlways", false, "Для отладки: показывать фон везде.");
			mode = cfg.Bind<string>("Rain", "Mode", "Camera", "Camera = рисовать внутри игровой камеры (скрывает фургон); Overlay = поверх экрана.");
			overlayOrder = cfg.Bind<int>("Rain", "OverlayOrder", -500, "Для Mode=Overlay: порядок слоя.");
			ConfigEntry<int> val = cfg.Bind<int>("Rain", "Count", 30, "Сколько картинок одновременно.");
			minSize = cfg.Bind<float>("Rain", "MinSize", 90f, "Мин. размер картинки (px при 1080p).");
			maxSize = cfg.Bind<float>("Rain", "MaxSize", 220f, "Макс. размер картинки.");
			minSpeed = cfg.Bind<float>("Rain", "MinSpeed", 120f, "Мин. скорость падения (px/сек).");
			maxSpeed = cfg.Bind<float>("Rain", "MaxSpeed", 380f, "Макс. скорость падения.");
			Texture2D val2 = new Texture2D(2, 2, (TextureFormat)4, false);
			ImageConversion.LoadImage(val2, File.ReadAllBytes(imagePath));
			((Texture)val2).filterMode = (FilterMode)1;
			root = new GameObject("PatronRainCanvas");
			root.transform.SetParent(((Component)this).transform, false);
			canvas = root.AddComponent<Canvas>();
			canvas.renderMode = (RenderMode)0;
			canvas.sortingOrder = overlayOrder.Value;
			CanvasScaler obj = root.AddComponent<CanvasScaler>();
			obj.uiScaleMode = (ScaleMode)1;
			obj.referenceResolution = new Vector2(1920f, 1080f);
			obj.matchWidthOrHeight = 0.5f;
			canvasRt = root.GetComponent<RectTransform>();
			GameObject val3 = new GameObject("Dark");
			val3.transform.SetParent(root.transform, false);
			Image obj2 = val3.AddComponent<Image>();
			((Graphic)obj2).color = new Color(0.03f, 0.03f, 0.04f, 1f);
			((Graphic)obj2).raycastTarget = false;
			RectTransform rectTransform = ((Graphic)obj2).rectTransform;
			rectTransform.anchorMin = Vector2.zero;
			rectTransform.anchorMax = Vector2.one;
			Vector2 offsetMin = (rectTransform.offsetMax = Vector2.zero);
			rectTransform.offsetMin = offsetMin;
			for (int i = 0; i < val.Value; i++)
			{
				GameObject val4 = new GameObject("Pic");
				val4.transform.SetParent(root.transform, false);
				RawImage val5 = val4.AddComponent<RawImage>();
				val5.texture = (Texture)(object)val2;
				((Graphic)val5).raycastTarget = false;
				Item item = new Item
				{
					rt = ((Graphic)val5).rectTransform
				};
				RectTransform rt = item.rt;
				offsetMin = (item.rt.anchorMax = Vector2.zero);
				rt.anchorMin = offsetMin;
				item.rt.pivot = new Vector2(0.5f, 0.5f);
				Respawn(item, anywhere: true);
				items.Add(item);
			}
			root.SetActive(false);
			log.LogInfo((object)("Готово. Картинок: " + items.Count));
		}

		private void Respawn(Item it, bool anywhere)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			Rect rect;
			float num;
			if ((Object)(object)canvasRt != (Object)null)
			{
				rect = canvasRt.rect;
				if (((Rect)(ref rect)).width > 1f)
				{
					rect = canvasRt.rect;
					num = ((Rect)(ref rect)).width;
					goto IL_0042;
				}
			}
			num = 1920f;
			goto IL_0042;
			IL_0042:
			float num2 = num;
			float num3;
			if ((Object)(object)canvasRt != (Object)null)
			{
				rect = canvasRt.rect;
				if (((Rect)(ref rect)).height > 1f)
				{
					rect = canvasRt.rect;
					num3 = ((Rect)(ref rect)).height;
					goto IL_0085;
				}
			}
			num3 = 1080f;
			goto IL_0085;
			IL_0085:
			float num4 = num3;
			it.size = Random.Range(minSize.Value, maxSize.Value);
			it.speed = Random.Range(minSpeed.Value, maxSpeed.Value);
			it.spin = Random.Range(-60f, 60f);
			it.rt.sizeDelta = new Vector2(it.size, it.size);
			float num5 = (anywhere ? Random.Range(0f, num4) : (num4 + it.size * 0.5f + Random.Range(0f, 300f)));
			it.rt.anchoredPosition = new Vector2(Random.Range(0f, num2), num5);
			((Transform)it.rt).localRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
		}

		private string GetLevelName()
		{
			try
			{
				if (runManagerType == null)
				{
					Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
					for (int i = 0; i < assemblies.Length; i++)
					{
						Type[] types = assemblies[i].GetTypes();
						foreach (Type type in types)
						{
							if (type.Name == "RunManager")
							{
								runManagerType = type;
								break;
							}
						}
						if (runManagerType != null)
						{
							break;
						}
					}
				}
				if (runManagerType == null)
				{
					return "";
				}
				object obj = null;
				FieldInfo field = runManagerType.GetField("instance", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					obj = field.GetValue(null);
				}
				else
				{
					PropertyInfo property = runManagerType.GetProperty("instance", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
					if (property != null)
					{
						obj = property.GetValue(null, null);
					}
				}
				if (obj == null)
				{
					return "";
				}
				FieldInfo field2 = runManagerType.GetField("levelCurrent", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				Object val = (Object)((field2 != null) ? /*isinst with value type is only supported in some contexts*/: null);
				return (val != (Object)null) ? val.name : "";
			}
			catch
			{
				return "";
			}
		}

		private bool ShouldShow()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			if (forceAlways.Value)
			{
				return true;
			}
			Scene activeScene = SceneManager.GetActiveScene();
			string text = ((Scene)(ref activeScene)).name + " | " + GetLevelName();
			if (text != lastContext)
			{
				lastContext = text;
				log.LogInfo((object)("Контекст: '" + text + "'"));
			}
			string text2 = text.ToLowerInvariant();
			string[] array = sceneKeywords.Value.Split(',');
			for (int i = 0; i < array.Length; i++)
			{
				string text3 = array[i].Trim().ToLowerInvariant();
				if (text3.Length > 0 && text2.Contains(text3))
				{
					return true;
				}
			}
			return false;
		}

		private void AttachCamera()
		{
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Invalid comparison between Unknown and I4
			if (mode.Value != "Camera")
			{
				return;
			}
			Camera val = Camera.main;
			if ((Object)(object)val == (Object)null || !((Behaviour)val).isActiveAndEnabled)
			{
				val = null;
				Camera[] allCameras = Camera.allCameras;
				foreach (Camera val2 in allCameras)
				{
					if (((Behaviour)val2).isActiveAndEnabled && ((Object)(object)val == (Object)null || val2.depth > val.depth))
					{
						val = val2;
					}
				}
			}
			if (!((Object)(object)val == (Object)null) && ((Object)(object)val != (Object)(object)cam || (int)canvas.renderMode != 1))
			{
				cam = val;
				canvas.renderMode = (RenderMode)1;
				canvas.worldCamera = val;
				canvas.planeDistance = val.nearClipPlane + 0.05f;
				canvas.sortingOrder = 32000;
				log.LogInfo((object)("Камера для фона: '" + ((Object)val).name + "' (RT=" + ((Object)(object)val.targetTexture != (Object)null) + ")"));
			}
		}

		private void DumpDiagnostics()
		{
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Camera[] allCameras = Camera.allCameras;
				foreach (Camera val in allCameras)
				{
					log.LogInfo((object)("[Cam] " + ((Object)val).name + " depth=" + val.depth + " rt=" + ((Object)(object)val.targetTexture != (Object)null) + " mask=" + val.cullingMask));
				}
				Canvas[] array = Resources.FindObjectsOfTypeAll<Canvas>();
				foreach (Canvas val2 in array)
				{
					Scene scene = ((Component)val2).gameObject.scene;
					if (((Scene)(ref scene)).IsValid() && val2.isRootCanvas)
					{
						log.LogInfo((object)("[Canvas] " + ((Object)val2).name + " mode=" + ((object)val2.renderMode/*cast due to .constrained prefix*/).ToString() + " order=" + val2.sortingOrder + " active=" + ((Component)val2).gameObject.activeInHierarchy));
					}
				}
			}
			catch
			{
			}
		}

		private void Update()
		{
			//IL_00a2: 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_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			if (Time.unscaledTime >= nextCheck)
			{
				nextCheck = Time.unscaledTime + 0.25f;
				bool flag = ShouldShow();
				if (flag != visible)
				{
					visible = flag;
					root.SetActive(flag);
					log.LogInfo((object)("Фон " + (flag ? "включён" : "выключен")));
					if (flag)
					{
						DumpDiagnostics();
					}
				}
				if (flag)
				{
					AttachCamera();
				}
			}
			if (!visible)
			{
				return;
			}
			float unscaledDeltaTime = Time.unscaledDeltaTime;
			foreach (Item item in items)
			{
				Vector2 anchoredPosition = item.rt.anchoredPosition;
				anchoredPosition.y -= item.speed * unscaledDeltaTime;
				item.rt.anchoredPosition = anchoredPosition;
				((Transform)item.rt).Rotate(0f, 0f, item.spin * unscaledDeltaTime);
				if (anchoredPosition.y < 0f - item.size)
				{
					Respawn(item, anywhere: false);
				}
			}
		}
	}
	[BepInPlugin("local.patron.logo", "PATRON Logo", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private ConfigEntry<string> keywords;

		private ConfigEntry<bool> logCandidates;

		private Texture2D tex;

		private Sprite sprite;

		private void Awake()
		{
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			keywords = ((BaseUnityPlugin)this).Config.Bind<string>("General", "NameKeywords", "logo,repo", "Через запятую: если в имени спрайта/текстуры/объекта есть одно из слов, картинка заменяется.");
			logCandidates = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LogCandidates", true, "Писать в BepInEx/LogOutput.log все UI-картинки главного меню (чтобы подобрать точное имя логотипа).");
			string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "patron_logo.png");
			tex = new Texture2D(2, 2, (TextureFormat)4, false);
			ImageConversion.LoadImage(tex, File.ReadAllBytes(path));
			((Texture)tex).filterMode = (FilterMode)1;
			((Object)tex).name = "patron_logo";
			sprite = Sprite.Create(tex, new Rect(0f, 0f, (float)((Texture)tex).width, (float)((Texture)tex).height), new Vector2(0.5f, 0.5f), 100f);
			((Object)sprite).name = "patron_logo";
			FallingBackground.Create(((BaseUnityPlugin)this).Config, Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "falling.png"));
			SceneManager.sceneLoaded += delegate
			{
				((MonoBehaviour)this).StartCoroutine(Scan());
			};
			((MonoBehaviour)this).StartCoroutine(Scan());
		}

		private IEnumerator Scan()
		{
			for (int i = 0; i < 10; i++)
			{
				yield return (object)new WaitForSeconds(0.5f);
				Replace();
			}
		}

		private bool Match(string n)
		{
			if (string.IsNullOrEmpty(n) || n == "patron_logo")
			{
				return false;
			}
			n = n.ToLowerInvariant();
			string[] array = keywords.Value.Split(',');
			foreach (string text in array)
			{
				if (text.Trim().Length > 0 && n.Contains(text.Trim().ToLowerInvariant()))
				{
					return true;
				}
			}
			return false;
		}

		private void Replace()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			Image[] array = Resources.FindObjectsOfTypeAll<Image>();
			Scene scene;
			foreach (Image val in array)
			{
				scene = ((Component)val).gameObject.scene;
				if (((Scene)(ref scene)).IsValid())
				{
					string text = (Object.op_Implicit((Object)(object)val.sprite) ? ((Object)val.sprite).name : "");
					if (logCandidates.Value)
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("[Image] obj='" + ((Object)val).name + "' sprite='" + text + "'"));
					}
					if (Match(text) || Match(((Object)val).name))
					{
						val.sprite = sprite;
						val.preserveAspect = true;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Заменён Image '" + ((Object)val).name + "'"));
					}
				}
			}
			RawImage[] array2 = Resources.FindObjectsOfTypeAll<RawImage>();
			foreach (RawImage val2 in array2)
			{
				scene = ((Component)val2).gameObject.scene;
				if (((Scene)(ref scene)).IsValid())
				{
					string text2 = (Object.op_Implicit((Object)(object)val2.texture) ? ((Object)val2.texture).name : "");
					if (logCandidates.Value)
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)("[RawImage] obj='" + ((Object)val2).name + "' tex='" + text2 + "'"));
					}
					if (Match(text2) || Match(((Object)val2).name))
					{
						val2.texture = (Texture)(object)tex;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Заменён RawImage '" + ((Object)val2).name + "'"));
					}
				}
			}
		}
	}
}