1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +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 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 - 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 ## 5.1.0
### MLEM ### MLEM
Additions Additions

View file

@ -59,9 +59,8 @@ namespace MLEM.Data.Content {
foreach (var ext in reader.GetFileExtensions()) { foreach (var ext in reader.GetFileExtensions()) {
var file = Path.Combine(this.RootDirectory, $"{assetName}.{ext}"); var file = Path.Combine(this.RootDirectory, $"{assetName}.{ext}");
triedFiles.Add(file); triedFiles.Add(file);
if (!File.Exists(file)) try {
continue; using (var stream = Path.IsPathRooted(file) ? File.OpenRead(file) : TitleContainer.OpenStream(file)) {
using (var stream = File.OpenRead(file)) {
var read = reader.Read(this, assetName, stream, typeof(T), existing); var read = reader.Read(this, assetName, stream, typeof(T), existing);
if (!(read is T t)) if (!(read is T t))
throw new ContentLoadException($"{reader} returned non-{typeof(T)} for asset {assetName}"); throw new ContentLoadException($"{reader} returned non-{typeof(T)} for asset {assetName}");
@ -70,6 +69,8 @@ namespace MLEM.Data.Content {
this.disposableAssets.Add(d); this.disposableAssets.Add(d);
return t; return t;
} }
} catch (FileNotFoundException) {
}
} }
} }
throw new ContentLoadException($"Asset {assetName} not found. Tried files {string.Join(", ", triedFiles)}"); throw new ContentLoadException($"Asset {assetName} not found. Tried files {string.Join(", ", triedFiles)}");

View file

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using MLEM.Data.Json; using MLEM.Data.Json;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -62,12 +63,12 @@ namespace MLEM.Data {
foreach (var extension in extensions ?? JsonExtensions) { foreach (var extension in extensions ?? JsonExtensions) {
var file = Path.Combine(content.RootDirectory, name + extension); var file = Path.Combine(content.RootDirectory, name + extension);
triedFiles.Add(file); triedFiles.Add(file);
if (!File.Exists(file)) try {
continue; using (var stream = Path.IsPathRooted(file) ? File.OpenText(file) : new StreamReader(TitleContainer.OpenStream(file))) {
using (var stream = File.OpenText(file)) { using (var reader = new JsonTextReader(stream))
using (var reader = new JsonTextReader(stream)) {
return serializerToUse.Deserialize<T>(reader); return serializerToUse.Deserialize<T>(reader);
} }
} catch (FileNotFoundException) {
} }
} }
throw new ContentLoadException($"Asset {name} not found. Tried files {string.Join(", ", triedFiles)}"); 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> /// <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> /// <returns>A new data texture atlas with the given settings</returns>
public static DataTextureAtlas LoadAtlasData(TextureRegion texture, ContentManager content, string infoPath, bool pivotRelative = false) { 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; string text;
using (var reader = info.OpenText()) if (Path.IsPathRooted(info)) {
text = File.ReadAllText(info);
} else {
using (var reader = new StreamReader(TitleContainer.OpenStream(info)))
text = reader.ReadToEnd(); text = reader.ReadToEnd();
}
var atlas = new DataTextureAtlas(texture); var atlas = new DataTextureAtlas(texture);
// parse each texture region: "<name> loc <u> <v> <w> <h> [piv <px> <py>] [off <ox> <oy>]" // parse each texture region: "<name> loc <u> <v> <w> <h> [piv <px> <py>] [off <ox> <oy>]"