예제 #1
0
        private void CleanUp(object sender, EventArgs e)
        {
            if (SelectedMod != null)
            {
                if (!QueryIgnoreEditedModPack())
                {
                    return;
                }

                if (MessageBox.Show("This will delete the directories 'battleterrain' and 'variantmodels' from your pack. " +
                                    "Do not do this if you changed anything there.\nContinue?", "Confirm cleanup",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                {
                    return;
                }

                PackFileCodec codec   = new PackFileCodec();
                PackFile      modPack = codec.Open(SelectedMod.PackFilePath);
                foreach (VirtualDirectory dir in modPack.Root.Subdirectories)
                {
                    if (dir.Name.Equals("battleterrain") || dir.Name.Equals("variantmodels"))
                    {
                        dir.Deleted = true;
                    }
                }
                if (modPack.Root.Modified)
                {
                    string tempFilePath = Path.GetTempFileName();
                    codec.WriteToFile(tempFilePath, modPack);
                    File.Delete(SelectedMod.PackFilePath);
                    File.Move(tempFilePath, SelectedMod.PackFilePath);
                }
            }
        }
예제 #2
0
 private void OptimizePack(object sender, EventArgs e)
 {
     if (!DBTypeMap.Instance.Initialized)
     {
         DBTypeMap.Instance.InitializeTypeMap(Path.GetDirectoryName(Application.ExecutablePath));
     }
     if (SelectedMod != null)
     {
         if (!File.Exists(SelectedMod.PackFilePath))
         {
             MessageBox.Show(string.Format("Pack file for mod \"{0}\" not found ", SelectedMod.Name));
             return;
         }
         else if (!QueryIgnoreEditedModPack())
         {
             return;
         }
         InputBox box = new InputBox {
             Text = "Enter prefix for rename"
         };
         PackFileCodec codec    = new PackFileCodec();
         PackFile      packFile = codec.Open(SelectedMod.PackFilePath);
         if (box.ShowDialog() == DialogResult.OK)
         {
             PackedFileRenamer renamer = new PackedFileRenamer(box.Input);
             renamer.Rename(packFile.Files);
         }
         statusLabel.Text = "Optimizing DB files...";
         Refresh();
         new DbFileOptimizer(Game.STW).CreateOptimizedFile(packFile);
         codec.Save(packFile);
         SetInstallDirectoryLabelText();
     }
 }
예제 #3
0
                PackFile getPackFile(ProgressUpdater pu, string gamedatapath, string packPath)
                {
                    string path = System.IO.Path.Combine(gamedatapath, packPath);

                    if (System.IO.File.Exists(path))
                    {
                        PackFileCodec codec = new PackFileCodec();
                        pu.ConnectPackCodec(codec, path.Replace('\\', System.IO.Path.DirectorySeparatorChar));
                        return(codec.Open(path));
                    }
                    else
                    {
                        return(null);
                    }
                }
예제 #4
0
 private List <PackedFile> ReadPackageFile(string fileName)
 {
     try
     {
         if (File.Exists(fileName))
         {
             PackFileCodec codec = new PackFileCodec();
             return(codec.Open(fileName).Files);
         }
         else
         {
             return(new List <PackedFile>());
         }
     }
     catch (Exception exception1)
     {
         MessageBox.Show(exception1.ToString(), "读取文件[" + fileName + "]出错了", MessageBoxButtons.OK, MessageBoxIcon.Hand);
         return(new List <PackedFile>());
     }
 }
        public void Initialize(IList <GameVersion> versions, string typemappath)
        {
            _pks = new List <string> {
                "id", "key", "unit", "unit_key", "unit_class"
            };

            // For reference purposes
            _versions = versions;

            DBTypeMap.Instance.InitializeTypeMap(typemappath);

            var codec = new PackFileCodec();

            _dataPack       = new Dictionary <string, List <PackFile> >();
            _localPack      = new Dictionary <string, List <PackFile> >();
            _loadedTables   = new Dictionary <string, DataTable>();
            _loadedLocFiles = new Dictionary <string, Dictionary <string, string> >();
            foreach (var v in versions)
            {
                foreach (var packFile in Directory.GetFiles(v.Data, "data*.pack"))
                {
                    if (_dataPack.ContainsKey(v.Id))
                    {
                        _dataPack[v.Id].Add(codec.Open(packFile));
                    }
                    else
                    {
                        _dataPack[v.Id] = new List <PackFile> {
                            codec.Open(packFile)
                        }
                    };
                }
                foreach (var packFile in Directory.GetFiles(v.Data, "local*.pack"))
                {
                    if (_localPack.ContainsKey(v.Id))
                    {
                        _localPack[v.Id].Add(codec.Open(packFile));
                    }
                    else
                    {
                        _localPack[v.Id] = new List <PackFile> {
                            codec.Open(packFile)
                        }
                    };
                }

                var dir = $"{Common.DataPath}/Export/{v.Id}";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);

                    // Export ALL the tables of the db subdirectories
                    var virtualDirectories = _dataPack[v.Id].Select(dp => dp.Root.GetSubdirectory("db"));
                    foreach (var tableName in virtualDirectories.SelectMany(vd => vd.Subdirectories.Select(s => s.Name)).Distinct())
                    {
                        try
                        {
                            var table = Table(v.Id, tableName);
                            var text  = Text(v.Id, tableName.Replace("_tables", ""));

                            var result = ConvertDataToString(GetPk(table), table, text);

                            File.WriteAllText($"{dir}/{tableName}.json", result);
                        } catch (Exception e)
                        {
                            Console.WriteLine($"Failed to export table {tableName}: {e.Message}");
                        }
                    }

                    // Export ALL the images in the ui directory and its subdirectories
                    virtualDirectories = _dataPack[v.Id].Select(dp => dp.Root.GetSubdirectory("ui"));
                    foreach (var vd in virtualDirectories)
                    {
                        exportImages(vd, dir);
                    }
                }
            }
        }
        public void Export(IList <GameVersion> versions)
        {
            // master_schema.xml is managed in the solution and copied to the bin root
            // so use "" as the basepath
            DBTypeMap.Instance.InitializeTypeMap("");

            var codec = new PackFileCodec();

            _dataPack     = new Dictionary <string, List <PackFile> >();
            _localPack    = new Dictionary <string, List <PackFile> >();
            _loadedTables = new Dictionary <string, DataTable>();

            foreach (var v in versions)
            {
                var exportDir = $"{Common.DataPath}/Export/{v.Id}";
                if (Directory.Exists(exportDir))
                {
                    Console.WriteLine($"[SKIP] Version {v.Name} already exists");
                    continue;
                }

                Console.WriteLine($"[EXPORT] Version {v.Name}");

                bool isTWW = (v.Game == Game.TWW || v.Game == Game.TWW2);

                // For Mods, first load the latest version packs
                if (!isTWW)
                {
                    var latestVersion = versions.First(ver => ver.Game == Game.TWW2);
                    foreach (var packFile in Directory.GetFiles(latestVersion.Data, "data*.pack"))
                    {
                        if (_dataPack.ContainsKey(v.Id))
                        {
                            _dataPack[v.Id].Add(codec.Open(packFile));
                        }
                        else
                        {
                            _dataPack[v.Id] = new List <PackFile> {
                                codec.Open(packFile)
                            }
                        };
                    }
                    foreach (var packFile in Directory.GetFiles(latestVersion.Data, "local*.pack"))
                    {
                        if (_localPack.ContainsKey(v.Id))
                        {
                            _localPack[v.Id].Add(codec.Open(packFile));
                        }
                        else
                        {
                            _localPack[v.Id] = new List <PackFile> {
                                codec.Open(packFile)
                            }
                        };
                    }
                }

                var dataPackPattern  = v.Game != Game.TWW2 ? "*.pack" : "data*.pack";
                var localPackPattern = v.Game != Game.TWW2 ? "*.pack" : "local*.pack";
                foreach (var packFile in Directory.GetFiles(v.Data, dataPackPattern))
                {
                    if (_dataPack.ContainsKey(v.Id))
                    {
                        _dataPack[v.Id].Add(codec.Open(packFile));
                    }
                    else
                    {
                        _dataPack[v.Id] = new List <PackFile> {
                            codec.Open(packFile)
                        }
                    };
                }
                foreach (var packFile in Directory.GetFiles(v.Data, localPackPattern))
                {
                    if (_localPack.ContainsKey(v.Id))
                    {
                        _localPack[v.Id].Add(codec.Open(packFile));
                    }
                    else
                    {
                        _localPack[v.Id] = new List <PackFile> {
                            codec.Open(packFile)
                        }
                    };
                }

                // DEBUGING PURPOSES
                //foreach (var p in _localPack[v.Id].Select(p => p.Root))
                //{
                //    foreach (var f in p.AllFiles)
                //    {
                //        byte[] data = f.Data;
                //        using (MemoryStream stream = new MemoryStream(data, 0, data.Length))
                //        {
                //            try
                //            {
                //                var locFile = LocCodec.Instance.Decode(stream);
                //                var found = locFile.Entries.FirstOrDefault(e => e.Localised.Contains("the minimum percentage of armour roll that can be applied"));
                //                if (found != null)
                //                {
                //                    Console.WriteLine($"FOUND IT: {p.Name} - {f.Name} - {found.Tag}");
                //                }
                //            }
                //            catch (Exception) { }
                //        }
                //    }
                //}


                Directory.CreateDirectory($"{exportDir}");

                // Export all xml files from the asssembly_kit if available
                var xmlFiles = Directory.GetFiles(v.Data, "*.xml", SearchOption.AllDirectories).ToList();
                xmlFiles.ForEach(f =>
                {
                    var xml = System.IO.File.ReadAllText(f);

                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xml);

                    var tableName = Path.GetFileNameWithoutExtension(f);
                    var entries   = doc.GetElementsByTagName(tableName).Cast <XmlNode>().Select(o =>
                    {
                        o.Attributes.RemoveAll();
                        foreach (XmlNode child in o.ChildNodes)
                        {
                            child.Attributes.RemoveAll();
                        }
                        return(JsonConvert.SerializeXmlNode(o, Newtonsoft.Json.Formatting.None, true));
                    });

                    string json = $"[{string.Join(',', entries)}]";

                    File.WriteAllText($"{exportDir}/{tableName}.json", json);
                });

                // Export ALL the tables of the db subdirectories
                // Only do this as a fallback if we don't have xml files present (i.e. no assembly kit data)
                if (!xmlFiles.Any())
                {
                    // Load all texts at once because Mods don't name their tables to match the original tables
                    var text = Text(v.Id);

                    _dataPack[v.Id].Select(dp => dp.Root.GetSubdirectory("db")).SelectMany(vd => vd.Subdirectories.Select(s => s.Name)).Distinct().ToList().ForEach(packTableName =>
                    {
                        var realTableName = packTableName.Replace("_tables", "");

                        // DEBUG
                        // if (realTableName != "battle_entities") { return; }

                        // Console.WriteLine($"=== {realTableName} ===");
                        try
                        {
                            var table = Table(v.Id, packTableName);

                            var result = ConvertDataToString(table, text, realTableName);

                            File.WriteAllText($"{exportDir}/{realTableName}.json", result);
                        }
                        catch (Exception e) {
                            Console.WriteLine($"ERROR: Could not process {realTableName}: {e}");
                        }
                    });
                }

                // Export ALL the images in the ui directory and its subdirectories
                _dataPack[v.Id].Select(dp => dp.Root.GetSubdirectory("ui")).ToList().ForEach(vd =>
                {
                    exportImages(vd, exportDir);
                });
            }
        }