Fix asset bundle issue and made a class of them

This commit is contained in:
Nick 2022-05-13 16:49:29 -04:00
parent e274fbc030
commit eebccceaab
6 changed files with 97 additions and 107 deletions

View File

@ -22,7 +22,7 @@ namespace NewHorizons.Builder.Props
if (detail.assetBundle != null) if (detail.assetBundle != null)
{ {
var prefab = PropBuildManager.LoadPrefab(detail.assetBundle, detail.path, uniqueModName, mod); var prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod);
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal); detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
} }
@ -31,7 +31,7 @@ namespace NewHorizons.Builder.Props
try try
{ {
var prefab = mod.ModHelper.Assets.Get3DObject(detail.objFilePath, detail.mtlFilePath); var prefab = mod.ModHelper.Assets.Get3DObject(detail.objFilePath, detail.mtlFilePath);
PropBuildManager.ReplaceShaders(prefab); AssetBundleUtilities.ReplaceShaders(prefab);
prefab.SetActive(false); prefab.SetActive(false);
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal); detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
} }

View File

@ -13,6 +13,7 @@ using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
using NewHorizons.Builder.ShipLog; using NewHorizons.Builder.ShipLog;
using NewHorizons.External.Configs; using NewHorizons.External.Configs;
using System.IO;
namespace NewHorizons.Builder.Props namespace NewHorizons.Builder.Props
{ {
@ -174,66 +175,5 @@ namespace NewHorizons.Builder.Props
} }
} }
} }
public static GameObject LoadPrefab(string assetBundle, string path, string uniqueModName, IModBehaviour mod)
{
string key = assetBundle;
AssetBundle bundle;
GameObject prefab;
try
{
if (Main.AssetBundles.ContainsKey(key)) bundle = Main.AssetBundles[key];
else
{
var completePath = mod.ModHelper.Manifest.ModFolderPath + assetBundle;
bundle = AssetBundle.LoadFromFile(completePath);
if(bundle == null)
{
Logger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
return null;
}
Main.AssetBundles[key] = bundle;
}
}
catch (Exception e)
{
Logger.LogError($"Couldn't load AssetBundle {assetBundle} : {e.Message}");
return null;
}
try
{
prefab = bundle.LoadAsset<GameObject>(path);
prefab.SetActive(false);
}
catch (Exception e)
{
Logger.LogError($"Couldn't load asset {path} from AssetBundle {assetBundle} : {e.Message}");
return null;
}
ReplaceShaders(prefab);
return prefab;
}
public static void ReplaceShaders(GameObject prefab)
{
foreach (var renderer in prefab.GetComponentsInChildren<Renderer>(true))
{
foreach (var material in renderer.sharedMaterials)
{
if (material == null)
{
continue;
}
var replacementShader = Shader.Find(material.shader.name);
if (replacementShader != null) material.shader = replacementShader;
}
}
}
} }
} }

View File

