示例#1
0
        public void FromSerializedMemory(BinaryReader reader)
        {
            Clear();

            var numItems = reader.ReadUInt16();

            for (UInt16 iItem = 0; iItem < numItems; iItem++)
            {
                var variableName = new StringCrc32(reader.ReadUInt32());
                var typeName     = new StringCrc32(reader.ReadUInt32());

                var typeInfo = TypeSerialization.GetTypeInfo(typeName);
                var value    = typeInfo.Deserializer(reader);
                Add(variableName, value);
            }
        }
示例#2
0
        public byte[] ToByteArray()
        {
            UInt16 NumItems = (UInt16)Count;

            using (MemoryStream outputStream = new MemoryStream())
                using (BinaryWriter writer = new BinaryWriter(outputStream))
                {
                    writer.Write(NumItems);

                    foreach (var itItem in this)
                    {
                        writer.Write(itItem.Key.StringHash);

                        var typeInfo = TypeSerialization.GetTypeInfo(itItem.Value);
                        writer.Write(typeInfo.TypeNameCrc.StringHash);

                        typeInfo.Serializer(writer, itItem.Value);
                    }

                    writer.Flush();
                    return(outputStream.ToArray());
                }
        }
示例#3
0
        static internal void MessageParseSetValue(string stringHash, string typeNameHash, IntPtr Value)
        {
            System.Diagnostics.Debug.Assert(stm_ParsingMessage != null);
            switch (typeNameHash)
            {
            case "Result":
                stm_ParsingMessage.SetValue(stringHash, new Result(Marshal.ReadInt32(Value)));
                break;

            case "int":
                stm_ParsingMessage.SetValue(stringHash, Marshal.ReadInt32(Value));
                break;

            case "uint":
                stm_ParsingMessage.SetValue(stringHash, (uint)Marshal.ReadInt32(Value));
                break;

            case "int8":
                stm_ParsingMessage.SetValue(stringHash, Marshal.ReadByte(Value));
                break;

            case "uint8":
                stm_ParsingMessage.SetValue(stringHash, (uint)Marshal.ReadByte(Value));
                break;

            case "int16":
                stm_ParsingMessage.SetValue(stringHash, Marshal.ReadInt16(Value));
                break;

            case "uint16":
                stm_ParsingMessage.SetValue(stringHash, (uint)Marshal.ReadInt16(Value));
                break;

            case "int32":
                stm_ParsingMessage.SetValue(stringHash, Marshal.ReadInt32(Value));
                break;

            case "uint32":
                stm_ParsingMessage.SetValue(stringHash, (uint)Marshal.ReadInt32(Value));
                break;

            case "int64":
                stm_ParsingMessage.SetValue(stringHash, Marshal.ReadInt64(Value));
                break;

            case "uint64":
                stm_ParsingMessage.SetValue(stringHash, (UInt64)Marshal.ReadInt64(Value));
                break;

            case "uint128":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(SFUInt128)));
                break;

            case "float":
                stm_ParsingMessage.SetValue(stringHash, ReadFloat(Value));
                break;

            case "double":
                stm_ParsingMessage.SetValue(stringHash, ReadDouble(Value));
                break;

            case "RouteContext":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(SFRouteContext)));
                break;

            case "NetAddress":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(NetAddress)));
                break;

            case "MatchingQueueTicket":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(MatchingQueueTicket)));
                break;

            case "PlayerInformation":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(PlayerInformation)));
                break;

            case "FriendInformation":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(FriendInformation)));
                break;

            case "TotalRankingPlayerInformation":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(TotalRankingPlayerInformation)));
                break;

            case "RelayPlayerInfo":
                stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(RelayPlayerInfo)));
                break;

            //case "ActorMovement":
            //    stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(ActorMovement)));
            //    break;
            //case "Vector2":
            //    stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(Vector2)));
            //    break;
            //case "Vector3":
            //    stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(Vector3)));
            //    break;
            //case "Vector4":
            //    stm_ParsingMessage.SetValue(stringHash, Marshal.PtrToStructure(Value, typeof(Vector4)));
            //    break;
            //case "VariableTable":
            //VariableTable parsedValue = new VariableTable();
            //parsedValue.FromSerializedMemory(arrayCount, Value);
            //stm_ParsingMessage.SetValue(stringHash, parsedValue);
            //break;
            default:
                // TODO: gradually move to TypeSerialization
                var typeInfo = TypeSerialization.GetTypeInfo(new StringCrc32(typeNameHash));
                if (typeInfo != null)
                {
                    stm_ParsingMessage.SetValue(stringHash, typeInfo.DeserializeNative(ref Value));
                }
                else
                {
                    SF.Log.Error("MessageParseSetValue failed, type:{0}", stringHash);
                }
                break;
            }
        }
