From 183b0b76babbc4f41e32bb3d7e3601b495a62b14 Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Wed, 18 Oct 2023 02:21:08 -0500 Subject: [PATCH] Deleted some methods that complicated the common file interface and switched them into extension methods. --- .../Fin/Fin/src/io/FileHierarchy.cs | 17 ---- FinModelUtility/Fin/Fin/src/io/FinFile.cs | 56 ----------- .../Fin/Fin/src/io/FinFileExtensions.cs | 98 +++++++++++++++++++ .../Fin/Fin/src/io/Interfaces_Generic.cs | 26 +---- .../Formats/Ast/Ast/src/api/AstAudioReader.cs | 1 + .../Formats/Dat/src/api/DatModelImporter.cs | 1 + .../J3d/J3d/src/api/BmdModelImporter.cs | 1 + .../Level5/Level5/src/api/XcModelImporter.cs | 1 + .../Mod/Mod/src/api/ModModelImporter.cs | 1 + .../Pikmin2/src/api/Pikmin2SceneImporter.cs | 3 +- .../UniversalAssetTool/src/config/Config.cs | 1 + .../src/scene/IScaleSource.cs | 1 + .../api/OotModelImporter.cs | 1 + 13 files changed, 109 insertions(+), 99 deletions(-) create mode 100644 FinModelUtility/Fin/Fin/src/io/FinFileExtensions.cs diff --git a/FinModelUtility/Fin/Fin/src/io/FileHierarchy.cs b/FinModelUtility/Fin/Fin/src/io/FileHierarchy.cs index f24117620..5bdcef4b9 100644 --- a/FinModelUtility/Fin/Fin/src/io/FileHierarchy.cs +++ b/FinModelUtility/Fin/Fin/src/io/FileHierarchy.cs @@ -380,23 +380,6 @@ public string DisplayFullPath => $"//{this.Hierarchy.Name}{this.LocalPath.Replace('\\', '/')}"; public FileSystemStream OpenRead() => this.Impl.OpenRead(); - public StreamReader OpenReadAsText() => this.Impl.OpenReadAsText(); - - public T ReadNew() where T : IBinaryDeserializable, new() - => this.Impl.ReadNew(); - - public T ReadNew(Endianness endianness) - where T : IBinaryDeserializable, new() - => this.Impl.ReadNew(endianness); - - public T ReadNewFromText() where T : ITextDeserializable, new() - => this.Impl.ReadNewFromText(); - - public byte[] ReadAllBytes() => this.Impl.ReadAllBytes(); - public string ReadAllText() => this.Impl.ReadAllText(); - public string[] ReadAllLines() => this.Impl.ReadAllLines(); - - public T Deserialize() => this.Impl.Deserialize(); } public IEnumerator GetEnumerator() { diff --git a/FinModelUtility/Fin/Fin/src/io/FinFile.cs b/FinModelUtility/Fin/Fin/src/io/FinFile.cs index e9420df57..545f20a54 100644 --- a/FinModelUtility/Fin/Fin/src/io/FinFile.cs +++ b/FinModelUtility/Fin/Fin/src/io/FinFile.cs @@ -112,72 +112,16 @@ public ISystemFile CloneWithFileType(string newExtension) { return new FinFile(this.FullNameWithoutExtension + newExtension); } - // JSON Serialization - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T Deserialize() => JsonUtil.Deserialize(this.ReadAllText()); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Serialize(T instance) where T : notnull - => this.WriteAllText(JsonUtil.Serialize(instance)); // Read methods [MethodImpl(MethodImplOptions.AggressiveInlining)] public FileSystemStream OpenRead() => FinFileStatic.OpenRead(this.FullPath); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public StreamReader OpenReadAsText() => new(this.OpenRead()); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T ReadNew() where T : IBinaryDeserializable, new() { - using var er = new EndianBinaryReader(this.OpenRead()); - return er.ReadNew(); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T ReadNew(Endianness endianness) - where T : IBinaryDeserializable, new() { - using var er = new EndianBinaryReader(this.OpenRead(), endianness); - return er.ReadNew(); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T ReadNewFromText() where T : ITextDeserializable, new() { - using var er = new TextReader(this.OpenRead()); - return er.ReadNew(); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public byte[] ReadAllBytes() - => FinFileSystem.File.ReadAllBytes(this.FullPath); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public string ReadAllText() - => FinFileSystem.File.ReadAllText(this.FullPath); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public string[] ReadAllLines() - => FinFileSystem.File.ReadAllLines(this.FullPath); - // Write methods [MethodImpl(MethodImplOptions.AggressiveInlining)] public FileSystemStream OpenWrite() => FinFileStatic.OpenWrite(this.FullPath); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public StreamWriter OpenWriteAsText() => new(this.OpenWrite()); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteAllBytes(ReadOnlyMemory bytes) { - using var s = this.OpenWrite(); - s.Write(bytes.Span); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteAllText(string text) { - using var sw = this.OpenWriteAsText(); - sw.Write(text); - } } } \ No newline at end of file diff --git a/FinModelUtility/Fin/Fin/src/io/FinFileExtensions.cs b/FinModelUtility/Fin/Fin/src/io/FinFileExtensions.cs new file mode 100644 index 000000000..3d5a0e1ab --- /dev/null +++ b/FinModelUtility/Fin/Fin/src/io/FinFileExtensions.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Abstractions; +using System.Linq; +using System.Runtime.CompilerServices; + +using fin.util.asserts; +using fin.util.json; + +using schema.binary; +using schema.text; +using schema.text.reader; +using schema.util.strings; + +using TextReader = schema.text.reader.TextReader; + +namespace fin.io { + public static class FinFileExtensions { + // JSON Serialization + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Deserialize(this IReadOnlyGenericFile file) + => JsonUtil.Deserialize(file.ReadAllText()); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Serialize(this IGenericFile file, T instance) + where T : notnull + => file.WriteAllText(JsonUtil.Serialize(instance)); + + + // Read methods + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static StreamReader OpenReadAsText(this IReadOnlyGenericFile file) + => new(file.OpenRead()); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T ReadNew(this IReadOnlyGenericFile file) + where T : IBinaryDeserializable, new() { + using var er = new EndianBinaryReader(file.OpenRead()); + return er.ReadNew(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T ReadNew(this IReadOnlyGenericFile file, + Endianness endianness) + where T : IBinaryDeserializable, new() { + using var er = new EndianBinaryReader(file.OpenRead(), endianness); + return er.ReadNew(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T ReadNewFromText(this IReadOnlyGenericFile file) + where T : ITextDeserializable, new() { + using var er = new TextReader(file.OpenRead()); + return er.ReadNew(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static byte[] ReadAllBytes(this IReadOnlyGenericFile file) { + using var s = file.OpenRead(); + using var ms = new MemoryStream(); + s.CopyTo(ms); + return ms.ToArray(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string ReadAllText(this IReadOnlyGenericFile file) { + using var sr = file.OpenReadAsText(); + return sr.ReadToEnd(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string[] ReadAllLines(this IReadOnlyGenericFile file) + => file.ReadAllText() + .Split(TextReaderConstants.NEWLINE_STRINGS, + StringSplitOptions.None); + + + // Write methods + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static StreamWriter OpenWriteAsText(this IGenericFile file) + => new(file.OpenWrite()); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteAllBytes(this IGenericFile file, + ReadOnlyMemory bytes) { + using var s = file.OpenWrite(); + s.Write(bytes.Span); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteAllText(this IGenericFile file, + string text) { + using var sw = file.OpenWriteAsText(); + sw.Write(text); + } + } +} \ No newline at end of file diff --git a/FinModelUtility/Fin/Fin/src/io/Interfaces_Generic.cs b/FinModelUtility/Fin/Fin/src/io/Interfaces_Generic.cs index 3438dff21..9597de640 100644 --- a/FinModelUtility/Fin/Fin/src/io/Interfaces_Generic.cs +++ b/FinModelUtility/Fin/Fin/src/io/Interfaces_Generic.cs @@ -1,36 +1,12 @@ -using System; -using System.IO; -using System.IO.Abstractions; - -using schema.binary; -using schema.text; +using System.IO.Abstractions; namespace fin.io { public interface IReadOnlyGenericFile { string DisplayFullPath { get; } - FileSystemStream OpenRead(); - StreamReader OpenReadAsText(); - - T ReadNew() where T : IBinaryDeserializable, new(); - T ReadNew(Endianness endianness) where T : IBinaryDeserializable, new(); - - T ReadNewFromText() where T : ITextDeserializable, new(); - - byte[] ReadAllBytes(); - string ReadAllText(); - string[] ReadAllLines(); - - T Deserialize(); } public interface IGenericFile : IReadOnlyGenericFile { FileSystemStream OpenWrite(); - StreamWriter OpenWriteAsText(); - - void WriteAllBytes(ReadOnlyMemory bytes); - void WriteAllText(string text); - - void Serialize(T instance) where T : notnull; } } \ No newline at end of file diff --git a/FinModelUtility/Formats/Ast/Ast/src/api/AstAudioReader.cs b/FinModelUtility/Formats/Ast/Ast/src/api/AstAudioReader.cs index e037b7ff0..6bcde26f9 100644 --- a/FinModelUtility/Formats/Ast/Ast/src/api/AstAudioReader.cs +++ b/FinModelUtility/Formats/Ast/Ast/src/api/AstAudioReader.cs @@ -2,6 +2,7 @@ using fin.audio; using fin.audio.io.importers; +using fin.io; using schema.binary; diff --git a/FinModelUtility/Formats/Dat/src/api/DatModelImporter.cs b/FinModelUtility/Formats/Dat/src/api/DatModelImporter.cs index cec511346..f5dfcc81e 100644 --- a/FinModelUtility/Formats/Dat/src/api/DatModelImporter.cs +++ b/FinModelUtility/Formats/Dat/src/api/DatModelImporter.cs @@ -1,5 +1,6 @@ using dat.schema; +using fin.io; using fin.model; using fin.model.impl; using fin.model.io.importers; diff --git a/FinModelUtility/Formats/J3d/J3d/src/api/BmdModelImporter.cs b/FinModelUtility/Formats/J3d/J3d/src/api/BmdModelImporter.cs index e652d32a3..cf173926d 100644 --- a/FinModelUtility/Formats/J3d/J3d/src/api/BmdModelImporter.cs +++ b/FinModelUtility/Formats/J3d/J3d/src/api/BmdModelImporter.cs @@ -5,6 +5,7 @@ using System.Numerics; using fin; +using fin.io; using fin.log; using fin.math.matrix.four; using fin.model; diff --git a/FinModelUtility/Formats/Level5/Level5/src/api/XcModelImporter.cs b/FinModelUtility/Formats/Level5/Level5/src/api/XcModelImporter.cs index 24a17593a..e0838b1be 100644 --- a/FinModelUtility/Formats/Level5/Level5/src/api/XcModelImporter.cs +++ b/FinModelUtility/Formats/Level5/Level5/src/api/XcModelImporter.cs @@ -1,6 +1,7 @@ using fin.data.lazy; using fin.data.nodes; using fin.data.queues; +using fin.io; using fin.math.rotations; using fin.model; using fin.model.impl; diff --git a/FinModelUtility/Formats/Mod/Mod/src/api/ModModelImporter.cs b/FinModelUtility/Formats/Mod/Mod/src/api/ModModelImporter.cs index 180271b42..2bc6df6ec 100644 --- a/FinModelUtility/Formats/Mod/Mod/src/api/ModModelImporter.cs +++ b/FinModelUtility/Formats/Mod/Mod/src/api/ModModelImporter.cs @@ -4,6 +4,7 @@ using fin.color; using fin.image; +using fin.io; using fin.model; using fin.model.impl; using fin.model.io.importers; diff --git a/FinModelUtility/Games/Pikmin2/Pikmin2/src/api/Pikmin2SceneImporter.cs b/FinModelUtility/Games/Pikmin2/Pikmin2/src/api/Pikmin2SceneImporter.cs index 0660ab5f7..67276f43a 100644 --- a/FinModelUtility/Games/Pikmin2/Pikmin2/src/api/Pikmin2SceneImporter.cs +++ b/FinModelUtility/Games/Pikmin2/Pikmin2/src/api/Pikmin2SceneImporter.cs @@ -1,4 +1,5 @@ -using fin.model; +using fin.io; +using fin.model; using fin.scene; using games.pikmin2.route; diff --git a/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/config/Config.cs b/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/config/Config.cs index 18dead51a..b8ec0a0aa 100644 --- a/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/config/Config.cs +++ b/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/config/Config.cs @@ -1,4 +1,5 @@ using fin.config; +using fin.io; using Newtonsoft.Json; using Newtonsoft.Json.Converters; diff --git a/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/scene/IScaleSource.cs b/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/scene/IScaleSource.cs index f2c3fe92d..129a4aded 100644 --- a/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/scene/IScaleSource.cs +++ b/FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/scene/IScaleSource.cs @@ -1,4 +1,5 @@ using fin.config; +using fin.io; using fin.io.bundles; using fin.model; using fin.model.util; diff --git a/FinModelUtility/Utility of Time CSharp/api/OotModelImporter.cs b/FinModelUtility/Utility of Time CSharp/api/OotModelImporter.cs index 5ee18efb4..ac1d0dd69 100644 --- a/FinModelUtility/Utility of Time CSharp/api/OotModelImporter.cs +++ b/FinModelUtility/Utility of Time CSharp/api/OotModelImporter.cs @@ -11,6 +11,7 @@ using f3dzex2.model; using fin.data.queues; +using fin.io; using fin.model; using fin.model.io.importers;