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.

61 lines
3.1 KiB

  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. namespace IPA
  5. {
  6. public class PatchContext
  7. {
  8. /// <summary>
  9. /// Gets the filename of the executable.
  10. /// </summary>
  11. public string Executable { get; private set; }
  12. public string DataPathSrc { get; private set; }
  13. public string LibsPathSrc { get; private set; }
  14. public string PluginsFolder { get; private set; }
  15. public string ProjectName { get; private set; }
  16. public string DataPathDst { get; private set; }
  17. public string LibsPathDst { get; private set; }
  18. public string ManagedPath { get; private set; }
  19. public string EngineFile { get; private set; }
  20. public string AssemblyFile { get; private set; }
  21. public string ProjectRoot { get; private set; }
  22. public string IPARoot { get; private set; }
  23. public string ShortcutPath { get; private set; }
  24. public string IPA { get; private set; }
  25. public string BackupPath { get; private set; }
  26. // This is only created by us; and all are assigned
  27. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  28. private PatchContext() { }
  29. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  30. public static PatchContext Create(string exe)
  31. {
  32. var context = new PatchContext
  33. {
  34. Executable = exe
  35. };
  36. context.ProjectRoot = new FileInfo(context.Executable).Directory?.FullName ?? throw new Exception();
  37. context.IPARoot = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location) ?? throw new InvalidOperationException(), "IPA");
  38. context.IPA = Assembly.GetExecutingAssembly().Location;
  39. context.DataPathSrc = Path.Combine(context.IPARoot, "Data");
  40. context.LibsPathSrc = Path.Combine(context.IPARoot, "Libs");
  41. context.PluginsFolder = Path.Combine(context.ProjectRoot ?? throw new InvalidOperationException(), "Plugins");
  42. context.ProjectName = Path.GetFileNameWithoutExtension(context.Executable);
  43. context.DataPathDst = Path.Combine(context.ProjectRoot, context.ProjectName + "_Data");
  44. context.LibsPathDst = Path.Combine(context.ProjectRoot, "Libs");
  45. context.ManagedPath = Path.Combine(context.DataPathDst, "Managed");
  46. context.EngineFile = Path.Combine(context.ManagedPath, "UnityEngine.CoreModule.dll");
  47. context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-CSharp.dll");
  48. context.BackupPath = Path.Combine(context.IPARoot, "Backups", context.ProjectName);
  49. string shortcutName = $"{context.ProjectName} (Patch & Launch)";
  50. context.ShortcutPath = Path.Combine(context.ProjectRoot, shortcutName) + ".lnk";
  51. _ = Directory.CreateDirectory(context.BackupPath);
  52. return context;
  53. }
  54. }
  55. }