public override List<Target> ProcessFile() { XmlReader fr = XmlReader.Create(this.FilePath); List<Target> _output = new List<Target>(); Target _current_target; while (fr.Read()) // read the xml file until the end. { string node_name = fr.Name.ToLower(); switch (fr.NodeType) { case XmlNodeType.Element: if (node_name != "targets" && node_name != "target") // if element is not named Targets or Target, file format is invalid. { throw new XmlException("Invalid Format."); } // if element is named Target, read it's attributes and store them as a Target. else if (node_name == "target") { _current_target = new Target(); while (fr.MoveToNextAttribute()) { // each attribute should be a target object attribute name and corresponding value. string attribute_name = fr.Name.ToLower(); string attribute_value = fr.Value.ToLower(); if (attribute_name != "isfriend" && attribute_name != "name") { SetTargetPositionValue(_current_target, attribute_name, attribute_value); } else { if (attribute_name == "isfriend") { SetTargetFriend(_current_target, attribute_value); } else if (attribute_name == "name") { _current_target.Name = attribute_value; } } } _output.Add(_current_target); } break; case XmlNodeType.Comment: // ignore comments break; case XmlNodeType.XmlDeclaration: // ignore declarations break; case XmlNodeType.Whitespace: // ignore whitespace break; case XmlNodeType.EndElement: // ignore end elements break; default: // if defaulted, throw format is invalid. throw new XmlException("Invalid Format."); } } return _output; }
/// <summary> /// Process ini file which processor was instantianted with. /// </summary> /// <returns> A List of Target objects</returns> /// <exception cref="InvalidIniFormat"></exception> public override List<Target> ProcessFile() { string[] fileLines = File.ReadAllLines(this.FilePath); List<Target> _output = new List<Target>(); Target _current_target = null; foreach (string line in fileLines) { string trimedLine = line.Trim(); if (trimedLine.StartsWith(";")) // ignore comments, continue on to next line. { continue; } else if (string.IsNullOrEmpty(trimedLine)) // also ignore blank lines. { continue; } else { if (lineMatches(trimedLine)) // if line is a group declaration add new target to the list. { _current_target = new Target(); _output.Add(_current_target); } else // line is a key=value pair. { // if (_current_target == null) { throw new InvalidIniFormat(_invalid_ini_format_message); } string[] keyvalue = trimedLine.Split('='); string key = keyvalue[0].Trim().ToLower(); // grab key, trim whitespace, and make all lower. string value = keyvalue[1].Trim().ToLower(); // same as above but for value. if (key == "friend") // if the left side of the keyvalue pair is "isFriend" set the friend value of the target. { TargetSetFriend(_current_target, value); } else if (key == "name") // if the left side of the keyvalue pair is "name" set the name of the target. { _current_target.Name = value; } else { TargetSetPositionValue(_current_target, key, value); } } } } return _output; }
public void GoTo(Target t) { if (!IsMoving) { var path = new Tuple <Target, Target>(Target, t); MoveDistance = Movements[path]; } if (MoveDistance == 0) { Wait(); return; } Console.WriteLine($"GOTO {t.ToString()}"); MoveDistance--; }
/// <summary> /// Set x,y,z position value, or throw an error if attribute_name isn't xpos, ypos, or zpos. /// </summary> /// <param name="_current_target">a Target obj</param> /// <param name="attribute_name">a string containing the attribute name</param> /// <param name="value">a string containing the attribue value</param> private void SetTargetPositionValue(Target _current_target, string attribute_name, string value) { switch (attribute_name) { case "xpos": _current_target.X_coordinate = Convert.ToDecimal(value); break; case "ypos": _current_target.Y_coordinate = Convert.ToDecimal(value); break; case "zpos": _current_target.Z_coordinate = Convert.ToDecimal(value); break; default: throw new XmlException("Invalid Format"); } }
/// <summary> /// Set the friend property of the target /// </summary> /// <param name="_current_target">A target object</param> /// <param name="attribute_value">a string containing "true" or "false"</param> private void SetTargetFriend(Target _current_target, string attribute_value) { if (attribute_value == "true") { _current_target.Friend = true; } else if (attribute_value == "false") { _current_target.Friend = false; } else { throw new XmlException("Invalid Format"); } }
/// <summary> /// Set x,y,z position value of target /// </summary> /// <param name="_current_target">A target object</param> /// <param name="key">string containing the key</param> /// <param name="value">string containing the value</param> private void TargetSetPositionValue(Target _current_target, string key, string value) { /* try/catch exceptions from Convert operation, solely for the purpose of changing them to InvalidIniFormat exceptions */ try { /* turn the left side of the key value pair into ASCII and check to make sure it is either x, y, or z, then add value to target if possible. */ switch (key) { case "x": _current_target.X_coordinate = Convert.ToDecimal(value); break; case "y": _current_target.Y_coordinate = Convert.ToDecimal(value); break; case "z": _current_target.Z_coordinate = Convert.ToDecimal(value); break; default: // if it reaches this point, it is an invalid file throw new InvalidIniFormat(_invalid_ini_format_message); } } catch (FormatException ex) // change exception from convert to invalidiniformat, but keep previous exception around for debugging. { throw new InvalidIniFormat(_invalid_ini_format_message, ex); } catch (OverflowException ex) // change exception from convert to invalidinitformat, but keep previous exception around for debugging. { throw new InvalidIniFormat(_invalid_ini_format_message, ex); } }
/// <summary> /// Set the friend property /// </summary> /// <param name="_current_target">A target object</param> /// <param name="value">string containing "true" or "false"</param> private void TargetSetFriend(Target _current_target, string value) { if (value == "yes") { _current_target.Friend = true; } else if (value == "no") { _current_target.Friend = false; } else { throw new InvalidIniFormat(_invalid_ini_format_message); // if the value is invalid throw exception. } }