-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deleted some methods that complicated the common file interface and s…
…witched them into extension methods.
- Loading branch information
1 parent
5b80fab
commit 183b0b7
Showing
13 changed files
with
109 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T>(this IReadOnlyGenericFile file) | ||
=> JsonUtil.Deserialize<T>(file.ReadAllText()); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void Serialize<T>(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<T>(this IReadOnlyGenericFile file) | ||
where T : IBinaryDeserializable, new() { | ||
using var er = new EndianBinaryReader(file.OpenRead()); | ||
return er.ReadNew<T>(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static T ReadNew<T>(this IReadOnlyGenericFile file, | ||
Endianness endianness) | ||
where T : IBinaryDeserializable, new() { | ||
using var er = new EndianBinaryReader(file.OpenRead(), endianness); | ||
return er.ReadNew<T>(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static T ReadNewFromText<T>(this IReadOnlyGenericFile file) | ||
where T : ITextDeserializable, new() { | ||
using var er = new TextReader(file.OpenRead()); | ||
return er.ReadNew<T>(); | ||
} | ||
|
||
[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<byte> 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T>() where T : IBinaryDeserializable, new(); | ||
T ReadNew<T>(Endianness endianness) where T : IBinaryDeserializable, new(); | ||
|
||
T ReadNewFromText<T>() where T : ITextDeserializable, new(); | ||
|
||
byte[] ReadAllBytes(); | ||
string ReadAllText(); | ||
string[] ReadAllLines(); | ||
|
||
T Deserialize<T>(); | ||
} | ||
|
||
public interface IGenericFile : IReadOnlyGenericFile { | ||
FileSystemStream OpenWrite(); | ||
StreamWriter OpenWriteAsText(); | ||
|
||
void WriteAllBytes(ReadOnlyMemory<byte> bytes); | ||
void WriteAllText(string text); | ||
|
||
void Serialize<T>(T instance) where T : notnull; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
using fin.audio; | ||
using fin.audio.io.importers; | ||
using fin.io; | ||
|
||
using schema.binary; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using dat.schema; | ||
|
||
using fin.io; | ||
using fin.model; | ||
using fin.model.impl; | ||
using fin.model.io.importers; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
FinModelUtility/Formats/Level5/Level5/src/api/XcModelImporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
FinModelUtility/Games/Pikmin2/Pikmin2/src/api/Pikmin2SceneImporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using fin.model; | ||
using fin.io; | ||
using fin.model; | ||
using fin.scene; | ||
|
||
using games.pikmin2.route; | ||
|
1 change: 1 addition & 0 deletions
1
FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/config/Config.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using fin.config; | ||
using fin.io; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
1 change: 1 addition & 0 deletions
1
FinModelUtility/UniversalAssetTool/UniversalAssetTool/src/scene/IScaleSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using fin.config; | ||
using fin.io; | ||
using fin.io.bundles; | ||
using fin.model; | ||
using fin.model.util; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters