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.

PatchContext.cs 3.2KB

6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace IPA
  8. {
  9. public class PatchContext
  10. {
  11. /// <summary>
  12. /// Gets the filename of the executable.
  13. /// </summary>
  14. public string Executable { get; private set; }
  15. /// <summary>
  16. /// Gets the path to the launcher executable (in the IPA folder)
  17. /// </summary>
  18. public string LauncherPathSrc { get; private set; }
  19. public string DataPathSrc { get; private set; }
  20. public string PluginsFolder { get; private set; }
  21. public string ProjectName { get; private set; }
  22. public string DataPathDst { get; private set; }
  23. public string ManagedPath { get; private set; }
  24. public string EngineFile { get; private set; }
  25. public string AssemblyFile { get; private set; }
  26. public string[] Args { get; private set; }
  27. public string ProjectRoot { get; private set; }
  28. public string IPARoot { get; private set; }
  29. public string ShortcutPath { get; private set; }
  30. public string IPA { get; private set; }
  31. public string BackupPath { get; private set; }
  32. private PatchContext() { }
  33. public static PatchContext Create(String[] args)
  34. {
  35. var context = new PatchContext();
  36. context.Args = args;
  37. context.Executable = args[0];
  38. context.ProjectRoot = new FileInfo(context.Executable).Directory.FullName;
  39. context.IPARoot = Path.Combine(context.ProjectRoot, "IPA");
  40. context.IPA = Assembly.GetExecutingAssembly().Location ?? Path.Combine(context.ProjectRoot, "IPA.exe");
  41. context.LauncherPathSrc = Path.Combine(context.IPARoot, "Launcher.exe");
  42. context.DataPathSrc = Path.Combine(context.IPARoot, "Data");
  43. context.PluginsFolder = Path.Combine(context.ProjectRoot, "Plugins");
  44. context.ProjectName = Path.GetFileNameWithoutExtension(context.Executable);
  45. context.DataPathDst = Path.Combine(context.ProjectRoot, context.ProjectName + "_Data");
  46. context.ManagedPath = Path.Combine(context.DataPathDst, "Managed");
  47. context.EngineFile = DetermineEngineFile(context.ManagedPath, "UnityEngine.dll", "UnityEngine.CoreModule.dll");
  48. context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-Csharp.dll");
  49. context.BackupPath = Path.Combine(Path.Combine(context.IPARoot, "Backups"), context.ProjectName);
  50. context.ShortcutPath = Path.Combine(context.ProjectRoot, $"{context.ProjectName} (Patch & Launch)") + ".lnk";
  51. Directory.CreateDirectory(context.BackupPath);
  52. return context;
  53. }
  54. private static string DetermineEngineFile(string basePath, params string[] candidates)
  55. {
  56. foreach(var candidate in candidates)
  57. {
  58. var fullPath = Path.Combine(basePath, candidate);
  59. if(File.Exists(fullPath))
  60. {
  61. return fullPath;
  62. }
  63. }
  64. throw new FileNotFoundException("Could not find engine DLL!");
  65. }
  66. }
  67. }