public KerbalNode(string name, KerbalNode parent = null) { Name = name; Parent = parent; Values = new Dictionary<string, List<string>>(); Children = new List<KerbalNode>(); if (Parent != null) Parent.Children.Add(this); }
public KerbalNode(string name, KerbalNode parent = null) { Name = name; Parent = parent; Values = new PropertyDictionary <string, string>(); //Values = new Dictionary<string, string>(); Children = new List <KerbalNode>(); Parent?.Children.Add(this); }
public KerbalNode(string name, KerbalNode parent = null) { Name = name; Parent = parent; Values = new Dictionary <string, List <string> >(); Children = new List <KerbalNode>(); if (Parent != null) { Parent.Children.Add(this); } }
public void PopulateFromSource(KerbalNode node) { var v = node.Values; if (PartName == null) PartName = v["name"].First(); if (v.ContainsKey("title")) { Title = v["title"].First(); } if (v.ContainsKey("description")) { Description = v["description"].First(); } if (v.ContainsKey("cost")) { int c; Int32.TryParse(v["cost"].First(), out c); Cost = c; } if (v.ContainsKey("TechRequired")) { TechRequired = v["TechRequired"].First(); } if (v.ContainsKey("category")) { Category = v["category"].First(); } }
public override TechNode PopulateFromSource(KerbalNode sourceNode) { TechNode newNode = new TechNode(); var v = sourceNode.Values; newNode.NodePart = v.ContainsKey("nodepart") ? v["nodepart"].First() : ""; double x; double y; newNode.Id = v.ContainsKey("id") ? v["id"].First() : ""; if (v.ContainsKey("pos")) { var posString = v["pos"].First(); var coordinates = posString.Split(','); if (coordinates.Length >= 2) { if (!Double.TryParse(coordinates[0], out x)) { x = 0; } if (!Double.TryParse(coordinates[1], out y)) { y = 0; } newNode.Pos = new Point(x, y); decimal z; if (!Decimal.TryParse(coordinates[2], out z)) { newNode.Zlayer = -1; } newNode.Zlayer = (int)z; } } if (v.ContainsKey("icon")) { String iconString = v["icon"].First(); int id = Array.FindIndex(IconStringConverter.IconString, row => row == iconString); if (id != -1) { newNode.Icon = (IconsEnum)id; } else { newNode.Icon = IconsEnum.RDicon_generic; } } if (v.ContainsKey("scale")) { var s = v["scale"].First(); newNode.Scale = Double.Parse(s); } newNode.Title = v.ContainsKey("title") ? v["title"].First() : ""; newNode.Description = v.ContainsKey("description") ? v["description"].First() : ""; newNode.AnyToUnlock = false; newNode.HideEmpty = false; newNode.HideIfNoBranchParts = false; if (v.ContainsKey("cost")) { int c; if (!Int32.TryParse(v["cost"].First(), out c)) { newNode.Cost = 0; } newNode.Cost = c; } if (v.ContainsKey("anyParent")) { switch (v["anyParent"].First().Trim().ToLower()) { case "true": newNode.AnyToUnlock = true; break; } } if (v.ContainsKey("hideEmpty")) { switch (v["hideEmpty"].First().Trim().ToLower()) { case "true": newNode.HideEmpty = true; break; } } if (v.ContainsKey("hideIfNoBranchParts")) { switch (v["hideIfNoBranchParts"].First().Trim().ToLower()) { case "true": newNode.HideIfNoBranchParts = true; break; } } // Create an empty parents collection, populated during linking newNode.Parents = new List<TechNode>(); var tmpParts = new List<string>(); foreach (var child in sourceNode.Children.Where(child => child.Name == "Unlocks").Where(child => child.Values.ContainsKey("part"))) { tmpParts.AddRange(child.Values["part"]); } newNode.Parts = new List<string>(tmpParts); return newNode; }
public KerbalNode ParseTree(StreamReader sr) { var headNodeName = Path.GetFileNameWithoutExtension(_configFile); if (headNodeName != null) { headNodeName = headNodeName.ToUpper(); } // Don't validate head node since a file name can be anything var node = new KerbalNode(headNodeName); string line; string previousLine = null; var depth = 1; while ((line = sr.ReadLine()) != null) { _lineNumber++; _currentLine = line; // Used for error info only // Ignore comments and empty lines if (line.Trim().StartsWith("//") || String.IsNullOrEmpty(line)) { continue; } if (_skipDepth > -1 && _skipDepth < depth && line.Trim().Contains("{")) { depth++; } if (line.Trim().Contains("{") && _skipDepth < 0) { var tokens = line.Trim().Split('{'); var nodeName = tokens[0].Trim(); if (String.IsNullOrEmpty(nodeName)) { if (previousLine != null) { if (previousLine.Contains("//")) { var index = previousLine. IndexOf( "//", StringComparison.Ordinal); previousLine = previousLine. Substring(0, index); } nodeName = previousLine.Trim(); } else { throw new ParseErrorException( "Parse error: Unexpected '{' at: " + _lineNumber + " " + _currentLine ); } } if (IsModuleManagerNode(nodeName) || (_filters != null && depth == 1 && !_filters.Contains(nodeName))) { _skipDepth = depth; depth++; } else if (!ValidateNodeName(nodeName)) { _skipDepth = depth; depth++; } else { if (tokens.Length > 1) { line = tokens[1]; } var parentNode = node; node = new KerbalNode(nodeName, parentNode); depth++; } } if (line.Trim().Contains("=") && _skipDepth < 0) { var tokens = line.Trim().Split('='); if (tokens.Length < 2) { throw new ParseErrorException( "Parse error: Unexpected '=' sign at: " + _lineNumber + ", " + _currentLine); } if (tokens[1].Contains("}")) { var subtokens = tokens[1].Trim().Split('}'); tokens[1] = subtokens[0]; if (subtokens.Length > 2) { throw new ParseErrorException( "Parse error: Unexpected '}' sign at: " + _lineNumber + ", " + _currentLine); } line = "}" + subtokens[1]; } var property = tokens[0].Trim(); var value = tokens[1].Trim(); if (String.IsNullOrEmpty(property)) { throw new ParseErrorException( "Parse error: Unexpected '=' sign at: " + _lineNumber + ", " + _currentLine); } if (node == null) { throw new ParseErrorException( "Parse error: Unexpected property/value" + "outside node at: " + _lineNumber + ", " + _currentLine); } AddItems(property, value, node.Values); } if (line.Trim().Contains("}") && _skipDepth < 0) { if (node == null) { throw new ParseErrorException( "Parse error: Unexpected '}' sign at:" + _lineNumber + ", " + _currentLine); } depth--; // Remove first leading bracket if (line.Trim().Substring(0, 1).Equals("}")) { line = line.Trim().Substring(1); } // Reached the end of current tree start reading the // next one. if (node.Parent == null) { return(node); } node = node.Parent; } if (_skipDepth > -1 && _skipDepth < depth && line.Trim().Contains("}")) { depth--; if (depth == _skipDepth) { _skipDepth = -1; } } previousLine = line; } // Parse error on missing matching bracket unless it's the last // bracket of the file, in which case the file is "closed" if (depth > 2) { throw new ParseErrorException( "Parse Error: Missing matching bracket at: " + _lineNumber); } return(node); }
private void PopulateYongeTechNode(KerbalNode sourceNode) { var v = sourceNode.Values; NodePart = v.ContainsKey("nodepart") ? v["nodepart"].First() : ""; double x; double y; Id = v.ContainsKey("id") ? v["id"].First() : ""; if (v.ContainsKey("pos")) { var posString = v["pos"].First(); var coordinates = posString.Split(','); if (coordinates.Length >= 2) { if (!Double.TryParse(coordinates[0], out x)) { x = 0; } if (!Double.TryParse(coordinates[1], out y)) { y = 0; } Pos = new Point(x, y); decimal z; if (!Decimal.TryParse(coordinates[2], out z)) { Zlayer = -1; } Zlayer = (int)z; } } if (v.ContainsKey("icon")) { IconsEnum icon; if (!Enum.TryParse(v["icon"].First(), true, out icon)) { icon = IconsEnum.RDicon_generic; } Icon = icon; } if (v.ContainsKey("scale")) { var s = v["scale"].First(); Scale = Double.Parse(s); } Title = v.ContainsKey("title") ? v["title"].First() : ""; Description = v.ContainsKey("description") ? v["description"].First() : ""; AnyToUnlock = false; HideEmpty = false; HideIfNoBranchParts = false; if (v.ContainsKey("cost")) { int c; if (!Int32.TryParse(v["cost"].First(), out c)) { Cost = 0; } Cost = c; } if (v.ContainsKey("anyParent")) { switch (v["anyParent"].First().Trim().ToLower()) { case "true": AnyToUnlock = true; break; } } if (v.ContainsKey("hideEmpty")) { switch (v["hideEmpty"].First().Trim().ToLower()) { case "true": HideEmpty = true; break; } } if (v.ContainsKey("hideIfNoBranchParts")) { switch (v["hideIfNoBranchParts"].First().Trim().ToLower()) { case "true": HideIfNoBranchParts = true; break; } } // Create an empty parents collection, populated during linking Parents = new List<TechNode>(); var tmpParts = new List<string>(); foreach (var child in sourceNode.Children.Where(child => child.Name == "Unlocks").Where(child => child.Values.ContainsKey("part"))) { tmpParts.AddRange(child.Values["part"]); } Parts = new List<string>(tmpParts); }
// Here if you add a new treetype public void PopulateFromSource( KerbalNode sourceNode, TreeType treeType = TreeType.YongeTech) { switch(treeType) { case TreeType.YongeTech: PopulateYongeTechNode(sourceNode); break; } }
public KerbalNode ParseTree(StreamReader sr) { var headNodeName = Path.GetFileNameWithoutExtension(_configFile); if (headNodeName != null) { headNodeName = headNodeName.ToUpper(); } // Don't validate head node since a file name can be anything var node = new KerbalNode(headNodeName); string line; string previousLine = null; var depth = 1; while ((line = sr.ReadLine()) != null) { _lineNumber++; _currentLine = line; // Used for error info only // Ignore comments and empty lines if (line.Trim().StartsWith("//") || String.IsNullOrEmpty(line)) { continue; } if (_skipDepth > -1 && _skipDepth < depth && line.Trim().Contains("{")) { depth++; } if (line.Trim().Contains("{") && _skipDepth < 0) { var tokens = line.Trim().Split('{'); var nodeName = tokens[0].Trim(); if (String.IsNullOrEmpty(nodeName)) { if (previousLine != null) { if (previousLine.Contains("//")) { var index = previousLine. IndexOf( "//", StringComparison.Ordinal); previousLine = previousLine. Substring(0, index); } nodeName = previousLine.Trim(); } else { throw new ParseErrorException( "Parse error: Unexpected '{' at: " + _lineNumber + " " + _currentLine ); } } if (IsModuleManagerNode(nodeName) || (_filters != null && depth == 1 && !_filters.Contains(nodeName))) { _skipDepth = depth; depth++; } else if (!ValidateNodeName(nodeName)) { _skipDepth = depth; depth++; } else { if (tokens.Length > 1) { line = tokens[1]; } var parentNode = node; node = new KerbalNode(nodeName, parentNode); depth++; } } if (line.Trim().Contains("=") && _skipDepth < 0) { var tokens = line.Trim().Split('='); if (tokens.Length < 2) { throw new ParseErrorException( "Parse error: Unexpected '=' sign at: " + _lineNumber + ", " + _currentLine); } if (tokens[1].Contains("}")) { var subtokens = tokens[1].Trim().Split('}'); tokens[1] = subtokens[0]; if (subtokens.Length > 2) { throw new ParseErrorException( "Parse error: Unexpected '}' sign at: " + _lineNumber + ", " + _currentLine); } line = "}" + subtokens[1]; } var property = tokens[0].Trim(); var value = tokens[1].Trim(); if (String.IsNullOrEmpty(property)) { throw new ParseErrorException( "Parse error: Unexpected '=' sign at: " + _lineNumber + ", " + _currentLine); } if (node == null) { throw new ParseErrorException( "Parse error: Unexpected property/value" + "outside node at: " + _lineNumber + ", " + _currentLine); } AddItems(property, value, node.Values); } if (line.Trim().Contains("}") && _skipDepth < 0) { if (node == null) { throw new ParseErrorException( "Parse error: Unexpected '}' sign at:" + _lineNumber + ", " + _currentLine); } depth--; // Remove first leading bracket if (line.Trim().Substring(0, 1).Equals("}")) line = line.Trim().Substring(1); // Reached the end of current tree start reading the // next one. if (node.Parent == null) { return node; } node = node.Parent; } if (_skipDepth > -1 && _skipDepth < depth && line.Trim().Contains("}")) { depth--; if (depth == _skipDepth) { _skipDepth = -1; } } previousLine = line; } // Parse error on missing matching bracket unless it's the last // bracket of the file, in which case the file is "closed" if (depth > 2) { throw new ParseErrorException( "Parse Error: Missing matching bracket at: " + _lineNumber); } return node; }
public abstract TechNode PopulateFromSource(KerbalNode sourceNode);
public void PopulateFromSource( KerbalNode sourceNode, TreeType treeType = TreeType.TechMananger) { var v = sourceNode.Values; NodeName = v.ContainsKey("name") ? v["name"].First() : ""; double x; double y; switch (treeType) { case TreeType.TechMananger: TechId = v.ContainsKey("techID") ? v["techID"].First() : ""; if (v.ContainsKey("pos")) { var posString = v["pos"].First(); var coordinates = posString.Split(','); if (coordinates.Length >= 2) { if (!Double.TryParse(coordinates[0], out x)) { x = 0; } if (!Double.TryParse(coordinates[1], out y)) { y = 0; } Pos = new Point(x, y); decimal z; if (!Decimal.TryParse(coordinates[2], out z)) { Zlayer = -1; } Zlayer = (int)z; } } break; case TreeType.ATC: TechId = GenerateTechId(); x = 0; y = 0; if (v.ContainsKey("posX")) { if (!Double.TryParse(v["posX"].First(), out x)) { x = 0; } } if (v.ContainsKey("posY")) { if (!Double.TryParse(v["posY"].First(), out y)) { y = 0; } } Pos = new Point(x, y); break; } if (v.ContainsKey("icon")) { Icon icon; if (!Enum.TryParse(v["icon"].First(), true, out icon)) { icon = Icon.UNDEFINED; } Icon = icon; } Title = v.ContainsKey("title") ? v["title"].First() : ""; Description = v.ContainsKey("description") ? v["description"].First() : ""; AnyParent = false; HideIfEmpty = false; HideIfNoBranchParts = false; if (treeType == TreeType.ATC) { if (v.ContainsKey("scienceCost")) { int c; if (!Int32.TryParse(v["scienceCost"].First(), out c)) { Cost = 0; } Cost = c; } if (v.ContainsKey("anyParentUnlocks")) { switch (v["anyParentUnlocks"].First().Trim().ToLower()) { case "true": AnyParent = true; break; } } if (v.ContainsKey("hideIfNoparts")) { switch (v["hideIfNoparts"].First().Trim().ToLower()) { case "true": HideIfEmpty = true; break; } } } else { if (v.ContainsKey("cost")) { int c; if (!Int32.TryParse(v["cost"].First(), out c)) { Cost = 0; } Cost = c; } if (v.ContainsKey("anyParent")) { switch (v["anyParent"].First().Trim().ToLower()) { case "true": AnyParent = true; break; } } if (v.ContainsKey("hideIfEmpty")) { switch (v["hideIfEmpty"].First().Trim().ToLower()) { case "true": HideIfEmpty = true; break; } } if (v.ContainsKey("hideIfNoBranchParts")) { switch (v["hideIfNoBranchParts"].First().Trim().ToLower()) { case "true": HideIfNoBranchParts = true; break; } } } // Create an empty parents collection, populated during linking Parents = new List<TechNode>(); var tmpParts = new List<string>(); foreach (var child in sourceNode.Children. Where(child => child.Name == "PARTS"). Where(child => child.Values.ContainsKey("name"))) { tmpParts.AddRange(child.Values["name"]); } Parts = new List<string>(tmpParts); }