From 29cdc9e49d61c6867bb3d22c5b01adbcbc222e52 Mon Sep 17 00:00:00 2001 From: Kythyria Tieran Date: Thu, 29 Jul 2021 14:29:57 +0100 Subject: [PATCH] Improve parsing of directives. --- PD2ModelParser/Modelscript/Script.cs | 8 ++++-- PD2ModelParser/Modelscript/ScriptCommands.cs | 29 +++++--------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/PD2ModelParser/Modelscript/Script.cs b/PD2ModelParser/Modelscript/Script.cs index 674d462..394f7ee 100644 --- a/PD2ModelParser/Modelscript/Script.cs +++ b/PD2ModelParser/Modelscript/Script.cs @@ -286,12 +286,16 @@ public virtual void ParseXml(XElement elem) continue; } + var underlying = Nullable.GetUnderlyingType(prop.PropertyType); + var requiredattr = (RequiredAttribute)(prop.GetCustomAttributes(typeof(RequiredAttribute), true).FirstOrDefault()); var nameoverride = (XmlAttributeAttribute)(prop.GetCustomAttributes(typeof(XmlAttributeAttribute), true).FirstOrDefault()); var attrname = nameoverride?.AttributeName ?? prop.Name.ToLower(); string attrvalue = elem.Attribute(attrname)?.Value; - if (requiredattr != null && attrvalue == null) + if (underlying != null && attrvalue == null) + continue; + else if (requiredattr != null && attrvalue == null) throw new Exception($"Missing \"{attrname}\" attribute for <{elem.Name}> element"); else if (requiredattr == null && attrvalue == null) continue; @@ -299,7 +303,7 @@ public virtual void ParseXml(XElement elem) if (!prop.CanWrite) continue; - var pt = prop.PropertyType; + var pt = underlying ?? prop.PropertyType; Type typ; Func parser; string errmsg; if (pt.BaseType == typeof(Enum)) diff --git a/PD2ModelParser/Modelscript/ScriptCommands.cs b/PD2ModelParser/Modelscript/ScriptCommands.cs index e118cd5..25e30a9 100644 --- a/PD2ModelParser/Modelscript/ScriptCommands.cs +++ b/PD2ModelParser/Modelscript/ScriptCommands.cs @@ -73,32 +73,16 @@ public override void Execute(ScriptState state) public class Import : ScriptItem { public string File { get; set; } - public FileTypeInfo ForceType { get; set; } - public string DefaultRootPoint { get; set; } - public Dictionary Parents { get; set; } = new Dictionary(); - public Dictionary ImporterOptions { get; set; } = new Dictionary(); + [XmlAttribute("type")] public FileTypeInfo ForceType { get; set; } public bool? CreateNewObjects { get; set; } + [NotAttribute] public string DefaultRootPoint { get; set; } + [NotAttribute] public Dictionary Parents { get; set; } = new Dictionary(); + [NotAttribute] public Dictionary ImporterOptions { get; set; } = new Dictionary(); + public override void ParseXml(XElement element) { - this.File = ScriptXml.RequiredAttr(element, "file"); - - var strType = element.Attribute("type")?.Value; - if (FileTypeInfo.TryParseName(strType, out var type)) - { - this.ForceType = type; - } - else { this.ForceType = null; } - - var strCreateObjects = element.Attribute("create_objects")?.Value; - if (strCreateObjects != null && bool.TryParse(strCreateObjects, out var createObjects)) - { - this.CreateNewObjects = createObjects; - } - else if (strCreateObjects != null) - { - throw new Exception($"create_objects must be boolean, \"{bool.TrueString}\" or \"{bool.FalseString}\""); - } + base.ParseXml(element); foreach (var child in element.Elements()) { @@ -194,6 +178,7 @@ S.Object3D ParentFinder(string name) opts.AddOption(kv.Key, kv.Value); } + state.Log.Status("CreateNewObjects: {0} ?? {1}", CreateNewObjects, state.CreateNewObjects); bool createObjects = CreateNewObjects ?? state.CreateNewObjects; effectiveType.Import(state.Data, filepath, createObjects, ParentFinder, opts);