コード例 #1
0
 public VPKProvider(String file)
 {
     if (VPK == null)
     {
         VPK = new VPKFile(file);
     }
 }
コード例 #2
0
        public void CloseStreams()
        {
            if (currentStream != null)
            {
                currentStream.Close();
            }

            if (VPK != null)
            {
                VPK.Dispose();
            }

            currentStream = null;
            VPK           = null;
        }
コード例 #3
0
        /// <summary>
        /// Creates a new instance of the VPKIndex class and creates
        /// an index of all .vpk files found in the specified directory.
        /// </summary>
        /// <param name="VPKDirectory">Directory that contains the .vpk files to index.</param>
        internal VPKIndex(string VPKDirectory)
            : base(FormatterType.Binary)
        {
            List<VPKIndexEntry> vpkIndexEntries = new List<VPKIndexEntry>();
            string[] vpkFiles = Directory.GetFiles(VPKDirectory, "*.vpk", SearchOption.TopDirectoryOnly);

            foreach (string currentVPKFile in vpkFiles)
            {
                VPKFile VPKFile = new VPKFile(currentVPKFile);
                VPKSupport.DirectoryEntry[] entries = VPKFile.GetEntries();
                foreach (VPKSupport.DirectoryEntry entry in entries)
                {
                    vpkIndexEntries.Add(new VPKIndexEntry(entry, currentVPKFile));
                }
            }

            vpkentries = vpkIndexEntries.ToArray();

            Lib.CommunicationsObject.AddLog("Indexed " + vpkIndexEntries.Count.ToString() + " files in VPKs");

            Save();
        }
コード例 #4
0
ファイル: VPK.cs プロジェクト: ModDota/ModDotaClient2
        /// <summary>
        /// Adds a file (already in memory) to a vpk.
        /// </summary>
        /// <param name="contents">The file contents.</param>
        /// <param name="internalfilename">The file name, including path, internally.</param>
        public void AddFile(byte[] contents, string internalfilename)
        {
            Crc32.Crc32Algorithm checker = new Crc32.Crc32Algorithm();
            byte[] crc = checker.ComputeHash(contents);
            if (crc.Length != 4)
            {
                Console.WriteLine("wtf the crc isn't 32 bits long");
            }
            VPKFile asfile = new VPKFile(null);

            asfile.CRC32        = BitConverter.ToUInt32(crc, 0);
            asfile.entry_length = (uint)(contents.Length);

            // Now that we have the basics of the file, we need to figure out how to store it
            // This involves first loading the vpk directory information
            List <VPKExt> dir = GetDirectory();
            // Now we need to just add the file, and write new directory information
            // Get the location to which to write the file
            Tuple <int, uint> slot = GetNewestSpace(dir);
            int  paddinglength     = (int)filealignment - (int)((int)(slot.Item2) % (int)filealignment);
            int  archivenum        = slot.Item1;
            uint offset            = slot.Item2;

            // Create a new archive file if necessary
            if (contents.Length + slot.Item2 + paddinglength > maxarchivesize)
            {
                paddinglength = 0;
                archivenum++;
                offset = 0;
            }
            // Write the file to the archive
            using (FileStream foo = File.OpenWrite(String.Format("{0}_{1:D3}.vpk", fileprefix, archivenum)))
            {
                using (BinaryWriter bw = new BinaryWriter(foo))
                {
                    bw.Seek((int)offset, SeekOrigin.Begin);
                    for (int i = 0; i < paddinglength; i++)
                    {
                        bw.Write((byte)0);
                    }
                    bw.Write(contents);
                }
            }
            // Add the padding to the offset so it properly reflects the file
            offset += (uint)paddinglength;
            // Add the file information to the directory
            string ext = internalfilename.Substring(internalfilename.IndexOf('.') + 1);

            internalfilename = internalfilename.Substring(0, internalfilename.IndexOf('.'));
            char[] lookfor = { '\\', '/' };
            string path    = internalfilename.Substring(0, internalfilename.LastIndexOfAny(lookfor));
            string name    = internalfilename.Substring(internalfilename.LastIndexOfAny(lookfor) + 1);
            // Find where to put it
            VPKExt writeext = null;

            foreach (VPKExt vext in dir)
            {
                if (vext.ext == ext)
                {
                    writeext = vext;
                }
            }
            if (writeext == null)
            {
                writeext             = new VPKExt();
                writeext.ext         = ext;
                writeext.directories = new List <VPKDirectory>();
                dir.Add(writeext);
            }
            VPKDirectory writedir = null;

            foreach (VPKDirectory vpkd in writeext.directories)
            {
                if (vpkd.path == path)
                {
                    writedir = vpkd;
                }
            }
            if (writedir == null)
            {
                writedir         = new VPKDirectory();
                writedir.path    = path;
                writedir.ext     = ext;
                writedir.entries = new List <VPKFileEntry>();
                writeext.directories.Add(writedir);
            }
            VPKFileEntry writefile = null;

            foreach (VPKFileEntry vpkfe in writedir.entries)
            {
                if (vpkfe.name == name)
                {
                    writefile = vpkfe;
                    //ok, we gotta do a file replacement
                    //thankfully, this is just overwriting a few values
                    writefile.body.archive_index = (ushort)archivenum;
                    writefile.body.CRC32         = BitConverter.ToUInt32(crc, 0);
                    writefile.body.entry_length  = (uint)(contents.Length);
                    writefile.body.entry_offset  = offset;
                    writefile.body.preload_bytes = 0;
                }
            }
            if (writefile == null)
            {
                // Didn't find a file entry, so let's add one
                writefile = new VPKFileEntry();
                writefile.body.archive_index = (ushort)archivenum;
                writefile.body.CRC32         = BitConverter.ToUInt32(crc, 0);
                writefile.body.entry_length  = (uint)(contents.Length);
                writefile.body.entry_offset  = offset;
                writefile.body.preload_bytes = 0;
                writedir.entries.Add(writefile);
            }
            // Write the new directory to the directory file.
            WriteDirectory(dir);
        }