Example #1
0
 public static string GetRootDir(byte b0)
 {
     if (rootDirs == null)
     {
         return(@"RomRoot\" + VarFix.ToString(b0));
     }
     else
     {
         return(rootDirs[b0]);
     }
 }
Example #2
0
        static RomRootDir()
        {
            if (!System.IO.File.Exists("DirPaths.conf"))
            {
                return;
            }
            string text = System.IO.File.ReadAllText(@"DirPaths.conf");

            text = text.Replace("\r", "");
            string[] rules = text.Split('\n');

            rootDirs = new string[256];
            for (int i = 0; i < 256; i++)
            {
                rootDirs[i] = @"RomRoot\" + VarFix.ToString((byte)i);
            }


            foreach (string rule in rules)
            {
                if (rule.Length < 6)
                {
                    continue;
                }

                string pStart = rule.Substring(0, 2);
                string ps0    = rule.Substring(2, 1);
                string pEnd   = rule.Substring(3, 2);
                string ps1    = rule.Substring(5, 1);
                string pDir   = rule.Substring(6);
                if (ps0 != "-")
                {
                    continue;
                }
                if (ps1 != "|")
                {
                    continue;
                }

                int iStart = Convert.ToInt32(pStart, 16);
                int iEnd   = Convert.ToInt32(pEnd, 16);

                for (int i = iStart; i <= iEnd; i++)
                {
                    rootDirs[i] = pDir + @"\" + VarFix.ToString((byte)i);
                }
            }
        }
Example #3
0
        public static string GetFilename(byte[] sha1, bool checkExists)
        {
            string path = "";

            bool exists = false;
            int  i      = 0;

            while (!exists)
            {
                string romRoot = AppSettings.ReadSetting("Depot" + i);
                if (romRoot == null)
                {
                    break;
                }

                path = romRoot + @"\" + VarFix.ToString(sha1[0]) + @"\" +
                       VarFix.ToString(sha1[1]) + @"\" +
                       VarFix.ToString(sha1[2]) + @"\" +
                       VarFix.ToString(sha1[3]) + @"\" +
                       VarFix.ToString(sha1) + ".gz";

                exists = (checkExists ? Alphaleonis.Win32.Filesystem.File.Exists(path) : true);
                i++;
            }

            if (!exists)
            {
                path = @"RomRoot\" + VarFix.ToString(sha1[0]) + @"\" +
                       VarFix.ToString(sha1[1]) + @"\" +
                       VarFix.ToString(sha1[2]) + @"\" +
                       VarFix.ToString(sha1[3]) + @"\" +
                       VarFix.ToString(sha1) + ".gz";
            }

            return(path);

            return(GetRootDir(sha1[0]) + @"\" +
                   VarFix.ToString(sha1[1]) + @"\" +
                   VarFix.ToString(sha1) + ".gz");
        }
Example #4
0
        public static void UpdateDB()
        {
            SetupSQLCommands();

            Program.db.ExecuteNonQuery(@"update game set dirid=(select dirId from DAT where game.Datid=dat.datid) where dirid is null;");

            using (DbDataReader drGame = ZipSetGetAllGames())
            {
                int commitCount = 0;
                Program.db.Begin();

                while (drGame.Read())
                {
                    int    GameId   = Convert.ToInt32(drGame["GameId"]);
                    string GameName = drGame["name"].ToString();
                    Debug.WriteLine("Game " + GameId + " Name: " + GameName);

                    Zip memZip = new Zip();
                    memZip.ZipCreateFake();

                    ulong fileOffset = 0;

                    int romCount = 0;
                    using (DbDataReader drRom = ZipSetGetRomsInGame(GameId))
                    {
                        while (drRom.Read())
                        {
                            int    RomId          = Convert.ToInt32(drRom["RomId"]);
                            string RomName        = drRom["name"].ToString();
                            ulong  size           = Convert.ToUInt64(drRom["size"]);
                            ulong  compressedSize = Convert.ToUInt64(drRom["compressedsize"]);
                            byte[] CRC            = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8);
                            byte[] SHA1           = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 40);
                            Debug.WriteLine("    Rom " + RomId + " Name: " + RomName + "  Size: " + size + "  Compressed: " + compressedSize + "  CRC: " + VarFix.ToString(CRC));

                            byte[] localHeader;
                            memZip.ZipFileAddFake(RomName, fileOffset, size, compressedSize, CRC, out localHeader);

                            ZipSetLocalFileHeader(RomId, localHeader, fileOffset, compressedSize, SHA1);

                            fileOffset  += (ulong)localHeader.Length + compressedSize;
                            commitCount += 1;
                            romCount    += 1;
                        }
                    }

                    byte[] centeralDir;
                    memZip.ZipFileCloseFake(fileOffset, out centeralDir);

                    if (romCount > 0)
                    {
                        ZipSetCentralFileHeader(GameId, fileOffset + (ulong)centeralDir.Length, DateTime.UtcNow.Ticks, centeralDir, fileOffset);
                    }

                    if (commitCount >= 100)
                    {
                        Program.db.Commit();
                        Program.db.Begin();
                        commitCount = 0;
                    }
                }
            }
            Program.db.Commit();

            MessageBox.Show("Zip Header Database Update Complete");
        }
