Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PK2Block" /> class.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="offset">The block offset.</param>
        public PK2Block(byte[] buffer, ulong offset)
        {
            Entries = new PK2Entry[20];
            Offset  = offset;

            using (var streamWorker = new StreamWorker(buffer, StreamOperation.Read))
            {
                for (var i = 0; i < 20; i++)
                {
                    var entryBuffer = streamWorker.ReadByteArray(128);

                    if (BlowfishUtilities.GetBlowfish() != null)
                    {
                        entryBuffer = BlowfishUtilities.GetBlowfish().Decode(entryBuffer);
                    }

                    Entries[i] = new PK2Entry(entryBuffer, this, (byte)i);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Creates the file.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="parent">The parent.</param>
        /// <param name="name">The name.</param>
        /// <param name="data">The data. Obsolete if the type is a directory.</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException">Can only create new items in a directory!</exception>
        /// <exception cref="PK2NotLoadedException"></exception>
        /// <exception cref="System.NullReferenceException">Can not write to an empty block</exception>
        public static PK2Entry Create(PK2EntryType type, PK2Entry parent, string name, byte[] data = null)
        {
            if (parent.Type != PK2EntryType.Directory)
            {
                throw new InvalidOperationException("Can only create new items in a directory!");
            }

            if (FileAdapter.GetInstance() == null)
            {
                throw new PK2NotLoadedException();
            }

            if (parent.GetChildBlock() == null)
            {
                throw new NullReferenceException("Can not write to an empty block");
            }

            var entryToOverwrite = parent.GetChildBlock().GetFirstEmptyEntry();

            //If all blocks are ocupied we have to create a new one
            if (entryToOverwrite == null)
            {
                var newBlock = PK2Block.Create();

                //Tell the last entry where to find our new block..
                var lastEntry = parent.GetChildBlock().GetLastBlock().Entries[19];
                lastEntry.NextChain = newBlock.Offset;
                lastEntry.Save();

                entryToOverwrite = newBlock.GetFirstEmptyEntry();
            }

            if (type == PK2EntryType.Directory && name == "." || name == "..")
            {
                entryToOverwrite.Position = name == "." ? entryToOverwrite.Block.Offset : parent.Block.Offset;
            }

            //create new block for the sub directories
            if (type == PK2EntryType.Directory && name != "." && name != "..")
            {
                var newBlock = PK2Block.Create();
                entryToOverwrite.Position = newBlock.Offset;
            }

            //Assign properties to the entry
            entryToOverwrite.Type       = type;
            entryToOverwrite.AccessTime = DateTime.Now;
            entryToOverwrite.CreateTime = DateTime.Now;
            entryToOverwrite.ModifyTime = DateTime.Now;
            entryToOverwrite.Name       = name;

            if (type == PK2EntryType.File && data != null)
            {
                entryToOverwrite.Size = (uint)data.Length;
            }

            //Write file to PK2 and assign the position
            if (type == PK2EntryType.File)
            {
                entryToOverwrite.Position = (ulong)FileAdapter.GetInstance().AppendData(data);
            }

            //Write the entry to the PK2
            entryToOverwrite.Save();

            if (type != PK2EntryType.Directory || name == "." || name == "..")
            {
                return(entryToOverwrite);
            }

            //Create those browser things...
            Create(PK2EntryType.Directory, entryToOverwrite, ".");
            Create(PK2EntryType.Directory, entryToOverwrite, "..");

            return(entryToOverwrite);
        }