Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PK2Entry" /> class.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="block">The block.</param>
        /// <param name="index">The index of the entry inside the block.</param>
        /// <exception cref="InvalidEntryException"></exception>
        public PK2Entry(byte[] buffer, PK2Block block, byte index)
        {
            Index = index;
            Block = block;

            using (var streamWorker = new StreamWorker(buffer, StreamOperation.Read))
            {
                try
                {
                    Type       = (PK2EntryType)streamWorker.ReadByte();
                    Name       = streamWorker.ReadString(81).Trim('\0');
                    AccessTime = DateTime.FromFileTimeUtc(streamWorker.ReadLong());
                    CreateTime = DateTime.FromFileTimeUtc(streamWorker.ReadLong());
                    ModifyTime = DateTime.FromFileTimeUtc(streamWorker.ReadLong());
                    Position   = streamWorker.ReadULong();
                    Size       = streamWorker.ReadUInt();
                    NextChain  = streamWorker.ReadULong();
                    streamWorker.ReadByteArray(2); //Padding to reach 128 bytes length
                }
                catch
                {
                    throw new InvalidEntryException(buffer);
                }
            }
        }
Beispiel #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);
        }