1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-24 17:29:23 +01:00

Fixed some content readers not being tried for RawContentManager

This commit is contained in:
Ell 2020-09-17 01:59:08 +02:00
parent df5a654500
commit 2342eb093a
2 changed files with 18 additions and 22 deletions

View file

@ -4,7 +4,7 @@ using Newtonsoft.Json;
namespace MLEM.Data.Content {
/// <inheritdoc />
public class RawJsonReader : RawContentReader {
public class JsonReader : RawContentReader {
/// <inheritdoc />
public override bool CanRead(Type t) {

View file

@ -49,35 +49,31 @@ namespace MLEM.Data.Content {
return this.Read<T>(assetName, default);
}
private static RawContentReader GetReader<T>() {
var reader = Readers.FirstOrDefault(r => r.CanRead(typeof(T)));
if (reader == null)
throw new ContentLoadException($"{typeof(T)} has no RawContentReader");
return reader;
}
/// <inheritdoc/>
protected override void ReloadAsset<T>(string originalAssetName, T currentAsset) {
this.Read(originalAssetName, currentAsset);
}
private T Read<T>(string assetName, T existing) {
var reader = GetReader<T>();
foreach (var ext in reader.GetFileExtensions()) {
var file = new FileInfo(Path.Combine(this.RootDirectory, $"{assetName}.{ext}"));
if (!file.Exists)
continue;
using (var stream = file.OpenRead()) {
var read = reader.Read(this, assetName, stream, typeof(T), existing);
if (!(read is T t))
throw new ContentLoadException($"{reader} returned non-{typeof(T)} for asset {assetName}");
this.LoadedAssets[assetName] = t;
if (t is IDisposable d && !this.disposableAssets.Contains(d))
this.disposableAssets.Add(d);
return t;
var triedFiles = new List<string>();
foreach (var reader in Readers.Where(r => r.CanRead(typeof(T)))) {
foreach (var ext in reader.GetFileExtensions()) {
var file = Path.Combine(this.RootDirectory, $"{assetName}.{ext}");
triedFiles.Add(file);
if (!File.Exists(file))
continue;
using (var stream = File.OpenRead(file)) {
var read = reader.Read(this, assetName, stream, typeof(T), existing);
if (!(read is T t))
throw new ContentLoadException($"{reader} returned non-{typeof(T)} for asset {assetName}");
this.LoadedAssets[assetName] = t;
if (t is IDisposable d && !this.disposableAssets.Contains(d))
this.disposableAssets.Add(d);
return t;
}
}
}
throw new ContentLoadException($"Asset {assetName} not found");
throw new ContentLoadException($"Asset {assetName} not found. Tried files {string.Join(", ", triedFiles)}");
}
/// <inheritdoc/>