Decompiled source of LoadingScreenSpriteFix v3.0.1

plugins/LoadingScreenFix.dll

Decompiled 3 weeks 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.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
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+cf1fdb673492109c02b84c30b8c6eb058b1e61d6")]
[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", "3.0.0")]
public class LoadingScreenFix : BaseUnityPlugin
{
	public const string MINI_SCENE_ADDRESS = "3a63b5675ffabd247aa47f7eeadf946e";

	public const string PluginGUID = "Nebby1999.LoadingScreenFix";

	public const string PluginAuthor = "Nebby1999";

	public const string PluginName = "LoadingScreenFix";

	public const string PluginVersion = "3.0.0";

	private GameObject _blackBackground;

	private GameObject _walkPrefab;

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

	private AssetBundle _myBundle;

	private static bool _pastOrigSelfOnPickRandomObjectOnAwake;

	private static bool _blackBackgroundsAdded;

	internal static LoadingScreenFix instance;

	internal static ManualLogSource logger;

	private static PickRandomObjectOnAwake _screenTintCanvasMiniScenePickRandomObjectOnAwake;

	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
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		instance = this;
		logger = ((BaseUnityPlugin)this).Logger;
		LoadBundle();
		PickRandomObjectOnAwake.OnEnable += new hook_OnEnable(AddBlackBackgroundAndSpriteAnimations);
		MainMenuController.Awake += new hook_Awake(UnhookDestroyBackgroundAndAddToMiniScenePrefab);
		SceneManager.sceneLoaded += DisableBlackBackgrounds;
		AsyncOperationHandle<GameObject> val = Addressables.LoadAssetAsync<GameObject>((object)"3a63b5675ffabd247aa47f7eeadf946e");
		val.Completed += delegate(AsyncOperationHandle<GameObject> obj)
		{
			_screenTintCanvasMiniScenePickRandomObjectOnAwake = obj.Result.GetComponent<PickRandomObjectOnAwake>();
		};
		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;
		}
		SimpleSpriteAnimation[] array = sourceBundle.LoadAllAssets<SimpleSpriteAnimation>();
		for (int i = 0; i < array.Length; i++)
		{
			AddSpriteAnimation(array[i]);
		}
	}

	[Obsolete("LoadingScrenFix no longer stores the source assetbundle, either call \"AddSpriteAnimations(AssetBundle)\" or \"AddSpriteAnimation(SimpleSpriteAnimation)\"")]
	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.");
		}
		else
		{
			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 UnhookDestroyBackgroundAndAddToMiniScenePrefab(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.OnEnable -= new hook_OnEnable(AddBlackBackgroundAndSpriteAnimations);
		MainMenuController.Awake -= new hook_Awake(UnhookDestroyBackgroundAndAddToMiniScenePrefab);
		SceneManager.sceneLoaded -= DisableBlackBackgrounds;
		if (Object.op_Implicit((Object)(object)_blackBackground))
		{
			Log("Destroying Black Background");
			Object.Destroy((Object)(object)_blackBackground);
		}
		if (Object.op_Implicit((Object)(object)_screenTintCanvasMiniScenePickRandomObjectOnAwake))
		{
			AddSprites(_screenTintCanvasMiniScenePickRandomObjectOnAwake);
		}
	}

	private void DisableBlackBackgrounds(Scene arg0, LoadSceneMode arg1)
	{
		Log("Checking if splash");
		if (((Scene)(ref arg0)).name == "splash")
		{
			Log("Its splash, deleting all black backgrounds.");
			Object.Destroy((Object)(object)_blackBackground);
			_blackBackground = null;
		}
		else
		{
			Log("Not Splash");
		}
	}

	private void AddBlackBackgroundAndSpriteAnimations(orig_OnEnable 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_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: 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);
				if (_blackBackgroundsAdded)
				{
					val.transform.localPosition = Vector3.zero;
				}
				((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_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Expected O, but got Unknown
		//IL_0050: 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_007e: Unknown result type (might be due to invalid IL or missing references)
		if (!_blackBackgroundsAdded)
		{
			_blackBackgroundsAdded = true;
			_blackBackground = new GameObject("BlackBackground");
			RectTransform obj = _blackBackground.AddComponent<RectTransform>();
			((Transform)obj).SetParent(((Component)self).transform.parent.parent);
			((Transform)obj).SetAsFirstSibling();
			obj.sizeDelta = new Vector2(256f, 256f);
			((Transform)obj).localPosition = new Vector3(800f, -477f, 0f);
			((Graphic)_blackBackground.AddComponent<Image>()).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]
			};
		}
		return CreateInternal(new SimpleSpriteAnimationArguments
		{
			framerate = framerate,
			frames = array3
		});
	}

	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
			};
		}
		return CreateInternal(new SimpleSpriteAnimationArguments
		{
			frames = array2,
			framerate = framerate
		});
	}

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

	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;
}