API SaveContentReader - shmellyorc/Box GitHub Wiki
Namespace: Box.Saves
SaveContentReader
extends BinaryReader to provide convenient, type-specific deserialization methods for common game data structures—such as Vect2
, Rect2
, Color
, Enums
via ReadEnum<T>
, and complex objects via DataContractSerializer
—directly from a binary stream. It combines the flexibility of raw binary access with the ease of high-level structured reads, streamlining the process of loading custom content formats in your application.
Instances are created internally via SaveTypeWriterReader
; the constructor is internal and not publicly exposed.
Property | Description |
---|---|
None | If there are no public properties beyond those inherited from BinaryReader |
Method Signature | Description | Returns |
---|---|---|
Vect2 ReadVect2() |
Reads two floats (X and Y) from the stream and returns a Vect2. | Vect2 |
Rect2 ReadRect2() |
Reads four integers (X, Y, Width, Height) and returns a Rect2. | Rect2 |
BoxColor ReadColor() |
Reads four integers (R, G, B, A) and returns a BoxColor instance. | BoxColor |
T ReadEnum<T>() where T : Enum |
Reads a byte and converts it to the specified enum type T. | T |
T ReadObject<T>() |
Reads an int length, then that many bytes, and deserializes an object of type T using DataContractSerializer. | T |
// Define a data type to save
public class PlayerData
{
public Vect2 Position { get; set; }
public Color Color { get; set; }
public Item Item { get; set; }
}
public class Item
{
public string Name { get; set; }
public int Damage { get;set; }
}
// Implement a concrete handler
public class PlayerDataHandler : SaveTypeWriterReader<PlayerData>
{
protected override void Save(SaveContentWriter writer, PlayerData value)
{
writer.Write(value.Position);
writer.Write(value.Color);
writer.Write(value.Item)
}
protected override PlayerData Load(SaveContentReader reader)
{
Vect2 position = reader.ReadVect2();
Color color = reader.ReadColor();
Item item = reader.ReadObject<Item>();
return new PlayerData() { Level = level, Name = name, Item = item };
}
}
// Usage
var handler = new PlayerDataHandler();
var player = new PlayerData
{
Level = 5,
Name = "Avery",
Item = new Item() { Name = "Long Sword", Damage = 4 }
};
// Save (auto-compress if beneficial)
handler.Save("save1.bin", player);
// Load (auto-decompress if needed)
PlayerData loaded = handler.Load("save1.bin");
Console.WriteLine($"Level: {loaded.Level}, Name: {loaded.Name}");