Exemplo n.º 1
0
        public IPackage OpenPackage(string path, string subPath)
        {
            IPackage p = null;

            if (!subPath.EndsWith("/") && subPath != "")
            {
                subPath += "/";
            }

            // first try as folder packages
            if (System.IO.Directory.Exists(path))
            {
                p = new PackageFolder("", path, subPath);
            }

            // then try as pak packages
            else if (System.IO.File.Exists(path + ".pak"))
            {
                p = new PackagePak("", path, subPath);
            }


            return(p);
        }
Exemplo n.º 2
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();
        }