Пример #1
0
        private static void Serialize(Stream stream, object obj)
        {
            // Write alias of type
            var alias    = s_typeAliasTable.GetAlias(obj.GetType());
            var aliasBuf = BitConverter.GetBytes(alias);

            stream.Write(aliasBuf, 0, aliasBuf.Length);

            // Write proto-buf stream of object
            s_typeModel.Serialize(stream, obj);
        }
Пример #2
0
        public void Spawn(int entityId, Type protoType, int ownerId, EntityFlags flags, ISpawnPayload payload)
        {
            _writer.Write((byte)2);
            _writer.Write(entityId);
            var typeAlias = TypeTable.GetAlias(protoType);

            if (typeAlias == 0)
            {
                throw new ArgumentException("Type of protoType doesn't have alias. Type: " + protoType.FullName);
            }
            _writer.Write(typeAlias);
            _writer.Write(ownerId);
            _writer.Write((byte)flags);
            ProtobufStreamHelper.WriteObject(_writer, payload, TypeTable, TypeModel);
        }
        private void WriteType(Stream stream, Type type)
        {
            var messageTypeAlias = _typeTable.GetAlias(type);

            if (messageTypeAlias != 0)
            {
                // Write big endian
                var bytes = BitConverter.GetBytes(messageTypeAlias);
                stream.WriteByte(bytes[3]);
                stream.WriteByte(bytes[2]);
                stream.WriteByte(bytes[1]);
                stream.WriteByte(bytes[0]);
            }
            else
            {
                // Write string with length 0x80 for making msb of first byte set
                var name  = type.AssemblyQualifiedName;
                var bytes = Encoding.UTF8.GetBytes(name);
                stream.Write7BitEncodedInt(0x80 + bytes.Length);
                stream.Write(bytes, 0, bytes.Length);
            }
        }
Пример #4
0
 public static void WriteObject(BinaryWriter writer, object obj, TypeAliasTable typeTable, TypeModel typeModel)
 {
     if (obj != null)
     {
         var pos = (int)writer.BaseStream.Position;
         writer.BaseStream.Seek(4, SeekOrigin.Current);
         var typeAlias = typeTable.GetAlias(obj.GetType());
         if (typeAlias == 0)
         {
             throw new ArgumentException("Type of object doesn't have alias. Type: " + obj.GetType().FullName);
         }
         writer.Write(typeAlias);
         typeModel.Serialize(writer.BaseStream, obj);
         var posEnd = (int)writer.BaseStream.Position;
         writer.Seek(pos, SeekOrigin.Begin);
         writer.Write((posEnd - pos) - 4);
         writer.Seek(posEnd, SeekOrigin.Begin);
     }
     else
     {
         writer.Write(0);
     }
 }