Skip to content

Commit

Permalink
Fix strong lag when loading sprites for the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy-XL committed Mar 2, 2025
1 parent 9777785 commit eb85df8
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions Modules/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AmongUs.GameOptions;
using Hazel;
using Il2CppInterop.Generator.Extensions;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using InnerNet;
using System;
using System.Data;
Expand Down Expand Up @@ -2716,7 +2717,7 @@ public static void FlashColor(Color color, float duration = 1f)
})));
}

public static Dictionary<string, Sprite> CachedSprites = [];
private readonly static Dictionary<string, Sprite> CachedSprites = [];
public static Sprite LoadSprite(string path, float pixelsPerUnit = 1f)
{
try
Expand All @@ -2733,15 +2734,21 @@ public static Sprite LoadSprite(string path, float pixelsPerUnit = 1f)
}
return null;
}
public static Texture2D LoadTextureFromResources(string path)
private static unsafe Texture2D LoadTextureFromResources(string path)
{
try
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
using MemoryStream ms = new();
stream.CopyTo(ms);
ImageConversion.LoadImage(texture, ms.ToArray(), false);
Texture2D texture = new(2, 2, TextureFormat.ARGB32, true);
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(path);
var length = stream.Length;
var byteTexture = new Il2CppStructArray<byte>(length);
stream.Read(new Span<byte>(IntPtr.Add(byteTexture.Pointer, IntPtr.Size * 4).ToPointer(), (int)length));
if (path.Contains("HorseHats"))
{
byteTexture = new Il2CppStructArray<byte>(byteTexture.Reverse().ToArray());
}
ImageConversion.LoadImage(texture, byteTexture, false);
return texture;
}
catch
Expand Down

0 comments on commit eb85df8

Please sign in to comment.