2019-01-26 00:39:37 +00:00
|
|
|
|
using System;
|
2019-01-29 09:50:55 +00:00
|
|
|
|
using System.IO;
|
2019-01-26 00:39:37 +00:00
|
|
|
|
using System.Text;
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
2019-02-01 17:19:20 +00:00
|
|
|
|
using RobloxFiles.BinaryFormat;
|
|
|
|
|
using RobloxFiles.XmlFormat;
|
2019-01-26 00:39:37 +00:00
|
|
|
|
|
2019-02-01 17:19:20 +00:00
|
|
|
|
namespace RobloxFiles
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
2019-01-29 09:50:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interface which represents a RobloxFile implementation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public interface IRobloxFile
|
|
|
|
|
{
|
2019-01-30 06:36:56 +00:00
|
|
|
|
Instance Contents { get; }
|
|
|
|
|
void ReadFile(byte[] buffer);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a loaded *.rbxl/*.rbxm Roblox file.
|
|
|
|
|
/// All of the surface-level Instances are stored in the RobloxFile's Trunk property.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RobloxFile : IRobloxFile
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
2019-01-29 09:50:55 +00:00
|
|
|
|
public bool Initialized { get; private set; }
|
|
|
|
|
public IRobloxFile InnerFile { get; private set; }
|
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
public Instance Contents => InnerFile.Contents;
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
public void ReadFile(byte[] buffer)
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Initialized)
|
|
|
|
|
{
|
|
|
|
|
if (buffer.Length > 14)
|
|
|
|
|
{
|
|
|
|
|
string header = Encoding.UTF7.GetString(buffer, 0, 14);
|
|
|
|
|
IRobloxFile file = null;
|
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
if (header == BinaryRobloxFile.MagicHeader)
|
|
|
|
|
file = new BinaryRobloxFile();
|
2019-01-29 09:50:55 +00:00
|
|
|
|
else if (header.StartsWith("<roblox"))
|
2019-01-30 06:36:56 +00:00
|
|
|
|
file = new XmlRobloxFile();
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
2019-01-30 06:36:56 +00:00
|
|
|
|
file.ReadFile(buffer);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
InnerFile = file;
|
|
|
|
|
|
|
|
|
|
Initialized = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Exception("Unrecognized header!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RobloxFile(byte[] buffer)
|
|
|
|
|
{
|
2019-01-30 06:36:56 +00:00
|
|
|
|
ReadFile(buffer);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RobloxFile(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer;
|
|
|
|
|
|
|
|
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
stream.CopyTo(memoryStream);
|
|
|
|
|
buffer = memoryStream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
ReadFile(buffer);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RobloxFile(string filePath)
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer = File.ReadAllBytes(filePath);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
ReadFile(buffer);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
2019-02-03 13:28:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Treats the provided string as if you were indexing a specific child or descendant of the `RobloxFile.Contents` folder.<para/>
|
|
|
|
|
/// The provided string can either be:<para/>
|
|
|
|
|
/// - The name of a child that is parented to RobloxFile.Contents ( Example: RobloxFile["Workspace"] )<para/>
|
|
|
|
|
/// - A period (.) separated path to a descendant of RobloxFile.Contents ( Example: RobloxFile["Workspace.Terrain"] )<para/>
|
|
|
|
|
/// This will throw an exception if any instance in the traversal is not found.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Instance this[string accessor] => Contents[accessor];
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|