Exemple #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.makePluginPath"/>.</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="AbstractPersistentFieldAttribute"/>).</param>
        /// <seealso cref="PersistentFieldAttribute"/>
        public static void ReadFieldsFromFile(string filePath, Type type, object instance,
                                              string nodePath = null,
                                              string group    = StdPersistentGroups.Default)
        {
            Logger.logInfo("Loading persistent fields: file={0}, group=\"{1}\"",
                           filePath, group ?? "<ALL>");
            var node = ConfigNode.Load(KspPaths.makePluginPath(filePath));

            if (node != null && nodePath.Length > 0)
            {
                node = node.GetNode(nodePath);
            }
            if (node != null)
            {
                ReadFieldsFromNode(node, type, instance, group: group);
            }
        }
Exemple #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);

            Logger.logInfo("Writing persistent fields: type={0}, group=\"{1}\"",
                           type, group ?? "<ALL>");
            foreach (var attr in attributes)
            {
                if (attr.configFilePath.Length > 0)
                {
                    WriteFieldsIntoFile(KspPaths.makePluginPath(attr.configFilePath), type, instance,
                                        rootNodePath: attr.nodePath, mergeMode: true, group: attr.group);
                }
                else
                {
                    Logger.logInfo("Not saving database group: {0}", attr.nodePath);
                }
            }
        }
Exemple #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.makePluginPath"/>.</param>
        /// <param name="rootNodePath">A path to the node in the file where the data should be
        /// written. If the node already exsist 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>
        /// <paramref name="rootNodePath"/> node will be updated in that file. Otherwise, a new file
        /// <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="AbstractPersistentFieldAttribute"/>).</param>
        /// <seealso cref="PersistentFieldAttribute"/>
        public static void WriteFieldsIntoFile(string filePath,
                                               Type type, object instance,
                                               string rootNodePath = null, bool mergeMode = true,
                                               string group        = StdPersistentGroups.Default)
        {
            Logger.logInfo("Writing persistent fields: file={0}, group=\"{1}\", isMerging={2}, root={3}",
                           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.makePluginPath(filePath));
        }