示例#1
0
        private static IScriptItem ParseXmlObject3d(XElement elem)
        {
            var name = ScriptXml.RequiredAttr(elem, "name");
            var mode = ScriptXml.RequiredAttr(elem, "mode");

            var pos   = new Vector3?();
            var scale = new Vector3?();
            var rot   = new Quaternion?();

            var    setParent = false;
            string parent    = null;

            foreach (var child in elem.Elements())
            {
                switch (child.Name.ToString())
                {
                case "position":
                    pos = ScriptXml.VectorElement(child);
                    break;

                case "rotation":
                    rot = ScriptXml.QuaternionElement(child);
                    break;

                case "scale":
                    scale = ScriptXml.VectorElement(child);
                    break;

                case "parent":
                    var root       = child.Attribute("root")?.Value;
                    var parentname = child.Attribute("name")?.Value;
                    if (root != null)
                    {
                    }
                    else if (root != null && parentname != null)
                    {
                        throw new Exception("parent must have either root or name attributes, not both");
                    }
                    else
                    {
                        parent = parentname;
                    }
                    setParent = true;
                    break;

                default:
                    throw new Exception($"Invalid Object3D child element {elem.Name}");
                }
            }

            if (mode == "add")
            {
                if (!setParent)
                {
                    throw new Exception("Newly created objects must have their parents explicitly given");
                }
                var result = new CreateObject3d()
                {
                    Name   = name,
                    Parent = parent
                };
                pos.WithValue(p => result.Position = p);
                scale.WithValue(s => result.Scale  = s);
                rot.WithValue(r => result.Rotation = r);
                return(result);
            }
            else if (mode == "edit")
            {
                return(new ModifyObject3d
                {
                    Name = name,
                    Parent = parent,
                    SetParent = setParent,
                    Position = pos,
                    Rotation = rot,
                    Scale = scale
                });
            }
            else
            {
                throw new Exception($"Object3D mode must be \"edit\" or \"add\".");
            }
        }
示例#2
0
        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}\"");
            }

            foreach (var child in element.Elements())
            {
                switch (child.Name.ToString())
                {
                case "rootpoint":
                    var targetname = ScriptXml.RequiredAttr(child, "name");
                    foreach (var rpitem in child.Elements())
                    {
                        switch (rpitem.Name.ToString())
                        {
                        case "object":
                            var childName = ScriptXml.RequiredAttr(rpitem, "name");
                            if (this.Parents.ContainsKey(childName))
                            {
                                throw new Exception($"Cannot redefine rootpoint for object {childName}");
                            }
                            this.Parents.Add(childName, targetname);
                            break;

                        case "default":
                            if (this.DefaultRootPoint == null)
                            {
                                this.DefaultRootPoint = targetname;
                            }
                            else
                            {
                                throw new Exception($"Cannot redefine default rootpoint to {targetname}");
                            }
                            break;

                        default:
                            throw new Exception($"Invalid <rootpoint> child: {rpitem.Name}");
                        }
                    }
                    break;

                case "option":
                    this.ImporterOptions.Add(ScriptXml.RequiredAttr(child, "name"), child.Value.Trim());
                    break;
                }
            }
        }