Ejemplo n.º 1
0
        internal const uint TemplateHash             = 0x6E167DD5; // crc32(Template)

        public static void MultiExportEntityLibrary(ObjectFileDefinition objectFileDef,
                                                    string basePath,
                                                    string outputPath,
                                                    InfoManager infoManager,
                                                    BinaryObjectFile bof)
        {
            var settings = new XmlWriterSettings
            {
                Encoding           = Encoding.UTF8,
                Indent             = true,
                CheckCharacters    = false,
                OmitXmlDeclaration = false
            };

            var objectDef = objectFileDef != null ? objectFileDef.Object : null;

            using (var writer = XmlWriter.Create(outputPath, settings))
            {
                writer.WriteStartDocument();

                var root = bof.Root;
                {
                    writer.WriteStartElement("object");
                    writer.WriteAttributeString("name", "EntityLibraries");

                    if (objectFileDef != null &&
                        string.IsNullOrEmpty(objectFileDef.Name) == false)
                    {
                        writer.WriteAttributeString("def", objectFileDef.Name);
                    }

                    var libraryNames = new Dictionary <string, int>();

                    Directory.CreateDirectory(basePath);

                    foreach (var library in root.Children)
                    {
                        var chain = new[] { bof.Root, library };

                        var libraryDef = objectDef != null
                                             ? objectDef.GetObjectDefinition(library.NameHash, null)
                                             : null;

                        var libraryName = FieldTypeDeserializers.Deserialize <string>(FieldType.String,
                                                                                      library.Fields[NameHash]);
                        var unsanitizedLibraryName = libraryName;

                        libraryName = libraryName.Replace('/', Path.DirectorySeparatorChar);
                        libraryName = libraryName.Replace('\\', Path.DirectorySeparatorChar);

                        if (libraryNames.ContainsKey(libraryName) == false)
                        {
                            libraryNames.Add(libraryName, 1);
                        }
                        else
                        {
                            libraryName = string.Format("{0} ({1})", libraryName, ++libraryNames[libraryName]);
                        }

                        var libraryPath = Path.Combine(libraryName, "@library.xml");

                        writer.WriteStartElement("object");
                        writer.WriteAttributeString("external", libraryPath);
                        writer.WriteEndElement();

                        libraryPath = Path.Combine(basePath, libraryPath);

                        var itemNames = new Dictionary <string, int>();

                        var libraryParentPath = Path.GetDirectoryName(libraryPath);
                        if (string.IsNullOrEmpty(libraryParentPath) == false)
                        {
                            Directory.CreateDirectory(libraryParentPath);
                        }

                        using (var libraryWriter = XmlWriter.Create(libraryPath, settings))
                        {
                            libraryWriter.WriteStartDocument();
                            libraryWriter.WriteStartElement("object");
                            libraryWriter.WriteAttributeString("name", "EntityLibrary");

                            libraryWriter.WriteStartElement("field");
                            libraryWriter.WriteAttributeString("name", "Name");
                            libraryWriter.WriteAttributeString("type", "String");
                            libraryWriter.WriteString(unsanitizedLibraryName);
                            libraryWriter.WriteEndElement();

                            foreach (var item in library.Children)
                            {
                                var itemDef = libraryDef != null
                                                  ? libraryDef.GetObjectDefinition(item.NameHash, null)
                                                  : null;

                                var itemName =
                                    FieldTypeDeserializers.Deserialize <string>(FieldType.String,
                                                                                item.Fields[NameHash]);
                                itemName = itemName.Replace('/', Path.DirectorySeparatorChar);
                                itemName = itemName.Replace('\\', Path.DirectorySeparatorChar);

                                if (itemNames.ContainsKey(itemName) == false)
                                {
                                    itemNames.Add(itemName, 1);
                                }
                                else
                                {
                                    itemName = string.Format("{0} ({1})", itemName, ++itemNames[itemName]);
                                }

                                var itemPath = itemName + ".xml";

                                libraryWriter.WriteStartElement("object");
                                libraryWriter.WriteAttributeString("external", itemPath);
                                libraryWriter.WriteEndElement();

                                itemPath = Path.Combine(basePath, libraryName, itemPath);

                                var itemParentPath = Path.GetDirectoryName(itemPath);
                                if (string.IsNullOrEmpty(itemParentPath) == false)
                                {
                                    Directory.CreateDirectory(itemParentPath);
                                }

                                using (var itemWriter = XmlWriter.Create(itemPath, settings))
                                {
                                    itemWriter.WriteStartDocument();
                                    WriteNode(infoManager,
                                              itemWriter,
                                              chain,
                                              item,
                                              itemDef,
                                              null);
                                    itemWriter.WriteEndDocument();
                                }
                            }

                            libraryWriter.WriteEndDocument();
                        }
                    }

                    writer.WriteEndElement();
                }

                writer.WriteEndDocument();
            }
        }
