Exemplo n.º 1
0
        public static bool TryWriteObjectToFile(string url, NjoxNode rootNode)
        {
            string dir = Path.GetDirectoryName(url);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            File.WriteAllText(url, rootNode.ToString());
            return(true);
        }
Exemplo n.º 2
0
        public bool TryGetFirstChild(string key, out NjoxNode child)
        {
            int index = m_ChildNodes.FindIndex(v => v.HasProperty(key));

            if (index == -1)
            {
                child = null;
                return(false);
            }

            child = m_ChildNodes[index];
            return(true);
        }
Exemplo n.º 3
0
        public static bool TryReadObjectFromFile(string url, out NjoxNode rootNode)
        {
            if (!File.Exists(url))
            {
                rootNode = null;
                return(false);
            }

            bool result = TryParseObject(File.ReadAllText(url), out rootNode);

            if (!result)
            {
                Logger.LogError("Failed to load '" + url + "'");
            }

            return(result);
        }
Exemplo n.º 4
0
 public void AddChild(NjoxNode child)
 {
     m_ChildNodes.Add(child);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Attempt to parse a njox object from a string
        /// </summary>
        /// <param name="raw">The raw string in njox format</param>
        /// <param name="njoxRoot">The object where the root njox will be stored</param>
        /// <returns>True if the string is successfully parsed</returns>
        public static bool TryParseObject(string raw, out NjoxNode njoxRoot)
        {
            njoxRoot = null;

            using (StringReader reader = new StringReader(raw))
            {
                List <NjoxNode> currentIndents = new List <NjoxNode>();

                while (reader.Peek() > -1)
                {
                    string line = reader.ReadLine();

                    int indentation = 0;
                    foreach (char c in line)
                    {
                        if (c != '\t')
                        {
                            break;
                        }
                        else
                        {
                            ++indentation;
                        }
                    }
                    line = line.Trim();

                    // Attempting to read root node
                    if (indentation == 0)
                    {
                        if (njoxRoot == null)
                        {
                            List <NjoxProperty> properties;

                            if (!TryParseProperties(line, out properties))
                            {
                                return(false);
                            }

                            // Could be comment line, so only use
                            if (properties.Count != 0)
                            {
                                njoxRoot = new NjoxNode(properties);
                                currentIndents.Add(njoxRoot);
                            }
                        }

                        // Discovered multiple root nodes
                        else
                        {
                            Logger.LogError("Found a second root node in njox file at '" + line + "'");
                            return(false);
                        }
                    }
                    else
                    {
                        // Check parent node exists
                        NjoxNode parentNode;
                        if (indentation - 1 < currentIndents.Count)
                        {
                            parentNode = currentIndents[indentation - 1];
                        }
                        else
                        {
                            Logger.LogError("Unexpected indentation at '" + line + "'");
                            return(false);
                        }

                        // Remove any previous sub-indentation indentations over this
                        if (currentIndents.Count >= indentation)
                        {
                            currentIndents.RemoveRange(indentation, currentIndents.Count - indentation);
                        }

                        // Attempt to read node
                        List <NjoxProperty> properties;

                        if (!TryParseProperties(line, out properties))
                        {
                            return(false);
                        }

                        // Could be comment line, so make sure to check
                        if (properties.Count != 0)
                        {
                            NjoxNode newNode = new NjoxNode(properties);
                            parentNode.AddChild(newNode);
                            currentIndents.Add(newNode);
                        }
                    }
                }
            }

            return(true);
        }