Example #5
0
        private static void ZipSetLocalFileHeader(int RomId, byte[] localHeader, ulong fileOffset, ulong compressedSize, byte[] sha1)
        {
            CommandWriteLocalHeaderToRom.Parameters["localFileHeader"].Value         = localHeader;
            CommandWriteLocalHeaderToRom.Parameters["localFileHeaderOffset"].Value   = fileOffset;
            CommandWriteLocalHeaderToRom.Parameters["localFileHeaderLength"].Value   = localHeader.Length;
            CommandWriteLocalHeaderToRom.Parameters["localFileSha1"].Value           = VarFix.ToString(sha1);
            CommandWriteLocalHeaderToRom.Parameters["localFileCompressedSize"].Value = compressedSize;

            CommandWriteLocalHeaderToRom.Parameters["RomId"].Value = RomId;
            CommandWriteLocalHeaderToRom.ExecuteNonQuery();
        }
Example #6
0
        public static void extract(string dirName, string datFilename)
        {
            Debug.WriteLine(dirName);

            SQLiteCommand getfiles = new SQLiteCommand(
                $@"
                    select dir.FullName as FullName,Game.Name as GameName, Rom.Name as RomName,Size,crc,sha1,md5 from DIR
                        inner join DAT on DIR.dirId=DAT.dirId
                        inner join GAME on DAT.DatId=GAME.DatId
                        inner join ROM on GAME.GameId=ROM.GameId
                    where DIR.FullName like '{dirName}%' and FileId is null
                    order by dir.FullName,GAME.Name,ROM.RomId", DBSqlite.db.Connection);

            DbDataReader reader = getfiles.ExecuteReader();

            StreamWriter _ts = new StreamWriter(datFilename);

            _ts.WriteLine("<?xml version=\"1.0\"?>");
            _ts.WriteLine("<!DOCTYPE datafile PUBLIC \"-//Logiqx//DTD ROM Management Datafile//EN\" \"http://www.logiqx.com/Dats/datafile.dtd\">");
            _ts.WriteLine("");
            _ts.WriteLine("<datafile>");
            _ts.WriteLine("\t<header>");
            _ts.WriteLine("\t\t<name>fix_" + Etxt(dirName) + "</name>");
            _ts.WriteLine("\t\t<description>fix_" + Etxt(dirName) + "</description>");
            _ts.WriteLine("\t\t<category>FIXDATFILE</category>");
            _ts.WriteLine("\t\t<version>" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "</version>");
            _ts.WriteLine("\t\t<date>" + DateTime.Now.ToString("MM/dd/yyyy") + "</date>");
            _ts.WriteLine("\t\t<author>RomVault</author>");
            _ts.WriteLine("\t</header>");

            List <string> matchingcrc = new List <string>();

            string lastname = "";

            while (reader.Read())
            {
                string filename = reader["FullName"].ToString();
                filename = filename.Substring(dirName.Length);
                string GameName = reader["GameName"].ToString();

                string RomName = reader["RomName"].ToString();
                ulong? size    = reader["size"].Equals(DBNull.Value) ? (ulong?)null : Convert.ToUInt64(reader["size"]);
                byte[] CRC     = VarFix.CleanMD5SHA1(reader["crc"].ToString(), 8);
                byte[] sha1    = VarFix.CleanMD5SHA1(reader["sha1"].ToString(), 40);
                byte[] md5     = VarFix.CleanMD5SHA1(reader["md5"].ToString(), 32);

                string strSize = size != null ? $" size=\"{size}\"" : "";
                string strCRC  = CRC != null ? $" crc=\"{VarFix.ToString(CRC)}\"" : "";
                string strSHA1 = sha1 != null ? $" sha1=\"{VarFix.ToString(sha1)}\"" : "";
                string strMD5  = md5 != null ? $" md5=\"{VarFix.ToString(md5)}\"" : "";


                if (matchingcrc.Contains(strCRC))
                {
                    continue;
                }

                matchingcrc.Add(strCRC);


                string thisFilename = filename + GameName;
                if (thisFilename != lastname)
                {
                    if (!string.IsNullOrEmpty(lastname))
                    {
                        _ts.WriteLine($"\t</game>");
                    }

                    _ts.WriteLine($"\t<game name=\"{Etxt(thisFilename.Replace('\\','-').Replace('/','-'))}\">");
                    _ts.WriteLine($"\t\t<description>{Etxt(thisFilename.Replace('\\', '-').Replace('/', '-'))}</description>");
                    lastname = thisFilename;
                }
                _ts.WriteLine($"\t\t<rom name=\"{Etxt(RomName)}\"{strSize}{strCRC}{strSHA1}{strMD5} />");
            }
            if (!string.IsNullOrEmpty(lastname))
            {
                _ts.WriteLine($"\t</game>");
            }



            _ts.WriteLine($"</datafile>");
            _ts.Flush();
            _ts.Close();
            _ts.Dispose();
        }
Example #7
0
        public static void extract(string dirName, string outPath)
        {
            if (CommandFindRomsInGame == null)
            {
                CommandFindRomsInGame = new SQLiteCommand(
                    @"SELECT
                    ROM.RomId, ROM.name, FILES.size, FILES.compressedsize, FILES.crc, FILES.sha1
                 FROM ROM,FILES WHERE ROM.FileId=FILES.FileId AND ROM.GameId=@GameId AND ROM.PutInZip ORDER BY ROM.RomId", DBSqlite.db.Connection);
            }
            CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId"));

            Debug.WriteLine(dirName);

            SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + "%'", DBSqlite.db.Connection);

            DbDataReader reader = getfiles.ExecuteReader();

            while (reader.Read())
            {
                string outputFile = reader["fullname"].ToString() + reader["Name"].ToString() + ".zip";
                outputFile = outputFile.Substring(dirName.Length);

                outputFile = Path.Combine(outPath, outputFile).Replace(@"/", @"\");

                Debug.WriteLine(outputFile);

                int    GameId   = Convert.ToInt32(reader["GameId"]);
                string GameName = reader["name"].ToString();
                Debug.WriteLine("Game " + GameId + " Name: " + GameName);

                Zip memZip = new Zip();
                memZip.ZipCreateFake();

                ulong fileOffset = 0;

                Stream zipFs = null;

                int romCount = 0;
                using (DbDataReader drRom = ZipSetGetRomsInGame(GameId))
                {
                    while (drRom.Read())
                    {
                        int    RomId          = Convert.ToInt32(drRom["RomId"]);
                        string RomName        = drRom["name"].ToString();
                        ulong  size           = Convert.ToUInt64(drRom["size"]);
                        ulong  compressedSize = Convert.ToUInt64(drRom["compressedsize"]);
                        byte[] CRC            = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8);
                        byte[] sha1           = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 32);

                        Debug.WriteLine("    Rom " + RomId + " Name: " + RomName + "  Size: " + size + "  Compressed: " + compressedSize + "  CRC: " + VarFix.ToString(CRC));

                        byte[] localHeader;
                        memZip.ZipFileAddFake(RomName, fileOffset, size, compressedSize, CRC, out localHeader);

                        //ZipSetLocalFileHeader(RomId, localHeader, fileOffset);
                        if (romCount == 0)
                        {
                            CompressUtils.CreateDirForFile(outputFile);
                            int errorCode = FileStream.OpenFileWrite(outputFile, out zipFs);
                        }
                        zipFs.Write(localHeader, 0, localHeader.Length);

                        gZip   GZip        = new gZip();
                        string strFilename = RomRootDir.Getfilename(sha1);
                        GZip.ZipFileOpen(strFilename, -1, true);
                        GZip.ZipFileOpenReadStream(0, true, out Stream oStr, out ulong _);

                        StreamCopier.StreamCopy(oStr, zipFs, compressedSize);

                        GZip.ZipFileCloseReadStream();
                        GZip.ZipFileClose();

                        fileOffset    += (ulong)localHeader.Length + compressedSize;
                        zipFs.Position = (long)fileOffset;

                        romCount += 1;
                    }
                }

                memZip.ZipFileCloseFake(fileOffset, out byte[] centeralDir);

                if (romCount > 0)
                {
                    zipFs.Write(centeralDir, 0, centeralDir.Length);
                    zipFs.Flush();
                    zipFs.Close();
                    zipFs.Dispose();
                }
            }
        }
