Skip to content

Commit

Permalink
refactor assets loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzyhau committed Jan 22, 2023
1 parent 807622a commit 2231b51
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion FEZ.HAT.mm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<Compile Include="Patches\StaticText.cs" />
<Compile Include="Properties\MonoModRules.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\AssetsHelper.cs" />
<Compile Include="Source\Asset.cs" />
<Compile Include="Source\Hat.cs" />
<Compile Include="Source\Mod.cs" />
</ItemGroup>
Expand Down
56 changes: 34 additions & 22 deletions Source/AssetsHelper.cs → Source/Asset.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FezEngine.Tools;
using FezEngine.Services;
using FezEngine.Tools;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -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<string, byte[]>;
cachedAssets[path] = data;
cachedAssets[AssetPath] = Data;
}

public static Dictionary<string, byte[]> LoadDirectory(string directoryPath)
public static List<Asset> LoadDirectory(string directoryPath)
{
var assets = new Dictionary<string, byte[]>();
var assets = new List<Asset>();

foreach (var path in Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories))
{
Expand All @@ -31,16 +55,16 @@ public static Dictionary<string, byte[]> 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<string, byte[]> LoadZip(ZipArchive archive, string assetsDirectory)
public static List<Asset> LoadZip(ZipArchive archive, string assetsDirectory)
{
var assets = new Dictionary<string, byte[]>();
var assets = new List<Asset>();

foreach (var zipEntry in archive.Entries.Where(e => e.FullName.StartsWith(assetsDirectory, StringComparison.OrdinalIgnoreCase)))
{
Expand All @@ -55,23 +79,11 @@ public static Dictionary<string, byte[]> 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;
}
}
}
10 changes: 5 additions & 5 deletions Source/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public struct Dependency
public string DirectoryName { get; private set; }
public List<Dependency> Dependencies { get; private set; }
public bool IsZip { get; private set; }
public Dictionary<string, byte[]> Assets { get; private set; }
public List<Asset> Assets { get; private set; }
public List<IGameComponent> Components { get; private set; }

public bool IsAssetMod => Assets.Count > 0;
Expand All @@ -75,7 +75,7 @@ public Mod(Hat modLoader)

RawAssembly = null;
Assembly = null;
Assets = new Dictionary<string, byte[]>();
Assets = new List<Asset>();
Components = new List<IGameComponent>();
Dependencies = new List<Dependency>();
}
Expand All @@ -86,7 +86,7 @@ public void InitializeAssets()
// override custom assets
foreach (var asset in Assets)
{
AssetsHelper.InjectAsset(asset.Key, asset.Value);
asset.Inject();
}
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 2231b51

Please sign in to comment.