private static void Main(string[] param) { if (param.Length >= 1) { if (param[0].Equals("-scanroms", StringComparison.CurrentCultureIgnoreCase)) { GameDBMgr.DoScanWithoutUI(param[1]); } else if (param[0].Equals("-convertimg", StringComparison.CurrentCultureIgnoreCase)) { GameDBMgr.ConvertImages(); } else { Console.WriteLine("Command Line usage:"); Console.WriteLine(); Console.WriteLine("GameDbManager [-scanroms directory_name|-convertimg]"); } return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new GameDBMgr()); }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofn = new OpenFileDialog(); ofn.CheckPathExists = true; ofn.Multiselect = false; ofn.Filter = "ROM Files (*.bin,*.md,*.sms,*.32x,*.sg)|*.bin;*.md;*.sms;*.32x;*.sg|CDRom cuesheets (*.cue)|*.cue"; if (ofn.ShowDialog() == DialogResult.OK) { uint crc; if (ofn.FileName.EndsWith(".bin", StringComparison.InvariantCultureIgnoreCase) || ofn.FileName.EndsWith(".md", StringComparison.InvariantCultureIgnoreCase) || ofn.FileName.EndsWith(".sms", StringComparison.InvariantCultureIgnoreCase) || ofn.FileName.EndsWith(".32x", StringComparison.InvariantCultureIgnoreCase) || ofn.FileName.EndsWith(".sg", StringComparison.InvariantCultureIgnoreCase) ) { byte[] data = File.ReadAllBytes(ofn.FileName); if ((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); } else { crc = Crc32.Compute(data); } } else if (ofn.FileName.EndsWith(".cue", StringComparison.InvariantCultureIgnoreCase)) { CueSheet cue = new CueSheet(ofn.FileName); crc = GameDBMgr.ComputeCueCrc(cue); } else { MessageBox.Show("Invalid file extension"); return; } //Check if the crc already exists string crcstr = crc.ToString("X8"); GameDB.GameCkRow ck = gameDB.GameCk.FirstOrDefault(x => x.Checksum == crcstr); if (ck != null) { MessageBox.Show(ck.GameID == gameID ? "This checksum already exists for this game" : "This checksum already exists for a different game!!"); } else { ck = gameDB.GameCk.NewGameCkRow(); ck.GameID = gameID; ck.Checksum = crcstr; gameDB.GameCk.AddGameCkRow(ck); } } }
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); } }