Example #8
0
 private static string Getfilename(byte[] SHA1)
 {
     return(@"RomRoot\" + VarFix.ToString(SHA1[0]) + @"\" +
            VarFix.ToString(SHA1[1]) + @"\" +
            VarFix.ToString(SHA1) + ".gz");
 }
Example #9
0
        private static void ScanADir(string directory)
        {
            _bgw.ReportProgress(0, new bgwText("Scanning Dir : " + directory));
            DirectoryInfo di = new DirectoryInfo(directory);

            FileInfo[] fi = di.GetFiles();

            _bgw.ReportProgress(0, new bgwRange2Visible(true));
            _bgw.ReportProgress(0, new bgwSetRange2(fi.Count()));

            for (int j = 0; j < fi.Count(); j++)
            {
                if (_bgw.CancellationPending)
                {
                    return;
                }

                FileInfo f = fi[j];
                _bgw.ReportProgress(0, new bgwValue2(j));
                _bgw.ReportProgress(0, new bgwText2(f.Name));
                string ext = Path.GetExtension(f.Name);

                if (ext.ToLower() == ".zip")
                {
                    ZipFile   fz = new ZipFile();
                    ZipReturn zr = fz.ZipFileOpen(f.FullName, f.LastWriteTime, true);
                    if (zr != ZipReturn.ZipGood)
                    {
                        continue;
                    }

                    fz.DeepScan();

                    int FileUsedCount = 0;

                    for (int i = 0; i < fz.LocalFilesCount(); i++)
                    {
                        Debug.WriteLine(fz.Filename(i));
                        RvFile tFile = new RvFile();
                        tFile.Size = fz.UncompressedSize(i);
                        tFile.CRC  = fz.CRC32(i);
                        tFile.MD5  = fz.MD5(i);
                        tFile.SHA1 = fz.SHA1(i);
                        Debug.WriteLine("CRC " + VarFix.ToString(tFile.CRC));
                        Debug.WriteLine("MD5 " + VarFix.ToString(tFile.MD5));
                        Debug.WriteLine("SHA1 " + VarFix.ToString(tFile.SHA1));


                        FindStatus res = fileneededTest(tFile);

                        if (res == FindStatus.FileUnknown)
                        {
                            continue;
                        }

                        FileUsedCount++;

                        if (res != FindStatus.FoundFileInArchive)
                        {
                            GZip gz = new GZip();
                            gz.crc              = tFile.CRC;
                            gz.md5Hash          = tFile.MD5;
                            gz.sha1Hash         = tFile.SHA1;
                            gz.uncompressedSize = tFile.Size;

                            bool   isZipTrrntzip = (fz.ZipStatus == ZipStatus.TrrntZip);
                            ulong  compressedSize;
                            ushort method;
                            Stream zds;

                            fz.ZipFileOpenReadStream(i, isZipTrrntzip, out zds, out compressedSize, out method);
                            gz.compressedSize = compressedSize;

                            string outfile = Getfilename(tFile.SHA1);
                            gz.WriteGZip(outfile, zds, isZipTrrntzip);
                            tFile.CompressedSize = gz.compressedSize;
                            fz.ZipFileCloseReadStream();

                            tFile.DBWrite();
                        }
                    }
                    fz.ZipFileClose();

                    if (delFiles && FileUsedCount == fz.LocalFilesCount())
                    {
                        File.SetAttributes(f.FullName, FileAttributes.Normal);
                        File.Delete(f.FullName);
                    }
                }
                else if (ext.ToLower() == ".gz")
                {
                    GZip      gZipTest  = new GZip();
                    ZipReturn errorcode = gZipTest.ReadGZip(f.FullName, true);
                    if (errorcode != ZipReturn.ZipGood)
                    {
                        continue;
                    }
                    RvFile tFile = new RvFile();
                    tFile.CRC  = gZipTest.crc;
                    tFile.MD5  = gZipTest.md5Hash;
                    tFile.SHA1 = gZipTest.sha1Hash;
                    tFile.Size = gZipTest.uncompressedSize;

                    FindStatus res = fileneededTest(tFile);

                    if (res == FindStatus.FileUnknown)
                    {
                        continue;
                    }

                    if (res != FindStatus.FoundFileInArchive)
                    {
                        GZip gz = new GZip();
                        gz.crc              = tFile.CRC;
                        gz.md5Hash          = tFile.MD5;
                        gz.sha1Hash         = tFile.SHA1;
                        gz.uncompressedSize = tFile.Size;

                        Stream ds;
                        gZipTest.GetStream(out ds);
                        string outfile = Getfilename(tFile.SHA1);
                        gz.WriteGZip(outfile, ds, false);
                        ds.Close();
                        ds.Dispose();

                        gZipTest.Close();
                        tFile.CompressedSize = gz.compressedSize;
                        tFile.DBWrite();
                    }

                    if (delFiles)
                    {
                        File.SetAttributes(f.FullName, FileAttributes.Normal);
                        File.Delete(f.FullName);
                    }
                }
                else
                {
                    RvFile tFile     = new RvFile();
                    int    errorcode = UnCompFiles.CheckSumRead(f.FullName, true, out tFile.CRC, out tFile.MD5, out tFile.SHA1, out tFile.Size);

                    if (errorcode != 0)
                    {
                        continue;
                    }

                    // test if needed.
                    FindStatus res = fileneededTest(tFile);

                    if (res == FindStatus.FileUnknown)
                    {
                        continue;
                    }

                    if (res != FindStatus.FoundFileInArchive)
                    {
                        GZip gz = new GZip();
                        gz.crc              = tFile.CRC;
                        gz.md5Hash          = tFile.MD5;
                        gz.sha1Hash         = tFile.SHA1;
                        gz.uncompressedSize = tFile.Size;

                        Stream ds;
                        int    errorCode = IO.FileStream.OpenFileRead(f.FullName, out ds);
                        string outfile   = Getfilename(tFile.SHA1);
                        gz.WriteGZip(outfile, ds, false);
                        ds.Close();
                        ds.Dispose();

                        tFile.CompressedSize = gz.compressedSize;
                        tFile.DBWrite();
                    }

                    if (delFiles)
                    {
                        File.SetAttributes(f.FullName, FileAttributes.Normal);
                        File.Delete(f.FullName);
                    }
                }
            }

            DirectoryInfo[] childdi = di.GetDirectories();
            foreach (DirectoryInfo d in childdi)
            {
                if (_bgw.CancellationPending)
                {
                    return;
                }
                ScanADir(d.FullName);
            }

            if (directory != rootDir && IsDirectoryEmpty(directory))
            {
                Directory.Delete(directory);
            }
        }
