/// <summary> /// This method will process a single number (integer) /// command-line argument. /// </summary> /// <param name="option">The CommandLineOption object (int)</param> /// <param name="theArg">The single command-line argument</param> private void _ProcessNumber(CommandLineOption <int> option, string theArg) { var valueFromArg = theArg.Split('=')[1]; var isRequired = IsPropertyMarkedAsRequired(option); if (isRequired) { option.LoadError = valueFromArg.Length == 0 ? true : false; } else { option.LoadError = false; } if (option.LoadError) { var msg = string.Format(Messages.Error_ErrorParsingOptionNoValueWasSet, new String(' ', 10), OptionPrefix + option.Name); LoadErrors.Add(msg); } option.OptionValue = Convert.ToInt32(valueFromArg); }
/// <summary> /// Download all docs. /// </summary> public void DownloadAll() { ConcurrentBag <ZipArchive> zips = new ConcurrentBag <ZipArchive>(); List <ManualResetEvent> resets = new List <ManualResetEvent>(); foreach (string src in DENIZEN_SOURCES.JoinWith(DENIZEN_ADDON_SOURCES)) { ManualResetEvent evt = new ManualResetEvent(false); resets.Add(evt); Task.Factory.StartNew(() => { try { ZipArchive zip = DownloadZip(src); zips.Add(zip); } catch (Exception ex) { Console.WriteLine($"Zip download exception {ex}"); LoadErrors.Add($"Zip download error: {ex.GetType().Name}: {ex.Message}"); } evt.Set(); }); } foreach (ManualResetEvent evt in resets) { evt.WaitOne(); } foreach (ZipArchive zip in zips) { try { string[] fullLines = ReadLines(zip); LoadDataFromLines(fullLines); } catch (Exception ex) { LoadErrors.Add($"Internal exception - {ex.GetType().FullName} ... see bot console for details."); Console.WriteLine($"Error: {ex.ToString()}"); } } foreach (MetaObject obj in AllMetaObjects()) { try { obj.PostCheck(this); obj.Searchable = obj.GetAllSearchableText().ToLowerFast(); } catch (Exception ex) { LoadErrors.Add($"Internal exception while checking {obj.Type.Name} '{obj.Name}' - {ex.GetType().FullName} ... see bot console for details."); Console.WriteLine($"Error with {obj.Type.Name} '{obj.Name}': {ex.ToString()}"); } } foreach (string str in LoadErrors) { Console.WriteLine($"Load error: {str}"); } }
/// <summary> /// This method will process a single string /// command-line argument. /// </summary> /// <param name="option">The CommandLineOption object (string)</param> /// <param name="theArg">The single command-line argument</param> private void _ProcessString(CommandLineOption <string> option, string theArg) { var valueFromArg = theArg.Split('=')[1]; var isRequired = IsPropertyMarkedAsRequired(option); if (isRequired) { option.LoadError = valueFromArg.Length == 0 ? true : false; } else { option.LoadError = false; } if (option.LoadError) { var msg = string.Format(Messages.Error_ErrorParsingOptionNoValueWasSet, new String(' ', 10), OptionPrefix + option.Name); LoadErrors.Add(msg); } // Now, if this property is marked with the [IsExistingFolder] attribute, // ensure that the value is an actual existing folder. if (IsPropertyMarkedAsExistingFolder(option)) { // Only do this if the value of the argument // has a length > 0. // No sense in checking for a directory of zero length. // Zero length arguments were handled in the previous // code block :) if (valueFromArg.Length > 0) { var folder = valueFromArg.Trim(); if (!Directory.Exists(folder)) { // Error: folder doesn't exist option.LoadError = true; var msg = string.Format(Messages.Error_ErrorParsingOptionTheFolderDoesNotExist, new String(' ', 10), OptionPrefix + option.Name, folder); LoadErrors.Add(msg); } } } option.OptionValue = valueFromArg; }
/// <summary> /// Load an object into the meta docs from the object's text definition. /// </summary> public void LoadInObject(string objectType, string file, string[] objectData) { try { if (!MetaObjectGetters.TryGetValue(objectType.ToLowerFast(), out Func <MetaObject> getter)) { LoadErrors.Add($"While processing {file} found unknown meta type '{objectType}'."); return; } MetaObject obj = getter(); string curKey = null; string curValue = null; foreach (string line in objectData) { if (line.StartsWith("@")) { if (curKey != null && curValue != null) { if (!obj.ApplyValue(curKey.ToLowerFast(), curValue.Trim(' ', '\t', '\n'))) { LoadErrors.Add($"While processing {file} in object type '{objectType}' for '{obj.Name}' could not apply key '{curKey}' with value '{curValue}'."); } curKey = null; curValue = null; } int space = line.IndexOf(' '); if (space == -1) { curKey = line.Substring(1); if (curKey == "end_meta") { break; } continue; } curKey = line.Substring(1, space - 1); curValue = line.Substring(space + 1); } else { curValue += "\n" + line; } } obj.AddTo(this); } catch (Exception ex) { Console.WriteLine($"Error in file {file} for object type {objectType}: {ex}"); LoadErrors.Add($"Error in file {file} for object type {objectType}: {ex.GetType().Name}: {ex.Message} ... see console for details."); } }
/// <summary> /// Load the meta doc data from lines. /// </summary> public void LoadDataFromLines(string[] lines) { string file = "<unknown>"; for (int i = 0; i < lines.Length; i++) { string line = lines[i]; if (line.StartsWith(START_OF_FILE_PREFIX)) { file = line.Substring(START_OF_FILE_PREFIX.Length); } else if (line.StartsWith("<--[") && line.EndsWith("]")) { string objectType = line.Substring("<--[".Length, line.Length - "<--[]".Length); List <string> objectData = new List <string>(); for (i++; i < lines.Length; i++) { if (lines[i] == "-->") { break; } else if (lines[i].StartsWith("<--[")) { LoadErrors.Add($"While processing {file} at line {i + 1} found the start of a meta block, while still processing the previous meta block."); break; } else if (lines[i] == END_OF_FILE_MARK || lines[i].StartsWith(START_OF_FILE_PREFIX)) { LoadErrors.Add($"While processing {file} was not able to find the end of an object's documentation!"); objectData = null; break; } objectData.Add(lines[i]); } if (objectData == null) { continue; } objectData.Add("@end_meta"); LoadInObject(objectType, file, objectData.ToArray()); } } }
/// <summary> /// Downloads guide source info. /// </summary> public void ReadGuides() { HttpClient client = new HttpClient { Timeout = new TimeSpan(0, 2, 0) }; string page = client.GetStringAsync(DENIZEN_GUIDE_SOURCE).Result; int contentIndex = page.IndexOf("<div class=\"section\" id=\"contents\">"); if (contentIndex == -1) { LoadErrors.Add("Guide page did not match expected format (table of contents div missing)."); return; } page = page.Substring(contentIndex); int linkIndex = 0; const string link_reference_text = "<a class=\"reference internal\" href=\""; while ((linkIndex = page.IndexOf(link_reference_text, linkIndex)) >= 0) { int linkEndIndex = page.IndexOf("</a>", linkIndex); string linkBody = DENIZEN_GUIDE_SOURCE + page[(linkIndex + link_reference_text.Length)..linkEndIndex];
public Actors(string FileName, string PackageName = "") { if (FileName.FileExists() == false) { throw new System.IO.FileNotFoundException(FileName); } BaseDirectory = FileName.GetDirectoryFromFileLocation(); XElement xelement = XElement.Load(FileName); this.ID = xelement.Element("id").Value; this.Name = xelement.Element("name").Value; this.DisplayName = xelement.Element("displayname").Value; this.Description = xelement.Element("description").Value; this.AuthorName = xelement.Element("author").Value; this.UpdateDate = xelement.Element("date").Value; this.PackageName = PackageName; IEnumerable <XElement> ActorObjects = xelement.Elements().Where(x => x.Name.ToString() == "actors"); foreach (var ActorObject in ActorObjects.Elements().Where(x => x.Name.ToString() == "actor")) { ActorInfo _NewActor = new ActorInfo(); _NewActor.ID = ActorObject.Element("id").Value; _NewActor.Name = ActorObject.Element("name").Value; _NewActor.DisplayName = ActorObject.Element("displayname").Value; _NewActor.Description = ActorObject.Element("description").Value; _NewActor.AuthorName = ActorObject.Element("author").Value; _NewActor.UpdateDate = ActorObject.Element("date").Value; _NewActor.DefaultFrameDelay = ActorObject.Element("defaultframedelay").Value.ToInt(0); IEnumerable <XElement> ActorAnimationObjects = ActorObject.Elements().Where(x => x.Name.ToString() == "animations"); foreach (var ActorAnimationObject in ActorAnimationObjects.Elements().Where(x => x.Name.ToString() == "animation")) { ActorAnimation _NewAnimation = new ActorAnimation(); _NewAnimation.Name = ActorAnimationObject.Element("name").Value; _NewAnimation.Filename = ActorAnimationObject.Element("filename").Value; if (System.IO.File.Exists(this.BaseDirectory + _NewAnimation.Filename) == false) { this.LoadErrors.Add(_NewActor.ID + "-" + _NewAnimation.Name + "-Error Locating Image File", this.BaseDirectory.EnsureDirectoryFormat() + _NewAnimation.Filename); continue; } if (this.Images.ContainsKey(_NewAnimation.Filename)) { } else { this.Images.Add(_NewAnimation.Filename, new CompressibleImage(System.Drawing.Image.FromFile(this.BaseDirectory.EnsureDirectoryFormat() + _NewAnimation.Filename), System.Drawing.Imaging.ImageFormat.Png)); } _NewAnimation.Direction = ActorAnimationObject.Element("direction").Value; try { _NewAnimation.DirectionValue = (Enumerations.RelativePosition)Enum.Parse(typeof(Enumerations.RelativePosition), _NewAnimation.Direction); } catch { LoadErrors.Add(_NewActor.ID + "-Animation-" + _NewAnimation.Name + "Direction", "Animation: " + _NewAnimation.Name + " - Error Converting Direction"); } IEnumerable <XElement> ActorAnimationFrameObjects = ActorAnimationObject.Elements().Where(x => x.Name.ToString() == "frames"); foreach (var ActorAnimationFrameObject in ActorAnimationFrameObjects.Elements().Where(x => x.Name.ToString() == "frame")) { ActorAnimationFrame _NewFrame = new ActorAnimationFrame(); _NewFrame.ID = ActorAnimationFrameObject.Attribute("id").Value.ToInt(-1); if (ActorAnimationFrameObject.Attribute("nextid").Value.StartsWith("{")) { // TODO Add Split Method } else { _NewFrame.NextID.Add(ActorAnimationFrameObject.Attribute("nextid").Value.ToInt(-1)); } var DrawingRec2 = ActorAnimationFrameObject.Attribute("imagerec").Value.ToRectangle(); if (DrawingRec2 == System.Drawing.Rectangle.Empty) { _NewFrame.ImageRect = Microsoft.Xna.Framework.Rectangle.Empty; LoadErrors.Add(_NewActor.ID + "-Animation-" + _NewAnimation.Name + "-Frame-" + _NewFrame.ID.ToString(), "Invalid Source Rect"); } else { _NewFrame.ImageRect = new Microsoft.Xna.Framework.Rectangle(DrawingRec2.X, DrawingRec2.Y, DrawingRec2.Width, DrawingRec2.Height); } try { var Anchor = ActorAnimationFrameObject.Attribute("anchor").Value; string[] _Points = Anchor.SplitString(",", StringSplitOptions.RemoveEmptyEntries); if (_Points.Length == 2) { _NewFrame.Anchor = new Vector2(_Points[0].ToInt(0), _Points[1].ToInt(0)); } else { _NewFrame.Anchor = new Vector2(0, 0); } } catch { _NewFrame.Anchor = new Vector2(0, 0); } try { _NewFrame.Delay = ActorAnimationFrameObject.Attribute("delay").Value.ToInt(_NewActor.DefaultFrameDelay); } catch { _NewFrame.Delay = _NewActor.DefaultFrameDelay; } _NewAnimation.Frames.Add(_NewFrame.ID, _NewFrame); } if (_NewActor.Animations.ContainsKey(_NewAnimation.Name + _NewAnimation.Direction)) { LoadErrors.Add(_NewActor.ID + "-Animation-" + _NewAnimation.Name + "-DuplicateAnimationName", "AnimationName: " + _NewAnimation.Name + _NewAnimation.Direction + " - Duplicate Animation Name Found"); } else { _NewActor.Animations.Add(_NewAnimation.Name + _NewAnimation.Direction, _NewAnimation); } } AllActors.Add(_NewActor.Name, _NewActor); } }