From 3c8f7ee5e74587d487659e99e2f84cc4401f4b44 Mon Sep 17 00:00:00 2001 From: Redforce04 <74625280+Redforce04@users.noreply.github.com> Date: Sat, 2 Dec 2023 21:47:29 -0600 Subject: [PATCH 1/2] Initialize the parser. --- LethalAPI.Core/Loader/PluginLoader.cs | 1 + .../Loader/Resources/AssetBundleParser.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 LethalAPI.Core/Loader/Resources/AssetBundleParser.cs diff --git a/LethalAPI.Core/Loader/PluginLoader.cs b/LethalAPI.Core/Loader/PluginLoader.cs index cb964ab..031379d 100644 --- a/LethalAPI.Core/Loader/PluginLoader.cs +++ b/LethalAPI.Core/Loader/PluginLoader.cs @@ -76,6 +76,7 @@ public PluginLoader(LoadMethod loadType) // Ensure that these are registered by loading the reference. _ = new UnknownResourceParser(); _ = new DllParser(); + _ = new BundleParser(); // Instance is stored in the type. _ = new EmbeddedResourceLoader(); diff --git a/LethalAPI.Core/Loader/Resources/AssetBundleParser.cs b/LethalAPI.Core/Loader/Resources/AssetBundleParser.cs new file mode 100644 index 0000000..2362ccb --- /dev/null +++ b/LethalAPI.Core/Loader/Resources/AssetBundleParser.cs @@ -0,0 +1,13 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) LethalAPI Modding Community. All rights reserved. +// Licensed under the LGPL-3.0 license. +// +// ----------------------------------------------------------------------- + +namespace LethalAPI.Core.Loader.Resources; + +public class AssetBundleParser +{ + +} \ No newline at end of file From 75bef6d4c2e54f351ff37b0c1422f2159a72fc04 Mon Sep 17 00:00:00 2001 From: Redforce04 <74625280+Redforce04@users.noreply.github.com> Date: Sat, 2 Dec 2023 21:49:09 -0600 Subject: [PATCH 2/2] Add the parser and rename it. --- LethalAPI.Core/Loader/PluginLoader.cs | 2 +- .../Loader/Resources/AssetBundleParser.cs | 87 ++++++++++++++++++- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/LethalAPI.Core/Loader/PluginLoader.cs b/LethalAPI.Core/Loader/PluginLoader.cs index 031379d..855bfbe 100644 --- a/LethalAPI.Core/Loader/PluginLoader.cs +++ b/LethalAPI.Core/Loader/PluginLoader.cs @@ -76,7 +76,7 @@ public PluginLoader(LoadMethod loadType) // Ensure that these are registered by loading the reference. _ = new UnknownResourceParser(); _ = new DllParser(); - _ = new BundleParser(); + _ = new AssetBundleParser(); // Instance is stored in the type. _ = new EmbeddedResourceLoader(); diff --git a/LethalAPI.Core/Loader/Resources/AssetBundleParser.cs b/LethalAPI.Core/Loader/Resources/AssetBundleParser.cs index 2362ccb..cf38b9d 100644 --- a/LethalAPI.Core/Loader/Resources/AssetBundleParser.cs +++ b/LethalAPI.Core/Loader/Resources/AssetBundleParser.cs @@ -7,7 +7,90 @@ namespace LethalAPI.Core.Loader.Resources; -public class AssetBundleParser +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; + +using JetBrains.Annotations; +using UnityEngine; + +/// +public class AssetBundleParser : ResourceParser { - + /// + /// Initializes a new instance of the class. + /// + internal AssetBundleParser() + { + Instance = this; + } + + /// + /// Gets the instance of this resource parser. + /// + public static ResourceParser Instance { get; private set; } = null!; + + /// + /// Gets a list of all cached asset bundles by name. + /// + /// + /// MyBundle.bundle -> CachedBundles[MyBundle]. + /// + public static Dictionary CachedBundles => new (); + + /// + public override string ExtensionName => "bundle"; + + /// + /// Tries to get an asset bundle from the embedded bundles. + /// + /// The name of the bundle. + /// The returned bundle. + /// True if the bundle was found, false if the bundle could not be found. + public static bool TryGetBundle(string name, [NotNullWhen(true)] out AssetBundle? bundle) + { + bundle = null; + if (CachedBundles.ContainsKey(name)) + { + bundle = CachedBundles[name]; + return true; + } + + return false; + } + + /// + /// Gets a cached asset resource. + /// + /// The name of the asset. + /// The name of the bundle (optional). + /// The type of resource to get. + /// The asset resource if found. Null if not found. + public static T? GetResource(string assetName, string bundleName = "") + where T : Object + { + if (bundleName != string.Empty && !CachedBundles.ContainsKey(bundleName)) + return null; + + List bundles = bundleName == string.Empty ? CachedBundles.Values.ToList() : new() { CachedBundles[bundleName] }; + foreach (AssetBundle bundle in bundles) + { + return bundle.LoadAsset(assetName); + } + + return null; + } + + /// + public override object Parse(MemoryStream stream) + { + return AssetBundle.LoadFromStream(stream); + } + + /// + public override void ResourceFound(EmbeddedResourceData resourceData) + { + CachedBundles.Add(resourceData.FileName.Replace(".bundle", string.Empty), (AssetBundle)Parse(resourceData.GetStream())); + } } \ No newline at end of file