Skip to content

Commit ada9cdd

Browse files
committed
fix: Add AssetBundle caching and fix overrideAssembly parameter
1 parent 86dc403 commit ada9cdd

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

S1API/AssetBundles/AssetLoader.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Reflection;
34

45
#if IL2CPPBEPINEX || IL2CPPMELON
@@ -18,21 +19,24 @@ namespace S1API.AssetBundles
1819
public static class AssetLoader
1920
{
2021
private static readonly Log _logger = new Log("AssetLoader");
22+
private static readonly Dictionary<string, WrappedAssetBundle> _cachedAssetBundles = new Dictionary<string, WrappedAssetBundle>();
2123

2224
#if IL2CPPMELON || IL2CPPBEPINEX
2325
/// <summary>
2426
/// Loads an Il2Cpp AssetBundle from an embedded resource stream by name.
2527
/// </summary>
2628
/// <param name="fullResourceName">The full embedded resource name (including namespace path).</param>
29+
/// <param name="overrideAssembly">The assembly to load the embedded resource from.</param>
2730
/// <returns>The loaded Il2CppAssetBundle, or throws on failure.</returns>
28-
public static WrappedAssetBundle GetAssetBundleFromStream(string fullResourceName)
31+
public static WrappedAssetBundle GetAssetBundleFromStream(string fullResourceName, Assembly overrideAssembly)
2932
{
30-
// Attempt to find the embedded resource in the executing assembly
31-
Assembly assembly = Assembly.GetExecutingAssembly();
33+
if (_cachedAssetBundles.TryGetValue(fullResourceName, out WrappedAssetBundle cachedWrappedAssetBundle))
34+
return cachedWrappedAssetBundle;
3235

33-
using Stream? stream = assembly.GetManifestResourceStream(fullResourceName);
36+
// Attempt to find the embedded resource in the executing assembly
37+
using Stream? stream = overrideAssembly.GetManifestResourceStream(fullResourceName);
3438
if (stream == null)
35-
throw new Exception($"Embedded resource '{fullResourceName}' not found in {assembly.FullName}."); // hoping these throws will be melon/bepinex-agnostic
39+
throw new Exception($"Embedded resource '{fullResourceName}' not found in {overrideAssembly.FullName}."); // hoping these throws will be melon/bepinex-agnostic
3640

3741
// Read the stream into a byte array
3842
byte[] data = new byte[stream.Length];
@@ -43,20 +47,29 @@ public static WrappedAssetBundle GetAssetBundleFromStream(string fullResourceNam
4347
if (bundle == null)
4448
throw new Exception($"Failed to load AssetBundle from memory: {fullResourceName}");
4549

46-
return new WrappedAssetBundle(bundle);
50+
WrappedAssetBundle wrappedAssetBundle = new WrappedAssetBundle(bundle);
51+
_cachedAssetBundles.TryAdd(fullResourceName, wrappedAssetBundle);
52+
return wrappedAssetBundle;
4753
}
4854
#elif MONOMELON || MONOBEPINEX
4955
/// <summary>
5056
/// Load a <see cref="WrappedAssetBundle"/> instance by <see cref="string"/> resource name.
5157
/// </summary>
5258
/// <param name="fullResourceName">The full embedded resource name (including namespace path);</param>
59+
/// <param name="overrideAssembly">The assembly to load the embedded resource from.</param>
5360
/// <returns>The loaded AssetBundle instance</returns>
54-
public static WrappedAssetBundle GetAssetBundleFromStream(string fullResourceName)
61+
public static WrappedAssetBundle GetAssetBundleFromStream(string fullResourceName, Assembly overrideAssembly)
5562
{
63+
// Attempt to retrieve the cached asset bundle
64+
if (_cachedAssetBundles.TryGetValue(fullResourceName, out WrappedAssetBundle cachedWrappedAssetBundle))
65+
return cachedWrappedAssetBundle;
66+
5667
// Attempt to find the embedded resource in the executing assembly
57-
var assembly = Assembly.GetExecutingAssembly();
58-
var stream = assembly.GetManifestResourceStream(fullResourceName);
59-
return new WrappedAssetBundle(AssetBundle.LoadFromStream(stream));
68+
var stream = overrideAssembly.GetManifestResourceStream(fullResourceName);
69+
70+
WrappedAssetBundle wrappedAssetBundle = new WrappedAssetBundle(AssetBundle.LoadFromStream(stream));
71+
_cachedAssetBundles.TryAdd(fullResourceName, wrappedAssetBundle);
72+
return wrappedAssetBundle;
6073
}
6174
#endif
6275

@@ -110,7 +123,7 @@ public static T EasyLoad<T>(string bundleName, string objectName, Assembly assem
110123
public static T EasyLoad<T>(string bundleName, string objectName, Assembly assemblyOverride, out WrappedAssetBundle bundle) where T : Object
111124
{
112125
// Get the asset bundle from the assembly
113-
bundle = GetAssetBundleFromStream($"{assemblyOverride.GetName().Name}.{bundleName}");
126+
bundle = GetAssetBundleFromStream($"{assemblyOverride.GetName().Name}.{bundleName}", assemblyOverride);
114127

115128
// Load the asset from the bundle
116129
return bundle.LoadAsset<T>(objectName);

0 commit comments

Comments
 (0)