コード例 #1
0
ファイル: fpkParser.cs プロジェクト: kkdevs/sb3u
        public override List <IWriteFile> ReadDirectory(string path)
        {
            List <IWriteFile> subfiles = null;

            using (BinaryReader reader = new BinaryReader(new BufferedStream(File.OpenRead(path))))
            {
                int numFiles = reader.ReadInt32();

                subfiles = new List <IWriteFile>(numFiles);
                for (int i = 0; i < numFiles; i++)
                {
                    fpkSubfile subfile = new fpkSubfile(path);
                    subfile.offset = reader.ReadUInt32();
                    subfile.size   = reader.ReadUInt32();
                    byte[] nameBuf = reader.ReadBytes(20);
                    int    len     = 0;
                    while (nameBuf[len] != 0)
                    {
                        len++;
                    }
                    subfile.Name = Utility.EncodingShiftJIS.GetString(nameBuf, 0, len);
                    subfile.crc  = reader.ReadInt32();
                    subfiles.Add(subfile);
                }
            }

            return(subfiles);
        }
コード例 #2
0
ファイル: fpkParser.cs プロジェクト: hejob/SB3Utility
        public override List<IWriteFile> ReadDirectory(string path)
        {
            List<IWriteFile> subfiles = null;
            using (BinaryReader reader = new BinaryReader(new BufferedStream(File.OpenRead(path))))
            {
                int numFiles = reader.ReadInt32();

                subfiles = new List<IWriteFile>(numFiles);
                for (int i = 0; i < numFiles; i++)
                {
                    fpkSubfile subfile = new fpkSubfile(path);
                    subfile.offset = reader.ReadUInt32();
                    subfile.size = reader.ReadUInt32();
                    byte[] nameBuf = reader.ReadBytes(20);
                    int len = 0;
                    while (nameBuf[len] != 0)
                        len++;
                    subfile.Name = Utility.EncodingShiftJIS.GetString(nameBuf, 0, len);
                    subfile.crc = reader.ReadInt32();
                    subfiles.Add(subfile);
                }
            }

            return subfiles;
        }