Example #10
0
        private void UpdateRomGrid(uint GameId)
        {
            RomGrid.Rows.Clear();

            IEnumerable <RvRom> roms = RvRom.ReadRoms(GameId);

            foreach (RvRom rom in roms)
            {
                RomGrid.Rows.Add();
                int iRow = RomGrid.Rows.Count - 1;

                RomGrid.Rows[iRow].Selected       = false;
                RomGrid.Rows[iRow].Tag            = rom.RomId;
                RomGrid.Rows[iRow].Cells[1].Value = rom.Name;
                if (rom.Size != null)
                {
                    RomGrid.Rows[iRow].Cells[2].Value = rom.Size;
                }
                else if (rom.FileSize != null)
                {
                    RomGrid.Rows[iRow].Cells[2].Style.ForeColor = Color.FromArgb(0, 0, 255);
                    RomGrid.Rows[iRow].Cells[2].Value           = rom.Size;
                }

                if (rom.FileCompressedSize != null)
                {
                    RomGrid.Rows[iRow].Cells[3].Style.ForeColor = Color.FromArgb(0, 0, 255);
                    RomGrid.Rows[iRow].Cells[3].Value           = rom.FileCompressedSize;
                }

                RomGrid.Rows[iRow].Cells[4].Value = rom.Merge;

                if (rom.CRC != null)
                {
                    RomGrid.Rows[iRow].Cells[5].Value = VarFix.ToString(rom.CRC);
                }
                else if (rom.FileCRC != null)
                {
                    RomGrid.Rows[iRow].Cells[5].Style.ForeColor = Color.FromArgb(0, 0, 255);
                    RomGrid.Rows[iRow].Cells[5].Value           = VarFix.ToString(rom.FileCRC);
                }

                if (rom.SHA1 != null)
                {
                    RomGrid.Rows[iRow].Cells[6].Value = VarFix.ToString(rom.SHA1);
                }
                else if (rom.FileSHA1 != null)
                {
                    RomGrid.Rows[iRow].Cells[6].Style.ForeColor = Color.FromArgb(0, 0, 255);
                    RomGrid.Rows[iRow].Cells[6].Value           = VarFix.ToString(rom.FileSHA1);
                }
                if (rom.MD5 != null)
                {
                    RomGrid.Rows[iRow].Cells[7].Value = VarFix.ToString(rom.MD5);
                }
                else if (rom.FileMD5 != null)
                {
                    RomGrid.Rows[iRow].Cells[7].Style.ForeColor = Color.FromArgb(0, 0, 255);
                    RomGrid.Rows[iRow].Cells[7].Value           = VarFix.ToString(rom.FileMD5);
                }
                RomGrid.Rows[iRow].Cells[8].Value = rom.Status;
                RomGrid.Rows[iRow].Cells[9].Value = rom.PutInZip;

                if (((rom.Status == "nodump") && (rom.CRC == null) && (rom.SHA1 == null) && (rom.MD5 == null) && (rom.FileId == null)) || !rom.PutInZip)
                {
                    RomGrid.Rows[iRow].DefaultCellStyle.BackColor = CGray;
                }
                else
                {
                    RomGrid.Rows[iRow].DefaultCellStyle.BackColor = rom.FileId != null ? CGreen : CRed;
                }
            }
        }
