Preloading bundles and setting subtitle path

This commit is contained in:
Nick 2024-02-03 16:09:50 -05:00
parent 0528360cc0
commit 1a56fe1ab2
5 changed files with 438 additions and 388 deletions

View File

@ -31,5 +31,17 @@ namespace NewHorizons.External.Configs
/// If popupMessage is set, should it repeat every time the game starts or only once /// If popupMessage is set, should it repeat every time the game starts or only once
/// </summary> /// </summary>
public bool repeatPopup; public bool repeatPopup;
/// <summary>
/// These asset bundles will be loaded on the title screen and stay loaded. Will improve initial load time at the cost of increased memory use.
/// The path is the relative directory of the asset bundle in the mod folder.
/// </summary>
public string[] preloadAssetBundles;
/// <summary>
/// The path to the addons subtitle for the main menu.
/// Defaults to "subtitle.png".
/// </summary>
public string subtitlePath = "subtitle.png";
} }
} }

View File

@ -61,11 +61,19 @@ namespace NewHorizons.Handlers
private void AddSubtitles() private void AddSubtitles()
{ {
foreach (var mod in Main.MountedAddons.Where(mod => File.Exists($"{mod.ModHelper.Manifest.ModFolderPath}subtitle.png"))) foreach (var mod in Main.MountedAddons)
{
if (Main.AddonConfigs.TryGetValue(mod, out var addonConfig) && File.Exists(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, addonConfig.subtitlePath)))
{
AddSubtitle(mod, addonConfig.subtitlePath);
}
// Else default to subtitle.png
else if (File.Exists(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, "subtitle.png")))
{ {
AddSubtitle(mod, "subtitle.png"); AddSubtitle(mod, "subtitle.png");
} }
} }
}
public void AddSubtitle(IModBehaviour mod, string filepath) public void AddSubtitle(IModBehaviour mod, string filepath)
{ {

View File

@ -50,9 +50,10 @@ namespace NewHorizons
private static bool _wasConfigured = false; private static bool _wasConfigured = false;
private static string _defaultSystemOverride; private static string _defaultSystemOverride;
public static Dictionary<string, NewHorizonsSystem> SystemDict = new Dictionary<string, NewHorizonsSystem>(); public static Dictionary<string, NewHorizonsSystem> SystemDict = new();
public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new Dictionary<string, List<NewHorizonsBody>>(); public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new();
public static List<IModBehaviour> MountedAddons = new List<IModBehaviour>(); public static List<IModBehaviour> MountedAddons = new();
public static Dictionary<IModBehaviour, AddonConfig> AddonConfigs = new();
public static float SecondsElapsedInLoop = -1; public static float SecondsElapsedInLoop = -1;
@ -747,6 +748,15 @@ namespace NewHorizons
{ {
MenuHandler.RegisterOneTimePopup(mod, TranslationHandler.GetTranslation(addonConfig.popupMessage, TranslationHandler.TextType.UI), addonConfig.repeatPopup); MenuHandler.RegisterOneTimePopup(mod, TranslationHandler.GetTranslation(addonConfig.popupMessage, TranslationHandler.TextType.UI), addonConfig.repeatPopup);
} }
if (addonConfig.preloadAssetBundles != null)
{
foreach (var bundle in addonConfig.preloadAssetBundles)
{
AssetBundleUtilities.PreloadBundle(bundle, mod);
}
}
AddonConfigs[mod] = addonConfig;
} }
private void LoadTranslations(string folder, IModBehaviour mod) private void LoadTranslations(string folder, IModBehaviour mod)

View File

@ -26,8 +26,7 @@ using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using static NewHorizons.External.Modules.ShipLogModule; using static NewHorizons.External.Modules.ShipLogModule;
namespace NewHorizons namespace NewHorizons;
{
public class NewHorizonsApi : INewHorizons public class NewHorizonsApi : INewHorizons
{ {
@ -338,4 +337,3 @@ namespace NewHorizons
public string GetTranslationForOtherText(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.OTHER); public string GetTranslationForOtherText(string text) => TranslationHandler.GetTranslation(text, TranslationHandler.TextType.OTHER);
} }
}

View File

@ -3,22 +3,45 @@ using OWML.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using UnityEngine; using UnityEngine;
namespace NewHorizons.Utility.Files namespace NewHorizons.Utility.Files;
{
public static class AssetBundleUtilities public static class AssetBundleUtilities
{ {
public static Dictionary<string, AssetBundle> AssetBundles = new Dictionary<string, AssetBundle>(); public static Dictionary<string, (AssetBundle bundle, bool keepLoaded)> AssetBundles = new Dictionary<string, (AssetBundle, bool)>();
public static void ClearCache() public static void ClearCache()
{ {
foreach (var pair in AssetBundles) foreach (var pair in AssetBundles)
{ {
if (pair.Value == null) NHLogger.LogError($"The asset bundle for {pair.Key} was null when trying to unload"); if (!pair.Value.keepLoaded)
else pair.Value.Unload(true); {
if (pair.Value.bundle == null)
{
NHLogger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
} }
AssetBundles.Clear(); else
{
pair.Value.bundle.Unload(true);
}
}
}
AssetBundles = AssetBundles.Where(x => x.Value.keepLoaded).ToDictionary(x => x.Key, x => x.Value);
}
public static void PreloadBundle(string assetBundleRelativeDir, IModBehaviour mod)
{
string key = Path.GetFileName(assetBundleRelativeDir);
var completePath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, assetBundleRelativeDir);
var request = AssetBundle.LoadFromFileAsync(completePath);
request.completed += _ =>
{
NHLogger.Log($"Finished loading async bundle {assetBundleRelativeDir}");
AssetBundles[key] = (request.assetBundle, true);
};
} }
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
@ -32,7 +55,7 @@ namespace NewHorizons.Utility.Files
if (AssetBundles.ContainsKey(key)) if (AssetBundles.ContainsKey(key))
{ {
bundle = AssetBundles[key]; bundle = AssetBundles[key].bundle;
} }
else else
{ {
@ -44,7 +67,7 @@ namespace NewHorizons.Utility.Files
return null; return null;
} }
AssetBundles[key] = bundle; AssetBundles[key] = (bundle, false);
} }
obj = bundle.LoadAsset<T>(pathInBundle); obj = bundle.LoadAsset<T>(pathInBundle);
@ -124,4 +147,3 @@ namespace NewHorizons.Utility.Files
} }
} }
} }
}