Пример #1
0
        /// <summary>Writes field into a config node.</summary>
        /// <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)
        {
            var value = fieldInfo.GetValue(instance);

            if (value == null)
            {
                return;
            }
            if (collectionFieldHandler != null)
            {
                collectionFieldHandler.SerializeValues(node, value);
            }
            else
            {
                var cfgData = ordinaryFieldHandler.SerializeValue(value);
                if (cfgData != null)
                {
                    if (ordinaryFieldHandler.IsCompound())
                    {
                        ConfigAccessor.SetNodeByPath(node, cfgPath, (ConfigNode)cfgData);
                    }
                    else
                    {
                        ConfigAccessor.SetValueByPath(node, cfgPath, (string)cfgData);
                    }
                }
            }
        }
Пример #2
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));
                }
            }
        }