Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using IsoFilePatcher.Iso9660;
  7. namespace IsoFilePatcher
  8. {
  9. internal static class Program
  10. {
  11. /// <summary>
  12. /// 应用程序的主入口点。
  13. /// </summary>
  14. [STAThread]
  15. private static void Main(string[] args)
  16. {
  17. try
  18. {
  19. MainInternal(args);
  20. }
  21. catch (Exception ex)
  22. {
  23. Console.WriteLine();
  24. Console.WriteLine("----ERROR BEGIN----");
  25. Console.WriteLine(ex);
  26. Console.WriteLine("-----ERROR END-----");
  27. }
  28. Console.WriteLine();
  29. Console.Write("Press ENTER to exit...");
  30. Console.ReadLine();
  31. }
  32. private static void MainInternal(string[] args)
  33. {
  34. if (args.Length != 3)
  35. {
  36. Console.WriteLine($"Useage: {Path.GetFileName(Application.ExecutablePath)} <IsoFile> <Path> <DataToReplaceFrom>");
  37. Console.WriteLine($"Example: {Path.GetFileName(Application.ExecutablePath)} C:\\PS3.ISO PS3_GAME\\USRDIR\\EBOOT.BIN C:\\RESIGNED_EBOOT.BIN");
  38. return;
  39. }
  40. var isoFile = args[0];
  41. var path = args[1];
  42. var dataToReplaceFrom = args[2];
  43. Console.WriteLine($"Opening {isoFile} ...");
  44. using (var isoStream = new FileStream(isoFile, FileMode.Open, FileAccess.ReadWrite))
  45. {
  46. Console.WriteLine("Skipping system area...");
  47. isoStream.Position = 16 * Constants.SectorSize;
  48. Console.WriteLine("Parsing VolumeDescriptor...");
  49. var descriptors = new List<VolumeDescriptor>();
  50. do
  51. {
  52. var item = VolumeDescriptor.Parse(isoStream);
  53. if (item.IsVolumeDescriptorSetTerminator) break;
  54. descriptors.Add(item);
  55. } while (true);
  56. Console.WriteLine("Parsing path entries...");
  57. var root = (descriptors.FirstOrDefault(p => p.Type == 2) ?? descriptors[0]).RootPathEntry;
  58. root.ScanChilds(isoStream);
  59. Console.WriteLine("Locating file...");
  60. var pathEntries = path.Split(Path.DirectorySeparatorChar);
  61. var currentPath = root;
  62. foreach (var item in pathEntries)
  63. {
  64. var target = currentPath.ChildPathEntries.FirstOrDefault(p => 0 == string.Compare(p.Name, item, StringComparison.OrdinalIgnoreCase));
  65. if (target == null)
  66. throw new ArgumentException("Path not found");
  67. currentPath = target;
  68. }
  69. if (currentPath.IsDirectory)
  70. throw new ArgumentException("Path is a directory");
  71. Console.WriteLine("Feasibility checking...");
  72. var dataFile = new FileInfo(dataToReplaceFrom);
  73. if (false == dataFile.Exists)
  74. throw new FileNotFoundException("File not found", dataToReplaceFrom);
  75. if (dataFile.Length > currentPath.DataLength)
  76. throw new ArgumentException("File large than orignal, can not continue");
  77. Console.WriteLine("Operation in progress...");
  78. isoStream.Position = currentPath.ExtentLba * currentPath.VolumeDescriptor.LogicalBlockSize;
  79. using (var stream = dataFile.OpenRead())
  80. {
  81. stream.CopyTo(isoStream);
  82. }
  83. Console.WriteLine("Flushing file...");
  84. isoStream.Flush();
  85. Console.WriteLine("Closing file...");
  86. isoStream.Close();
  87. isoStream.Dispose();
  88. Console.WriteLine("Operation completed successfully.");
  89. }
  90. }
  91. }
  92. }