Roblox-File-Format/RobloxFile.cs
CloneTrooper1019 de8df15d3f Large scale refactor to add class support!
Instance classes are now strongly typed with real property fields that
are derived from the JSON API Dump! This required a lot of reworking
across the board:

- Classes and Enums are auto-generated in the 'Generated' folder now.
This is done using a custom built-in plugin, which can be found in
the Plugins folder of this project.
- Property objects are now tied to .NET's reflection system. Reading
and writing from them will try to redirect into a field of the
Instance they are bound to.
- Property types that were loosely defined now have proper data types
(such as Color3uint8, Content, ProtectedString, SharedString, etc)
- Fixed an error with the CFrame directional vectors.
- The binary PRNT chunk now writes instances in child->parent order.
- Enums are now generated correctly, with up-to-date values.
- INST chunks are now referred to as 'Classes' instead of 'Types'.
- Unary operator added to Vector2 and Vector3.
- CollectionService tags can now be manipulated per-instance using
the Instance.Tags member.
- The Instance.Archivable property now works correctly.
- XML files now save/load metadata correctly.
- Cleaned up the property tokens directory.

I probably missed a few things, but that's a general overview of
everything that changed.
2019-06-30 17:01:19 -05:00

97 lines
3.0 KiB
C#

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace RobloxFiles
{
/// <summary>
/// Represents a loaded *.rbxl/*.rbxm Roblox file.
/// The contents of the RobloxFile are stored as its children.
/// </summary>
public abstract class RobloxFile : Instance
{
protected abstract void ReadFile(byte[] buffer);
public abstract void Save(Stream stream);
/// <summary>
/// Opens a RobloxFile using the provided buffer.
/// </summary>
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("<roblox"))
file = new XmlRobloxFile();
if (file != null)
{
file.ReadFile(buffer);
return file;
}
}
throw new Exception("Unrecognized header!");
}
/// <summary>
/// Opens a Roblox file by reading from a provided Stream.
/// </summary>
/// <param name="stream">The stream to read the Roblox file from.</param>
public static RobloxFile Open(Stream stream)
{
byte[] buffer;
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
buffer = memoryStream.ToArray();
}
return Open(buffer);
}
/// <summary>
/// Opens a Roblox file from a provided file path.
/// </summary>
/// <param name="filePath">A path to a Roblox file to be opened.</param>
public static RobloxFile Open(string filePath)
{
byte[] buffer = File.ReadAllBytes(filePath);
return Open(buffer);
}
/// <summary>
/// Creates and runs a Task to open a Roblox file from a byte sequence that represents the file.
/// </summary>
/// <param name="buffer">A byte sequence that represents the file.</param>
public static Task<RobloxFile> OpenAsync(byte[] buffer)
{
return Task.Run(() => Open(buffer));
}
/// <summary>
/// Creates and runs a Task to open a Roblox file using a provided Stream.
/// </summary>
/// <param name="stream">The stream to read the Roblox file from.</param>
public static Task<RobloxFile> OpenAsync(Stream stream)
{
return Task.Run(() => Open(stream));
}
/// <summary>
/// Opens a Roblox file from a provided file path.
/// </summary>
/// <param name="filePath">A path to a Roblox file to be opened.</param>
public static Task<RobloxFile> OpenAsync(string filePath)
{
return Task.Run(() => Open(filePath));
}
}
}