Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/Lua/IO/BinaryData.cs

This file was deleted.

88 changes: 0 additions & 88 deletions src/Lua/IO/BinaryLuaStream.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Lua/IO/CompositeLoaderFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public bool IsReadable(string path)
return false;
}

public async ValueTask<ILuaStream> Open(string path, LuaFileMode mode, CancellationToken cancellationToken)
public async ValueTask<ILuaStream> Open(string path, LuaFileOpenMode mode, CancellationToken cancellationToken)
{
if (cached != null)
{
Expand Down
38 changes: 12 additions & 26 deletions src/Lua/IO/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public static (FileMode, FileAccess access) GetFileMode(LuaFileOpenMode luaFileO
LuaFileOpenMode.Read => (FileMode.Open, FileAccess.Read),
LuaFileOpenMode.Write => (FileMode.Create, FileAccess.Write),
LuaFileOpenMode.Append => (FileMode.Append, FileAccess.Write),
LuaFileOpenMode.ReadWriteOpen => (FileMode.Open, FileAccess.ReadWrite),
LuaFileOpenMode.ReadWriteCreate => (FileMode.Truncate, FileAccess.ReadWrite),
LuaFileOpenMode.ReadAppend => (FileMode.Append, FileAccess.ReadWrite),
LuaFileOpenMode.ReadUpdate => (FileMode.Open, FileAccess.ReadWrite),
LuaFileOpenMode.WriteUpdate => (FileMode.Truncate, FileAccess.ReadWrite),
LuaFileOpenMode.AppendUpdate => (FileMode.Append, FileAccess.ReadWrite),
_ => throw new ArgumentOutOfRangeException(nameof(luaFileOpenMode), luaFileOpenMode, null)
};
}
Expand All @@ -22,12 +22,12 @@ public bool IsReadable(string path)
}


ILuaStream Open(string path, LuaFileOpenMode openMode, LuaFileContentType contentType)
public ValueTask<ILuaStream> Open(string path, LuaFileOpenMode openMode, CancellationToken cancellationToken)
{
var (mode, access) = GetFileMode(openMode);
Stream stream;

if (openMode == LuaFileOpenMode.ReadAppend)
if (openMode == LuaFileOpenMode.AppendUpdate)
{
stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete);
}
Expand All @@ -36,32 +36,18 @@ ILuaStream Open(string path, LuaFileOpenMode openMode, LuaFileContentType conten
stream = File.Open(path, mode, access, FileShare.ReadWrite | FileShare.Delete);
}

var fileMode = LuaFileModeExtensions.GetMode(openMode, contentType);
ILuaStream wrapper = contentType == LuaFileContentType.Binary
? new BinaryLuaStream(fileMode, stream)
: new TextLuaStream(fileMode, stream);
ILuaStream wrapper =
new TextLuaStream(openMode, stream);

if (openMode == LuaFileOpenMode.ReadAppend)
if (openMode == LuaFileOpenMode.AppendUpdate)
{
wrapper.Seek(0, SeekOrigin.End);
}

return wrapper;
return new(wrapper);
}

public ValueTask<ILuaStream> Open(string path, LuaFileMode mode, CancellationToken cancellationToken)
{
if (mode is LuaFileMode.Load)
{
return new ( new LuaChunkStream(File.OpenRead(path)));
}

var openMode = mode.GetOpenMode();
var contentType = mode.GetContentType();
return new(Open(path, openMode, contentType));
}

