internal DynamicSnapshotField(Snapshot parent, object id, SnapshotFieldType type, object defaultValue,
                               SnapshotPrimitiveType idPrimitiveType,
                               SnapshotPrimitiveType primitiveType = SnapshotPrimitiveType.None)
     : base(parent, type, defaultValue, primitiveType)
 {
     Id = id;
     IdPrimitiveType = idPrimitiveType;
 }
 internal SnapshotField(Snapshot parent, SnapshotFieldType type,
                        object defaultValue, SnapshotPrimitiveType primtiveType)
 {
     Parent        = parent;
     Type          = type;
     Value         = defaultValue;
     PrimitiveType = primtiveType;
 }
Exemple #3
0
        protected DynamicSnapshotField AddPrimitiveField <T, K>(K dynamicKey, T defaultValue = default(T))
            where T : struct
            where K : struct
        {
            SnapshotPrimitiveType primType = GetPrimitiveType(defaultValue.GetType());
            DynamicSnapshotField  field    = new DynamicSnapshotField(this, dynamicKey, SnapshotFieldType.Primitive,
                                                                      defaultValue, GetPrimitiveType(typeof(K)), primType);

            dynamicFields.Add(dynamicKey, field);
            return(field);
        }
Exemple #4
0
        protected StaticSnapshotField AddPrimitiveField <T>(T defaultValue = default(T))
            where T : struct
        {
            if (IsReady)
            {
                throw new InvalidOperationException("Cannot add static snapshot field after initialization!");
            }

            ushort id = currentFieldId++;
            SnapshotPrimitiveType primType = GetPrimitiveType(defaultValue.GetType());
            StaticSnapshotField   field    = new StaticSnapshotField(this, id, SnapshotFieldType.Primitive,
                                                                     defaultValue, primType);

            staticFields.Add(id, field);
            return(field);
        }
Exemple #5
0
        static object ReadPrimitive(NetBuffer buffer, SnapshotPrimitiveType type)
        {
            switch (type)
            {
            case SnapshotPrimitiveType.Byte:
                return(buffer.ReadByte());

            case SnapshotPrimitiveType.Char:
                return(buffer.ReadChar());

            case SnapshotPrimitiveType.Boolean:
                return(buffer.ReadBool());

            case SnapshotPrimitiveType.Int16:
            case SnapshotPrimitiveType.SByte:     // SByte is written as a short
                return(buffer.ReadInt16());

            case SnapshotPrimitiveType.UInt16:
                return(buffer.ReadUInt16());

            case SnapshotPrimitiveType.Int32:
                return(buffer.ReadInt32());

            case SnapshotPrimitiveType.UInt32:
                return(buffer.ReadUInt32());

            case SnapshotPrimitiveType.Int64:
                return(buffer.ReadInt64());

            case SnapshotPrimitiveType.UInt64:
                return(buffer.ReadUInt64());

            case SnapshotPrimitiveType.Single:
                return(buffer.ReadFloat());

            case SnapshotPrimitiveType.Double:
                return(buffer.ReadDouble());

            case SnapshotPrimitiveType.ByteFlag:
                return(buffer.ReadByteFlag());

            default:
                throw new Exception("Snapshot primitive type '" + type + "' is not supported!");
            }
        }
Exemple #6
0
        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;
                }
            }
        }
Exemple #7
0
        public virtual void Deserialize(NetBuffer buffer)
        {
            // Reset all triggers
            foreach (SnapshotField field in staticFields.Values)
            {
                if (field.Type == SnapshotFieldType.Trigger)
                {
                    Trigger t = (Trigger)field.Value;
                    t.Iterations = 0;
                }
            }

            foreach (SnapshotField field in dynamicFields.Values)
            {
                if (field.Type == SnapshotFieldType.Trigger)
                {
                    Trigger t = (Trigger)field.Value;
                    t.Iterations = 0;
                }
            }

            // Read number of fields
            ushort numStaticFields  = buffer.ReadUInt16();
            ushort numDynamicFields = buffer.ReadUInt16();

            // Read delta id
            byte deltaId = buffer.ReadByte();

            if (IsDeltaCompressing)
            {
                AcknowledgedDeltaIds.Add(deltaId);
            }

            // Read static fields
            for (int i = 0; i < numStaticFields; i++)
            {
                // Read field id
                ushort fieldId = buffer.ReadUInt16();

                // Try and find the field (it's ok if we can't)
                StaticSnapshotField field;
                staticFields.TryGetValue(fieldId, out field);

                // Read the field
                ReadField(buffer, field);
            }

            // Read dynamic fields
            for (int i = 0; i < numDynamicFields; i++)
            {
                // Read field id
                SnapshotPrimitiveType idPrimType = (SnapshotPrimitiveType)buffer.ReadByte();
                object fieldId = ReadPrimitive(buffer, idPrimType);

                // Try and find the field (it's ok if we can't)
                DynamicSnapshotField field;
                dynamicFields.TryGetValue(fieldId, out field);

                // Read the field
                ReadField(buffer, field);
            }
        }
 internal StaticSnapshotField(Snapshot parent, ushort id, SnapshotFieldType type, object defaultValue,
                              SnapshotPrimitiveType primitiveType = SnapshotPrimitiveType.None)
     : base(parent, type, defaultValue, primitiveType)
 {
     Id = id;
 }