protected DynamicSnapshotField AddCustomField <K>(K dynamicKey, CustomSnapshot child) where K : struct { DynamicSnapshotField field = new DynamicSnapshotField(this, dynamicKey, SnapshotFieldType.Custom, child, GetPrimitiveType(typeof(K))); dynamicFields.Add(dynamicKey, field); return(field); }
protected StaticSnapshotField AddCustomField(CustomSnapshot child) { if (IsReady) { throw new InvalidOperationException("Cannot add static snapshot field after initialization!"); } ushort id = currentFieldId++; StaticSnapshotField field = new StaticSnapshotField(this, id, SnapshotFieldType.Custom, child); staticFields.Add(id, field); return(field); }
bool SerializeField(SnapshotField field, NetBuffer buffer, DeltaSnapshot prev) { // Write field type buffer.Write((byte)field.Type); // Write the field data if (field.Type == SnapshotFieldType.Primitive) { // Write primitive buffer.Write((byte)field.PrimitiveType); buffer.WriteDynamic(field.Value); } else if (field.Type == SnapshotFieldType.Trigger) { Trigger t = (Trigger)field.Value; // Write trigger buffer.Write(t.Iterations); // Reset trigger t.Iterations = 0; } else if (field.Type == SnapshotFieldType.Snapshot) { // Write nested snapshot Snapshot ns = (Snapshot)field.Value; ns.Serialize(buffer); } else if (field.Type == SnapshotFieldType.Custom) { // Write custom snapshot CustomSnapshot cs = (CustomSnapshot)field.Value; cs.Serialize(buffer); } return(true); }
void ReadField(NetBuffer buffer, SnapshotField field) { // Read field type SnapshotFieldType fieldType = (SnapshotFieldType)buffer.ReadByte(); // Read field data if (fieldType == SnapshotFieldType.Primitive) { // Read primitive SnapshotPrimitiveType primType = (SnapshotPrimitiveType)buffer.ReadByte(); object v = ReadPrimitive(buffer, primType); if (field != null) { field.Value = v; } } else if (fieldType == SnapshotFieldType.Trigger) { // Read trigger byte it = buffer.ReadByte(); if (field != null) { Trigger t = (Trigger)field.Value; t.Iterations = it; } } else if (fieldType == SnapshotFieldType.Snapshot) { // Read nested snapshot Snapshot ns; if (field != null) { ns = (Snapshot)field.Value; } else { // Make temp snapshot to just read ns ns = new TempSnapshot(snapshotSystem); } ns.Deserialize(buffer); } else if (fieldType == SnapshotFieldType.Custom) { // Read custom snapshot // Read snapshot size ushort bufferSize = buffer.ReadUInt16(); if (field != null) { // Read custom snapshot CustomSnapshot cs = (CustomSnapshot)field.Value; cs.Deserialize(buffer); } else { // Skip buffer buffer.Position += bufferSize; } } }