コード例 #1
0
    public static object getValueFromSyncFieldMessage(FieldInfoMessage message)
    {
        switch (message.syncedValueType)
        {
        case FieldInfoMessage.syncType.INT: {
            FieldInfoIntMessage syncMessageDetailed = (FieldInfoIntMessage)message;
            return(syncMessageDetailed.value);
        }

        case FieldInfoMessage.syncType.FLOAT: {
            FieldInfoFloatMessage syncMessageDetailed = (FieldInfoFloatMessage)message;
            return(syncMessageDetailed.value);
        }

        case FieldInfoMessage.syncType.BOOL: {
            FieldInfoBoolMessage syncMessageDetailed = (FieldInfoBoolMessage)message;
            return(syncMessageDetailed.value);
        }

        case FieldInfoMessage.syncType.STRING: {
            FieldInfoStringMessage syncMessageDetailed = (FieldInfoStringMessage)message;
            return(syncMessageDetailed.value);
        }

        case FieldInfoMessage.syncType.ARRAY: {
            FieldInfoArrayMessage syncMessageDetailed = (FieldInfoArrayMessage)message;

            List <object> values = new List <object>();
            if (syncMessageDetailed.value == null)
            {
                return(values);
            }
            for (int i = 0; i < syncMessageDetailed.value.Length; i++)
            {
                values.Add(getValueFromSyncFieldMessage(syncMessageDetailed.value[i]));
            }

            return(values.ToArray());
        }

        case FieldInfoMessage.syncType.DICTIONARY: {
            FieldInfoDictionaryMessage syncMessageDetailed = (FieldInfoDictionaryMessage)message;

            Dictionary <object, object> values = new Dictionary <object, object>();
            if (syncMessageDetailed.value == null)
            {
                return(values);
            }
            foreach (FieldInfoMessage key in syncMessageDetailed.value.Keys)
            {
                values.Add(getValueFromSyncFieldMessage(key), getValueFromSyncFieldMessage(syncMessageDetailed.value[key]));
            }

            return(values);
        }

        default: {
            return(null);
        }
        }
    }
コード例 #2
0
    public static FieldInfoMessage getSyncFieldMessage(string fieldName, object fieldValue)
    {
        FieldInfoMessage message = new FieldInfoMessage();

        if (fieldValue.GetType() == typeof(int))
        {
            message = new FieldInfoIntMessage();
            ((FieldInfoIntMessage)message).value = (int)fieldValue;
            message.syncedValueType = FieldInfoMessage.syncType.INT;
        }
        else if (fieldValue.GetType() == typeof(float))
        {
            message = new FieldInfoFloatMessage();
            ((FieldInfoFloatMessage)message).value = (float)fieldValue;
            message.syncedValueType = FieldInfoMessage.syncType.FLOAT;
        }
        else if (fieldValue.GetType() == typeof(bool))
        {
            message = new FieldInfoBoolMessage();
            ((FieldInfoBoolMessage)message).value = (bool)fieldValue;
            message.syncedValueType = FieldInfoMessage.syncType.BOOL;
        }
        else if (fieldValue.GetType() == typeof(string))
        {
            message = new FieldInfoStringMessage();
            ((FieldInfoStringMessage)message).value = (string)fieldValue;
            message.syncedValueType = FieldInfoMessage.syncType.STRING;
        }
        else if (fieldValue.GetType().IsArray)
        {
            message = new FieldInfoArrayMessage();

            List <FieldInfoMessage> messages           = new List <FieldInfoMessage>();
            IEnumerable             originalEnumerable = fieldValue as IEnumerable;
            object[] originalArray = originalEnumerable.OfType <object>().ToArray();

            for (int i = 0; i < originalArray.Length; i++)
            {
                messages.Add(getSyncFieldMessage(i.ToString(), originalArray[i]));
            }

            ((FieldInfoArrayMessage)message).value = messages.ToArray();
            message.syncedValueType = FieldInfoArrayMessage.syncType.ARRAY;
        }
        else if (typeof(IDictionary).IsAssignableFrom(fieldValue.GetType()))
        {
            message = new FieldInfoDictionaryMessage();

            Dictionary <FieldInfoMessage, FieldInfoMessage> messages = new Dictionary <FieldInfoMessage, FieldInfoMessage>();
            IDictionary originalDictionary = fieldValue as IDictionary;

            int i = 0;
            foreach (object key in originalDictionary.Keys)
            {
                messages.Add(getSyncFieldMessage(i + "-0", key), getSyncFieldMessage(i + "-1", originalDictionary[key]));
                i++;
            }

            ((FieldInfoDictionaryMessage)message).value = messages;
            message.syncedValueType = FieldInfoDictionaryMessage.syncType.DICTIONARY;
        }
        else
        {
            QTDebugger.instance.debugWarning(QTDebugger.debugType.NETWORK, "Unknown synced type -> " + fieldName + " of " + fieldValue.GetType().Name);
            return(null);
        }

        message.fieldName = fieldName;
        return(message);
    }