Skip to content

Commit

Permalink
Deleted some methods that complicated the common file interface and s…
Browse files Browse the repository at this point in the history
…witched them into extension methods.
  • Loading branch information
MeltyPlayer committed Oct 18, 2023
1 parent 5b80fab commit 183b0b7
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 99 deletions.
17 changes: 0 additions & 17 deletions FinModelUtility/Fin/Fin/src/io/FileHierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>() where T : IBinaryDeserializable, new()
=> this.Impl.ReadNew<T>();

public T ReadNew<T>(Endianness endianness)
where T : IBinaryDeserializable, new()
=> this.Impl.ReadNew<T>(endianness);

public T ReadNewFromText<T>() where T : ITextDeserializable, new()
=> this.Impl.ReadNewFromText<T>();

public byte[] ReadAllBytes() => this.Impl.ReadAllBytes();
public string ReadAllText() => this.Impl.ReadAllText();
public string[] ReadAllLines() => this.Impl.ReadAllLines();

public T Deserialize<T>() => this.Impl.Deserialize<T>();
}

public IEnumerator<IFileHierarchyDirectory> GetEnumerator() {
Expand Down
56 changes: 0 additions & 56 deletions FinModelUtility/Fin/Fin/src/io/FinFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,72 +112,16 @@ public ISystemFile CloneWithFileType(string newExtension) {
return new FinFile(this.FullNameWithoutExtension + newExtension);
}

// JSON Serialization
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Deserialize<T>() => JsonUtil.Deserialize<T>(this.ReadAllText());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Serialize<T>(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<T>() where T : IBinaryDeserializable, new() {
using var er = new EndianBinaryReader(this.OpenRead());
return er.ReadNew<T>();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T ReadNew<T>(Endianness endianness)
where T : IBinaryDeserializable, new() {
using var er = new EndianBinaryReader(this.OpenRead(), endianness);
return er.ReadNew<T>();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T ReadNewFromText<T>() where T : ITextDeserializable, new() {
using var er = new TextReader(this.OpenRead());
return er.ReadNew<T>();
}

[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<byte> 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);
}
}
}
98 changes: 98 additions & 0 deletions FinModelUtility/Fin/Fin/src/io/FinFileExtensions.cs
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);
}
}
}
26 changes: 1 addition & 25 deletions FinModelUtility/Fin/Fin/src/io/Interfaces_Generic.cs
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;
}
}
1 change: 1 addition & 0 deletions FinModelUtility/Formats/Ast/Ast/src/api/AstAudioReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using fin.audio;
using fin.audio.io.importers;
using fin.io;

using schema.binary;

Expand Down
1 change: 1 addition & 0 deletions FinModelUtility/Formats/Dat/src/api/DatModelImporter.cs
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Numerics;

using fin;
using fin.io;
using fin.log;
using fin.math.matrix.four;
using fin.model;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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;
Expand Down
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;
Expand Down
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using f3dzex2.model;

using fin.data.queues;
using fin.io;
using fin.model;
using fin.model.io.importers;

Expand Down

0 comments on commit 183b0b7

Please sign in to comment.