Ejemplo n.º 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));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>Stores collection values into a config node.</summary>
        /// <param name="node">A node to add values into.</param>
        /// <param name="value">A collection instance to get values from.</param>
        internal void SerializeValues(ConfigNode node, object value)
        {
            var proto = collectionProto as GenericCollectionTypeProto;

            foreach (var itemValue in proto.GetEnumerator(value))
            {
                if (itemValue == null)
                {
                    continue;
                }
                var cfgData = persistentField.ordinaryFieldHandler.SerializeValue(itemValue);
                if (cfgData != null)
                {
                    if (cfgData is ConfigNode)
                    {
                        ConfigAccessor.AddNodeByPath(node, persistentField.cfgPath, (ConfigNode)cfgData);
                    }
                    else
                    {
                        ConfigAccessor.AddValueByPath(node, persistentField.cfgPath, (string)cfgData);
                    }
                }
            }
        }