Example #11
0
        public static void extract(string dirName)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            DialogResult        result = folderBrowserDialog1.ShowDialog();

            if (result != DialogResult.OK)
            {
                return;
            }
            string outPath = folderBrowserDialog1.SelectedPath;

            if (CommandFindRomsInGame == null)
            {
                CommandFindRomsInGame = new SQLiteCommand(
                    @"SELECT
                    ROM.RomId, ROM.name, FILES.size, FILES.compressedsize, FILES.crc, FILES.sha1
                 FROM ROM,FILES WHERE ROM.FileId=FILES.FileId AND ROM.GameId=@GameId AND ROM.PutInZip ORDER BY ROM.RomId", Program.db.Connection);
            }
            CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId"));


            byte[] buff = new byte[1024];

            Debug.WriteLine(dirName);

            SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + "%'", Program.db.Connection);

            DbDataReader reader = getfiles.ExecuteReader();

            while (reader.Read())
            {
                string outputFile = reader["fullname"].ToString() + reader["Name"].ToString() + ".zip";
                outputFile = outputFile.Substring(dirName.Length);

                outputFile = Path.Combine(outPath, outputFile).Replace(@"/", @"\");

                Debug.WriteLine(outputFile);

                int    GameId   = Convert.ToInt32(reader["GameId"]);
                string GameName = reader["name"].ToString();
                Debug.WriteLine("Game " + GameId + " Name: " + GameName);

                ZipFile memZip = new ZipFile();
                memZip.ZipCreateFake();

                ulong fileOffset = 0;

                Stream _zipFs = null;

                int romCount = 0;
                using (DbDataReader drRom = ZipSetGetRomsInGame(GameId))
                {
                    while (drRom.Read())
                    {
                        int    RomId          = Convert.ToInt32(drRom["RomId"]);
                        string RomName        = drRom["name"].ToString();
                        ulong  size           = Convert.ToUInt64(drRom["size"]);
                        ulong  compressedSize = Convert.ToUInt64(drRom["compressedsize"]);
                        byte[] CRC            = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8);
                        byte[] sha1           = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 32);

                        Debug.WriteLine("    Rom " + RomId + " Name: " + RomName + "  Size: " + size + "  Compressed: " + compressedSize + "  CRC: " + VarFix.ToString(CRC));

                        byte[] localHeader;
                        memZip.ZipFileAddFake(RomName, fileOffset, size, compressedSize, CRC, out localHeader);

                        //ZipSetLocalFileHeader(RomId, localHeader, fileOffset);
                        if (romCount == 0)
                        {
                            ZipFile.CreateDirForFile(outputFile);
                            int errorCode = FileStream.OpenFileWrite(outputFile, out _zipFs);
                        }
                        _zipFs.Write(localHeader, 0, localHeader.Length);

                        GZip   GZip        = new GZip();
                        string strFilename = RomRootDir.GetFilename(sha1, true);
                        GZip.ReadGZip(strFilename, false);
                        Stream oStr;
                        GZip.GetRawStream(out oStr);

                        ulong sizetogo = compressedSize;
                        while (sizetogo > 0)
                        {
                            ulong sizenow = sizetogo > 1024 ? 1024 : sizetogo;
                            oStr.Read(buff, 0, (int)sizenow);
                            _zipFs.Write(buff, 0, (int)sizenow);
                            sizetogo -= sizenow;
                        }
                        oStr.Dispose();
                        GZip.Close();

                        fileOffset     += (ulong)localHeader.Length + compressedSize;
                        _zipFs.Position = (long)fileOffset;

                        romCount += 1;
                    }
                }

                byte[] centeralDir;
                memZip.ZipFileCloseFake(fileOffset, out centeralDir);

                if (romCount > 0)
                {
                    _zipFs.Write(centeralDir, 0, centeralDir.Length);
                    _zipFs.Flush();
                    _zipFs.Close();
                    _zipFs.Dispose();
                }
            }
        }
