using System; using System.Diagnostics.CodeAnalysis; using System.IO; using JetBrains.Annotations; using Robust.Shared.Utility; namespace Robust.Shared.ContentPack { [PublicAPI] public static class WritableDirProviderExt { /// /// Opens a file for reading. /// /// The writable directory provider /// The path of the file to open. /// A valid file stream. /// /// Thrown if the file does not exist. /// public static Stream OpenRead(this IWritableDirProvider provider, ResPath path) { return provider.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); } /// /// Opens a file for reading. /// /// The writable directory provider /// The path of the file to open. /// A valid stream reader. /// /// Thrown if the file does not exist. /// public static StreamReader OpenText(this IWritableDirProvider provider, ResPath path) { var stream = OpenRead(provider, path); return new StreamReader(stream, EncodingHelpers.UTF8); } /// /// Opens a file for writing. If the file already exists, it will be overwritten. /// /// The writable directory provider /// The path of the file to open. /// A valid file stream. public static Stream OpenWrite(this IWritableDirProvider provider, ResPath path) { return provider.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None); } /// /// Opens a file for writing. If the file already exists, it will be overwritten. /// /// The writable directory provider /// The path of the file to open. /// A valid file stream writer. public static StreamWriter OpenWriteText(this IWritableDirProvider provider, ResPath path) { var stream = OpenWrite(provider, path); return new StreamWriter(stream, EncodingHelpers.UTF8); } /// /// Appends a string to the end of a file. If the file does not /// exist, creates it. /// /// The writable directory provider /// Path of file to append to. /// String to append. public static void AppendAllText(this IWritableDirProvider provider, ResPath path, ReadOnlySpan content) { using var stream = provider.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read); using var writer = new StreamWriter(stream, EncodingHelpers.UTF8); writer.Write(content); } /// /// Reads the entire contents of a file to a string. /// /// /// File to read. /// String of the file contents public static string ReadAllText(this IWritableDirProvider provider, ResPath path) { using var reader = provider.OpenText(path); return reader.ReadToEnd(); } /// /// Reads the entire contents of a path to a string. /// /// The writable directory to look for the path in. /// The path to read the contents from. /// The content read from the path, or null if the path did not exist. /// true if path was successfully read; otherwise, false. public static bool TryReadAllText(this IWritableDirProvider provider, ResPath path, [NotNullWhen(true)] out string? text) { try { text = ReadAllText(provider, path); return true; } catch(FileNotFoundException) { text = null; return false; } } /// /// Reads the entire contents of a path to a byte array. /// /// The writable directory to look for the path in. /// The path to read the contents from. /// The contents of the path as a byte array. public static byte[] ReadAllBytes(this IWritableDirProvider provider, ResPath path) { using var stream = provider.OpenRead(path); using var memoryStream = new MemoryStream((int) stream.Length); stream.CopyTo(memoryStream); return memoryStream.ToArray(); } /// /// Writes the content string to a file. If the file exists, its existing contents will /// be replaced. /// /// The writable directory provider /// Path of the file to write to. /// String contents of the file. public static void WriteAllText(this IWritableDirProvider provider, ResPath path, ReadOnlySpan content) { using var writer = provider.OpenWriteText(path); writer.Write(content); } /// /// Writes the sequence of bytes to a file. If the file exists, its existing contents will /// be replaced. /// /// The writable directory provider /// Path of the file to write to. /// Bytes to write to the file. public static void WriteAllBytes(this IWritableDirProvider provider, ResPath path, ReadOnlySpan content) { using var stream = provider.OpenWrite(path); stream.Write(content); } } }