@ -46,7 +46,7 @@ namespace NewHorizons.Builder.Props
Random.InitState(propInfo.seed); Random.InitState(propInfo.seed);
GameObject prefab; GameObject prefab;
if (propInfo.assetBundle != null) prefab = PropBuildManager.LoadPrefab(propInfo.assetBundle, propInfo.path, uniqueModName, mod); if (propInfo.assetBundle != null) prefab = AssetBundleUtilities.LoadPrefab(propInfo.assetBundle, propInfo.path, mod);
else prefab = GameObject.Find(propInfo.path); else prefab = GameObject.Find(propInfo.path);
for (int i = 0; i < propInfo.count; i++) for (int i = 0; i < propInfo.count; i++)
{ {

View File

@ -10,44 +10,10 @@ namespace NewHorizons.Builder.StarSystem
{ {
public class SkyboxBuilder public class SkyboxBuilder
{ {
public static Material LoadMaterial(string assetBundle, string path, string uniqueModName, IModBehaviour mod)
{
string key = uniqueModName + "." + assetBundle;
AssetBundle bundle;
Material cubemap;
try
{
if (Main.AssetBundles.ContainsKey(key)) bundle = Main.AssetBundles[key];
else
{
bundle = mod.ModHelper.Assets.LoadBundle(assetBundle);
Main.AssetBundles[key] = bundle;
}
}
catch (Exception e)
{
Logger.LogError($"Couldn't load AssetBundle {assetBundle} : {e.Message}");
return null;
}
try
{
cubemap = bundle.LoadAsset<Material>(path);
}
catch (Exception e)
{
Logger.Log($"Couldn't load asset {path} from AssetBundle {assetBundle} : {e.Message}");
return null;
}
return cubemap;
}
public static void Make(StarSystemConfig.SkyboxConfig info, IModBehaviour mod) public static void Make(StarSystemConfig.SkyboxConfig info, IModBehaviour mod)
{ {
Logger.Log("Building Skybox"); Logger.Log("Building Skybox");
Material skyBoxMaterial = LoadMaterial(info.assetBundle, info.path, mod.ModHelper.Manifest.UniqueName, mod); var skyBoxMaterial = AssetBundleUtilities.Load<Material>(info.assetBundle, info.path, mod);
RenderSettings.skybox = skyBoxMaterial; RenderSettings.skybox = skyBoxMaterial;
DynamicGI.UpdateEnvironment(); DynamicGI.UpdateEnvironment();
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() =>

View File

@ -42,7 +42,6 @@ namespace NewHorizons
public static Dictionary<string, NewHorizonsSystem> SystemDict = new Dictionary<string, NewHorizonsSystem>(); public static Dictionary<string, NewHorizonsSystem> SystemDict = new Dictionary<string, NewHorizonsSystem>();
public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new Dictionary<string, List<NewHorizonsBody>>(); public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new Dictionary<string, List<NewHorizonsBody>>();
public static Dictionary<string, AssetBundle> AssetBundles = new Dictionary<string, AssetBundle>();
public static List<IModBehaviour> MountedAddons = new List<IModBehaviour>(); public static List<IModBehaviour> MountedAddons = new List<IModBehaviour>();
public static float SecondsLeftInLoop = -1; public static float SecondsLeftInLoop = -1;
@ -115,13 +114,6 @@ namespace NewHorizons
} }
}; };
foreach (var pair in AssetBundles)
{
if (pair.Value == null) Logger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
else pair.Value.Unload(true);
}
AssetBundles.Clear();
if (!resetTranslation) return; if (!resetTranslation) return;
TranslationHandler.ClearTables(); TranslationHandler.ClearTables();
TextTranslation.Get().SetLanguage(TextTranslation.Get().GetLanguage()); TextTranslation.Get().SetLanguage(TextTranslation.Get().GetLanguage());
@ -180,6 +172,7 @@ namespace NewHorizons
SearchUtilities.ClearCache(); SearchUtilities.ClearCache();
ImageUtilities.ClearCache(); ImageUtilities.ClearCache();
AudioUtilities.ClearCache(); AudioUtilities.ClearCache();
AssetBundleUtilities.ClearCache();
IsSystemReady = false; IsSystemReady = false;
} }

View File

@ -0,0 +1,91 @@
using OWML.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace NewHorizons.Utility
{
public static class AssetBundleUtilities
{
public static Dictionary<string, AssetBundle> AssetBundles = new Dictionary<string, AssetBundle>();
public static void ClearCache()
{
foreach (var pair in AssetBundles)
{
if (pair.Value == null) Logger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
else pair.Value.Unload(true);
}
AssetBundles.Clear();
}
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
{
string key = Path.GetFileName(assetBundleRelativeDir);
T obj;
try
{
AssetBundle bundle;
if (AssetBundles.ContainsKey(key))
{
bundle = AssetBundles[key];
}
else
{
var completePath = mod.ModHelper.Manifest.ModFolderPath + assetBundleRelativeDir;
bundle = AssetBundle.LoadFromFile(completePath);
if (bundle == null)
{
Logger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
return null;
}
AssetBundles[key] = bundle;
}
obj = bundle.LoadAsset<T>(pathInBundle);
}
catch (Exception e)
{
Logger.LogError($"Couldn't load asset {pathInBundle} from AssetBundle {assetBundleRelativeDir} : {e.Message}");
return null;
}
return obj;
}
public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod)
{
var prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod);
prefab.SetActive(false);
ReplaceShaders(prefab);
return prefab;
}
public static void ReplaceShaders(GameObject prefab)
{
foreach (var renderer in prefab.GetComponentsInChildren<Renderer>(true))
{
foreach (var material in renderer.sharedMaterials)
{
if (material == null)
{
continue;
}
var replacementShader = Shader.Find(material.shader.name);
if (replacementShader != null) material.shader = replacementShader;
}
}
}
}
}