Пример #1
0
        /// <summary>Writes field into a config node.</summary>
        /// <remarks>
        /// This method is not expected to fail since converting any type into string is expected to
        /// succeeed on any value.
        /// </remarks>
        /// <param name="node">A node to write state to.</param>
        /// <param name="instance">An owner of the field. Can be <c>null</c> for static fields.</param>
        public void WriteToConfig(ConfigNode node, object instance)
        {
            if (isDisabled)
            {
                return; // Field is not supported.
            }
            var value = fieldInfo.GetValue(instance);

            if (value == null)
            {
                Debug.LogWarningFormat("Skip writing field {0}.{1} due to its value is NULL",
                                       fieldInfo.DeclaringType.FullName, fieldInfo.Name);
                return;
            }
            if (collectionProto != null)
            {
                // For collections iterative via proto class and serialize item values.
                foreach (var itemValue in collectionProto.GetEnumerator(value))
                {
                    if (itemValue != null)
                    {
                        if (isCompound)
                        {
                            ConfigAccessor.AddNodeByPath(node, cfgPath, SerializeCompoundFieldsToNode(itemValue));
                        }
                        else if (isCustomSimpleType)
                        {
                            ConfigAccessor.AddValueByPath(
                                node, cfgPath, ((IPersistentField)itemValue).SerializeToString());
                        }
                        else
                        {
                            ConfigAccessor.AddValueByPath(
                                node, cfgPath, simpleTypeProto.SerializeToString(itemValue));
                        }
                    }
                }
            }
            else
            {
                // For ordinal values just serialize the value.
                if (isCompound)
                {
                    ConfigAccessor.SetNodeByPath(node, cfgPath, SerializeCompoundFieldsToNode(value));
                }
                else if (isCustomSimpleType)
                {
                    ConfigAccessor.SetValueByPath(node, cfgPath, ((IPersistentField)value).SerializeToString());
                }
                else
                {
                    ConfigAccessor.SetValueByPath(node, cfgPath, simpleTypeProto.SerializeToString(value));
                }
            }
        }
Пример #2
0
 /// <summary>Converts field value into a form suitable for storing into config file.</summary>
 /// <remarks>Values that can be handled by the proto are transformed into simple strings, and
 /// saved as string values into the config. Structs and classes are considred "compound types"
 /// (see <see cref="IsCompound()"/>), i.e. types that have nested fields in them. Such types are
 /// converted into a config node.</remarks>
 /// <param name="value">A field's value to convert.</param>
 /// <returns>String or <see cref="ConfigNode"/>.</returns>
 internal object SerializeValue(object value)
 {
     if (simpleTypeProto.CanHandle(valueType))
     {
         return(simpleTypeProto.SerializeToString(value));
     }
     if (IsCompound())
     {
         return(persistentField.SerializeCompoundFieldsToNode(value));
     }
     Logger.logError("{0} doesn't know how to store value type: {1}", GetType(), valueType);
     return(null);
 }
Пример #3
0
        /// <summary>
        /// Stores a value of arbitrary type <typeparamref name="T"/> into a config node.
        /// </summary>
        /// <param name="node">A node to set data in.</param>
        /// <param name="pathKeys">An array of values that makes the full path. First node in the array is
        /// the top most component of the path.</param>
        /// <param name="value">A value to store. The <paramref name="typeProto"/> handler must know how
        /// to convert value's type into string.</param>
        /// <param name="typeProto">A proto capable to handle the type of <paramref name="value"/>. If not
        /// set then <see cref="StandardOrdinaryTypesProto"/> is used.</param>
        /// <typeparam name="T">The value type to store. Type proto must be able to handle it.
        /// </typeparam>
        /// <exception cref="ArgumentException">If type cannot be handled by the proto.</exception>
        public static void SetValueByPath <T>(ConfigNode node, string[] pathKeys, T value,
                                              AbstractOrdinaryValueTypeProto typeProto = null)
        {
            if (typeProto == null)
            {
                typeProto = standardTypesProto;
            }
            if (!typeProto.CanHandle(typeof(T)))
            {
                throw new ArgumentException(string.Format(
                                                "Proto {0} cannot handle type {1}", typeProto.GetType(), typeof(T)));
            }
            var strValue = typeProto.SerializeToString(value);

            SetValueByPath(node, pathKeys, strValue);
        }