Exemple #1
0
        public void LoadXml(string filename)
        {
            Prototypes = new SortedDictionary <long, NomadObject>();

            var inputDir = Path.GetDirectoryName(filename);

            var xml = new XmlDocument();

            xml.Load(filename);

            var libsElem = xml.DocumentElement;

            if ((libsElem == null) || (libsElem.Name != "EntityLibraries"))
            {
                throw new InvalidOperationException("Not a valid EntityLibraries node");
            }

            var versionAttr = new AttributeData(DataType.Int32, libsElem.GetAttribute("Version"));
            var version     = versionAttr.ToInt32();

            if (version == 0)
            {
                throw new InvalidDataException("Invalid/Missing 'Version' attribute!");
            }

            Use64Bit = (version == 2);

            Root = new NomadObject("EntityLibraries");

            Context.Log("Loading libraries...");
            foreach (var node in libsElem.ChildNodes.OfType <XmlElement>())
            {
                if (node.Name != "EntityLibrary")
                {
                    throw new InvalidDataException($"What the hell do I do with a '{node.Name}' element?!");
                }

                var libName = node.GetAttribute("Name");

                if (String.IsNullOrEmpty(libName))
                {
                    throw new InvalidDataException($"EntityLibrary is malformed: no name specified!");
                }

                var lib = new NomadObject("EntityLibrary");
                lib.SetAttributeValue("Name", DataType.String, libName);

                Root.Children.Add(lib);

                foreach (var childNode in node.ChildNodes.OfType <XmlElement>())
                {
                    if (childNode.Name != "Include")
                    {
                        throw new InvalidDataException($"EntityLibrary['{libName.ToString()}'] is malformed: unexpected element '{childNode.Name}'");
                    }

                    var pathAttr = childNode.GetAttribute("Path");

                    if (String.IsNullOrEmpty(pathAttr))
                    {
                        throw new InvalidDataException($"EntityLibrary['{libName.ToString()}'] is malformed: include element is missing path!");
                    }

                    var libFile = Path.Combine(inputDir, pathAttr);

                    Context.LogDebug($"Loading archetype '{libFile}'...");
                    var arkRes = ResourceFactory.Open(libFile);

                    var ark   = arkRes.Root;
                    var arkId = ark.GetAttribute("UID");

                    long uid = 0;

                    if (Use64Bit)
                    {
                        uid = BitConverter.ToInt64(arkId.Data.Buffer, 0);
                    }
                    else
                    {
                        uid = BitConverter.ToUInt32(arkId.Data.Buffer, 0);
                    }

                    // remove attributes
                    ark.Attributes.Clear();

                    Prototypes.Add(uid, ark);
                    lib.Children.Add(ark);
                }
            }
        }