Skip to content

Commit 2bb7ecf

Browse files
committed
Added GIF support, fixed minor inconsistencies
1 parent 1909488 commit 2bb7ecf

6 files changed

Lines changed: 75 additions & 41 deletions

File tree

Data/CustomImage.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using CustomLoadingScreens.Managers;
1+
using CustomLoadingScreens.Managers;
72
using SixLabors.ImageSharp;
83
using SixLabors.ImageSharp.PixelFormats;
94
using SixLabors.ImageSharp.Processing;
@@ -17,10 +12,13 @@ internal class CustomImage
1712
private readonly Configuration _config = Configuration.Default;
1813

1914
internal readonly string Name;
20-
internal readonly Sprite Sprite;
15+
internal readonly List<Sprite> Sprites;
16+
internal readonly int FramesPerSecond;
17+
internal readonly int FrameCount;
2118

2219
internal CustomImage(string path)
2320
{
21+
Sprites = new List<Sprite>();
2422
var logger = new Logger(nameof(CustomImage));
2523
_config.PreferContiguousImageBuffers = true;
2624

@@ -29,25 +27,31 @@ internal CustomImage(string path)
2927
// Unity loads textures upside-down so flip it
3028
var image = Image.Load<Rgba32>(path);
3129
image.Mutate(c => c.Flip(FlipMode.Vertical));
32-
33-
var texture = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false);
3430

35-
var getPixelDataResult = image.DangerousTryGetSinglePixelMemory(out var memory);
36-
if (!getPixelDataResult)
31+
FramesPerSecond = image.Frames.RootFrame.Metadata.GetGifMetadata().FrameDelay * 10;
32+
FrameCount = image.Frames.Count;
33+
34+
foreach (var frame in image.Frames)
3735
{
38-
logger.Error("Failed to get pixel data.");
39-
return;
40-
}
36+
var texture = new Texture2D(frame.Width, frame.Height, TextureFormat.RGBA32, false);
4137

42-
using var handle = memory.Pin();
38+
var getPixelDataResult = frame.DangerousTryGetSinglePixelMemory(out var memory);
39+
if (!getPixelDataResult)
40+
{
41+
logger.Error("Failed to get pixel data.");
42+
return;
43+
}
4344

44-
// Ugly unsafe block to save a hard copy of memory for performance
45-
unsafe { texture.LoadRawTextureData((IntPtr)handle.Pointer, memory.Length * sizeof(IntPtr)); }
46-
47-
texture.Apply(false);
45+
using var handle = memory.Pin();
4846

49-
Sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
50-
Sprite.hideFlags |= HideFlags.DontUnloadUnusedAsset;
47+
// Ugly unsafe block to save a hard copy of memory for performance
48+
unsafe { texture.LoadRawTextureData((IntPtr)handle.Pointer, memory.Length * sizeof(IntPtr)); }
49+
50+
texture.Apply(false);
51+
52+
Sprites.Add( Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)));
53+
Sprites.Last().hideFlags |= HideFlags.DontUnloadUnusedAsset;
54+
}
5155

5256
logger.Msg("Created sprite.");
5357
}

Main.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
using CustomLoadingScreens.Managers;
2-
using CustomLoadingScreens.Utilities;
1+
using CustomLoadingScreens.Data;
2+
using CustomLoadingScreens.Managers;
3+
using CustomLoadingScreens.Patches;
4+
using Il2CppInterop.Runtime.InteropTypes.Arrays;
35
using MelonLoader;
6+
using UnityEngine.UI;
7+
using Logger = CustomLoadingScreens.Utilities.Logger;
48

59
namespace CustomLoadingScreens
610
{
711
public class Main : MelonMod
812
{
913
private static readonly Logger Logger = new("CustomLoadingScreens");
14+
internal static Il2CppArrayBase<Image> Images;
15+
internal static CustomImage CurrentCustomImage;
1016
public override void OnInitializeMelon()
1117
{
1218
ModSettings.Register();
@@ -21,5 +27,15 @@ public override void OnInitializeMelon()
2127
$"{(CustomDataManager.CustomImages.Count != 1 ? "s" : "")} and {CustomDataManager.GetNumberOfQuotes()} custom loading quote" +
2228
$"{(CustomDataManager.GetNumberOfQuotes() != 1 ? "s" : "")}.", false);
2329
}
30+
31+
/// <summary>
32+
/// Adds support for GIF images.
33+
/// </summary>
34+
public override void OnUpdate()
35+
{
36+
base.OnUpdate();
37+
LoadingImagePatch.LoadingImgPatch.Update();
38+
}
2439
}
40+
2541
}

Managers/CustomDataManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using CustomLoadingScreens.Data;
22
using CustomLoadingScreens.Utilities;
3-
using Il2Cpp;
43

