示例#1
0
        public void SaveXml(string filename)
        {
            var rootDir = Path.GetDirectoryName(filename);

            var libsDir  = "libraries";
            var libsPath = Path.Combine(rootDir, libsDir);

            if (!Directory.Exists(libsPath))
            {
                Directory.CreateDirectory(libsPath);
            }

            var xml      = new XmlDocument();
            var libsElem = xml.CreateElement("EntityLibraries");

            var version = 1;

            if (Use64Bit)
            {
                version = 2;
            }

            libsElem.SetAttribute("Version", version.ToString());

            var libs = new Dictionary <string, List <string> >();

            Context.Log("Splitting libraries...");
            foreach (var lib in Root.Children)
            {
                if (lib.Id != "EntityLibrary")
                {
                    throw new InvalidDataException($"Expected an EntityLibrary but got '{lib.Id}' instead.");
                }

                var libName = lib.GetAttributeValue("Name");

                if (libName == null)
                {
                    throw new InvalidDataException($"Couldn't get name for EntityLibrary!");
                }

                var arkList = new List <string>();

                foreach (var child in lib.Children)
                {
                    if (child.Id != "EntityPrototype")
                    {
                        throw new InvalidDataException($"Expected an EntityPrototype but got '{child.Id}' instead.");
                    }

                    var arkArk  = child.GetChild("Entity");
                    var arkName = arkArk.GetAttributeValue("hidName");

                    if (String.IsNullOrEmpty(arkName))
                    {
                        throw new InvalidOperationException("Can't figure out EntityPrototype name!");
                    }

                    var arkPath = arkName.Replace('.', '\\');
                    var arkFile = Path.Combine(libsDir, $"{arkPath}.xml");

                    // attempt to serialize data
                    var arkRes = ResourceFactory.Create <NomadXmlSerializer>(child);
                    arkRes.Save(Path.Combine(rootDir, arkFile));

                    arkList.Add(arkFile);
                }

                libs.Add(libName, arkList);
            }

            Context.Log("Saving XML...");
            foreach (var lib in libs)
            {
                var name  = lib.Key;
                var files = lib.Value;

                // write import statement
                var import = xml.CreateElement("EntityLibrary");

                import.SetAttribute("Name", name);

                foreach (var ent in files)
                {
                    var include = xml.CreateElement("Include");

                    include.SetAttribute("Path", ent);

                    import.AppendChild(include);
                }

                libsElem.AppendChild(import);
            }

            xml.AppendChild(libsElem);
            xml.SaveFormatted(filename);
        }
 public NomadGenericResourceSerializer(NomadFileInfo info)
 {
     Info   = info;
     Format = ResourceFactory.GetFormat(info.Version);
 }
示例#3
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);
                }
            }
        }