Decompiled source of Background v1.0.2

Background.dll

Decompiled 21 hours 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 System.Threading.Tasks;
using Background.Config;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using CellMenu;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("Background")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Background")]
[assembly: AssemblyTitle("Background")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace Background
{
	public class BackgroundManager : MonoBehaviour
	{
		private static readonly BackgroundTarget[] _targets = Enum.GetValues<BackgroundTarget>();

		private static readonly Dictionary<BackgroundTarget, BgConf> ObjectLayerMap = new Dictionary<BackgroundTarget, BgConf>
		{
			[BackgroundTarget.Settings] = new BgConf
			{
				GameObjectLayer = () => LayerManager.LAYER_MENUDEFERREDSCENE
			},
			[BackgroundTarget.Rundown] = new BgConf
			{
				GameObjectLayer = () => LayerManager.LAYER_MENUDEFERREDSCENE
			},
			[BackgroundTarget.Lobby] = new BgConf
			{
				GameObjectLayer = () => LayerManager.LAYER_MENUDEFERREDSCENE,
				MinDistFromCam = 4000f
			},
			[BackgroundTarget.Map] = new BgConf
			{
				GameObjectLayer = () => LayerManager.LAYER_UI,
				SpriteOrder = -20
			},
			[BackgroundTarget.ExpeditionFail] = new BgConf
			{
				GameObjectLayer = () => LayerManager.LAYER_UI,
				SpriteOrder = 1
			},
			[BackgroundTarget.ExpeditionSuccess] = new BgConf
			{
				GameObjectLayer = () => LayerManager.LAYER_UI,
				SpriteOrder = 1
			}
		};

		private static BackgroundManager? _instance;

		private bool _dirty;

		private Dictionary<BackgroundTarget, Sprite> _sp = new Dictionary<BackgroundTarget, Sprite>();

		private Dictionary<BackgroundTarget, SpriteRenderer> _sr = new Dictionary<BackgroundTarget, SpriteRenderer>();

		private Dictionary<BackgroundTarget, SpriteScreenFitter> _fitter = new Dictionary<BackgroundTarget, SpriteScreenFitter>();

		public static BackgroundManager? Instance => _instance;

		public void RefreshStyles()
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			BackgroundTarget[] targets = _targets;
			foreach (BackgroundTarget target in targets)
			{
				((MonoBehaviour)this).StartCoroutine(CollectionExtensions.WrapToIl2Cpp(TextureLoader.LoadTexture(ConfigMgr.GetTargetImgPath(target), delegate(Texture2D tex)
				{
					//IL_0019: Unknown result type (might be due to invalid IL or missing references)
					//IL_0028: Unknown result type (might be due to invalid IL or missing references)
					Sprite val = Sprite.Create(tex, new Rect(0f, 0f, (float)((Texture)tex).width, (float)((Texture)tex).height), new Vector2(0.5f, 0.5f));
					((Object)val).name = $"BG.Sprite.{target}";
					_sp[target] = val;
					if (_sr.TryGetValue(target, out SpriteRenderer value2))
					{
						value2.sprite = val;
					}
					OnBackgroundSet(target);
				}, delegate
				{
					_sp.Remove(target);
					if (_sr.TryGetValue(target, out SpriteRenderer value))
					{
						value.sprite = null;
					}
					OnBackgroundUnset(target);
				})));
				_sr[target].color = ConfigMgr.GetTargetColor(target);
			}
		}

		public void SetDirty()
		{
			_dirty = true;
		}

		private void Awake()
		{
			if ((Object)(object)_instance != (Object)null)
			{
				Object.Destroy((Object)(object)this);
				return;
			}
			_instance = this;
			Object.DontDestroyOnLoad((Object)(object)this);
			CreateSR();
			RefreshStyles();
		}

		private void CreateSR()
		{
			BackgroundTarget[] targets = _targets;
			foreach (BackgroundTarget target in targets)
			{
				CreateAndSetupTarget(target);
			}
		}

		private void Update()
		{
			if (_dirty)
			{
				RefreshStyles();
				_dirty = false;
			}
		}

		private Transform GetParent(BackgroundTarget target)
		{
			return (Transform)(target switch
			{
				BackgroundTarget.Settings => ((Component)GuiManager.MainMenuLayer.PageSettings).transform, 
				BackgroundTarget.Rundown => ((Component)GuiManager.MainMenuLayer.PageRundownNew).transform, 
				BackgroundTarget.Lobby => ((Component)GuiManager.MainMenuLayer.PageLoadout).transform, 
				BackgroundTarget.Map => ((Component)GuiManager.MainMenuLayer.PageMap).transform, 
				BackgroundTarget.ExpeditionFail => ((Component)GuiManager.MainMenuLayer.PageExpeditionFail).transform, 
				BackgroundTarget.ExpeditionSuccess => ((Component)GuiManager.MainMenuLayer.PageExpeditionSuccess).transform, 
				_ => ((Component)((GuiLayer)GuiManager.MainMenuLayer).GuiLayerBase).transform, 
			});
		}

		private void CreateAndSetupTarget(BackgroundTarget target)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject($"BG.{target}");
			SpriteRenderer val2 = val.AddComponent<SpriteRenderer>();
			SpriteScreenFitter spriteScreenFitter = val.AddComponent<SpriteScreenFitter>();
			val.transform.SetParent(GetParent(target), false);
			if (ObjectLayerMap.TryGetValue(target, out var value))
			{
				value.Apply(val2, spriteScreenFitter);
			}
			_sr[target] = val2;
			_fitter[target] = spriteScreenFitter;
		}

		private void OnBackgroundSet(BackgroundTarget target)
		{
			switch (target)
			{
			case BackgroundTarget.ExpeditionSuccess:
				((Component)((CM_PageBase)GuiManager.MainMenuLayer.PageExpeditionSuccess).m_backgroundMiddle).gameObject.SetActive(false);
				break;
			case BackgroundTarget.ExpeditionFail:
				((Component)((CM_PageBase)GuiManager.MainMenuLayer.PageExpeditionFail).m_backgroundMiddle).gameObject.SetActive(false);
				break;
			}
		}

		private void OnBackgroundUnset(BackgroundTarget target)
		{
			switch (target)
			{
			case BackgroundTarget.ExpeditionSuccess:
				((Component)((CM_PageBase)GuiManager.MainMenuLayer.PageExpeditionSuccess).m_backgroundMiddle).gameObject.SetActive(true);
				break;
			case BackgroundTarget.ExpeditionFail:
				((Component)((CM_PageBase)GuiManager.MainMenuLayer.PageExpeditionFail).m_backgroundMiddle).gameObject.SetActive(true);
				break;
			}
		}
	}
	public enum BackgroundTarget
	{
		Settings,
		Rundown,
		Lobby,
		Map,
		ExpeditionFail,
		ExpeditionSuccess
	}
	public struct BgConf
	{
		public Func<int>? GameObjectLayer;

		public int SpriteLayerID;

		public int SpriteOrder;

		public int SpriteGroupID;

		public int SpriteGroupOrder;

		public float MinDistFromCam;

		public BgConf()
		{
			GameObjectLayer = null;
			SpriteLayerID = 0;
			SpriteOrder = 0;
			SpriteGroupID = 0;
			SpriteGroupOrder = 0;
			MinDistFromCam = 0f;
		}

		public void Apply(SpriteRenderer sr, SpriteScreenFitter fitter)
		{
			((Component)sr).gameObject.layer = GameObjectLayer?.Invoke() ?? 0;
			((Renderer)sr).sortingLayerID = SpriteLayerID;
			((Renderer)sr).sortingOrder = SpriteOrder;
			((Renderer)sr).sortingGroupID = SpriteGroupID;
			((Renderer)sr).sortingGroupOrder = SpriteGroupOrder;
			fitter.enforceMinDistance = true;
			fitter.minDistanceFromCamera = MinDistFromCam;
		}
	}
	public enum ConfigEntryRule
	{
		Min,
		Max
	}
	public class ConfigEntryExtended<T>
	{
		private Dictionary<ConfigEntryRule, T> _rules = new Dictionary<ConfigEntryRule, T>();

		private ConfigEntry<T> _entry;

		public T Value
		{
			get
			{
				return _entry.Value;
			}
			set
			{
				_entry.Value = Enforce(value);
			}
		}

		public object BoxedValue
		{
			get
			{
				return ((ConfigEntryBase)_entry).BoxedValue;
			}
			set
			{
				((ConfigEntryBase)_entry).BoxedValue = Enforce((T)value);
			}
		}

		public ConfigEntryExtended(ConfigEntry<T> entry)
		{
			_entry = entry;
		}

		public bool AddRule(ConfigEntryRule rule, T ruleValue)
		{
			if (_rules.TryAdd(rule, ruleValue))
			{
				Enforce(rule);
				return true;
			}
			return false;
		}

		private void Enforce()
		{
			T val = Enforce(_entry.Value);
			if (val != null && !val.Equals(_entry.Value))
			{
				_entry.Value = val;
			}
		}

		private T Enforce(T val)
		{
			foreach (var (configEntryRule2, val3) in _rules)
			{
				switch (configEntryRule2)
				{
				case ConfigEntryRule.Min:
					if (Comparer<T>.Default.Compare(val, val3) < 0)
					{
						val = val3;
					}
					break;
				case ConfigEntryRule.Max:
					if (Comparer<T>.Default.Compare(val, val3) > 0)
					{
						val = val3;
					}
					break;
				}
			}
			return val;
		}

		private void Enforce(ConfigEntryRule rule)
		{
			T value = _entry.Value;
			value = Enforce(value, rule);
			if (value != null && !value.Equals(_entry.Value))
			{
				_entry.Value = value;
			}
		}

		private T Enforce(T val, ConfigEntryRule rule)
		{
			if (!_rules.TryGetValue(rule, out var value))
			{
				return val;
			}
			switch (rule)
			{
			case ConfigEntryRule.Min:
				if (Comparer<T>.Default.Compare(val, value) < 0)
				{
					val = value;
				}
				break;
			case ConfigEntryRule.Max:
				if (Comparer<T>.Default.Compare(val, value) > 0)
				{
					val = value;
				}
				break;
			}
			return val;
		}

		public static implicit operator ConfigEntryExtended<T>(ConfigEntry<T> entry)
		{
			return new ConfigEntryExtended<T>(entry);
		}
	}
	[HarmonyPatch]
	public class EventPatch
	{
		public static event Action? OnMainMenuSetup;

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MainMenuGuiLayer), "Setup")]
		public static void MainMenu_Setup__Postfix(MainMenuGuiLayer __instance)
		{
			EventPatch.OnMainMenuSetup?.Invoke();
		}
	}
	internal static class Logger
	{
		private static ManualLogSource _mLogSource;

		public static bool Ready => _mLogSource != null;

		public static void Setup()
		{
			_mLogSource = Logger.CreateLogSource("io.takina.gtfo.Background");
		}

		public static void SetupFromInit(ManualLogSource logSource)
		{
			_mLogSource = logSource;
		}

		private static string Format(object data)
		{
			return data.ToString();
		}

		public static void Debug(object msg)
		{
			if (ConfigMgr.Debug)
			{
				_mLogSource.LogInfo((object)(" [DEBUG] " + Format(msg)));
			}
		}

		public static void Debug(string fmt, params object[] args)
		{
			if (ConfigMgr.Debug)
			{
				_mLogSource.LogInfo((object)("[DEBUG] " + Format(string.Format(fmt, args))));
			}
		}

		public static void Info(object msg)
		{
			_mLogSource.LogInfo((object)Format(msg));
		}

		public static void Info(string fmt, params object[] args)
		{
			_mLogSource.LogInfo((object)Format(string.Format(fmt, args)));
		}

		public static void Warn(object msg)
		{
			_mLogSource.LogWarning((object)Format(msg));
		}

		public static void Warn(string fmt, params object[] args)
		{
			_mLogSource.LogWarning((object)Format(string.Format(fmt, args)));
		}

		public static void Error(object msg)
		{
			_mLogSource.LogError((object)Format(msg));
		}

		public static void Error(string fmt, params object[] args)
		{
			_mLogSource.LogError((object)Format(string.Format(fmt, args)));
		}

		public static void Fatal(object msg)
		{
			_mLogSource.LogFatal((object)Format(msg));
		}

		public static void Fatal(string fmt, params object[] args)
		{
			_mLogSource.LogFatal((object)Format(string.Format(fmt, args)));
		}
	}
	[BepInPlugin("io.takina.gtfo.Background", "Background", "1.0.2")]
	public class Plugin : BasePlugin
	{
		public const string NAME = "Background";

		public const string GUID = "io.takina.gtfo.Background";

		public const string VERSION = "1.0.2";

		private GameObject _plugObj;

		public override void Load()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			Logger.Setup();
			Logger.Info("Background [io.takina.gtfo.Background @ 1.0.2]");
			Harmony val = new Harmony("io.takina.gtfo.Background");
			ConfigMgr.Init();
			ClassInjector.RegisterTypeInIl2Cpp<BackgroundManager>();
			ClassInjector.RegisterTypeInIl2Cpp<SpriteScreenFitter>();
			EventPatch.OnMainMenuSetup += Initialize;
			Logger.Info("Patching...");
			ApplyPatch<EventPatch>(val);
			Logger.Info("Finished Patching");
		}

		private static void ApplyPatch<T>(Harmony h)
		{
			h.PatchAll(typeof(T));
		}

		private void Initialize()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_plugObj = new GameObject("io.takina.gtfo.Background");
			_plugObj.AddComponent<BackgroundManager>();
		}

		public override bool Unload()
		{
			return true;
		}
	}
	public sealed class SpriteScreenFitter : MonoBehaviour
	{
		public enum FitMode
		{
			Cover,
			Contain
		}

		public Camera TargetCamera;

		public FitMode fitMode;

		public bool lockToCameraCenterXY = true;

		public float minDistanceFromCamera;

		public bool enforceMinDistance = true;

		public bool autoRecalcOnChange = true;

		private SpriteRenderer _sr;

		private bool _dirty = true;

		private Sprite? _lastSprite;

		private int _lastW;

		private int _lastH;

		private float _lastAspect;

		private bool _lastOrtho;

		private float _lastOrthoSize;

		private float _lastFov;

		private Vector3 _lastCamPos;

		private Quaternion _lastCamRot;

		private void Awake()
		{
			_sr = ((Component)this).GetComponent<SpriteRenderer>();
			if ((Object)(object)TargetCamera == (Object)null)
			{
				TargetCamera = GuiManager.MainMenuLayer.m_menuCamera.m_camera;
			}
			CaptureState();
			_dirty = true;
		}

		public void RequestRecalculate()
		{
			_dirty = true;
		}

		private void LateUpdate()
		{
			if (!((Object)(object)_sr == (Object)null) && !((Object)(object)_sr.sprite == (Object)null) && !((Object)(object)TargetCamera == (Object)null))
			{
				if (autoRecalcOnChange && HasRelevantChange())
				{
					_dirty = true;
				}
				if (_dirty)
				{
					RecalculateNow();
					CaptureState();
					_dirty = false;
				}
			}
		}

		private void RecalculateNow()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			Camera targetCamera = TargetCamera;
			if (lockToCameraCenterXY)
			{
				Vector3 position = ((Component)this).transform.position;
				Vector3 position2 = ((Component)targetCamera).transform.position;
				((Component)this).transform.position = new Vector3(position2.x, position2.y, position.z);
			}
			if (enforceMinDistance && minDistanceFromCamera > 0f)
			{
				float num = Vector3.Dot(((Component)this).transform.position - ((Component)targetCamera).transform.position, ((Component)targetCamera).transform.forward);
				if (num < minDistanceFromCamera)
				{
					float num2 = minDistanceFromCamera - num;
					Transform transform = ((Component)this).transform;
					transform.position += ((Component)targetCamera).transform.forward * num2;
				}
			}
			Vector2 cameraWorldSizeAtPlane = GetCameraWorldSizeAtPlane(targetCamera, ((Component)this).transform);
			Bounds bounds = _sr.sprite.bounds;
			Vector2 val = Vector2.op_Implicit(((Bounds)(ref bounds)).size);
			if (!(val.x <= 0f) && !(val.y <= 0f))
			{
				float num3 = cameraWorldSizeAtPlane.x / val.x;
				float num4 = cameraWorldSizeAtPlane.y / val.y;
				float num5 = ((fitMode == FitMode.Cover) ? Mathf.Max(num3, num4) : Mathf.Min(num3, num4));
				((Component)this).transform.localScale = new Vector3(num5, num5, 1f);
			}
		}

		private static Vector2 GetCameraWorldSizeAtPlane(Camera cam, Transform plane)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			if (cam.orthographic)
			{
				float num = cam.orthographicSize * 2f;
				return new Vector2(num * cam.aspect, num);
			}
			float num2 = Vector3.Dot(plane.position - ((Component)cam).transform.position, ((Component)cam).transform.forward);
			if (num2 <= 0.0001f)
			{
				num2 = 0.0001f;
			}
			float num3 = 2f * Mathf.Tan(cam.fieldOfView * 0.5f * ((float)Math.PI / 180f)) * num2;
			return new Vector2(num3 * cam.aspect, num3);
		}

		private bool HasRelevantChange()
		{
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: 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)
			if ((Object)(object)_sr.sprite != (Object)(object)_lastSprite)
			{
				return true;
			}
			if (Screen.width != _lastW || Screen.height != _lastH)
			{
				return true;
			}
			Camera targetCamera = TargetCamera;
			if (targetCamera.orthographic != _lastOrtho)
			{
				return true;
			}
			if (!Mathf.Approximately(targetCamera.aspect, _lastAspect))
			{
				return true;
			}
			if (targetCamera.orthographic)
			{
				if (!Mathf.Approximately(targetCamera.orthographicSize, _lastOrthoSize))
				{
					return true;
				}
			}
			else if (!Mathf.Approximately(targetCamera.fieldOfView, _lastFov))
			{
				return true;
			}
			if (((Component)targetCamera).transform.position != _lastCamPos || ((Component)targetCamera).transform.rotation != _lastCamRot)
			{
				return true;
			}
			return false;
		}

		private void CaptureState()
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			_lastSprite = _sr.sprite;
			_lastW = Screen.width;
			_lastH = Screen.height;
			Camera targetCamera = TargetCamera;
			if (!((Object)(object)targetCamera == (Object)null))
			{
				_lastAspect = targetCamera.aspect;
				_lastOrtho = targetCamera.orthographic;
				_lastOrthoSize = targetCamera.orthographicSize;
				_lastFov = targetCamera.fieldOfView;
				_lastCamPos = ((Component)targetCamera).transform.position;
				_lastCamRot = ((Component)targetCamera).transform.rotation;
			}
		}
	}
	public class TextureLoader
	{
		private static readonly Dictionary<string, Texture2D> Cache = new Dictionary<string, Texture2D>();

		public static IEnumerator LoadTexture(string path, Action<Texture2D> onLoad, Action? onError = null)
		{
			if (string.IsNullOrEmpty(path))
			{
				Logger.Warn("[LoadTexture] Texture path is null or empty. This might be intentional if you are unsetting a background, but if not, please check your configuration.");
				Logger.Warn("^^^ YOU CAN SUPPRESS THIS WARNING by setting the path to a single exclamation mark (!), this tells us that you DO NOT want to load a texture for that menu/page.");
				onError?.Invoke();
				yield break;
			}
			if (path.Trim() == "!")
			{
				onError?.Invoke();
				yield break;
			}
			if (Cache.TryGetValue(path, out Texture2D value))
			{
				onLoad(value);
				yield break;
			}
			string uri;
			try
			{
				Uri uri2 = new Uri(path);
				uri = uri2.AbsoluteUri;
			}
			catch (Exception ex)
			{
				Logger.Error("[LoadTexture] Invalid URI: \"" + path + "\". Exception: " + ex.Message);
				onError?.Invoke();
				yield break;
			}
			UnityWebRequest req = UnityWebRequest.Get(uri);
			yield return req.SendWebRequest();
			if (req.responseCode >= 400)
			{
				Logger.Error("[LoadTexture] Failed to load texture from \"" + uri + "\": " + req.error);
				onError?.Invoke();
				yield break;
			}
			byte[] array = Il2CppArrayBase<byte>.op_Implicit((Il2CppArrayBase<byte>)(object)req.downloadHandler.data);
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			ImageConversion.LoadImage(val, Il2CppStructArray<byte>.op_Implicit(array));
			((Object)val).name = uri;
			Cache[path] = val;
			onLoad(val);
		}
	}
}
namespace Background.Config
{
	internal static class ConfigMgr
	{
		private const float DefaultBgAlpha = 0.05f;

