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.

104 lines
3.7KB

  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(Environment.CurrentDirectory, 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. CopyAll(new DirectoryInfo(managedFolder), new DirectoryInfo(dataPath));
  34. Console.WriteLine("Successfully copied files!");
  35. if(!Directory.Exists(pluginsFolder))
  36. {
  37. Directory.CreateDirectory(pluginsFolder);
  38. }
  39. // Patching
  40. var patchedModule = PatchedModule.Load(engineFile);
  41. if(!patchedModule.IsPatched)
  42. {
  43. BackupManager.MakeBackup(engineFile);
  44. patchedModule.Patch();
  45. }
  46. // Virtualizing
  47. var virtualizedModule = VirtualizedModule.Load(assemblyFile);
  48. if(!virtualizedModule.IsVirtualized)
  49. {
  50. BackupManager.MakeBackup(assemblyFile);
  51. virtualizedModule.Virtualize();
  52. }
  53. } catch(Exception e)
  54. {
  55. Fail("Oops! This should not have happened.\n\n" + e);
  56. }
  57. }
  58. public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
  59. {
  60. Directory.CreateDirectory(target.FullName);
  61. // Copy each file into the new directory.
  62. foreach (FileInfo fi in source.GetFiles())
  63. {
  64. string targetFile = Path.Combine(target.FullName, fi.Name);
  65. if (!File.Exists(targetFile) || File.GetLastWriteTimeUtc(targetFile) < fi.LastWriteTimeUtc)
  66. {
  67. Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
  68. fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
  69. }
  70. }
  71. // Copy each subdirectory using recursion.
  72. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
  73. {
  74. DirectoryInfo nextTargetSubDir =
  75. target.CreateSubdirectory(diSourceSubDir.Name);
  76. CopyAll(diSourceSubDir, nextTargetSubDir);
  77. }
  78. }
  79. static void Fail(string message)
  80. {
  81. Console.Error.Write("ERROR: " + message);
  82. if (!Environment.CommandLine.Contains("--nowait"))
  83. {
  84. Console.WriteLine("\n\n[Press any key to quit]");
  85. Console.ReadKey();
  86. }
  87. Environment.Exit(1);
  88. }
  89. }
  90. }