From 2231b510b4651cf29c32af993b597bc81cf68442 Mon Sep 17 00:00:00 2001 From: Krzyhau Date: Sun, 22 Jan 2023 15:22:50 +0100 Subject: [PATCH] refactor assets loading --- FEZ.HAT.mm.csproj | 2 +- Source/{AssetsHelper.cs => Asset.cs} | 56 +++++++++++++++++----------- Source/Mod.cs | 10 ++--- 3 files changed, 40 insertions(+), 28 deletions(-) rename Source/{AssetsHelper.cs => Asset.cs} (65%) diff --git a/FEZ.HAT.mm.csproj b/FEZ.HAT.mm.csproj index 1250e54..e68d4be 100644 --- a/FEZ.HAT.mm.csproj +++ b/FEZ.HAT.mm.csproj @@ -96,7 +96,7 @@ - + diff --git a/Source/AssetsHelper.cs b/Source/Asset.cs similarity index 65% rename from Source/AssetsHelper.cs rename to Source/Asset.cs index 903f62b..d84734d 100644 --- a/Source/AssetsHelper.cs +++ b/Source/Asset.cs @@ -1,4 +1,5 @@ -using FezEngine.Tools; +using FezEngine.Services; +using FezEngine.Tools; using System; using System.Collections.Generic; using System.IO; @@ -8,18 +9,41 @@ namespace HatModLoader.Source { - public static class AssetsHelper + public class Asset { - public static void InjectAsset(string path, byte[] data) + public string AssetPath { get; private set; } + public string Extension { get; private set; } + public byte[] Data { get; private set; } + + public bool Converted { get; private set; } + public bool IsMusicFile { get; private set; } + + public Asset(string path, string extension, byte[] data) + { + AssetPath = path; + Extension = extension; + Data = data; + + TryConvertAsset(); + } + + private void TryConvertAsset() + { + // TODO: special conversion handling for different types, like images or animation + + Converted = false; + } + + public void Inject() { var cachedAssetsField = typeof(MemoryContentManager).GetField("cachedAssets", BindingFlags.NonPublic | BindingFlags.Static); var cachedAssets = cachedAssetsField.GetValue(null) as Dictionary; - cachedAssets[path] = data; + cachedAssets[AssetPath] = Data; } - public static Dictionary LoadDirectory(string directoryPath) + public static List LoadDirectory(string directoryPath) { - var assets = new Dictionary(); + var assets = new List(); foreach (var path in Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories)) { @@ -31,16 +55,16 @@ public static Dictionary LoadDirectory(string directoryPath) if (extension.Length > 0) { byte[] bytes = File.ReadAllBytes(path); - assets.Add(relativePath, ConvertFile(bytes, extension)); + assets.Add(new Asset(relativePath, extension, bytes)); } } return assets; } - public static Dictionary LoadZip(ZipArchive archive, string assetsDirectory) + public static List LoadZip(ZipArchive archive, string assetsDirectory) { - var assets = new Dictionary(); + var assets = new List(); foreach (var zipEntry in archive.Entries.Where(e => e.FullName.StartsWith(assetsDirectory, StringComparison.OrdinalIgnoreCase))) { @@ -55,23 +79,11 @@ public static Dictionary LoadZip(ZipArchive archive, string asse byte[] bytes = new byte[zipFile.Length]; zipFile.Read(bytes, 0, bytes.Length); - assets.Add(relativePath, ConvertFile(bytes, extension)); + assets.Add(new Asset(relativePath, extension, bytes)); } } return assets; } - - public static byte[] ConvertFile(byte[] original, string extension) - { - // TODO: special conversion handling for different types, like images or animation - - if (extension == ".xnb") - { - return original; - } - - return original; - } } } diff --git a/Source/Mod.cs b/Source/Mod.cs index 842324c..3f15f7b 100644 --- a/Source/Mod.cs +++ b/Source/Mod.cs @@ -63,7 +63,7 @@ public struct Dependency public string DirectoryName { get; private set; } public List Dependencies { get; private set; } public bool IsZip { get; private set; } - public Dictionary Assets { get; private set; } + public List Assets { get; private set; } public List Components { get; private set; } public bool IsAssetMod => Assets.Count > 0; @@ -75,7 +75,7 @@ public Mod(Hat modLoader) RawAssembly = null; Assembly = null; - Assets = new Dictionary(); + Assets = new List(); Components = new List(); Dependencies = new List(); } @@ -86,7 +86,7 @@ public void InitializeAssets() // override custom assets foreach (var asset in Assets) { - AssetsHelper.InjectAsset(asset.Key, asset.Value); + asset.Inject(); } } @@ -262,7 +262,7 @@ public static bool TryLoadFromDirectory(Hat modLoader, string directoryName, out var relativeDirName = new DirectoryInfo(path).Name; if (relativeDirName.Equals(AssetsDirectoryName, StringComparison.OrdinalIgnoreCase)) { - mod.Assets = AssetsHelper.LoadDirectory(path); + mod.Assets = Asset.LoadDirectory(path); break; } } @@ -311,7 +311,7 @@ public static bool TryLoadFromZip(Hat modLoader, string zipName, out Mod mod) { if (zipEntry.FullName.StartsWith(AssetsDirectoryName, StringComparison.OrdinalIgnoreCase)) { - mod.Assets = AssetsHelper.LoadZip(archive, AssetsDirectoryName); + mod.Assets = Asset.LoadZip(archive, AssetsDirectoryName); break; } }