Example #12
0
 public static string Getfilename(byte[] sha1)
 {
     return(GetRootDir(sha1[0]) + @"\" +
            VarFix.ToString(sha1[1]) + @"\" +
            VarFix.ToString(sha1) + ".gz");
 }
Example #13
0
        public static void extract(string dirName)
        {
            if (CommandFindRomsInGame == null)
            {
                CommandFindRomsInGame = new SQLiteCommand(
                    @"SELECT
                    ROM.RomId, ROM.name, ROM.size, ROM.crc, ROM.SHA1
                 FROM ROM WHERE ROM.FileId is null AND ROM.GameId=@GameId ORDER BY ROM.RomId", Program.db.Connection);
            }
            CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId"));

            Debug.WriteLine(dirName);

            SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dat.datid,dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + @"%' 
and (select count(1) from ROM WHERE ROM.FileId is null AND ROM.GameId=Game.GameId)>0 order by dat.datid,GameId", Program.db.Connection);

            DbDataReader reader = getfiles.ExecuteReader();

            int DatId = -1;

            while (reader.Read())
            {
                int thisDatId = Convert.ToInt32(reader["datId"]);
                if (thisDatId == DatId)
                {
                    string dirFullName = reader["FullName"].ToString();
                    Debug.WriteLine(dirFullName);
                    DatId = thisDatId;
                }
                int    GameId   = Convert.ToInt32(reader["GameId"]);
                string GameName = reader["name"].ToString();
                Debug.WriteLine("Game " + GameId + " Name: " + GameName);

                int romCount = 0;
                using (DbDataReader drRom = ZipSetGetRomsInGame(GameId))
                {
                    while (drRom.Read())
                    {
                        int    RomId   = Convert.ToInt32(drRom["RomId"]);
                        string RomName = drRom["name"].ToString();
                        ulong  size    = Convert.ToUInt64(drRom["size"]);
                        byte[] CRC     = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8);
                        byte[] sha1    = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 32);

                        Debug.WriteLine("    Rom " + RomId + " Name: " + RomName + "  Size: " + size + "  CRC: " + VarFix.ToString(CRC));


                        romCount += 1;
                    }
                }
            }
        }
