Ejemplo n.º 1
0
        private static Tuple <ushort, List <ObjectInfo> > GetInfos(GsWriteStream writeStream)
        {
            var infos      = new List <ObjectInfo>();
            var bufferSize = sizeof(byte); // Size of ObjectsInfo

            while (writeStream.CanRead())
            {
                bufferSize += sizeof(byte); // Add Type Size
                var obj = writeStream.GetObject();
                switch (obj)
                {
                case null:
                    infos.Add(new ObjectInfo(null, Types.Null));
                    break;

                case bool _:
                    infos.Add(new ObjectInfo(obj, Types.Bool));
                    bufferSize += sizeof(byte);
                    break;

                case byte _:
                    infos.Add(new ObjectInfo(obj, Types.Byte));
                    bufferSize += sizeof(byte);
                    break;

                case char _:
                    infos.Add(new ObjectInfo(obj, Types.Char));
                    bufferSize += sizeof(char);
                    break;

                case double _:
                    infos.Add(new ObjectInfo(obj, Types.Double));
                    bufferSize += sizeof(double);
                    break;

                case float _:
                    infos.Add(new ObjectInfo(obj, Types.Float));
                    bufferSize += sizeof(float);
                    break;

                case int _:
                    infos.Add(new ObjectInfo(obj, Types.Int));
                    bufferSize += sizeof(int);
                    break;

                case long _:
                    infos.Add(new ObjectInfo(obj, Types.Long));
                    bufferSize += sizeof(long);
                    break;

                case short _:
                    infos.Add(new ObjectInfo(obj, Types.Short));
                    bufferSize += sizeof(short);
                    break;

                case uint _:
                    infos.Add(new ObjectInfo(obj, Types.Uint));
                    bufferSize += sizeof(uint);
                    break;

                case ulong _:
                    infos.Add(new ObjectInfo(obj, Types.Ulong));
                    bufferSize += sizeof(ulong);
                    break;

                case ushort _:
                    infos.Add(new ObjectInfo(obj, Types.Ushort));
                    bufferSize += sizeof(ushort);
                    break;

                case string s:
                    var sBuffer = GetBuffer(s, true);
                    infos.Add(new ObjectInfo(sBuffer, Types.String));
                    bufferSize += sizeof(ushort) + sBuffer.Length;
                    break;

                case byte[] ba:
                    infos.Add(new ObjectInfo(ba, Types.ByteArray));
                    bufferSize += sizeof(ushort) + ba.Length;
                    break;

                case char[] ca:
                    var     cBuffer = GetBuffer(ca, true);
                    infos.Add(new ObjectInfo(cBuffer, Types.CharArray));
                    bufferSize += sizeof(ushort) + cBuffer.Length;
                    break;

                case double[] da:
                    var       dBuffer = GetBuffer(da);
                    infos.Add(new ObjectInfo(dBuffer, Types.DoubleArray));
                    bufferSize += sizeof(ushort) + dBuffer.Length;
                    break;

                case float[] fa:
                    var      fBuffer = GetBuffer(fa);
                    infos.Add(new ObjectInfo(fBuffer, Types.FloatArray));
                    bufferSize += sizeof(ushort) + fBuffer.Length;
                    break;

                case int[] ia:
                    var    iBuffer = GetBuffer(ia);
                    infos.Add(new ObjectInfo(iBuffer, Types.IntArray));
                    bufferSize += sizeof(ushort) + iBuffer.Length;
                    break;

                case long[] la:
                    var     lBuffer = GetBuffer(la);
                    infos.Add(new ObjectInfo(lBuffer, Types.LongArray));
                    bufferSize += sizeof(ushort) + lBuffer.Length;
                    break;

                case string[] sa:
                    var       saBuffer = GetBuffer(sa);
                    infos.Add(new ObjectInfo(saBuffer, Types.StringArray));
                    bufferSize += sizeof(ushort) + saBuffer.Length;
                    break;

                case BaseSerializer _:
                    infos.Add(new ObjectInfo(obj, Types.CustomObject));
                    break;

                default:
                    if (TypeUtil.HaveType(obj))
                    {
                        var(hash, stream) = TypeUtil.GetWriteStream(obj);
                        var bufferObj = Serialize(stream);
                        infos.Add(new ObjectInfo(bufferObj, Types.CustomObject, hash));
                        bufferSize += sizeof(int) + sizeof(ushort) + bufferObj.Length;
                    }
                    else
                    {
                        throw new GameServiceException("SerializerUtil -> The Type " + obj.GetType() +
                                                       " is Not Supported")
                              .LogException(typeof(SerializerUtil), DebugLocation.RealTime, "GetInfos");
                    }

                    break;
                }
            }


            if (bufferSize >= ushort.MaxValue)
            {
                throw new GameServiceException("SerializerUtil -> The Buffer is Too Large!")
                      .LogException(typeof(SerializerUtil), DebugLocation.RealTime, "GetInfos");
            }

            return(Tuple.Create((ushort)bufferSize, infos));
        }
