Example #1
0
 public VDFNode(string name, VDFData parentVDFStructure, VDFNode parent = null)
 {
     Name               = name;
     Parent             = parent;
     ParentVDFStructure = parentVDFStructure;
     InitializeKeysAndNodesList();
 }
        /// <summary>
        /// Tries to parse a VDF Data File and returns true if it is successful. The resulting VDF Data structure is also returned.
        /// </summary>
        /// <param name="path">The path to the VDF File.</param>
        /// <param name="result">The reference to the variable that will contain the result of the parse if it is successful.</param>
        /// <returns></returns>
        public static bool TryParseFile(string path, out VDFData result)
        {
            try
            {
                result = new VDFData(path);
            }
            catch
            {
                result = null;
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Creates a copy of the node and its elements.
        /// </summary>
        /// <param name="node">The Node to be duplicated.</param>
        /// <param name="parent">Indicates which node will parent the duplicate node. Set it to null if you want the duplicate node to be a root node.</param>
        /// <returns></returns>
        public static VDFNode Duplicate(this VDFNode node, VDFNode parent, VDFData parentVDFStructure)
        {
            VDFNode newNode = new VDFNode(node.Name, parentVDFStructure, parent); //Create our clone node and set it's name to the name of the original node but set the parent to the one specified by the calling method.

            //Do a null check for the Nodes and Keys List of the original node. If they aren't null, loop through the elements of the lists and call the duplicate method for each elements. Make sure to set their parent to the clone node.
            if (node.Nodes != null)
            {
                foreach (VDFNode curNode in node.Nodes)
                {
                    newNode.Nodes.Add(curNode.Duplicate(newNode, parentVDFStructure));
                }
            }
            if (node.Keys != null)
            {
                foreach (VDFKey curKey in node.Keys)
                {
                    newNode.Keys.Add(curKey.Duplicate(newNode));
                }
            }

            return(newNode);
        }