Beispiel #1
0
    public void OnReceive(byte[] msg, int client)
    {
        Action action = SerializerExtension.Deserialize <Action>(msg);

        action.local = false;
        action.host  = isHost;
        action.perform();

        if (isHost)
        {
            for (int current = 1; (current < clients.Length); current++)
            {
                if (clients[current] != client)
                {
                    adapter.send(action.Serialize(), clients[current]);
                }
            }
        }
    }
        public T Deserialize(ListEntry e)
        {
            var t = typeof(T);
            var r = (T)Activator.CreateInstance(t);

            foreach (ListEntry.Custom c in e.Elements)
            {
                var property = t.GetProperty(c.LocalName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                if (property == null)
                {
                    continue;
                }
                if (property.CanWrite)
                {
                    try {
                        if (property.PropertyType.IsArray)
                        {
                            const char DELIMETER = ','; // '\n'

                            if (property.PropertyType.GetElementType().IsEnum)
                            {
                                var values = c.Value.Split(DELIMETER)
                                             .Select(s => s.Replace(" ", string.Empty))
                                             .Select(i => Enum.Parse(property.PropertyType.GetElementType(), i))
                                             .ToArray();

                                Array array = (Array)Activator.CreateInstance(property.PropertyType, values.Length);

                                // copy the object values to the array
                                int l = 0;
                                foreach (var value in values)
                                {
                                    array.SetValue(value, l++);
                                }

                                property.SetValue(r, array, null);
                            }
                            else
                            {
                                // remove whitespace between each of element
                                string str = new string(c.Value.ToCharArray()
                                                        .Where(ch => !Char.IsWhiteSpace(ch))
                                                        .ToArray());

                                // remove ',', if it is found at the end.
                                char[] charToTrim = { ',', ' ' };
                                str = str.TrimEnd(charToTrim);

                                // split by ','
                                object[] temp = str.Split(DELIMETER);

                                Array array = (Array)Activator.CreateInstance(property.PropertyType, temp.Length);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    bool isResourceType;
                                    //Debug.LogFormat("name: {0} type: {1} i: {2} is null {3}", c.LocalName, property.PropertyType, temp[i].ToString(), String.IsNullOrEmpty(temp[i].ToString()));
                                    int resourceID = SerializerExtension.GetResourceID(temp[i].ToString(), out isResourceType);
                                    if (isResourceType)
                                    {
                                        if (String.IsNullOrEmpty(temp[i].ToString()))
                                        {
                                            array = null;
                                            break;
                                            //array.SetValue(Convert.ChangeType(null, property.PropertyType.GetElementType()), i);
                                        }
                                        else
                                        {
                                            array.SetValue(Convert.ChangeType(resourceID, property.PropertyType.GetElementType()), i);
                                        }
                                    }
                                    else
                                    {
                                        if (String.IsNullOrEmpty(temp[i].ToString()))
                                        {
                                            array = null;
                                            break;
                                            //array.SetValue(Convert.ChangeType(0, property.PropertyType.GetElementType()), i);
                                        }
                                        else
                                        {
                                            array.SetValue(Convert.ChangeType(temp[i], property.PropertyType.GetElementType()), i);
                                        }
                                    }
                                }

                                property.SetValue(r, array, null);
                            }
                        }
                        else
                        {
                            object value = new object();
                            if (property.PropertyType.IsEnum)
                            {
                                value = c.Value.Replace(" ", string.Empty);
                                value = Enum.Parse(property.PropertyType, value.ToString());
                            }
                            else
                            {
                                bool isResourceType;
                                int  resourceID = SerializerExtension.GetResourceID(c.Value, out isResourceType);
                                //Debug.LogFormat("name: {0} type: {1} value: {2} isResource: {3} id: {4}", c.LocalName, property.PropertyType, c.Value, isResourceType, resourceID);
                                if (isResourceType)
                                {
                                    if (String.IsNullOrEmpty(resourceID.ToString()))
                                    {
                                        value = ConvertFrom(null, property.PropertyType);
                                    }
                                    else
                                    {
                                        value = ConvertFrom(resourceID, property.PropertyType);
                                    }
                                }
                                else
                                {
                                    if (String.IsNullOrEmpty(resourceID.ToString()))
                                    {
                                        value = ConvertFrom(0, property.PropertyType);
                                    }
                                    else
                                    {
                                        value = ConvertFrom(c.Value, property.PropertyType);
                                    }
                                }
                            }
                            property.SetValue(r, value, null);
                        }
                    } catch (Exception exc) {
                        Debug.LogError("GDataDB Serialization Exception: " + exc.Message + " ListEntry LocalName: " + c.LocalName);
                    }
                }
            }
            return(r);
        }