Ejemplo n.º 1
0
        private static CpkFile PackCpkDoAppend(Options options, string cpkBaseName, string cpkDir)
        {
            CpkFile cpk;

            Console.WriteLine($"Appending to: {options.AppendPath}");
            Console.WriteLine($"Loading CPK: {options.AppendPath}");
            cpk = new CpkFile(options.AppendPath);

            // Create new PAC
            var newPacIndex = options.PacIndex.GetValueOrDefault(cpk.Entries.Select(x => x.PacIndex).Max() + 1);
            var pacName     = FormatPacName($"{GetPacBaseNameFromCpkBaseName( cpkDir, cpkBaseName )}", newPacIndex);

            Console.WriteLine("Creating PAC: {pacName}");
            var pacPath = Path.Combine(cpkDir, pacName);

            var pac = DwPackFile.Pack(options.InputPath, newPacIndex, options.Compress, (e =>
            {
                Console.WriteLine($"Adding {e}");
                return(true);
            }));

            using var packFile = File.Create(pacPath);
            pac.Write(packFile, options.Compress, (e => Console.WriteLine($"Writing {e.Path}")));

            // Add entries to CPK
            for (var i = 0; i < pac.Entries.Count; i++)
            {
                var entry = pac.Entries[i];
                cpk.Entries.Add(new CpkFileEntry(entry.Path, (short)i, (short)newPacIndex));
            }

            return(cpk);
        }
Ejemplo n.º 2
0
        public void RepackFile(string inputPath, string outputFile, bool compress)
        {
            var fileName  = Path.GetFileName(outputFile);
            var extension = Path.GetExtension(outputFile);

            if (extension.StartsWith(".cpk"))
            {
                CpkFile.Repack(inputPath, outputFile, compress);
            }
        }
Ejemplo n.º 3
0
        public void ExtractFile(string inputFile, string outputPath)
        {
            var fileName  = Path.GetFileName(inputFile);
            var extension = Path.GetExtension(inputFile);

            if (extension.StartsWith(".cpk"))
            {
                CpkFile.Extract(inputFile, outputPath);
            }
        }
Ejemplo n.º 4
0
        public void LoadFromFile(string filePath, Stream stream)
        {
            FileName = Path.GetFileNameWithoutExtension(filePath);

            // Read compressed buffer
            var buffer     = (void *)Marshal.AllocHGlobal((int)stream.Length);
            var bufferSpan = new Span <byte>(buffer, (int)stream.Length);

            stream.Read(bufferSpan);
            Native = new CompressedCpkPtr(buffer);

            // Parse CPK
            mWrapper = new CpkFile();
            mWrapper.Read(bufferSpan, CompressionState.Compressed);
        }
Ejemplo n.º 5
0
        static int UnpackCpk(Options options)
        {
            var name = Path.GetFileNameWithoutExtension(options.InputPath);
            var dir  = Path.GetDirectoryName(options.InputPath);

            // Try to detect pac base name
            var pacBaseName = GetPacBaseNameFromCpkBaseName(dir, name);
            var cpk         = new CpkFile(options.InputPath);

            // Load needed pacs
            var packs = new List <DwPackFile>();

            foreach (var pacIdx in cpk.Entries.Select(x => x.PacIndex).Distinct().OrderBy(x => x))
            {
                var pacName = $"{pacBaseName}{pacIdx:D5}.pac";
                var pacPath = Path.Combine(dir, pacName);
                if (!File.Exists(pacPath))
                {
                    Console.WriteLine($"Failed to unpack: Missing {pacName}");
                    return(1);
                }

                var pac          = new DwPackFile(pacPath);
                var refFileCount = cpk.Entries.Where(x => x.PacIndex == pacIdx)
                                   .Select(x => x.FileIndex)
                                   .Max() + 1;
                if (refFileCount > pac.Entries.Count)
                {
                    Console.WriteLine($"Failed to unpack: CPK references {refFileCount} in {pacName} but only {pac.Entries.Count} exist.");
                    return(1);
                }

                packs.Add(pac);
            }

            cpk.Unpack(packs, options.OutputPath, (e =>
            {
                if (!ShouldUnpack(e.Path))
                {
                    return(false);
                }
                Console.WriteLine($"Extracting {e.Path} (pac: {e.PacIndex}, file: {e.FileIndex})");
                return(true);
            }));
            return(0);
        }
Ejemplo n.º 6
0
 private static CpkFile PackCpkDoPack(Options options, string cpkName, string cpkBaseName, string cpkDir)
 {
     return(CpkFile.Pack(options.InputPath, options.Compress,
                         (p =>
     {
         Console.WriteLine($"{cpkName}: Adding {p}");
         return true;
     }),
                         (pack =>
     {
         var pacName = FormatPacName(cpkBaseName, pack.Index);
         var pacPath = Path.Combine(cpkDir, pacName);
         using var pacFile = File.Create(pacPath);
         Console.WriteLine($"Creating {pacName}");
         pack.Write(pacFile, options.Compress,
                    (p => Console.WriteLine($"{pacName}: Writing {p.Path}")));
     })));
 }