		private static readonly ConfigFile Conf;

		private static readonly FileSystemWatcher? ConfigWatcher;

		private static readonly ConfigEntry<bool> DebugConf;

		private static readonly ConfigEntry<string> BgTintConf;

		private static readonly ConfigEntry<string> ImageGlobalPathConf;

		private static readonly Dictionary<BackgroundTarget, ConfigEntryExtended<string>> ImagePathConfs;

		private static readonly Dictionary<BackgroundTarget, ConfigEntryExtended<float>> ImageAlphaConfs;

		public static bool Processed { get; private set; }

		public static bool Debug => DebugConf.Value;

		public static Color BgTint
		{
			get
			{
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				Color result = default(Color);
				if (!ColorUtility.TryParseHtmlString(BgTintConf.Value, ref result))
				{
					return Color.white;
				}
				return result;
			}
		}

		public static string ImageGlobalPath => ImageGlobalPathConf.Value;

		public static void Init()
		{
			Logger.Info($"debug={Debug}");
			Process();
		}

		public static void Process()
		{
			BackgroundManager.Instance?.SetDirty();
			Processed = true;
		}

		public static string GetTargetImgPath(BackgroundTarget target)
		{
			string text = "";
			if (ImagePathConfs.TryGetValue(target, out ConfigEntryExtended<string> value))
			{
				text = value.Value;
			}
			if (string.IsNullOrEmpty(text))
			{
				text = ImageGlobalPath;
			}
			return text;
		}