Example #14
0
        public static void extract(string dirName)
        {

            if (CommandFindRomsInGame == null)
                CommandFindRomsInGame = new SQLiteCommand(
                    @"SELECT
                    ROM.RomId, ROM.name, ROM.size, ROM.crc, ROM.SHA1, ROM.MD5
                 FROM ROM WHERE ROM.GameId=@GameId and ROM.FileId is null ORDER BY ROM.RomId", Program.db.Connection);
            CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId"));

            Debug.WriteLine(dirName);

            SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dat.datid,dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + @"%' 
and (select count(1) from ROM WHERE ROM.FileId is null AND ROM.GameId=Game.GameId)>0 order by dat.datid,GameId", Program.db.Connection);

            DbDataReader reader = getfiles.ExecuteReader();

            int DatId = -1;

            string datFilename = @"D:\fixOut.dat";

            StreamWriter _ts = new StreamWriter(datFilename);

            _ts.WriteLine("<?xml version=\"1.0\"?>");
            _ts.WriteLine(
                "<!DOCTYPE datafile PUBLIC \"-//Logiqx//DTD ROM Management Datafile//EN\" \"http://www.logiqx.com/Dats/datafile.dtd\">");
            _ts.WriteLine("");
            _ts.WriteLine("<datafile>");
            _ts.WriteLine("\t<header>");
            _ts.WriteLine("\t\t<name>fix_" + Etxt(dirName) + "</name>");
            _ts.WriteLine("\t\t<description>fix_" + Etxt(dirName) + "</description>");
            _ts.WriteLine("\t\t<category>FIXDATFILE</category>");
            _ts.WriteLine("\t\t<version>" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "</version>");
            _ts.WriteLine("\t\t<date>" + DateTime.Now.ToString("MM/dd/yyyy") + "</date>");
            _ts.WriteLine("\t\t<author>RomVault</author>");
            _ts.WriteLine("\t</header>");

            List<string> matchingcrc=new List<string>();

            while (reader.Read())
            {
                int thisDatId = Convert.ToInt32(reader["datId"]);
                if (thisDatId == DatId)
                {
                    string dirFullName = reader["FullName"].ToString();
                    Debug.WriteLine(dirFullName);
                    DatId = thisDatId;
                }
                string filename = reader["FullName"].ToString();
                filename = filename.Substring(dirName.Length);
                int GameId = Convert.ToInt32(reader["GameId"]);
                string GameName = reader["name"].ToString();
                Debug.WriteLine("Fullname: " + filename + " Game: " + GameId + " Name: " + GameName);

                bool found = false;
                int romCount = 0;
                using (DbDataReader drRom = ZipSetGetRomsInGame(GameId))
                {
                    while (drRom.Read())
                    {
                        int RomId = Convert.ToInt32(drRom["RomId"]);
                        string RomName = drRom["name"].ToString();
                        ulong? size = drRom["size"].Equals(DBNull.Value) ? (ulong?)null : Convert.ToUInt64(drRom["size"]);
                        byte[] CRC = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8);
                        byte[] sha1 = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 40);
                        byte[] md5 = VarFix.CleanMD5SHA1(drRom["md5"].ToString(), 32);

                        string strSize = size != null ? $" size=\"{size}\"" : "";
                        string strCRC = CRC != null ? $" crc=\"{VarFix.ToString(CRC)}\"" : "";
                        string strSHA1 = sha1 != null ? $" sha1=\"{VarFix.ToString(sha1)}\"" : "";
                        string strMD5 = md5 != null ? $" md5=\"{VarFix.ToString(md5)}\"" : "";

                        if (matchingcrc.Contains(strCRC))
                            continue;

                        matchingcrc.Add(strCRC);

                        if (!found)
                        {
                            _ts.WriteLine($"\t<game name=\"{Etxt(filename + GameName)}\">");
                            _ts.WriteLine($"\t\t<description>{Etxt(filename + GameName)}</description>");
                            found = true;
                        }
                        Debug.WriteLine("    Rom " + RomId + " Name: " + RomName + "  Size: " + size + "  CRC: " + VarFix.ToString(CRC));
                        _ts.WriteLine($"\t\t<rom name=\"{Etxt(RomName)}\"{strSize}{strCRC}{strSHA1}{strMD5} />");

                        romCount += 1;
                    }
                }
                if (found)
                    _ts.WriteLine($"\t</game>");

            }

            _ts.WriteLine($"</datafile>");
            _ts.Flush();
            _ts.Close();
            _ts.Dispose();
        }