54
namespace CustomLoadingScreens.Managers
65
{
@@ -9,6 +8,7 @@ internal class CustomDataManager
98
private const string JpgSearchPattern = "*.jpg";
109
private const string PngSearchPattern = "*.png";
1110
private const string TxtSearchPattern = "*.txt";
11+
private const string GifSearchPattern = "*.gif";
1212

1313
internal static readonly List<CustomImage> CustomImages = new();
1414
internal static readonly List<string> CustomQuotes = new();
@@ -41,7 +41,8 @@ private static void AddImage(string path)
4141
internal static void LoadImages()
4242
{
4343
var imageFiles = Directory.EnumerateFiles(ModSettings.CustomImageFolder, JpgSearchPattern)
44-
.Concat(Directory.EnumerateFiles(ModSettings.CustomImageFolder, PngSearchPattern));
44+
.Concat(Directory.EnumerateFiles(ModSettings.CustomImageFolder, PngSearchPattern))
45+
.Concat(Directory.EnumerateFiles(ModSettings.CustomImageFolder, GifSearchPattern));
4546
foreach (var image in imageFiles)
4647
{
4748
AddImage(image);

Patches/LoadingImagePatch.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using CustomLoadingScreens.Managers;
22
using HarmonyLib;
33
using Il2Cpp;
4-
using Il2CppAssets.Scripts.PeroTools.Commons;
5-
using Il2CppInterop.Runtime.InteropTypes.Arrays;
64
using UnityEngine;
75
using UnityEngine.UI;
86
using Logger = CustomLoadingScreens.Utilities.Logger;
@@ -11,45 +9,44 @@ namespace CustomLoadingScreens.Patches
119
{
1210
internal class LoadingImagePatch
1311
{
14-
1512
/// <summary>
1613
/// This HarmonyPatch class does all the work for setting the custom backgrounds and text.
1714
/// </summary>
1815
[HarmonyPatch(typeof(LoadingImg), nameof(LoadingImg.OnEnable))]
1916
internal class LoadingImgPatch
2017
{
2118
private static readonly Logger Logger = new(nameof(LoadingImgPatch));
22-
private static bool Is43AspectRatio(double width, double height) => width % height / height <= 1.7;
19+
private static bool Is43AspectRatio(double width, double height) => width % height / height <= 1.7;
2320
private static void Postfix(ref LoadingImg __instance)
2421
{
2522

2623
// Rolled a custom image and we have a custom image
2724
if (ProbabilityManager.UseCustomImage && CustomDataManager.CustomImages.Any())
2825
{
2926
var customImage = ProbabilityManager.GetRandomImage();
30-
Il2CppArrayBase<Image> components;
27+
Main.CurrentCustomImage = customImage;
3128

3229
__instance.simpleIllus.SetActive(false);
3330
__instance.specialIllus.SetActive(false);
3431
__instance.verySpecialIllus.SetActive(false);
3532
__instance.specialIllusfor43.SetActive(false);
3633

3734
// Deal with the specialIllus variables, respective of aspect ratio
38-
if (Is43AspectRatio(customImage.Sprite.texture.width, customImage.Sprite.texture.height))
35+
if (Is43AspectRatio(customImage.Sprites[0].texture.width, customImage.Sprites[0].texture.height))
3936
{
4037
__instance.specialIllusfor43.SetActive(true);
41-
components = __instance.specialIllusfor43.GetComponents<Image>();
38+
Main.Images = __instance.specialIllusfor43.GetComponents<Image>();
4239
}
4340
else
4441
{
4542
__instance.specialIllus.SetActive(true);
46-
components = __instance.simpleIllus.GetComponents<Image>();
43+
Main.Images = __instance.simpleIllus.GetComponents<Image>();
4744
}
4845

4946
// Sets all image components in the Illus to the custom image
50-
foreach (var image in components)
47+
foreach (var image in Main.Images)
5148
{
52-
image.sprite = customImage.Sprite;
49+
image.sprite = customImage.Sprites[0];
5350
}
5451

5552
// We can only use bound quotes if that image exists, bound quotes will always be displayed
@@ -85,6 +82,24 @@ private static void Postfix(ref LoadingImg __instance)
8582

8683
Logger.Msg("Custom random loading text has been loaded.");
8784
}
85+
86+
/// <summary>
87+
/// Adds support for GIF images.
88+
/// </summary>
89+
internal static void Update()
90+
{
91+
if (Main.CurrentCustomImage is null || Main.Images == null || Main.CurrentCustomImage.FramesPerSecond == 0) return;
92+
93+
var frame = (int)Mathf.Floor(Time.time * 1000) %
94+
(Main.CurrentCustomImage.FramesPerSecond * Main.CurrentCustomImage.FrameCount) / Main.CurrentCustomImage.FramesPerSecond;
95+
96+
// Updates animated loading screens
97+
foreach (var compImage in Main.Images)
98+
{
99+
if (compImage == null) return;
100+
compImage.sprite = Main.CurrentCustomImage.Sprites[frame];
101+
}
102+
}
88103
}
89104
}
90105
}

Properties/AssemblyInfo.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
4-
using CustomLoadingScreens;
53
using MelonLoader;
64

75
// General Information about an assembly is controlled through the following
@@ -16,7 +14,7 @@
1614
[assembly: AssemblyTrademark("")]
1715
[assembly: AssemblyCulture("")]
1816

19-
[assembly: MelonInfo(typeof(CustomLoadingScreens.Main), "CustomLoadingScreens", "1.0.0", "Mr. Talk")]
17+
[assembly: MelonInfo(typeof(CustomLoadingScreens.Main), "CustomLoadingScreens", "1.1.0", "Mr. Talk")]
2018
[assembly: MelonGame("PeroPeroGames", "MuseDash")]
2119

2220
// Setting ComVisible to false makes the types in this assembly not visible
@@ -37,5 +35,5 @@
3735
// You can specify all the values or you can default the Build and Revision Numbers
3836
// by using the '*' as shown below:
3937
// [assembly: AssemblyVersion("1.0.*")]
40-
[assembly: AssemblyVersion("1.0.0.0")]
41-
[assembly: AssemblyFileVersion("1.0.0.0")]
38+
[assembly: AssemblyVersion("1.1.0.0")]
39+
[assembly: AssemblyFileVersion("1.1.0.0")]

Utilities/Logger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void Warning(object message)
2222
_logger.Msg(ConsoleColor.Yellow, "Warning: " + message);
2323
}
2424

25-
public void Error(string message)
25+
public void Error(object message)
2626
{
2727
_logger.Msg(ConsoleColor.Red, "ERROR: " + message);
2828
}

0 commit comments

Comments
 (0)