public ValueTask Rename(string oldName, string newName,CancellationToken cancellationToken)
public ValueTask Rename(string oldName, string newName, CancellationToken cancellationToken)
{
if (oldName == newName) return default;
if (File.Exists(newName)) File.Delete(newName);
Expand All @@ -70,7 +56,7 @@ public ValueTask Rename(string oldName, string newName,CancellationToken cancell
return default;
}

public ValueTask Remove(string path,CancellationToken cancellationToken)
public ValueTask Remove(string path, CancellationToken cancellationToken)
{
File.Delete(path);
return default;
Expand All @@ -86,7 +72,7 @@ public string GetTempFileName()

public ValueTask<ILuaStream> OpenTempFileStream(CancellationToken cancellationToken)
{
return new( new TextLuaStream(LuaFileMode.ReadUpdateText, File.Open(Path.GetTempFileName(), FileMode.Open, FileAccess.ReadWrite)));
return new(new TextLuaStream(LuaFileOpenMode.WriteUpdate, File.Open(Path.GetTempFileName(), FileMode.Open, FileAccess.ReadWrite)));
}
}
}
2 changes: 1 addition & 1 deletion src/Lua/IO/ILuaFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface ILuaFileSystem
{
public bool IsReadable(string path);
public ValueTask<ILuaStream> Open(string path, LuaFileMode mode, CancellationToken cancellationToken);
public ValueTask<ILuaStream> Open(string path, LuaFileOpenMode mode, CancellationToken cancellationToken);
public ValueTask Rename(string oldName, string newName, CancellationToken cancellationToken);
public ValueTask Remove(string path, CancellationToken cancellationToken);
public string DirectorySeparator => "/";
Expand Down
43 changes: 15 additions & 28 deletions src/Lua/IO/ILuaStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{
public interface ILuaStream : IDisposable
{
public LuaFileMode Mode { get; }
public LuaFileOpenMode Mode { get; }

public ValueTask<LuaFileContent> ReadAllAsync(CancellationToken cancellationToken)
public ValueTask<string> ReadAllAsync(CancellationToken cancellationToken)
{
Mode.ThrowIfNotReadable();

Expand All @@ -16,7 +16,6 @@ public ValueTask<LuaFileContent> ReadAllAsync(CancellationToken cancellationToke
{
Mode.ThrowIfNotReadable();

Mode.ThrowIfNotText();

// Default implementation using ReadStringAsync
throw new NotImplementedException($"ReadLineAsync must be implemented by {GetType().Name}");
Expand All @@ -26,26 +25,19 @@ public ValueTask<LuaFileContent> ReadAllAsync(CancellationToken cancellationToke
{
Mode.ThrowIfNotReadable();

Mode.ThrowIfNotText();
// Default implementation using ReadAllAsync
throw new NotImplementedException($"ReadStringAsync must be implemented by {GetType().Name}");
}

public ValueTask WriteAsync(LuaFileContent content, CancellationToken cancellationToken)
public ValueTask WriteAsync(ReadOnlyMemory<char> content, CancellationToken cancellationToken)
{
Mode.ThrowIfNotWritable();
if (content.Type == LuaFileContentType.Binary)
{
Mode.ThrowIfNotBinary();
}
else
{
Mode.ThrowIfNotText();
}

throw new NotImplementedException($"WriteAsync must be implemented by {GetType().Name}");
}

public ValueTask WriteAsync(string content, CancellationToken cancellationToken) => WriteAsync(content.AsMemory(), cancellationToken);

public ValueTask FlushAsync(CancellationToken cancellationToken)
{
// Default implementation does nothing (no buffering)
Expand All @@ -62,24 +54,19 @@ public long Seek(long offset, SeekOrigin origin)
throw new NotSupportedException($"Seek is not supported by {GetType().Name}");
}

public static ILuaStream CreateStreamWrapper(Stream stream, LuaFileOpenMode openMode, LuaFileContentType contentType = LuaFileContentType.Text)
public static ILuaStream CreateStreamWrapper(Stream stream, LuaFileOpenMode openMode)
{
var mode = LuaFileModeExtensions.GetMode(openMode, contentType);
return contentType == LuaFileContentType.Binary
? new BinaryLuaStream(mode, stream)
: new TextLuaStream(mode, stream);
return new TextLuaStream(openMode, stream);
}

public static ILuaStream CreateFromFileContent(LuaFileContent content)

public static ILuaStream CreateFromFileString(string content)
{
return new StringStream(content);
}

public static ILuaStream CreateFromMemory(ReadOnlyMemory<char> content)
{
if (content.Type == LuaFileContentType.Binary)
{
return new ByteMemoryStream(content.ReadBytes() );
}
else
{
return new StringStream(content.ReadString());
}
return new CharMemoryStream(content);
}


Expand Down
Loading