using System; using System.IO; using System.Text; using System.Threading.Tasks; namespace RobloxFiles { /// /// Represents a loaded Roblox place/model file. /// RobloxFile is an Instance and its children are the contents of the file. /// public abstract class RobloxFile : Instance { public static bool LogErrors = false; protected abstract void ReadFile(byte[] buffer); /// /// Saves this RobloxFile to the provided stream. /// /// The stream to save to. public abstract void Save(Stream stream); /// /// Opens a RobloxFile using the provided buffer. /// /// The opened RobloxFile. public static RobloxFile Open(byte[] buffer) { if (buffer.Length > 14) { string header = Encoding.UTF7.GetString(buffer, 0, 14); RobloxFile file = null; if (header == BinaryRobloxFile.MagicHeader) file = new BinaryRobloxFile(); else if (header.StartsWith(" /// Opens a Roblox file by reading from a provided Stream. /// /// The stream to read the Roblox file from. /// The opened RobloxFile. public static RobloxFile Open(Stream stream) { byte[] buffer; using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); buffer = memoryStream.ToArray(); } return Open(buffer); } /// /// Opens a Roblox file from a provided file path. /// /// A path to a Roblox file to be opened. /// The opened RobloxFile. public static RobloxFile Open(string filePath) { byte[] buffer = File.ReadAllBytes(filePath); return Open(buffer); } /// /// Creates and runs a Task to open a Roblox file from a byte sequence that represents the file. /// /// A byte sequence that represents the file. /// A task which will complete once the file is opened with the resulting RobloxFile. public static Task OpenAsync(byte[] buffer) { return Task.Run(() => Open(buffer)); } /// /// Creates and runs a Task to open a Roblox file using a provided Stream. /// /// The stream to read the Roblox file from. /// A task which will complete once the file is opened with the resulting RobloxFile. public static Task OpenAsync(Stream stream) { return Task.Run(() => Open(stream)); } /// /// Opens a Roblox file from a provided file path. /// /// A path to a Roblox file to be opened. /// A task which will complete once the file is opened with the resulting RobloxFile. public static Task OpenAsync(string filePath) { return Task.Run(() => Open(filePath)); } /// /// Saves this RobloxFile to the provided file path. /// /// A path to where the file should be saved. public void Save(string filePath) { using (FileStream stream = File.OpenWrite(filePath)) { Save(stream); } } /// /// Asynchronously saves this RobloxFile to the provided stream. /// /// The stream to save to. /// A task which will complete upon the save's completion. public Task SaveAsync(Stream stream) { return Task.Run(() => Save(stream)); } /// /// Asynchronously saves this RobloxFile to the provided file path. /// /// A path to where the file should be saved. /// A task which will complete upon the save's completion. public Task SaveAsync(string filePath) { return Task.Run(() => Save(filePath)); } } }