Exemplo n.º 1
0
        /// <summary>Reads values of the annotated persistent fields from a config file.</summary>
        /// <param name="filePath">
        /// A relative or an absolute path to the file. It's resolved via
        /// <see cref="KspPaths.MakeAbsPathForGameData"/>.
        /// </param>
        /// <param name="type">A type to load fields for.</param>
        /// <param name="instance">
        /// An instance of type <paramref name="type"/>. If it's <c>null</c> then
        /// only static fields will be loaded.
        /// </param>
        /// <param name="nodePath">
        /// An optional path in the file. All type's field will be read relative to this part.
        /// </param>
        /// <param name="group">A group tag (see <see cref="BasePersistentFieldAttribute"/>).</param>
        /// <seealso cref="PersistentFieldAttribute"/>
        public static void ReadFieldsFromFile(string filePath, Type type, object instance,
                                              string nodePath = null,
                                              string group    = StdPersistentGroups.Default)
        {
            DebugEx.Fine("Loading persistent fields: file={0}, group=\"{1}\"",
                         KspPaths.MakeRelativePathToGameData(filePath), group ?? "<ALL>");
            var node = ConfigNode.Load(KspPaths.MakeAbsPathForGameData(filePath));

            if (node != null && nodePath.Length > 0)
            {
                node = node.GetNode(nodePath);
            }
            if (node != null)
            {
                ReadFieldsFromNode(node, type, instance, group: group);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes persistent fields into the config files specified by the class annotation.
        /// </summary>
        /// <remarks>Method updates the config file(s) by preserving top level nodes that are not
        /// specified as targets for the requested group.
        /// <para>Note, that fields cannot be writtent into database. Such annotations will be skipped
        /// during the save.</para>
        /// </remarks>
        /// <param name="type">A type to write fields for.</param>
        /// <param name="instance">An instance of type <paramref name="type"/>. If it's <c>null</c> then
        /// only static fields will be written.</param>
        /// <param name="group">A group to write fields for. If <c>null</c> then all groups that are
        /// defined in the class annotation via <see cref="PersistentFieldsFileAttribute"/> will be
        /// written.</param>
        /// <seealso cref="PersistentFieldAttribute"/>
        /// <seealso cref="PersistentFieldsFileAttribute"/>
        public static void WriteFieldsFromType(Type type, object instance,
                                               string group = StdPersistentGroups.Default)
        {
            var attributes = GetPersistentFieldsFiles(type, group);

            DebugEx.Fine("Writing persistent fields: type={0}, group=\"{1}\"", type, group ?? "<ALL>");
            foreach (var attr in attributes)
            {
                if (attr.configFilePath.Length > 0)
                {
                    WriteFieldsIntoFile(KspPaths.MakeAbsPathForGameData(attr.configFilePath), type, instance,
                                        rootNodePath: attr.nodePath, mergeMode: true, group: attr.group);
                }
                else
                {
                    DebugEx.Fine("Not saving database group: {0}", attr.nodePath);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>Writes values of the annotated persistent fields into a file.</summary>
        /// <remarks>
        /// All persitent values are <b>added</b> into the file provided. I.e. if node had already had a
        /// value being persited then it either overwritten (ordinary fields) or extended (collection
        /// fields).
        /// </remarks>
        /// <param name="filePath">
        /// A relative or an absolute path to the file. It's resolved via
        /// <see cref="KspPaths.MakeAbsPathForGameData"/>.
        /// </param>
        /// <param name="rootNodePath">
        /// A path to the node in the file where the data should be written. If the node already exsists
        /// it will be deleted.
        /// </param>
        /// <param name="type">A type to write fields for.</param>
        /// <param name="instance">
        /// An instance of type <paramref name="type"/>. If it's <c>null</c> then only static fields will
        /// be written.
        /// </param>
        /// <param name="mergeMode">
        /// If <c>true</c> and the file already exists then only will be created.
        /// </param>
        /// <param name="group">A group tag (see <see cref="BasePersistentFieldAttribute"/>).</param>
        /// <seealso cref="PersistentFieldAttribute"/>
        public static void WriteFieldsIntoFile(string filePath,
                                               Type type, object instance,
                                               string rootNodePath = null, bool mergeMode = true,
                                               string group        = StdPersistentGroups.Default)
        {
            DebugEx.Fine("Writing persistent fields: file={0}, group=\"{1}\", isMerging={2}, root={3}",
                         KspPaths.MakeRelativePathToGameData(filePath),
                         group ?? "<ALL>", mergeMode, rootNodePath ?? "/");
            var node = mergeMode
        ? ConfigNode.Load(filePath) ?? new ConfigNode()  // Make empty node if file doesn't exist.
        : new ConfigNode();
            var tagetNode = node;

            if (rootNodePath != null)
            {
                tagetNode = GetNodeByPath(node, rootNodePath, createIfMissing: true);
                tagetNode.ClearData(); // In case of it's an existing node.
            }
            WriteFieldsIntoNode(tagetNode, type, instance, group: group);
            node.Save(KspPaths.MakeAbsPathForGameData(filePath));
        }