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

Use TitleContainer for opening streams where possible

This commit is contained in:
Ell 2021-10-16 20:22:06 +02:00
parent 45f970e0f2
commit 65908688f1
4 changed files with 28 additions and 18 deletions

View file

@ -24,6 +24,10 @@ Fixes
- Fixed VerticalSpace height parameter being an integer
- Fixed text not being pasted into a text field at all if it contains characters that don't match the input rule
### MLEM.Data
Improvements
- Use TitleContainer for opening streams where possible
## 5.1.0
### MLEM
Additions

View file

@ -59,16 +59,17 @@ namespace MLEM.Data.Content {
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;
try {
using (var stream = Path.IsPathRooted(file) ? File.OpenRead(file) : TitleContainer.OpenStream(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;
}
} catch (FileNotFoundException) {
}
}
}

View file

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using MLEM.Data.Json;
using Newtonsoft.Json;
@ -62,12 +63,12 @@ namespace MLEM.Data {
foreach (var extension in extensions ?? JsonExtensions) {
var file = Path.Combine(content.RootDirectory, name + extension);
triedFiles.Add(file);
if (!File.Exists(file))
continue;
using (var stream = File.OpenText(file)) {
using (var reader = new JsonTextReader(stream)) {
return serializerToUse.Deserialize<T>(reader);
try {
using (var stream = Path.IsPathRooted(file) ? File.OpenText(file) : new StreamReader(TitleContainer.OpenStream(file))) {
using (var reader = new JsonTextReader(stream))
return serializerToUse.Deserialize<T>(reader);
}
} catch (FileNotFoundException) {
}
}
throw new ContentLoadException($"Asset {name} not found. Tried files {string.Join(", ", triedFiles)}");

View file

@ -69,10 +69,14 @@ namespace MLEM.Data {
/// <param name="pivotRelative">If this value is true, then the pivot points passed in the info file will be relative to the coordinates of the texture region, not relative to the entire texture's origin.</param>
/// <returns>A new data texture atlas with the given settings</returns>
public static DataTextureAtlas LoadAtlasData(TextureRegion texture, ContentManager content, string infoPath, bool pivotRelative = false) {
var info = new FileInfo(Path.Combine(content.RootDirectory, infoPath));
var info = Path.Combine(content.RootDirectory, infoPath);
string text;
using (var reader = info.OpenText())
text = reader.ReadToEnd();
if (Path.IsPathRooted(info)) {
text = File.ReadAllText(info);
} else {
using (var reader = new StreamReader(TitleContainer.OpenStream(info)))
text = reader.ReadToEnd();
}
var atlas = new DataTextureAtlas(texture);
// parse each texture region: "<name> loc <u> <v> <w> <h> [piv <px> <py>] [off <ox> <oy>]"