示例#4
0
        static internal void MessageParseSetArray(string stringHash, string typeNameHash, int arrayCount, IntPtr Value)
        {
            switch (typeNameHash)
            {
            case "int8":
            {
                var newArray = new sbyte[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "uint8":
            {
                var newArray = new byte[arrayCount];
                if (arrayCount > 0)
                {
                    Marshal.Copy(Value, newArray, 0, arrayCount);
                }
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "int16":
            {
                var newArray = new Int16[arrayCount];
                if (arrayCount > 0)
                {
                    Marshal.Copy(Value, newArray, 0, arrayCount);
                }
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "uint16":
            {
                var newArray = new UInt16[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "int":
            case "int32":
            {
                var newArray = new int[arrayCount];
                if (arrayCount > 0)
                {
                    Marshal.Copy(Value, newArray, 0, arrayCount);
                }
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "uint":
            case "uint32":
            {
                var newArray = new uint[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "int64":
            {
                var newArray = new Int64[arrayCount];
                if (arrayCount > 0)
                {
                    Marshal.Copy(Value, newArray, 0, arrayCount);
                }
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "uint64":
            {
                var newArray = new UInt64[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "uint128":
            {
                var newArray = new SFUInt128[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "float":
            {
                var newArray = new float[arrayCount];
                if (arrayCount > 0)
                {
                    Marshal.Copy(Value, newArray, 0, arrayCount);
                }
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "double":
            {
                var newArray = new double[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "string":
            {
                var newArray = new string[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "single_string":     // Regular string parameter comes here
            {
                stm_ParsingMessage.SetValue(stringHash, ReadString(arrayCount, Value));
            }
            break;

            case "RouteContext":
            {
                var newArray = new SFRouteContext[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "NetAddress":
            {
                var newArray = new NetAddress[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "PlayerInformation":
            {
                var newArray = new PlayerInformation[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "FriendInformation":
            {
                var newArray = new FriendInformation[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "TotalRankingPlayerInformation":
            {
                var newArray = new TotalRankingPlayerInformation[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "RelayPlayerInfo":
            {
                var newArray = new RelayPlayerInfo[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            //case "ActorMovement":
            //    {
            //        var newArray = new ActorMovement[arrayCount];
            //        MarshalCopy(Value, newArray, 0, arrayCount);
            //        stm_ParsingMessage.SetValue(stringHash, newArray);
            //    }
            //    break;
            case "Vector2":
            {
                var newArray = new Vector2[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "Vector3":
            {
                var newArray = new Vector3[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "Vector4":
            {
                var newArray = new Vector4[arrayCount];
                MarshalCopy(Value, newArray, 0, arrayCount);
                stm_ParsingMessage.SetValue(stringHash, newArray);
            }
            break;

            case "VariableTable":
            {
                VariableTable parsedValue = new VariableTable();
                parsedValue.FromSerializedMemory(arrayCount, Value);
                stm_ParsingMessage.SetValue(stringHash, parsedValue);
            }
            break;

            case "Array<VariableTable>":
            {
                byte[] byteData = new byte[arrayCount];
                Marshal.Copy(Value, byteData, 0, byteData.Length);
                List <VariableTable> tableList = new List <VariableTable>();
                using (BinaryReader reader = new BinaryReader(new MemoryStream(byteData)))
                {
                    var NumElement = reader.ReadInt16();
                    for (int iElement = 0; iElement < NumElement; iElement++)
                    {
                        VariableTable parsedValue = new VariableTable();
                        parsedValue.FromSerializedMemory(reader);
                        tableList.Add(parsedValue);
                    }
                }

                stm_ParsingMessage.SetValue(stringHash, tableList.ToArray());
            }
            break;

            default:
                // TODO: gradually move to TypeSerialization
                var typeInfo = TypeSerialization.GetTypeInfo(new StringCrc32(typeNameHash));
                if (typeInfo != null)
                {
                    var arrayValue = (Array)Array.CreateInstance(typeInfo.Type, arrayCount);
                    if (arrayValue != null)
                    {
                        for (int index = 0; index < arrayCount; index++)
                        {
                            arrayValue.SetValue(typeInfo.DeserializeNative(ref Value), index);
                        }
                    }
                    stm_ParsingMessage.SetValue(stringHash, arrayValue);
                }
                else
                {
                    SF.Log.Error("MessageParseSetArray failed, type:{0}", stringHash);
                }
                break;
            }
        }