Ejemplo n.º 2
0
        internal static GsReadStream Deserialize(byte[] buffer)
        {
            var readStream = new GsReadStream();

            using (var packetReader = ByteArrayReaderWriter.Get(buffer))
            {
                var count = packetReader.ReadByte();
                for (var i = 0; i < count; i++)
                {
                    var type = (Types)packetReader.ReadByte();
                    switch (type)
                    {
                    case Types.Bool:
                        readStream.Add(packetReader.ReadByte() != 0x0);
                        break;

                    case Types.Byte:
                        readStream.Add(packetReader.ReadByte());
                        break;

                    case Types.Char:
                        readStream.Add(packetReader.ReadChar());
                        break;

                    case Types.Double:
                        readStream.Add(BitConverter.ToDouble(packetReader.ReadBytes(sizeof(double)), 0));
                        break;

                    case Types.Float:
                        readStream.Add(BitConverter.ToSingle(packetReader.ReadBytes(sizeof(float)), 0));
                        break;

                    case Types.Short:
                        readStream.Add(packetReader.ReadInt16());
                        break;

                    case Types.Int:
                        readStream.Add(packetReader.ReadInt32());
                        break;

                    case Types.Long:
                        readStream.Add(packetReader.ReadInt64());
                        break;

                    case Types.Ushort:
                        readStream.Add(packetReader.ReadUInt16());
                        break;

                    case Types.Uint:
                        readStream.Add(packetReader.ReadUInt32());
                        break;

                    case Types.Ulong:
                        readStream.Add(packetReader.ReadUInt64());
                        break;

                    case Types.String:
                        readStream.Add(GetStringFromBuffer(packetReader.ReadBytes(packetReader.ReadUInt16()),
                                                           true));
                        break;

                    case Types.ByteArray:
                        readStream.Add(packetReader.ReadBytes(packetReader.ReadUInt16()));
                        break;

                    case Types.CharArray:
                        readStream.Add(GetCharsFromBuffers(packetReader.ReadBytes(packetReader.ReadUInt16())));
                        break;

                    case Types.DoubleArray:
                        readStream.Add(GetDoublesFromBuffers(packetReader.ReadBytes(packetReader.ReadUInt16())));
                        break;

                    case Types.FloatArray:
                        readStream.Add(GetFloatsFromBuffers(packetReader.ReadBytes(packetReader.ReadUInt16())));
                        break;

                    case Types.IntArray:
                        readStream.Add(GetIntsFromBuffers(packetReader.ReadBytes(packetReader.ReadUInt16())));
                        break;

                    case Types.LongArray:
                        readStream.Add(GetLongsFromBuffers(packetReader.ReadBytes(packetReader.ReadUInt16())));
                        break;

                    case Types.StringArray:
                        readStream.Add(GetStringsFromBuffer(packetReader.ReadBytes(packetReader.ReadUInt16())));
                        break;

                    case Types.Null:
                        readStream.Add(null);
                        break;

                    case Types.CustomObject:
                        var id         = packetReader.ReadInt32();
                        var bufferData = packetReader.ReadBytes(packetReader.ReadUInt16());
                        readStream.Add(TypeUtil.GetFinalObject(id, Deserialize(bufferData)));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }

            return(readStream);
        }