using System; using System.Reflection; using System.Resources; using MelonLoader; using UnityEngine; namespace ADOFAIRenderer.Util; /// /// Loads the Unity asset bundles (fonts, shaders) embedded in the mod DLL as manifest resources. One bundle set is /// embedded per platform (Bundle/Win64, Bundle/OSX, Bundle/Linux). /// Main thread (AssetBundle loading). /// internal static class BundleLoader { /// Resource folder of the running platform: "Win64" on Windows, "OSX" on macOS, "Linux" otherwise. private static readonly string BundlePlatform; /// The mod assembly that contains the bundle resources. private static readonly Assembly? ExecutingAssembly; static BundleLoader() { BundlePlatform = Application.platform switch { RuntimePlatform.WindowsPlayer => "Win64", RuntimePlatform.OSXPlayer => "OSX", _ => "Linux" }; ExecutingAssembly = Assembly.GetExecutingAssembly(); } /// /// Loads the bundle stored as manifest resource "ADOFAIRenderer.Bundle.<platform>.<resourcePath>" with /// AssetBundle.LoadFromStream. The resource stream is disposed when this method returns. The caller owns the returned /// bundle. /// Reference: https://docs.unity3d.com/ScriptReference/AssetBundle.LoadFromStream.html /// /// Bundle name inside the platform folder, for example "fonts" or "shaders". /// The loaded bundle, or null when Unity fails to load it. /// The resource is not embedded in the DLL. /// The executing assembly is not available. public static AssetBundle GetBundle(string resourcePath) { if (ExecutingAssembly != null) { var bundlePath = $"ADOFAIRenderer.Bundle.{BundlePlatform}.{resourcePath}"; using var resourceStream = ExecutingAssembly.GetManifestResourceStream(bundlePath); if (resourceStream != null) { return AssetBundle.LoadFromStream(resourceStream); } Melon.Logger.Error($"Could not find bundle resource in DLL.\n{bundlePath}"); throw new MissingManifestResourceException("Could not find bundle resource in DLL."); } Melon.Logger.Error("Executing assembly has not been set properly."); throw new InvalidOperationException("ExecutingAssembly has not been set."); } }