コード例 #1
0
ファイル: PK2Archive.cs プロジェクト: x3nx1a/SlimPK2
        /// <summary>
        /// Initializes a new instance of the <see cref="PK2Archive"/> class.
        /// The key is set to the isro standard encryption key.
        /// Do not provide any baseKey, if you wish to use the default base key for the archive.
        /// </summary>
        /// <exception cref="System.IO.FileNotFoundException"></exception>
        /// <exception cref="SlimPK2.Security.BlowfishSecurityException"></exception>
        /// <exception cref="SlimPK2.Types.InvalidHeaderException"></exception>
        public PK2Archive(string path, PK2Config config = null)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }

            //Initialize default config
            if (config == null)
            {
                config = PK2Config.GetDefault();
            }

            //assign the config to this instance
            Config = config;

            //Create file reader...
            FileAdapter.SetInstance(new FileAdapter(path));
            Path = path;

            //Read header...
            Header = new PK2Header(FileAdapter.GetInstance().ReadData(0, 256));

            if (Header.Encrypted)
            {
                var blowfishKey = BlowfishUtilities.GenerateFinalBlowfishKey(Config.Key, Config.BaseKey);
                var blowfish    = new Blowfish();
                blowfish.Initialize(blowfishKey);
                BlowfishUtilities.SetBlowfish(blowfish);

                var tempChecksum =
                    BlowfishUtilities.GetBlowfish().Encode(Encoding.ASCII.GetBytes("Joymax Pak File"));

                //Check if the security checksum equals the generated checksum
                if (tempChecksum[0] != Header.SecurityChecksum[0] ||
                    tempChecksum[1] != Header.SecurityChecksum[1] ||
                    tempChecksum[2] != Header.SecurityChecksum[2])
                {
                    throw new BlowfishSecurityException(Config.Key);
                }
            }

            if (Config.Mode != PK2Mode.FreeBrowse)
            {
                switch (Config.Mode)
                {
                case PK2Mode.IndexBlocks:
                    Blocks = new List <PK2Block>();
                    ReadBlockAt(256);
                    break;

                case PK2Mode.Index:
                    _files       = new List <PK2File>();
                    _directories = new List <PK2Directory>();

                    ReadBlockAt(256, new PK2Directory());
                    break;
                }
            }
            else
            {
                _navigator = new PK2Navigator();
            }

            Loaded = true;
        }