Decompiled source of LoadingScreenSpriteFix v2.0.0

plugins/LoadingScreenFix.dll

Decompiled a month 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HG;
using On.RoR2;
using On.RoR2.UI.MainMenu;
using RoR2;
using RoR2.UI;
using RoR2.UI.MainMenu;
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("LoadingScreenFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LoadingScreenFix")]
[assembly: AssemblyTitle("LoadingScreenFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace LoadingScreenFix;

[BepInPlugin("Nebby1999.LoadingScreenFix", "LoadingScreenFix", "2.0.0")]
public class LoadingScreenFix : BaseUnityPlugin
{
	public const string PluginGUID = "Nebby1999.LoadingScreenFix";

	public const string PluginAuthor = "Nebby1999";

	public const string PluginName = "LoadingScreenFix";

	public const string PluginVersion = "2.0.0";

	private GameObject _blackBackground;

	private GameObject _walkPrefab;

	private List<SimpleSpriteAnimation> _spriteAnimations = new List<SimpleSpriteAnimation>();

	private HashSet<AssetBundle> _assetBundles = new HashSet<AssetBundle>();

	private List<Image> _blackBackgroundInstances = new List<Image>();

	private AssetBundle _myBundle;

	private static bool _pastOrigSelfOnPickRandomObjectOnAwake;

	internal static LoadingScreenFix instance;

	internal static ManualLogSource logger;

	private void Awake()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		instance = this;
		logger = ((BaseUnityPlugin)this).Logger;
		LoadBundle();
		PickRandomObjectOnAwake.Awake += new hook_Awake(AddBlackBackgroundAndSpriteAnimations);
		MainMenuController.Awake += new hook_Awake(FreeMemoryAndDestroySelf);
		SceneManager.sceneLoaded += DisableBlackBackgrounds;
		Log("Awake Called");
	}

	public static void AddSpriteAnimations(AssetBundle sourceBundle)
	{
		if (_pastOrigSelfOnPickRandomObjectOnAwake)
		{
			((BaseUnityPlugin)instance).Logger.LogInfo((object)"Too late! we're already past the loading screen, consider calling the API's methods on your mod's awake.");
			return;
		}
		instance._assetBundles.Add(sourceBundle);
		SimpleSpriteAnimation[] array = sourceBundle.LoadAllAssets<SimpleSpriteAnimation>();
		for (int i = 0; i < array.Length; i++)
		{
			AddSpriteAnimation(array[i]);
		}
	}

	public static void AddSpriteAnimation(SimpleSpriteAnimation spriteAnimation, AssetBundle sourceBundle)
	{
		if (_pastOrigSelfOnPickRandomObjectOnAwake)
		{
			((BaseUnityPlugin)instance).Logger.LogInfo((object)"Too late! we're already past the loading screen, consider calling the API's methods on your mod's awake.");
			return;
		}
		instance._assetBundles.Add(sourceBundle);
		AddSpriteAnimation(spriteAnimation);
	}

	public static void AddSpriteAnimation(SimpleSpriteAnimation spriteAnimation)
	{
		if (_pastOrigSelfOnPickRandomObjectOnAwake)
		{
			((BaseUnityPlugin)instance).Logger.LogInfo((object)"Too late! we're already past the loading screen, consider calling the API's methods on your mod's awake.");
		}
		else
		{
			instance._spriteAnimations.Add(spriteAnimation);
		}
	}

	private void LoadBundle()
	{
		string text = Path.Combine(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "assetbundles"), "loadingscreenutility");
		_myBundle = AssetBundle.LoadFromFile(text);
		_walkPrefab = _myBundle.LoadAsset<GameObject>("CustomSpriteWalk");
	}

	private void FreeMemoryAndDestroySelf(orig_Awake orig, MainMenuController self)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Expected O, but got Unknown
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Expected O, but got Unknown
		orig.Invoke(self);
		Log("Freeing resources and killing self.");
		PickRandomObjectOnAwake.Awake -= new hook_Awake(AddBlackBackgroundAndSpriteAnimations);
		MainMenuController.Awake -= new hook_Awake(FreeMemoryAndDestroySelf);
		SceneManager.sceneLoaded -= DisableBlackBackgrounds;
		if (Object.op_Implicit((Object)(object)_blackBackground))
		{
			Log("Destroying Black Background");
			Object.Destroy((Object)(object)_blackBackground);
		}
		foreach (SimpleSpriteAnimation spriteAnimation in _spriteAnimations)
		{
			Log("Destroying " + (object)spriteAnimation);
			Object.Destroy((Object)(object)spriteAnimation);
		}
		foreach (AssetBundle assetBundle in _assetBundles)
		{
			Log("Unloading " + (object)assetBundle);
			assetBundle.Unload(true);
		}
		Log("Unloading own bundle");
		_myBundle.Unload(true);
	}

	private void DisableBlackBackgrounds(Scene arg0, LoadSceneMode arg1)
	{
		Log("Checking if splash");
		if (((Scene)(ref arg0)).name == "splash")
		{
			Log("Its splash, disabling all black backgrounds.");
			{
				foreach (Image blackBackgroundInstance in _blackBackgroundInstances)
				{
					((Behaviour)blackBackgroundInstance).enabled = false;
				}
				return;
			}
		}
		Log("Not Splash");
	}

	private void AddBlackBackgroundAndSpriteAnimations(orig_Awake orig, PickRandomObjectOnAwake self)
	{
		if (((Object)((Component)self).gameObject).name != "MiniScene")
		{
			orig.Invoke(self);
			return;
		}
		_pastOrigSelfOnPickRandomObjectOnAwake = true;
		AddSprites(self);
		AddBackgrounds(self);
		orig.Invoke(self);
	}

	private void AddSprites(PickRandomObjectOnAwake self)
	{
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		foreach (SimpleSpriteAnimation spriteAnimation in _spriteAnimations)
		{
			try
			{
				Log($"Creating sprite for {spriteAnimation}");
				GameObject val = Object.Instantiate<GameObject>(_walkPrefab, ((Component)self).transform);
				((Object)val).name = $"{spriteAnimation}Animator";
				SimpleSpriteAnimator componentInChildren = val.GetComponentInChildren<SimpleSpriteAnimator>();
				componentInChildren.animation = spriteAnimation;
				componentInChildren.target.sprite = spriteAnimation.frames.FirstOrDefault().sprite;
				ArrayUtils.ArrayAppend<GameObject>(ref self.ObjectsToSelect, ref val);
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to add sprite animation for {spriteAnimation}.\n{arg}");
			}
		}
	}

	private void AddBackgrounds(PickRandomObjectOnAwake self)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: 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)
		_blackBackground = new GameObject("BlackBackground");
		RectTransform obj = _blackBackground.AddComponent<RectTransform>();
		((Transform)obj).SetParent(((Component)self).transform.parent);
		((Transform)obj).SetAsFirstSibling();
		obj.sizeDelta = new Vector2(256f, 256f);
		((Transform)obj).localPosition = new Vector3(64f, 16f, -16f);
		Image val = _blackBackground.AddComponent<Image>();
		_blackBackgroundInstances.Add(val);
		((Graphic)val).color = Color.black;
	}

	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	private void Log(object thing)
	{
	}
}
public static class SimpleSpriteAnimationGenerator
{
	public static SimpleSpriteAnimation CreateSpriteAnimation(IEnumerable<Sprite> sprites, IEnumerable<int> durations, float framerate)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		Sprite[] array = sprites.ToArray();
		int[] array2 = durations.ToArray();
		int num = array.Length;
		Frame[] array3 = (Frame[])(object)new Frame[num];
		for (int i = 0; i < num; i++)
		{
			int duration = 1;
			if (ArrayUtils.IsInBounds<int>(array2, i))
			{
				duration = array2[i];
			}
			array3[i] = new Frame
			{
				duration = duration,
				sprite = array[i]
			};
		}
		SimpleSpriteAnimationArguments args = default(SimpleSpriteAnimationArguments);
		args.framerate = framerate;
		args.frames = array3;
		return CreateInternal(args);
	}

	public static SimpleSpriteAnimation CreateSpriteAnimation(IEnumerable<(Sprite, int)> spritesAndDurations, float framerate)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		(Sprite, int)[] array = spritesAndDurations.ToArray();
		int num = array.Length;
		Frame[] array2 = (Frame[])(object)new Frame[num];
		for (int i = 0; i < num; i++)
		{
			array2[i] = new Frame
			{
				sprite = array[i].Item1,
				duration = array[i].Item2
			};
		}
		SimpleSpriteAnimationArguments args = default(SimpleSpriteAnimationArguments);
		args.frames = array2;
		args.framerate = framerate;
		return CreateInternal(args);
	}

	public static SimpleSpriteAnimation CreateSpriteAnimation(Frame[] frames, float framerate)
	{
		SimpleSpriteAnimationArguments args = default(SimpleSpriteAnimationArguments);
		args.frames = frames;
		args.framerate = framerate;
		return CreateInternal(args);
	}

	public static SimpleSpriteAnimation CreateSpriteAnimation(SimpleSpriteAnimationArguments args)
	{
		return CreateInternal(args);
	}

	private static SimpleSpriteAnimation CreateInternal(SimpleSpriteAnimationArguments args)
	{
		SimpleSpriteAnimation obj = ScriptableObject.CreateInstance<SimpleSpriteAnimation>();
		obj.frames = args.frames;
		obj.frameRate = args.framerate;
		return obj;
	}
}
public struct SimpleSpriteAnimationArguments
{
	public Frame[] frames;

	public float framerate;
}