public Patcher(CrafArchive archive) { _archive = archive; _candidateIds = new List <int>(); for (var i = 0; i < _archive.Count(); i++) { var filename = _archive.VfsFilename(i); if (filename.StartsWith("g_buffer") && filename.EndsWith(".ps.sb")) { _candidateIds.Add(i); } } }
static void Extract(string archivePath, string destinationFolder) { CrafArchive archive = null; try { archive = CrafArchive.Open(archivePath); var task = archive.LoadAsync(new Progress <int>()); task.Wait(); archive.CloseReader(); } catch (IOException) { Console.WriteLine("Could not open archive"); Environment.Exit(-1); } var ct = archive.Count(); try { Directory.CreateDirectory(destinationFolder); for (var i = 0; i < ct; i++) { var outputPath = Path.Combine(destinationFolder, archive.DiskPath(i)); Console.WriteLine(string.Format("{0} ({1}/{2})", outputPath, i + 1, ct)); Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); File.WriteAllBytes(outputPath, archive.Get(i)); } } catch (IOException ex) { Console.WriteLine("Error while extracting archive"); Console.WriteLine("Details: " + ex.Message); Environment.Exit(-1); } }
private async void processButton_Click(object sender, EventArgs e) { processButton.Enabled = false; foreach (var btn in radioSet) { btn.Enabled = false; } _archive.Append(dummyFileName, dummyVfsPath, false, dummyFileContent); progressBar.Value = 0; progressBar.Maximum = _patcher.CandidateCount(); statusLabel.Text = "Processing shaders"; if (offRadio.Checked) { await _patcher.PatchAsync(new Progress <int>(UpdateProgressBar), Patcher.PatchMode.DisableDithering, 0.0f); } else if (wideRadio.Checked) { await _patcher.PatchAsync(new Progress <int>(UpdateProgressBar), Patcher.PatchMode.NarrowDithering, 32.0f); } else if (narrowRadio.Checked) { await _patcher.PatchAsync(new Progress <int>(UpdateProgressBar), Patcher.PatchMode.NarrowDithering, 56.0f); } else { await _patcher.PatchAsync(new Progress <int>(UpdateProgressBar), Patcher.PatchMode.NarrowDithering, 40.0f); } progressBar.Value = 0; progressBar.Maximum = _archive.Count(); statusLabel.Text = "Writing archive"; try { await _archive.SaveAsync(tempArchivePath, new Progress <int>(UpdateProgressBar)); // Use case: // - User applies patch, producing patched archive and backup archive // - Game update overwrites patched archive // - Backup archive is now present despite regular archive having no patch marker if (File.Exists(backupArchivePath)) { File.Delete(backupArchivePath); } File.Move(archivePath, backupArchivePath); File.Move(tempArchivePath, archivePath); } catch (IOException ex) { MessageBox.Show("Could not write shader archive. " + "Please ensure nothing is accessing the following paths (close the game if you're running it):\n\n" + archivePath + "\n" + tempArchivePath + "\n" + backupArchivePath + "\n\n" + "If you're still having problems, try running as Administrator and/or disabling your antivirus.\n\n" + "Details:\n" + ex.Message); Application.Exit(); } //await _patcher.DumpDiscardPsAsync(new Progress<int>(UpdateProgressBar)); statusLabel.Text = "Done"; }
static void ReplaceAll(string archivePath, string replacementFolder, string newArchivePath) { CrafArchive archive = null; try { archive = CrafArchive.Open(archivePath); var task = archive.LoadAsync(new Progress <int>()); task.Wait(); archive.CloseReader(); } catch (IOException) { Console.WriteLine("Could not open archive"); Environment.Exit(-1); } var ct = archive.Count(); Console.WriteLine("Loading replacement files..."); for (var i = 0; i < ct; i++) { var fileInArchive = archive.DiskPath(i); var combined = Path.Combine(replacementFolder, fileInArchive); if (File.Exists(combined)) { Console.WriteLine(string.Format("{0} ({1}/{2})", combined, i + 1, ct)); try { archive.Replace(i, File.ReadAllBytes(combined)); } catch (IOException ex) { Console.WriteLine("Could not read replacement file"); Console.WriteLine("Details: " + ex.Message); Environment.Exit(-1); } } } Console.WriteLine(""); Console.WriteLine("Writing back files..."); var fivePercent = ct / 20; if (fivePercent == 0) { fivePercent = 1; } try { var task2 = archive.SaveAsync(newArchivePath, new Progress <int>((i) => { if (i % fivePercent == 0) { Console.Write("."); } })); task2.Wait(); } catch (IOException ex) { Console.WriteLine(""); Console.WriteLine("Could not write new archive"); Console.WriteLine("Details: " + ex.Message); Environment.Exit(-1); } Console.WriteLine(""); }
static void Replace(string archivePath, string fileInArchive, string fileOnDisk, string newArchivePath) { CrafArchive archive = null; try { archive = CrafArchive.Open(archivePath); var task = archive.LoadAsync(new Progress <int>()); task.Wait(); archive.CloseReader(); } catch (IOException) { Console.WriteLine("Could not open archive"); Environment.Exit(-1); } var id = archive.IndexOfDiskPath(fileInArchive); if (id == -1) { Console.WriteLine("File in archive not found"); Environment.Exit(-1); } Console.WriteLine("Loading new file..."); try { archive.Replace(id, File.ReadAllBytes(fileOnDisk)); } catch (IOException ex) { Console.WriteLine("Could not read replacement file"); Console.WriteLine("Details: " + ex.Message); Environment.Exit(-1); } Console.WriteLine("Writing back files..."); var fivePercent = archive.Count() / 20; if (fivePercent == 0) { fivePercent = 1; } try { var task2 = archive.SaveAsync(newArchivePath, new Progress <int>((i) => { if (i % fivePercent == 0) { Console.Write("."); } })); task2.Wait(); } catch (IOException ex) { Console.WriteLine(""); Console.WriteLine("Could not write new archive"); Console.WriteLine("Details: " + ex.Message); Environment.Exit(-1); } Console.WriteLine(""); }