A fork of Eusth's IPA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.1KB

  1. using IPA.Patcher;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace IPA
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. if(args.Length < 1 || !args[0].EndsWith(".exe"))
  14. {
  15. Fail("Drag an (executable) file on the exe!");
  16. }
  17. string launcherSrc = Path.Combine("IPA", "Launcher.exe");
  18. string managedFolder = Path.Combine("IPA", "Managed");
  19. string pluginsFolder = "Plugins";
  20. string projectName = Path.GetFileNameWithoutExtension(args[0]);
  21. string dataPath = Path.Combine(Path.Combine(Path.GetDirectoryName(args[0]), projectName + "_Data"), "Managed");
  22. string engineFile = Path.Combine(dataPath, "UnityEngine.dll");
  23. string assemblyFile = Path.Combine(dataPath, "Assembly-Csharp.dll");
  24. // Sanitizing
  25. if (!File.Exists(launcherSrc)) Fail("Couldn't find DLLs! Make sure you extracted all contents of the release archive.");
  26. if(!Directory.Exists(dataPath) || !File.Exists(engineFile) || !File.Exists(assemblyFile))
  27. {
  28. Fail("Game does not seem to be a Unity project. Could not find the libraries to patch.");
  29. }
  30. try
  31. {
  32. // Copying
  33. Console.Write("Updating files... ");
  34. CopyAll(new DirectoryInfo(managedFolder), new DirectoryInfo(dataPath));
  35. Console.WriteLine("Successfully updated files!");
  36. if (!Directory.Exists(pluginsFolder))
  37. {
  38. Console.WriteLine("Creating plugins folder... ");
  39. Directory.CreateDirectory(pluginsFolder);
  40. }
  41. // Patching
  42. var patchedModule = PatchedModule.Load(engineFile);
  43. if(!patchedModule.IsPatched)
  44. {
  45. Console.Write("Patching UnityEngine.dll... ");
  46. BackupManager.MakeBackup(engineFile);
  47. patchedModule.Patch();
  48. Console.WriteLine("Done!");
  49. }
  50. // Virtualizing
  51. var virtualizedModule = VirtualizedModule.Load(assemblyFile);
  52. if(!virtualizedModule.IsVirtualized)
  53. {
  54. Console.Write("Virtualizing Assembly-Csharp.dll... ");
  55. BackupManager.MakeBackup(assemblyFile);
  56. virtualizedModule.Virtualize();
  57. Console.WriteLine("Done!");
  58. }
  59. } catch(Exception e)
  60. {
  61. Fail("Oops! This should not have happened.\n\n" + e);
  62. }
  63. Console.WriteLine("Finished!");
  64. }
  65. public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
  66. {
  67. Directory.CreateDirectory(target.FullName);
  68. // Copy each file into the new directory.
  69. foreach (FileInfo fi in source.GetFiles())
  70. {
  71. string targetFile = Path.Combine(target.FullName, fi.Name);
  72. if (!File.Exists(targetFile) || File.GetLastWriteTimeUtc(targetFile) < fi.LastWriteTimeUtc)
  73. {
  74. Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
  75. fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
  76. }
  77. }
  78. // Copy each subdirectory using recursion.
  79. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
  80. {
  81. DirectoryInfo nextTargetSubDir =
  82. target.CreateSubdirectory(diSourceSubDir.Name);
  83. CopyAll(diSourceSubDir, nextTargetSubDir);
  84. }
  85. }
  86. static void Fail(string message)
  87. {
  88. Console.Error.Write("ERROR: " + message);
  89. if (!Environment.CommandLine.Contains("--nowait"))
  90. {
  91. Console.WriteLine("\n\n[Press any key to quit]");
  92. Console.ReadKey();
  93. }
  94. Environment.Exit(1);
  95. }
  96. }
  97. }