		public static Color GetTargetColor(BackgroundTarget target)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			float num = 1f;
			if (ImageAlphaConfs.TryGetValue(target, out ConfigEntryExtended<float> value))
			{
				num = value.Value;
			}
			Color bgTint = BgTint;
			bgTint.a = Mathf.Clamp01(num);
			return bgTint;
		}

		static ConfigMgr()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			Processed = false;
			ImagePathConfs = new Dictionary<BackgroundTarget, ConfigEntryExtended<string>>();
			ImageAlphaConfs = new Dictionary<BackgroundTarget, ConfigEntryExtended<float>>();
			string text = "Background.cfg";
			string text2 = Path.Combine(Paths.ConfigPath, text);
			Logger.Info("cfgPath = " + text2);
			Conf = new ConfigFile(text2, true);
			ConfigWatcher = new FileSystemWatcher(Paths.ConfigPath, text)
			{
				NotifyFilter = NotifyFilters.LastWrite,
				EnableRaisingEvents = true
			};
			ConfigWatcher.Changed += async delegate
			{
				ConfigWatcher.EnableRaisingEvents = false;
				await Task.Delay(500);
				Logger.Debug("Reloading config...");
				Conf.Reload();
				await Task.Delay(250);
				Process();
				await Task.Delay(250);
				ConfigWatcher.EnableRaisingEvents = true;
			};
			int num = 1;
			string text3 = $"({num++}) Display";
			BgTintConf = Conf.Bind<string>(text3, "Background Tint", "#FFFFFF", "Hex color code for tinting backgrounds. Format: #RRGGBB or yellow (color names in HTML)");
			text3 = $"({num++}) Images";
			ImageGlobalPathConf = Conf.Bind<string>(text3, "Global Image Path", string.Empty, "Path to global image. Page specific images will default to this if unset.");
			BackgroundTarget[] values = Enum.GetValues<BackgroundTarget>();
			foreach (BackgroundTarget backgroundTarget in values)
			{
				ConfigEntryExtended<string> value = Conf.Bind<string>(text3, $"{backgroundTarget} Image Path", string.Empty, $"Path to image for {backgroundTarget} page background.");
				ImagePathConfs[backgroundTarget] = value;
			}
			text3 = $"({num++}) Alpha / Brightness";
			values = Enum.GetValues<BackgroundTarget>();
			foreach (BackgroundTarget backgroundTarget2 in values)
			{
				ConfigEntryExtended<float> configEntryExtended = Conf.Bind<float>(text3, $"{backgroundTarget2} Image Alpha / Brightness", 0.05f, $"The alpha of the {backgroundTarget2} page background, think of it like brightness. (min: 0, max: 1)");
				configEntryExtended.AddRule(ConfigEntryRule.Min, 0f);
				configEntryExtended.AddRule(ConfigEntryRule.Max, 1f);
				ImageAlphaConfs[backgroundTarget2] = configEntryExtended;
			}
			text3 = "(Z) Dev";
			DebugConf = Conf.Bind<bool>(text3, "Enable Debug Logs", false, "debug logging");
		}
	}
}