|
namespace ApexSteamLauncher |
|
{ |
|
using System; |
|
using System.Diagnostics; |
|
using System.IO; |
|
using System.Threading; |
|
|
|
using Microsoft.Win32.TaskScheduler; |
|
|
|
public static class Program |
|
{ |
|
private const string OriginPath = @"E:\Program Files (x86)\Origin\Origin.exe"; |
|
|
|
private const string OriginDirectory = @"E:\Program Files (x86)\Origin"; |
|
|
|
private const string ApexPath = @"origin://LaunchGame/Origin.OFR.50.0002694"; |
|
|
|
private const string ApexDirectory = @"G:\Origin\games\Apex"; |
|
|
|
private const string OriginRelauncherTaskName = "sharparam_apexsteamlauncher_relaunchorigin"; |
|
|
|
public static void Main(string[] args) |
|
{ |
|
if (args.Length > 0 && args[0] == "relaunch") |
|
{ |
|
LaunchOrigin(); |
|
RemoveRelauncherTask(); |
|
return; |
|
} |
|
|
|
var originRunning = KillOrigin(); |
|
|
|
var launchSuccess = LaunchApexLegends(); |
|
|
|
if (launchSuccess) |
|
{ |
|
Console.WriteLine("Launch successful, delaying keep-alive check by 30 seconds"); |
|
Thread.Sleep(30_000); |
|
WaitForApexExit(); |
|
} |
|
|
|
KillOrigin(); |
|
|
|
if (originRunning) |
|
{ |
|
Console.WriteLine("Origin was running at program start, scheduling its re-launch"); |
|
ScheduleOriginLaunch(); |
|
} |
|
|
|
Console.WriteLine("Launcher done, exiting"); |
|
} |
|
|
|
private static bool KillOrigin() |
|
{ |
|
Console.WriteLine("Closing Origin"); |
|
|
|
var origins = Process.GetProcessesByName("Origin"); |
|
|
|
if (origins.Length == 0) |
|
{ |
|
Console.WriteLine("There was no Origin processes to close"); |
|
return false; |
|
} |
|
|
|
foreach (var process in origins) |
|
{ |
|
process.Kill(); |
|
process.WaitForExit(1000); |
|
} |
|
|
|
Console.WriteLine("Origin closed"); |
|
return true; |
|
} |
|
|
|
private static bool LaunchApexLegends() |
|
{ |
|
Console.WriteLine("Launching Apex Legends"); |
|
|
|
var startInfo = new ProcessStartInfo(ApexPath) |
|
{ |
|
UseShellExecute = true |
|
}; |
|
|
|
var process = Process.Start(startInfo); |
|
|
|
if (process != null) |
|
return true; |
|
|
|
Console.WriteLine("Failed to launch Apex, process is NULL"); |
|
return false; |
|
} |
|
|
|
private static bool LaunchOrigin() |
|
{ |
|
Console.WriteLine("Launching Origin"); |
|
|
|
var startInfo = new ProcessStartInfo(OriginPath) |
|
{ |
|
WorkingDirectory = OriginDirectory |
|
}; |
|
|
|
var process = Process.Start(startInfo); |
|
|
|
if (process != null) |
|
return true; |
|
|
|
Console.WriteLine("Failed to launch Origin, process is NULL"); |
|
return false; |
|
} |
|
|
|
private static void WaitForApexExit() |
|
{ |
|
Console.WriteLine("Waiting for Apex process to exit"); |
|
|
|
do |
|
{ |
|
Thread.Sleep(5000); |
|
} while (IsApexRunning()); |
|
|
|
Console.WriteLine("Apex process has exited"); |
|
} |
|
|
|
private static bool IsApexRunning() |
|
{ |
|
var apexProcesses = Process.GetProcessesByName("r5apex"); |
|
var eacLauncherProcesses = Process.GetProcessesByName("EasyAntiCheat_launcher"); |
|
return apexProcesses.Length > 0 || eacLauncherProcesses.Length > 0; |
|
} |
|
|
|
private static void ScheduleOriginLaunch() |
|
{ |
|
var currentProcess = Process.GetCurrentProcess(); |
|
|
|
var path = currentProcess.MainModule.FileName; |
|
var directory = Path.GetDirectoryName(path); |
|
|
|
using (var taskService = new TaskService()) |
|
{ |
|
var definition = taskService.NewTask(); |
|
|
|
definition.RegistrationInfo.Author = "Sharparam"; |
|
definition.RegistrationInfo.Description = "Re-launches Origin"; |
|
|
|
definition.Triggers.Add(new TimeTrigger(DateTime.UtcNow.AddSeconds(10))); |
|
|
|
definition.Actions.Add(new ExecAction(path, "relaunch", directory)); |
|
|
|
taskService.RootFolder.RegisterTaskDefinition(OriginRelauncherTaskName, definition); |
|
} |
|
} |
|
|
|
private static void RemoveRelauncherTask() |
|
{ |
|
Console.WriteLine("Removing Origin relaunch task"); |
|
using (var taskService = new TaskService()) |
|
{ |
|
taskService.RootFolder.DeleteTask(OriginRelauncherTaskName); |
|
} |
|
} |
|
} |
|
} |