From 65908688f190404e354d33968d068394ab16b1ab Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 16 Oct 2021 20:22:06 +0200 Subject: [PATCH] Use TitleContainer for opening streams where possible --- CHANGELOG.md | 4 ++++ MLEM.Data/Content/RawContentManager.cs | 21 +++++++++++---------- MLEM.Data/ContentExtensions.cs | 11 ++++++----- MLEM.Data/DataTextureAtlas.cs | 10 +++++++--- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca657d4..fd52627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/MLEM.Data/Content/RawContentManager.cs b/MLEM.Data/Content/RawContentManager.cs index 40c57d6..cd4f04b 100644 --- a/MLEM.Data/Content/RawContentManager.cs +++ b/MLEM.Data/Content/RawContentManager.cs @@ -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) { } } } diff --git a/MLEM.Data/ContentExtensions.cs b/MLEM.Data/ContentExtensions.cs index 2801a20..1e109c9 100644 --- a/MLEM.Data/ContentExtensions.cs +++ b/MLEM.Data/ContentExtensions.cs @@ -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(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(reader); } + } catch (FileNotFoundException) { } } throw new ContentLoadException($"Asset {name} not found. Tried files {string.Join(", ", triedFiles)}"); diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index 0dab8d0..e9b8e0b 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -69,10 +69,14 @@ namespace MLEM.Data { /// 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. /// A new data texture atlas with the given settings 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: " loc [piv ] [off ]"