Exemple #1
0
        void process(string path)
        {
            try
            {
                stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader reader = new BinaryReader(stream);

                header.FileCount = reader.ReadInt32();
                header.Version   = reader.ReadInt32();
                header.Type      = (PackageType)reader.ReadInt32();

                for (int i = 0; i < header.FileCount; i++)
                {
                    PakFileInfo info = new PakFileInfo();
                    info.Position = reader.ReadInt32();
                    info.Length   = reader.ReadInt32();
                    info.Name     = reader.ReadString().ToLower();

                    if (info.Name.StartsWith(subPath))
                    {
                        info.Name = info.Name.Substring(subPath.Length);
                        dicFiles.Add(info.Name, info);
                    }
                }

                basePos = (int)stream.Position;
            }
            catch
            {
                return;
            }
        }
Exemple #2
0
        public File GetFile(FilePath filePath, FileOpenMode mode)
        {
            if (mode != FileOpenMode.Read)
            {
                throw new InvalidOperationException();
            }

            // lock to only one thread at a time
            lock (this)
            {
                if (!dicFiles.ContainsKey(filePath.PathWithoutPackage))
                {
                    return(null);
                }

                PakFileInfo info = dicFiles[filePath.PathWithoutPackage];

                MemoryStream fileInMemory = new MemoryStream();
                stream.Seek(info.Position + basePos, SeekOrigin.Begin);

                int    remaining = info.Length;
                byte[] buf       = new byte[1024 * 4];
                while (remaining > 0)
                {
                    int copy = System.Math.Min(buf.Length, remaining);
                    remaining -= copy;

                    stream.Read(buf, 0, copy);
                    fileInMemory.Write(buf, 0, copy);
                }

                fileInMemory.Seek(0, SeekOrigin.Begin);

                return(new File(fileInMemory, info.Name));
            }
        }
Exemple #3
0
        static public void ConvertFolderToPak(string path, ConvertFeedback feedback)
        {
            if (feedback != null)
            {
                feedback(0);
            }

            IPackage package = new PackageFolder("test", path, "");

            string outputPath = path + ".pak";

            System.IO.FileStream stream = new System.IO.FileStream(outputPath, System.IO.FileMode.Create);
            stream.SetLength(0);
            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

            // calculate positions
            int position             = 0;
            List <PakFileInfo> infos = new List <PakFileInfo>();

            foreach (string fileName in package.Files)
            {
                // exclude pdb files and files starting with .
                if (fileName.StartsWith(".") || fileName.EndsWith(".pdb", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                File        file = package.GetFile(fileName, FileOpenMode.Read);
                PakFileInfo info = new PakFileInfo();
                info.Name     = fileName;
                info.Position = position;
                info.Length   = (int)file.Stream.Length;

                position += info.Length;
                file.Close();

                infos.Add(info);
            }

            PakHeader header = new PakHeader();

            header.FileCount = infos.Count;
            header.Version   = 00010; //0.00.10
            header.Type      = PackageType.Main;

            // write header
            writer.Write(header.FileCount);
            writer.Write(header.Version);
            writer.Write((int)header.Type);

            foreach (PakFileInfo info in infos)
            {
                writer.Write(info.Position);
                writer.Write(info.Length);
                writer.Write(info.Name);
            }

            int written = 0;

            // write files
            foreach (PakFileInfo info in infos)
            {
                File file = package.GetFile(info.Name, FileOpenMode.Read);

                int remaining = (int)file.Stream.Length;

                byte[] buf = new byte[1024 * 1024 * 4];
                while (remaining > 0)
                {
                    int copy = System.Math.Min(buf.Length, remaining);
                    file.Stream.Read(buf, 0, copy);
                    stream.Write(buf, 0, copy);
                    remaining -= copy;
                }

                written += (int)file.Stream.Length;
                file.Close();
                if (feedback != null)
                {
                    feedback(written / (float)position);
                }
            }

            // finish
            writer.Flush();
            writer.Close();
            stream.Close();
        }