public DataTable Table(string version, string name)
        {
            var key = $"{version}_{name}";

            lock (_dataPack)
            {
                if (_loadedTables.ContainsKey(key))
                {
                    return(_loadedTables[key]);
                }

                var table_files = _dataPack[version].Select(dp => dp.Root.GetSubdirectory("db")?.GetSubdirectory(name)).Where(tf => tf != null);

                DataTable table = null;
                foreach (var file in table_files.SelectMany(tf => tf.Files))
                {
                    var editedFile  = PackedFileDbCodec.Decode(file);
                    var table_piece = CreateTable(file, editedFile);

                    if (table == null)
                    {
                        table = table_piece;
                    }
                    else
                    {
                        table.Merge(table_piece);
                    }
                }

                _loadedTables[key] = table;

                return(table);
            }
        }
        public FieldCorrespondencyFinder(string packFile, string xmlDir)
        {
            xmlDirectory = xmlDir;
            DBTypeMap.Instance.InitializeTypeMap(Directory.GetCurrentDirectory());
            // initialize patchFileValues from pack file
            PackFile pack = new PackFileCodec().Open(packFile);

            foreach (PackedFile contained in pack.Files)
            {
                if (contained.FullPath.StartsWith("db"))
                {
                    // no need to resolve if it's done already...
                    string tableName = DBFile.Typename(contained.FullPath).Replace("_tables", "");
                    try {
                        PackedFileDbCodec codec = PackedFileDbCodec.GetCodec(contained);
                        codec.AutoadjustGuid = false;
                        DBFile dbFile = codec.Decode(contained.Data);

                        MappedDataTable table = new MappedDataTable(tableName);
                        ValuesFromPack(table, dbFile);
                        ValuesFromXml(table);

                        mappedTables[tableName] = table;
#if DEBUG
                    } catch (Exception e) {
                        Console.Error.WriteLine(e.Message);
                    }
#else
                    } catch { }
 public byte[] Process(PackedFile file)
 {
     byte[] result = file.Data;
     using (MemoryStream stream = new MemoryStream()) {
         DBFile dbFile = PackedFileDbCodec.Decode(file);
         TextDbCodec.Instance.Encode(stream, dbFile);
         result = stream.ToArray();
     }
     return(result);
 }
Ejemplo n.º 4
0
        public static bool CanDecode(PackedFile dbFile)
        {
            bool valid = false;

            try {
                DBFileHeader header  = PackedFileDbCodec.readHeader(dbFile);
                DBFile       decoded = PackedFileDbCodec.Decode(dbFile);
                valid = (decoded.Entries.Count == header.EntryCount);
                return(valid);
            } catch (Exception) {
            }
            return(valid);
        }
Ejemplo n.º 5
0
        /*
         * Create db file from the given packed file.
         * Will not throw an exception on error, but return null.
         */
        DBFile FromPacked(PackedFile packed)
        {
            DBFile result = null;

            try {
                PackedFileDbCodec codec = PackedFileDbCodec.GetCodec(packed);
                if (codec != null)
                {
                    result = codec.Decode(packed.Data);
                }
            } catch {}
            return(result);
        }
Ejemplo n.º 6
0
        /*
         * Open or create db file in the target pack file.
         */
        DBFile GetTargetFile(PackedFile packed)
        {
            DBFile     targetFile   = null;
            PackedFile existingPack = SaveTo[packed.FullPath] as PackedFile;

            if (existingPack != null)
            {
                targetFile = PackedFileDbCodec.Decode(existingPack);
            }
            if (targetFile == null)
            {
                DBFile file = PackedFileDbCodec.Decode(packed);
                targetFile = new DBFile(file.Header, file.CurrentType);
            }
            return(targetFile);
        }
 /*
  * Select all rows matching the where clause (or all in none was given)
  * and set the given values to all corresponding fields.
  * Note: If the assignment list contains a non-existing field,
  * that assignment is ignored without warning.
  */
 public override void Execute()
 {
     foreach (PackedFile packed in PackedFiles)
     {
         DBFile dbFile = PackedFileDbCodec.Decode(packed);
         foreach (List <FieldInstance> fieldInstance in dbFile.Entries)
         {
             if (whereClause != null && !whereClause.Accept(fieldInstance))
             {
                 continue;
             }
             AdjustValues(fieldInstance);
         }
         packed.Data = PackedFileDbCodec.GetCodec(packed).Encode(dbFile);
     }
 }
Ejemplo n.º 8
0
        public void ImportExistingPack(string packFileName)
        {
            string modName = PACK_FILE_RE.Replace(Path.GetFileName(packFileName), "");

            // trigger creation of backup folder
            new Mod(modName);
            MultiMods.Instance.AddMod(modName);

            PackFile pack = new PackFileCodec().Open(packFileName);

            foreach (PackedFile packed in pack)
            {
                // extract to working_data as binary
                List <string> extractPaths = new List <string>();
                byte[]        data         = null;
                if (DBTypeMap.Instance.IsSupported(DBFile.typename(packed.FullPath)))
                {
                    PackedFileDbCodec codec       = PackedFileDbCodec.GetCodec(packed);
                    Codec <DBFile>    writerCodec = new ModToolDBCodec(FieldMappingManager.Instance);
                    DBFile            dbFile      = codec.Decode(packed.Data);
                    using (var stream = new MemoryStream()) {
                        writerCodec.Encode(stream, dbFile);
                        data = stream.ToArray();
                    }
                    string extractFilename = string.Format("{0}.xml", packed.Name);
                    extractPaths.Add(Path.Combine(ModTools.Instance.InstallDirectory, "raw_data", "db", extractFilename));
                    extractPaths.Add(Path.Combine(ModTools.Instance.RawDataPath, "EmpireDesignData", "db", extractFilename));
                }
                else
                {
                    extractPaths.Add(Path.Combine(ModTools.Instance.WorkingDataPath, packed.FullPath));
                    data = packed.Data;
                }
                foreach (string path in extractPaths)
                {
                    string extractDir = Path.GetDirectoryName(path);
                    if (!Directory.Exists(extractDir))
                    {
                        Directory.CreateDirectory(extractDir);
                    }
                    File.WriteAllBytes(path, data);
                }
            }
        }
Ejemplo n.º 9
0
        private List <ModLine> ReadPackedField(PackedFile file, string fileName)
        {
            if (File.Exists(fileName))
            {
                string       key    = DBFile.Typename(file.FullPath);
                DBFileHeader header = PackedFileDbCodec.readHeader(file);

                string exp = null;
                if (PackedFileDbCodec.CanDecode(file, out exp))
                {
                    PackedFileDbCodec codec = PackedFileDbCodec.GetCodec(file);

                    DBFile f = codec.Decode(file.Data);

                    if (f != null)
                    {
                        if (f.Entries.Count > 0)
                        {
                            //寻找主键字段
                            List <string> keys = f.CurrentType.Fields.Where(p => p.PrimaryKey).Select(p => p.Name).ToList();
                            if (keys.Count > 0)
                            {
                                return(f.Entries.Select(line =>
                                {
                                    ModLine modLine = new ModLine();
                                    modLine.FileName = fileName;
                                    modLine.TableName = f.CurrentType.Name;
                                    modLine.FieldKeyName = new string[keys.Count];
                                    modLine.FieldKeyValue = new string[keys.Count];
                                    for (int i = 0; i < keys.Count; i++)
                                    {
                                        modLine.FieldKeyName[i] = keys[i];
                                        modLine.FieldKeyValue[i] = line[keys[i]].Value;
                                    }
                                    return modLine;
                                }).ToList());
                            }
                        }
                    }
                }
            }
            return(new List <ModLine>());
        }
Ejemplo n.º 10
0
        public static Dictionary <string, DBFile> getDBFiles(this VirtualDirectory DB, string tablepathstring)
        {
            VirtualDirectory dir = DB.Subdirectories.FirstOrDefault(x => x.Name.Equals(tablepathstring));

            if (dir == null)
            {
                return(new Dictionary <string, DBFile>(0));
            }

            Dictionary <string, DBFile> dbFiles = new Dictionary <string, DBFile>(dir.Count());

            foreach (var file in dir)
            {
                DBFile temp;
                try { temp = PackedFileDbCodec.Decode(file); }
                catch { continue; }
                dbFiles.Add(file.Name, temp);
            }
            return(dbFiles);
        }
Ejemplo n.º 11
0
 public override void TestFile(PackedFile packed)
 {
     allTestedFiles.Add(packed.FullPath);
     if (packed.Data.Length == 0)
     {
         emptyFiles.Add(packed.FullPath);
         return;
     }
     using (var stream = new MemoryStream(packed.Data)) {
         BuildingModelFile bmFile = new BuildingModelFile(PackedFileDbCodec.Decode(packed));
         if (bmFile.Header.EntryCount != bmFile.Models.Count)
         {
             countErrors.Add(string.Format("{0}: invalid count. Should be {1}, is {2}",
                                           packed.Name, bmFile.Header.EntryCount, bmFile.Models.Count));
         }
         else
         {
             successes.Add(string.Format("{0}", packed.FullPath));
         }
     }
 }
Ejemplo n.º 12
0
 /*
  * Delete all entries matching the where clause if any was given,
  * or all entries if none was given.
  */
 public override void Execute()
 {
     if (SaveTo == null)
     {
         return;
     }
     foreach (PackedFile packed in PackedFiles)
     {
         PackedFile   result = new PackedFile(packed.FullPath, false);
         DBFile       dbFile = PackedFileDbCodec.Decode(packed);
         List <DBRow> kept   = new List <DBRow>();
         foreach (DBRow field in dbFile.Entries)
         {
             if (whereClause != null && !whereClause.Accept(field))
             {
                 kept.Add(field);
             }
         }
         DBFile newDbFile = new DBFile(dbFile.Header, dbFile.CurrentType);
         newDbFile.Entries.AddRange(kept);
         result.Data = PackedFileDbCodec.GetCodec(packed).Encode(newDbFile);
         SaveTo.Add(result, true);
     }
 }
Ejemplo n.º 13
0
        public static void Main(string[] args)
        {
            PackFile exported    = null;
            bool     showManager = (args.Length > 0 && args[0].Equals("-g"));

            if (showManager)
            {
                DBTableDisplay display = new DBTableDisplay();
                display.ShowDialog();
            }
            else
            {
                bool export = (args.Length > 0 && args[0].Equals("-x"));
                Console.WriteLine("exporting undecoded to file");
                exported = new PackFile("undecoded.pack", new PFHeader("PFH4"));
                DBTypeMap.Instance.initializeFromFile("master_schema.xml");
                foreach (Game game in Game.Games)
                {
                    LoadGameLocationFromFile(game);
                    if (game.IsInstalled)
                    {
                        foreach (string packFileName in Directory.EnumerateFiles(game.DataDirectory, "*pack"))
                        {
                            Console.WriteLine("checking {0}", packFileName);
                            PackFile packFile = new PackFileCodec().Open(packFileName);
                            foreach (VirtualDirectory dir in packFile.Root.Subdirectories.Values)
                            {
                                if (dir.Name.Equals("db"))
                                {
                                    foreach (PackedFile dbFile in dir.AllFiles)
                                    {
                                        if (dbFile.Name.Contains("models_naval"))
                                        {
                                            continue;
                                        }
                                        // DBFileHeader header = PackedFileDbCodec.readHeader(dbFile);
                                        DBFile       decoded = PackedFileDbCodec.Decode(dbFile);
                                        DBFileHeader header  = PackedFileDbCodec.readHeader(dbFile);
                                        if (decoded == null && header.EntryCount != 0)
                                        {
                                            Console.WriteLine("failed to read {0} in {1}", dbFile.FullPath, packFile);
                                            if (export)
                                            {
                                                String     exportFileName = String.Format("db/{0}_{1}_{2}", game.Id, dbFile.Name, Path.GetFileName(packFileName)).ToLower();
                                                PackedFile exportedDbFile = new PackedFile(exportFileName, false)
                                                {
                                                    Data = dbFile.Data
                                                };
                                                exported.Add(exportedDbFile);
                                            }
                                            else
                                            {
                                                string key     = DBFile.Typename(dbFile.FullPath);
                                                bool   unicode = false;
                                                if (game == Game.ETW ||
                                                    game == Game.NTW ||
                                                    game == Game.STW)
                                                {
                                                    unicode = true;
                                                }
                                                DecodeTool.DecodeTool decoder = new DecodeTool.DecodeTool(unicode)
                                                {
                                                    TypeName = key,
                                                    Bytes    = dbFile.Data
                                                };
                                                decoder.ShowDialog();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (export)
                        {
                            new PackFileCodec().Save(exported);
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("Game {0} not installed in {1}", game, game.GameDirectory);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        // test the given packed file as a database file
        // tests PackedFileCodec and the db definitions we have
        public override void TestFile(PackedFile file)
        {
            if (!DBTypeMap.Instance.Initialized)
            {
                DBTypeMap.Instance.InitializeTypeMap(Directory.GetCurrentDirectory());
            }
            allTestedFiles.Add(file.FullPath);
            if (file.Size == 0)
            {
                emptyTables.Add(new Tuple <string, int>(DBFile.Typename(file.FullPath), -1));
                return;
            }

            // PackedFileDbCodec packedCodec = PackedFileDbCodec.FromFilename(file.FullPath);
            string              type   = DBFile.Typename(file.FullPath);
            DBFileHeader        header = PackedFileDbCodec.readHeader(file);
            Tuple <string, int> tuple  = new Tuple <string, int>(string.Format("{0} # {1}", type, header.GUID), header.Version);

            if (OutputTable)
            {
                Console.WriteLine("TABLE:{0}#{1}#{2}", type, header.Version, header.GUID);
            }
            Console.Out.Flush();
            if (header.EntryCount == 0)
            {
                // special case: we will never find out the structure of a file
                // if it contains no data
                emptyTables.Add(tuple);
            }
            else if (DBTypeMap.Instance.IsSupported(type))
            {
                SortedSet <Tuple <string, int> > addTo = null;
                try {
#if DEBUG
                    if (!string.IsNullOrEmpty(debug_at) && file.FullPath.Contains(debug_at))
                    {
                        Console.WriteLine("stop right here");
                    }
#endif
                    // a wrong db definition might not cause errors,
                    // but read less entries than there are
                    DBFile dbFile = PackedFileDbCodec.Decode(file);
                    if (dbFile.Entries.Count == dbFile.Header.EntryCount)
                    {
                        addTo = supported;

                        //if (!string.IsNullOrEmpty(header.GUID)) {
                        //    List<FieldInfo> fields = new List<FieldInfo>(DBTypeMap.Instance.GetVersionedInfo(header.GUID, type, header.Version).fields);
                        //    DBTypeMap.Instance.SetByGuid(header.GUID, type, header.Version, fields);
                        //}

                        // only test tsv import/export if asked,
                        // it takes some time more than just the read checks
                        if (TestTsv)
                        {
                            TestTsvExport(dbFile);
                        }
                    }
                    else
                    {
                        // didn't get what we expect
                        addTo = invalidDefForVersion;
#if DEBUG
                        if (!string.IsNullOrEmpty(debug_at) && file.FullPath.EndsWith(debug_at))
                        {
                            Console.WriteLine("adding watched to invalid");
                        }
#endif
                    }
                } catch {
#if DEBUG
                    if (!string.IsNullOrEmpty(debug_at) && file.FullPath.EndsWith(debug_at))
                    {
                        Console.WriteLine("adding watched to invalid");
                    }
#endif
                    addTo = invalidDefForVersion;
                }
                addTo.Add(tuple);
            }
            else
            {
                noDefinition.Add(tuple);
            }
        }
Ejemplo n.º 15
0
        public void Open()
        {
#if DEBUG
            Console.WriteLine("Opening {0}", CurrentPackedFile.FullPath);
#endif
            string key = DBFile.Typename(CurrentPackedFile.FullPath);

            if (!DBTypeMap.Instance.IsSupported(key))
            {
                ShowDBFileNotSupportedMessage("Sorry, this db file isn't supported yet.\r\n\r\nCurrently supported files:\r\n");
                if (Settings.Default.ShowDecodeToolOnError)
                {
                    var decoder = new DecodeTool.DecodeTool {
                        TypeName = key, Bytes = CurrentPackedFile.Data
                    };
                    decoder.ShowDialog();
                    if (!DBTypeMap.Instance.IsSupported(key))
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            try {
                EditedFile = PackedFileDbCodec.Decode(CurrentPackedFile);
            } catch {
                if (Settings.Default.ShowDecodeToolOnError)
                {
                    var decoder = new DecodeTool.DecodeTool {
                        TypeName = key, Bytes = CurrentPackedFile.Data
                    };
                    decoder.ShowDialog();
                    try {
                        EditedFile = PackedFileDbCodec.Decode(CurrentPackedFile);
                    } catch {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }

            if (EditedFile == null)
            {
                return;
            }

            Codec = PackedFileDbCodec.FromFilename(CurrentPackedFile.FullPath);
            TypeInfo info = EditedFile.CurrentType;

            dataGridView.EndEdit();
            dataGridView.Columns.Clear();
            if (currentDataTable != null)
            {
                currentDataTable.DataSet.Clear();
                currentDataTable.Clear();
            }

            CreateDataTable();
            for (int i = 0; i < EditedFile.CurrentType.Fields.Count; i++)
            {
                dataGridView.Columns.Add(CreateColumn(i));
            }

            DataSet currentDataSet = new DataSet(info.Name + "_DataSet");
            currentDataSet.Tables.Add(currentDataTable);
            dataGridView.DataSource = new BindingSource(currentDataSet, info.Name + "_DataTable");

            FirstColumnAsRowHeader = Settings.Default.UseFirstColumnAsRowHeader;
            FillRowHeaders();
            addNewRowButton.Enabled = true;
            importButton.Enabled    = true;
            exportButton.Enabled    = true;

            dataGridView.Visible = true;
            unsupportedDBErrorTextBox.Visible = false;

            // cannot edit contained complex types
            foreach (FieldInfo f in EditedFile.CurrentType.Fields)
            {
                if (f is ListType)
                {
                    Console.WriteLine("cannot edit this");
                    ReadOnly = true;
                    break;
                }
            }
        }