private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { switch (e.ColumnIndex) { //screenshot case 3: { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Images (*.png)|*.png"; ofd.ValidateNames = true; ofd.CheckFileExists = true; DialogResult dr = ofd.ShowDialog(); if (dr == DialogResult.OK) { dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = ofd.FileName; } break; } //hash case 4: { Hashes hh = new GameDbManagerMega.Hashes(); GameDB.GameRow gr = (GameDB.GameRow)((DataRowView)dataGridView1.Rows[e.RowIndex].DataBoundItem).Row; hh.gdb = gameDB; hh.gameID = gr.ID; hh.ShowDialog(); break; } } }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3) //screenshot { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Images (*.png)|*.png"; ofd.ValidateNames = true; ofd.CheckFileExists = true; DialogResult dr = ofd.ShowDialog(); if (dr == DialogResult.OK) { string cwd = Directory.GetCurrentDirectory(); string fname = ofd.FileName; if (fname.StartsWith(cwd)) { fname = fname.Substring(cwd.Length + 1); } dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = fname; } } else if (e.ColumnIndex == 4) //hash { Hashes hh = new GameDbManager.Hashes(); GameDB.GameRow gr = (GameDB.GameRow)((DataRowView)dataGridView1.Rows[e.RowIndex].DataBoundItem).Row; hh.gdb = gameDB; hh.gameID = gr.ID; hh.ShowDialog(); } }
private static void ScanDir(string dir, GameDB db, GameDBMgr form) { ushort gamecnt = 0; ushort sccnt = 0; Game[] games = new Game[1024]; byte[] scshots = new byte[1024 * 3072]; if (form != null) { form.Invoke(new Action(() => { form.label1.Text = Path.GetFileName(dir); })); } else { Console.WriteLine("Processing directory " + Path.GetFileName(dir)); } foreach (string f in Directory.GetFiles(dir, "*.*")) { if (f.Contains(".wav")) { continue; } if (f.Contains("Track ") && f.Contains(".bin")) { continue; } if (GetSize(f) > 16 * 1024 * 1024) { continue; } byte[] data = File.ReadAllBytes(f); uint crc = Crc32.Compute(data); GameDB.GameCkRow ck = db.GameCk.FirstOrDefault(w => w.Checksum == crc.ToString("X8")); if (ck == null && (data.Length & 0xFFF) != 0) { uint l = (uint)data.Length & 0xFFFFF000; byte[] dat = new byte[l]; Array.Copy(data, data.Length & 0xFFF, dat, 0, l); data = dat; crc = Crc32.Compute(data); ck = db.GameCk.FirstOrDefault(w => w.Checksum == crc.ToString("X8")); } if (ck != null) { //var namecrc = Crc32.Compute(Encoding.ASCII.GetBytes(f.Substring(f.LastIndexOf('\\') + 1))); string nn = f.Substring(f.LastIndexOfAny(new char[] { '\\', '/' }) + 1); nn = nn.Substring(0, nn.LastIndexOf('.')); byte[] name = Encoding.ASCII.GetBytes(nn); byte[] namecnv = new byte[56]; if (name.Length > 56) { Array.Copy(name, namecnv, 56); } else { Array.Copy(name, namecnv, name.Length); } for (int i = 0; i < namecnv.Length; ++i) { namecnv[i] = ProcessChar(namecnv[i]); } uint namecrc = Crc32.update_crc(0xFFFFFFFF, namecnv, 56); Game existing = games.Take(gamecnt).FirstOrDefault(x => x.checksum != 0 && x.GameID == ck.GameID); if (existing != null) { Game g = new Game(); g.checksum = namecrc; g.remap = existing.remap; g.GameID = existing.GameID; games[gamecnt] = g; ++gamecnt; } else { GameDB.GameRow gg = db.Game.FirstOrDefault(x => x.ID == ck.GameID); if (gg != null && !gg.IsScreenshotNull()) { string dst = "TileCache/" + gg.Screenshot.Substring(gg.Screenshot.LastIndexOfAny(new char[] { '\\', '/' }) + 1); dst = dst.Replace(".png", ".tile"); if (File.Exists(dst)) { Game g = new Game(); g.checksum = namecrc; g.remap = sccnt; g.GameID = ck.GameID; games[gamecnt] = g; ++gamecnt; //copy screenshot to the scshot block byte[] scshot = File.ReadAllBytes(dst); byte[] scshot2 = new byte[2048]; Array.Copy(scshot, scshot2, scshot.Length); //add year, genre... scshot2[0x700] = 0x1; //version if (!gg.IsGenreNull()) { scshot2[0x701] = (byte)gg.Genre; } if (!gg.IsYearNull()) { scshot2[0x702] = (byte)(gg.Year & 0xFF); scshot2[0x703] = (byte)((gg.Year >> 8) & 0xFF); } Debug.Assert(scshot2.Length == 2048); Array.Copy(scshot2, 0, scshots, 2048 * sccnt, scshot2.Length); sccnt++; } } } } else { //Console.WriteLine("Missing crc " + f); } } //scan dirs for cds foreach (string d in Directory.GetDirectories(dir)) { string[] cues = Directory.GetFiles(d, "*.cue"); if (cues.Length == 1) { try { CueSheet cue = new CueSheet(cues[0]); uint crc = ComputeCueCrc(cue); GameDB.GameCkRow ck = db.GameCk.FirstOrDefault(w => w.Checksum == crc.ToString("X8")); if (ck != null) { //var namecrc = Crc32.Compute(Encoding.ASCII.GetBytes(f.Substring(f.LastIndexOf('\\') + 1))); string nn = d.Substring(d.LastIndexOfAny(new char[] { '\\', '/' }) + 1); byte[] name = Encoding.ASCII.GetBytes(nn); byte[] namecnv = new byte[56]; Array.Copy(name, namecnv, name.Length > 56 ? 56 : name.Length); for (int i = 0; i < namecnv.Length; ++i) { namecnv[i] = ProcessChar(namecnv[i]); } uint namecrc = Crc32.update_crc(0xFFFFFFFF, namecnv, 56); Game existing = games.Take(gamecnt).FirstOrDefault(x => x.checksum != 0 && x.GameID == ck.GameID); if (existing != null) { Game g = new Game(); g.checksum = namecrc; g.remap = existing.remap; g.GameID = existing.GameID; games[gamecnt] = g; ++gamecnt; } else { GameDB.GameRow gg = db.Game.FirstOrDefault(x => x.ID == ck.GameID); if (gg != null && !gg.IsScreenshotNull()) { string dst = "TileCache/" + gg.Screenshot.Substring(gg.Screenshot.LastIndexOfAny(new char[] { '\\', '/' }) + 1); dst = dst.Replace(".png", ".tile"); if (File.Exists(dst)) { Game g = new Game(); g.checksum = namecrc; g.remap = sccnt; g.GameID = ck.GameID; games[gamecnt] = g; ++gamecnt; //copy screenshot to the scshot block byte[] scshot = File.ReadAllBytes(dst); byte[] scshot2 = new byte[2048]; Array.Copy(scshot, scshot2, scshot.Length); //add year, genre... scshot2[0x700] = 0x1; //version if (!gg.IsGenreNull()) { scshot2[0x701] = (byte)gg.Genre; } if (!gg.IsYearNull()) { scshot2[0x702] = (byte)(gg.Year & 0xFF); scshot2[0x703] = (byte)((gg.Year >> 8) & 0xFF); } Debug.Assert(scshot2.Length == 2048); Array.Copy(scshot2, 0, scshots, 2048 * sccnt, scshot2.Length); sccnt++; } } } } else { //Console.WriteLine("Missing crc " + f); } } catch (Exception ex) { Console.WriteLine("Error processing " + cues[0] + " . Skipped"); Console.WriteLine(ex.Message); } } } if (gamecnt != 0) { games = games.Take(gamecnt).OrderBy(x => x.checksum).ToArray(); //Console.WriteLine("Writting game.db"); using (FileStream fs = new FileStream(dir + "/games.dbs", FileMode.Create, FileAccess.Write)) { using (BinaryWriter bw = new BinaryWriter(fs)) { foreach (Game g in games) { bw.Write(g.checksum); } for (int i = 0; i < 1024 - games.Length; ++i) { bw.Write(0xFFFFFFFF); } foreach (Game g in games) { bw.Write(g.remap); } for (int i = 0; i < 1024 - games.Length; ++i) { bw.Write((ushort)0xFFFF); } bw.Write(scshots, 0, 2048 * sccnt); } } } foreach (string d in Directory.GetDirectories(dir)) { ScanDir(d, db, form); } }