Ejemplo n.º 2
0
        private static void WriteNode(InfoManager infoManager,
                                      XmlWriter writer,
                                      IEnumerable <BinaryObject> parentChain,
                                      BinaryObject node,
                                      ClassDefinition def,
                                      ObjectFileDefinition objectFileDef)
        {
            var chain = parentChain.Concat(new[] { node });

            var originalDef = def;

            if (def != null &&
                def.ClassFieldHash.HasValue == true)
            {
                if (node.Fields.ContainsKey(def.ClassFieldHash.Value) == true)
                {
                    var hash = FieldTypeDeserializers.Deserialize <uint>(FieldType.UInt32,
                                                                         node.Fields[def.ClassFieldHash.Value]);
                    def = infoManager.GetClassDefinition(hash);
                }
            }

            writer.WriteStartElement("object");

            if (originalDef != null && originalDef.Name != null && originalDef.Hash == node.NameHash)
            {
                writer.WriteAttributeString("name", originalDef.Name);
            }
            else if (def != null && def.Name != null && def.Hash == node.NameHash)
            {
                writer.WriteAttributeString("name", def.Name);
            }
            else
            {
                writer.WriteAttributeString("hash", node.NameHash.ToString("X8"));
            }

            if (objectFileDef != null &&
                string.IsNullOrEmpty(objectFileDef.Name) == false)
            {
                writer.WriteAttributeString("def", objectFileDef.Name);
            }

            if (node.Fields != null)
            {
                foreach (var kv in node.Fields)
                {
                    writer.WriteStartElement("field");

                    var fieldDef = def != null?def.GetFieldDefinition(kv.Key, chain) : null;

                    if (fieldDef != null && fieldDef.Name != null && fieldDef.Hash == kv.Key)
                    {
                        writer.WriteAttributeString("name", fieldDef.Name);
                    }
                    else
                    {
                        writer.WriteAttributeString("hash", kv.Key.ToString("X8"));
                    }

                    if (fieldDef == null)
                    {
                        writer.WriteAttributeString("type", FieldType.BinHex.GetString());
                        writer.WriteBinHex(kv.Value, 0, kv.Value.Length);
                    }
                    else
                    {
                        writer.WriteAttributeString("type", fieldDef.Type.GetString());

                        if (fieldDef.ArrayType != FieldType.Invalid)
                        {
                            writer.WriteAttributeString("array_type", fieldDef.ArrayType.GetString());
                        }

                        FieldTypeDeserializers.Deserialize(writer, fieldDef, kv.Value);
                    }

                    writer.WriteEndElement();
                }
            }

            if (def == null || def.DynamicNestedClasses == false)
            {
                foreach (var childNode in node.Children)
                {
                    var childDef = def != null?def.GetObjectDefinition(childNode.NameHash, chain) : null;

                    WriteNode(infoManager, writer, chain, childNode, childDef, null);
                }
            }
            else if (def.DynamicNestedClasses == true)
            {
                foreach (var childNode in node.Children)
                {
                    var childDef = infoManager.GetClassDefinition(childNode.NameHash);
                    WriteNode(infoManager, writer, chain, childNode, childDef, null);
                }
            }

            writer.WriteEndElement();
        }