Class DeltaPackCodec<T>
- Namespace
- DeltaPack
- Assembly
- DeltaPack.dll
Unity-friendly codec for encoding/decoding C# types. Create once during initialization, reuse in hot paths.
public sealed class DeltaPackCodec<T> where T : class
Type Parameters
T
- Inheritance
-
DeltaPackCodec<T>
- Inherited Members
Examples
// During loading/initialization
var playerCodec = new DeltaPackCodec<Player>();
// In game loop - no reflection, no allocations
byte[] encoded = playerCodec.Encode(player);
Player decoded = playerCodec.Decode(encoded);
Constructors
DeltaPackCodec()
Creates a new codec for type T. Call this during initialization (e.g., loading screen), not in hot paths.
public DeltaPackCodec()
DeltaPackCodec(Func<T>?)
Creates a new codec with a custom factory for creating instances. Useful for types without parameterless constructors.
public DeltaPackCodec(Func<T>? factory)
Parameters
factoryFunc<T>
Properties
Schema
The schema generated from type T. Useful for debugging or inspection.
public IReadOnlyDictionary<string, SchemaType> Schema { get; }
Property Value
Methods
Clone(T)
Creates a deep copy of an object.
public T Clone(T obj)
Parameters
objTThe object to clone.
Returns
- T
A new object with the same values.
Decode(byte[])
Deserializes an object from a byte array.
public T Decode(byte[] buf)
Parameters
bufbyte[]The byte array to decode.
Returns
- T
The decoded object.
DecodeDiff(T, byte[])
Applies a diff to a previous state to produce the current state.
public T DecodeDiff(T a, byte[] diff)
Parameters
aTThe previous state.
diffbyte[]The diff produced by EncodeDiff(T, T).
Returns
- T
The reconstructed current state.
Encode(T)
Serializes an object to a byte array.
public byte[] Encode(T obj)
Parameters
objTThe object to encode.
Returns
- byte[]
The encoded byte array.
EncodeDiff(T, T)
Encodes only the differences between two objects. The resulting diff is typically smaller than a full encode when few fields changed.
public byte[] EncodeDiff(T a, T b)
Parameters
aTThe previous state.
bTThe current state.
Returns
- byte[]
A byte array representing the differences.
Equals(T, T)
Performs a deep equality comparison between two objects.
public bool Equals(T a, T b)
Parameters
aTThe first object.
bTThe second object.
Returns
- bool
True if the objects are deeply equal, false otherwise.
FromJson(JsonElement)
Deserializes an object from a JSON element. Performs lenient parsing (e.g., accepts numeric strings as numbers).
public T FromJson(JsonElement json)
Parameters
jsonJsonElementThe JSON element to parse.
Returns
- T
The deserialized object.
ToJson(T)
Serializes an object to a JSON element.
public JsonElement ToJson(T obj)
Parameters
objTThe object to serialize.
Returns
- JsonElement
The JSON representation.