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.

105 lines
3.5KB

  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Windows.Forms;
  7. namespace Launcher
  8. {
  9. internal static class Program
  10. {
  11. private static readonly string[] TABOO_NAMES = {
  12. //"Start",
  13. //"Update",
  14. //"Awake",
  15. //"OnDestroy"
  16. };
  17. private static readonly string[] ENTRY_TYPES = { "Display" };
  18. /// <summary>
  19. /// The main entry point for the application.
  20. /// </summary>
  21. [STAThread]
  22. private static void Main()
  23. {
  24. try
  25. {
  26. var execPath = Application.ExecutablePath;
  27. var fileName = Path.GetFileNameWithoutExtension(execPath);
  28. if (fileName.IndexOf("VR") == -1 && fileName.IndexOf("_") == -1)
  29. {
  30. Fail("File not named correctly!");
  31. }
  32. bool vrMode = fileName.IndexOf("VR") > 0;
  33. string baseName = execPath.Substring(0, vrMode
  34. ? execPath.LastIndexOf("VR")
  35. : execPath.LastIndexOf("_"));
  36. string executable = baseName + ".exe";
  37. var file = new FileInfo(executable);
  38. if (file.Exists)
  39. {
  40. var args = Environment.GetCommandLineArgs().ToList();
  41. if (vrMode)
  42. args.Add("--vr");
  43. EnsureIPA(executable);
  44. StartGame(executable, args.ToArray());
  45. }
  46. else
  47. {
  48. MessageBox.Show("Could not find: " + file.FullName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  49. }
  50. }
  51. catch (Exception globalException)
  52. {
  53. MessageBox.Show(globalException.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  54. }
  55. }
  56. private static void EnsureIPA(string executable)
  57. {
  58. var processStart = new ProcessStartInfo("IPA.exe", EncodeParameterArgument(executable) + " --nowait");
  59. processStart.UseShellExecute = false;
  60. processStart.CreateNoWindow = true;
  61. processStart.RedirectStandardError = true;
  62. var process = Process.Start(processStart);
  63. process.WaitForExit();
  64. if (process.ExitCode != 0)
  65. {
  66. Fail(process.StandardError.ReadToEnd());
  67. }
  68. }
  69. private static void StartGame(string executable, string[] args)
  70. {
  71. var arguments = string.Join(" ", args.ToArray());
  72. Process.Start(executable, arguments);
  73. }
  74. private static void Fail(string reason)
  75. {
  76. throw new Exception(reason);
  77. }
  78. /// <summary>
  79. /// Encodes an argument for passing into a program
  80. /// </summary>
  81. /// <param name="original">The value that should be received by the program</param>
  82. /// <returns>The value which needs to be passed to the program for the original value
  83. /// to come through</returns>
  84. private static string EncodeParameterArgument(string original)
  85. {
  86. if (string.IsNullOrEmpty(original))
  87. return original;
  88. string value = Regex.Replace(original, @"(\\*)" + "\"", @"$1\$0");
  89. value = Regex.Replace(value, @"^(.*\s.*?)(\\*)$", "\"$1$2$2\"");
  90. return value;
  91. }
  92. }
  93. }