Exemple #1
0
        public static DataDefinition LoadDefinition(XElement element, string forceLoadAs = null)
        {
            if (element.Name.ToString() == "Const")
            {
                var constDef = new ConstDefinition();
                constDef.Parse(element);
                return(constDef);
            }

            var name = element.Attribute(MetaNS + "RefKey")?.Value.ToString().ToUpper();

            if (name == null)
            {
                name = element.Attribute("RefKey")?.Value.ToString().ToUpper();
            }
            if (name == null)
            {
                name = element.Name.ToString().ToUpper();
            }

            if (name.EndsWith("DEF"))
            {
                name = name.Substring(0, name.Length - "DEF".Length);
            }

            if (forceLoadAs != null)
            {
                name = forceLoadAs.ToUpper();
            }

            DataDefinition definition = null;

            if (name == "STRING")
            {
                definition = new StringDefinition();
            }
            else if (name == "MULTILINESTRING")
            {
                definition = new MultilineStringDefinition();
            }
            else if (name == "STRUCT")
            {
                definition = new StructDefinition();
            }
            else if (name == "REFERENCE")
            {
                definition = new ReferenceDefinition();
            }
            else if (name == "COLLECTION")
            {
                definition = new CollectionDefinition();
            }
            else if (name == "NUMBER")
            {
                definition = new NumberDefinition();
            }
            else if (name == "BOOLEAN")
            {
                definition = new BooleanDefinition();
            }
            else if (name == "COLOUR")
            {
                definition = new ColourDefinition();
            }
            else if (name == "ENUM")
            {
                definition = new EnumDefinition();
            }
            else if (name == "FLAGS")
            {
                definition = new FlagsDefinition();
            }
            else if (name == "FILE")
            {
                definition = new FileDefinition();
            }
            else if (name == "TREE")
            {
                definition = new TreeDefinition();
            }
            else if (name == "VECTOR")
            {
                definition = new VectorDefinition();
            }
            else if (name == "TIMELINE")
            {
                definition = new TimelineDefinition();
            }
            else if (name == "GRAPHSTRUCT")
            {
                definition = new GraphStructDefinition();
            }
            else if (name == "GRAPHCOLLECTION")
            {
                definition = new GraphCollectionDefinition();
            }
            else if (name == "GRAPHREFERENCE")
            {
                definition = new GraphReferenceDefinition();
            }
            else if (name == "KEYFRAME")
            {
                definition = new KeyframeDefinition();
            }
            else if (name == "COMMENT")
            {
                definition = new CommentDefinition();
            }
            else if (name == "SKELETALANIMATION")
            {
                definition = new SkeletalAnimationDefinition();
            }
            else
            {
                throw new Exception("Unknown definition type " + name + "!");
            }

            definition.Name = element.Attribute("Name")?.Value?.ToString();
            if (string.IsNullOrWhiteSpace(definition.Name))
            {
                definition.Name = definition.GetType().ToString().Replace("Definition", "");
            }

            definition.ToolTip = element.Attribute("ToolTip")?.Value?.ToString();

            var col = element.Attribute("TextColour")?.Value?.ToString();

            if (col != null)
            {
                if (Colours.ContainsKey(col))
                {
                    col = Colours[col];
                }
                definition.TextColour = col;
            }

            var attEl = element.Element("Attributes");

            if (attEl != null)
            {
                foreach (var att in attEl.Elements())
                {
                    var attDef = LoadDefinition(att);
                    if (attDef is PrimitiveDataDefinition)
                    {
                        definition.Attributes.Add(attDef as PrimitiveDataDefinition);
                    }
                    else
                    {
                        throw new Exception("Cannot put a non-primitive into attributes!");
                    }
                }
            }

            definition.IsGlobal      = definition.TryParseBool(element, "IsGlobal");
            definition.VisibleIf     = element.Attribute("VisibleIf")?.Value?.ToString();
            definition.SkipIfDefault = definition.TryParseBool(element, "SkipIfDefault", true);

            definition.Parse(element);

            return(definition);
        }
        protected override void DoRecursivelyResolve(Dictionary <string, DataDefinition> local, Dictionary <string, DataDefinition> global, Dictionary <string, Dictionary <string, DataDefinition> > referenceableDefinitions)
        {
            if (DefKey != null)
            {
                var key = DefKey.ToLower();

                Dictionary <string, DataDefinition> defs = null;
                if (local.ContainsKey(key))
                {
                    defs = local;
                }
                else if (global.ContainsKey(key))
                {
                    defs = global;
                }

                if (defs != null)
                {
                    var def = defs[key] as ReferenceDefinition;

                    foreach (var keydef in def.Keys)
                    {
                        var childDef = def.Definitions[keydef.Item1];
                        childDef.RecursivelyResolve(local, global, referenceableDefinitions);

                        var keyframeDef = new KeyframeDefinition();
                        keyframeDef.CreateFrom(childDef);

                        KeyframeDefinitions.Add(keyframeDef);
                        Keys.Add(new Tuple <KeyframeDefinition, string>(keyframeDef, keydef.Item2));
                    }
                }
                else
                {
                    throw new Exception("Failed to find key " + DefKey + "!");
                }
            }

            foreach (var key in DefKeys)
            {
                Dictionary <string, DataDefinition> defs = null;
                if (local.ContainsKey(key.Item1.ToLower()))
                {
                    defs = local;
                }
                else if (global.ContainsKey(key.Item1.ToLower()))
                {
                    defs = global;
                }

                if (defs != null)
                {
                    var childDef = defs[key.Item1.ToLower()];
                    childDef.RecursivelyResolve(local, global, referenceableDefinitions);

                    var keyframeDef = new KeyframeDefinition();
                    keyframeDef.CreateFrom(childDef);

                    KeyframeDefinitions.Add(keyframeDef);
                    Keys.Add(new Tuple <KeyframeDefinition, string>(keyframeDef, key.Item2));
                }
                else if (key.Item1 != "---")
                {
                    throw new Exception("Failed to find key " + key.Item1 + "!");
                }
            }

            foreach (var def in KeyframeDefinitions)
            {
                def.RecursivelyResolve(local, global, referenceableDefinitions);
            }
        }