コード例 #3
0
ファイル: fpkParser.cs プロジェクト: kkdevs/sb3u
        public override List <IWriteFile> ReadDirectory(string path)
        {
            List <IWriteFile> subfiles = null;

            using (BinaryReader reader = new BinaryReader(File.OpenRead(path)))
            {
                int numFiles = reader.ReadInt32() & (int)~0x80000000;
                reader.BaseStream.Seek(-8, SeekOrigin.End);
                DirMask     = reader.ReadBytes(4);
                DirPosition = reader.ReadInt32();
                reader.BaseStream.Seek(DirPosition, SeekOrigin.Begin);

                ICryptoTransform cryptoTrans  = new CryptoTransformOneCode(DirMask);
                CryptoStream     cryptoStream = new CryptoStream(reader.BaseStream, cryptoTrans, CryptoStreamMode.Read);
                using (BinaryReader cryptoReader = new BinaryReader(cryptoStream))
                {
                    subfiles = new List <IWriteFile>(numFiles);
                    for (int i = 0; i < numFiles; i++)
                    {
                        fpkSubfile subfile = new fpkSubfile(path);
                        subfile.offset = cryptoReader.ReadUInt32();
                        subfile.size   = cryptoReader.ReadUInt32();
                        byte[] nameBuf = cryptoReader.ReadBytes(128);
                        int    len     = 0;
                        while (len < nameBuf.Length && nameBuf[len] != 0)
                        {
                            len++;
                        }
                        subfile.Name = Utility.EncodingShiftJIS.GetString(nameBuf, 0, len);
                        subfile.crc  = cryptoReader.ReadInt32();
                        subfiles.Add(subfile);

                        try
                        {
                            if (Hash(nameBuf) != subfile.crc)
                            {
                                Report.ReportLog("bad hash code for <" + subfile.Name + ">");
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }

            return(subfiles);
        }
コード例 #4
0
ファイル: fpkParser.cs プロジェクト: kkdevs/sb3u
        void RestoreBackup(string destPath, string backup)
        {
            if (File.Exists(destPath) && File.Exists(backup))
            {
                File.Delete(destPath);

                if (backup != null)
                {
                    File.Move(backup, destPath);

                    if (destPath.Equals(this.FilePath, StringComparison.InvariantCultureIgnoreCase))
                    {
                        for (int i = 0; i < Subfiles.Count; i++)
                        {
                            fpkSubfile subfile = Subfiles[i] as fpkSubfile;
                            if ((subfile != null) && subfile.fpkPath.Equals(backup, StringComparison.InvariantCultureIgnoreCase))
                            {
                                subfile.fpkPath = this.FilePath;
                            }
                        }
                    }
                }
            }
        }
コード例 #5
0
ファイル: fpkParser.cs プロジェクト: kkdevs/sb3u
        void writeArchiveWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;
            string           backup = null;

            string dirName = Path.GetDirectoryName(destPath);

            if (dirName == String.Empty)
            {
                dirName = @".\";
            }
            DirectoryInfo dir = new DirectoryInfo(dirName);

            if (!dir.Exists)
            {
                dir.Create();
            }

            if (File.Exists(destPath))
            {
                backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(destPath));
                File.Move(destPath, backup);

                if (destPath.Equals(this.FilePath, StringComparison.InvariantCultureIgnoreCase))
                {
                    for (int i = 0; i < Subfiles.Count; i++)
                    {
                        fpkSubfile subfile = Subfiles[i] as fpkSubfile;
                        if ((subfile != null) && subfile.fpkPath.Equals(this.FilePath, StringComparison.InvariantCultureIgnoreCase))
                        {
                            subfile.fpkPath = backup;
                        }
                    }
                }
            }

            try
            {
                using (BinaryWriter writer = new BinaryWriter(File.Create(destPath)))
                {
                    this.DirFormat.WriteHeader(writer.BaseStream, this.Subfiles);
                    int   offset = (int)writer.BaseStream.Position;
                    int[] sizes  = new int[Subfiles.Count];
                    int[] CRCs   = new int[Subfiles.Count];

                    for (int i = 0; i < Subfiles.Count; i++)
                    {
                        if (worker.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }

                        worker.ReportProgress(i * 100 / Subfiles.Count);

                        fpkSubfile subfile = Subfiles[i] as fpkSubfile;
                        if (subfile != null)
                        {
                            using (BinaryReader reader = new BinaryReader(File.OpenRead(subfile.fpkPath)))
                            {
                                reader.BaseStream.Seek(subfile.offset, SeekOrigin.Begin);

                                uint readSteps = subfile.size / Utility.BufSize;
                                for (int j = 0; j < readSteps; j++)
                                {
                                    writer.Write(reader.ReadBytes(Utility.BufSize));
                                }
                                writer.Write(reader.ReadBytes(subfile.size % Utility.BufSize));
                            }
                            CRCs[i] = this.DirFormat is fpkDirFormat_Style2 && subfile.crc == 0 ?
                                      fpkDirFormat_Style2.Hash(Utility.EncodingShiftJIS.GetBytes(Subfiles[i].Name)) : subfile.crc;
                        }
                        else
                        {
                            fpkSubfileFormat format = compress ? (fpkSubfileFormat) new fpkSubfileFormat_ZLC2() : (fpkSubfileFormat) new fpkSubfileFormat_Uncompressed();
                            Stream           stream = format.WriteStream(writer.BaseStream);
                            Subfiles[i].WriteTo(stream);
                            format.FinishWriteTo(stream);
                            CRCs[i] = this.DirFormat is fpkDirFormat_Style2?
                                      fpkDirFormat_Style2.Hash(Utility.EncodingShiftJIS.GetBytes(Subfiles[i].Name)) : 0;
                        }

                        int pos = (int)writer.BaseStream.Position;
                        sizes[i] = pos - offset;
                        offset   = pos;
                    }

                    if (!e.Cancel)
                    {
                        this.DirFormat.WriteFooter(writer.BaseStream, this.Subfiles, sizes, CRCs);
                    }
                }

                if (e.Cancel)
                {
                    RestoreBackup(destPath, backup);
                }
                else
                {
                    this.FilePath = destPath;

                    if ((backup != null) && !keepBackup)
                    {
                        File.Delete(backup);
                    }
                }
            }
            catch (Exception ex)
            {
                RestoreBackup(destPath, backup);
                Utility.ReportException(ex);
            }
        }
コード例 #6
0
ファイル: fpkParser.cs プロジェクト: hejob/SB3Utility
        public override List<IWriteFile> ReadDirectory(string path)
        {
            List<IWriteFile> subfiles = null;
            using (BinaryReader reader = new BinaryReader(File.OpenRead(path)))
            {
                int numFiles = reader.ReadInt32() & (int)~0x80000000;
                reader.BaseStream.Seek(-8, SeekOrigin.End);
                DirMask = reader.ReadBytes(4);
                DirPosition = reader.ReadInt32();
                reader.BaseStream.Seek(DirPosition, SeekOrigin.Begin);

                ICryptoTransform cryptoTrans = new CryptoTransformOneCode(DirMask);
                CryptoStream cryptoStream = new CryptoStream(reader.BaseStream, cryptoTrans, CryptoStreamMode.Read);
                using (BinaryReader cryptoReader = new BinaryReader(cryptoStream))
                {
                    subfiles = new List<IWriteFile>(numFiles);
                    for (int i = 0; i < numFiles; i++)
                    {
                        fpkSubfile subfile = new fpkSubfile(path);
                        subfile.offset = cryptoReader.ReadUInt32();
                        subfile.size = cryptoReader.ReadUInt32();
                        byte[] nameBuf = cryptoReader.ReadBytes(128);
                        int len = 0;
                        while (len < nameBuf.Length && nameBuf[len] != 0)
                            len++;
                        subfile.Name = Utility.EncodingShiftJIS.GetString(nameBuf, 0, len);
                        subfile.crc = cryptoReader.ReadInt32();
                        subfiles.Add(subfile);

                        try
                        {
                            if (Hash(nameBuf) != subfile.crc)
                                Report.ReportLog("bad hash code for <" + subfile.Name + ">");
                        }
                        catch
                        {
                        }
                    }
                }
            }

            return subfiles;
        }