Skip to content

Commit

Permalink
Improve parsing of <import> directives.
Browse files Browse the repository at this point in the history
  • Loading branch information
kythyria committed Jul 29, 2021
1 parent 928a563 commit 29cdc9e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
8 changes: 6 additions & 2 deletions PD2ModelParser/Modelscript/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,24 @@ 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;

if (!prop.CanWrite)
continue;

var pt = prop.PropertyType;
var pt = underlying ?? prop.PropertyType;

Type typ; Func<string, object> parser; string errmsg;
if (pt.BaseType == typeof(Enum))
Expand Down
29 changes: 7 additions & 22 deletions PD2ModelParser/Modelscript/ScriptCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> Parents { get; set; } = new Dictionary<string, string>();
public Dictionary<string, string> ImporterOptions { get; set; } = new Dictionary<string, string>();
[XmlAttribute("type")] public FileTypeInfo ForceType { get; set; }
public bool? CreateNewObjects { get; set; }

[NotAttribute] public string DefaultRootPoint { get; set; }
[NotAttribute] public Dictionary<string, string> Parents { get; set; } = new Dictionary<string, string>();
[NotAttribute] public Dictionary<string, string> ImporterOptions { get; set; } = new Dictionary<string, string>();

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())
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 29cdc9e

Please sign in to comment.