示例#1
0
        public static void Run(string currentProject, List <string> args)
        {
            var manager = ProjectData.Manager.Load(currentProject);

            if (manager.ActiveProject == null)
            {
                Console.WriteLine("Warning: no active project loaded.");
            }

            string inputPath  = args[0];
            string outputPath = args.Count > 1 ? args[1] : Path.ChangeExtension(inputPath, null) + ".bin";
            string xmlPath;

            if (File.Exists(inputPath) == true &&
                Path.GetFileName(inputPath) == "@resource.xml")
            {
                xmlPath   = inputPath;
                inputPath = Path.GetDirectoryName(inputPath);
            }
            else
            {
                xmlPath = Path.Combine(inputPath, "@resource.xml");
            }

            if (inputPath == null)
            {
                throw new InvalidOperationException();
            }

            var resources = new List <Tuple <string, string, DataFormats.PropertySetResourceFlags> >();

            using (var xml = File.OpenRead(xmlPath))
            {
                var doc  = new XPathDocument(xml);
                var nav  = doc.CreateNavigator();
                var root = nav.SelectSingleNode("Resources");
                if (root == null)
                {
                    throw new InvalidOperationException();
                }

                var rawResources = root.Select("Resource");
                while (rawResources.MoveNext() == true)
                {
                    var rawResource = rawResources.Current;
                    var name        = rawResource.ParseAttributeString("name");
                    var flags       = rawResource.ParseAttributeEnum <DataFormats.PropertySetResourceFlags>("flags");
                    var path        = rawResource.Value;
                    resources.Add(new Tuple <string, string, DataFormats.PropertySetResourceFlags>(path, name, flags));
                }
            }

            var inventory = new PropertySetInventory();

            foreach (var resource in resources)
            {
                var itemPath = Path.Combine(inputPath, resource.Item1);

                PropertySetFormats.PropertySet propertySet;
                using (var xml = File.OpenRead(itemPath))
                {
                    var doc = new XPathDocument(xml);
                    var nav = doc.CreateNavigator();

                    var root = nav.SelectSingleNode("Resource");
                    if (root == null)
                    {
                        throw new InvalidOperationException();
                    }

                    propertySet = ReadPropertySet(root, null);
                }

                inventory.Items.Add(new PropertySetInventory.Item()
                {
                    Id             = propertySet.Name.Id,
                    DebugName      = PropertySetInventory.Item.GetDebugName(resource.Item2),
                    Flags          = resource.Item3,
                    SourceTextHash = 0x20424947u,
                    Name           = resource.Item2,
                    Root           = propertySet,
                });
            }

            using (var output = File.Create(outputPath))
            {
                inventory.Serialize(output, Endian.Little);
            }
        }
示例#2
0
        public static void Run(string currentProject, List <string> extras)
        {
            var manager = ProjectData.Manager.Load(currentProject);

            if (manager.ActiveProject == null)
            {
                Console.WriteLine("Warning: no active project loaded.");
            }

            _PropertyNames = manager.LoadListsPropertySetPropertyNames();
            _SymbolNames   = manager.LoadListsPropertySetSymbolNames();

            string inputPath  = extras[0];
            string outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputPath, null) + "_unpack";

            Directory.CreateDirectory(outputPath);

            var inventory = new PropertySetInventory();

            using (var input = File.OpenRead(inputPath))
            {
                inventory.Deserialize(input, Endian.Little);
            }

            foreach (var item in inventory.Items)
            {
                if (item.Name.HashSymbol() == item.Id && _SymbolNames.Contains(item.Id) == false)
                {
                    _SymbolNames.Add(item.Id, item.Name);
                }
            }

            var settings = new XmlWriterSettings
            {
                Indent          = true,
                IndentChars     = "\t",
                CheckCharacters = false,
            };

            var indexPath = Path.Combine(outputPath, "@resource.xml");

            using (var indexWriter = XmlWriter.Create(indexPath, settings))
            {
                indexWriter.WriteStartDocument();
                indexWriter.WriteStartElement("Resources");

                foreach (var item in inventory.Items)
                {
                    string xmlName;
                    if (_SymbolNames.Contains(item.Id) == false)
                    {
                        if ((uint)item.Name.HashSymbol() != item.Id)
                        {
                            // todo: make this look up correct names from lists
                            Console.WriteLine(
                                "Hash of {0:X8} doesn't match hash of '{1}' -- name probably got truncated!",
                                item.Id,
                                item.Name);
                            xmlName = string.Format(@"__TRUNCATED\{0}_{1:X8}.xml", item.Name, item.Id);
                        }
                        else
                        {
                            xmlName = item.Name + ".xml";
                        }
                    }
                    else
                    {
                        xmlName = _SymbolNames[item.Id] + ".xml";
                    }

                    if (item.Id != item.Root.Name.Id)
                    {
                        throw new InvalidOperationException();
                    }

                    indexWriter.WriteStartElement("Resource");
                    indexWriter.WriteAttributeString("name", item.Name);
                    indexWriter.WriteAttributeString("flags", item.Flags.ToString());
                    indexWriter.WriteValue(xmlName);
                    indexWriter.WriteEndElement();

                    var itemPath = Path.Combine(outputPath, xmlName);
                    using (var itemWriter = XmlWriter.Create(itemPath, settings))
                    {
                        itemWriter.WriteStartDocument();
                        itemWriter.WriteStartElement("Resource");
                        WritePropertySet(itemWriter, item.Root);
                        itemWriter.WriteEndElement();
                        itemWriter.WriteEndDocument();
                        itemWriter.Flush();
                    }
                }

                indexWriter.WriteEndElement();
                indexWriter.WriteEndDocument();
                indexWriter.Flush();
            }
        }