Skip to content

Instantly share code, notes, and snippets.

@Sharparam
Last active February 23, 2019 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sharparam/04aa34e8e550bd3bca295cf63493bfb4 to your computer and use it in GitHub Desktop.
Save Sharparam/04aa34e8e550bd3bca295cf63493bfb4 to your computer and use it in GitHub Desktop.
Apex Steam Launcher

Apex Steam Launcher

I made this to launch Apex and get the Steam overlay, while not having to open/close Origin manually for each game launch.

Basically, this will:

  1. Kill Origin if it's running
  2. Start Apex Legends via an origin:// launch URL
  3. Wait for Apex to exit
  4. Use Windows Task Scheduler to run itself with the "relaunch" arg in 10 seconds
  5. Close itself and Origin (this triggers Steam to clear the in-game status)
  6. In 10 seconds, the program will run again with the "relaunch" argument which simply launches Origin again
  7. Delete the created task scheduler task to clean up

In my initial tests, I didn't close origin with the application, and that caused Steam to never clear the in-game status. For some reason this does not happen when launching Apex from Steam directly with an origin:// URL, but using that method you have to exit Origin manually before launching Apex, or Steam won't detect you as in-game and won't give you an overlay.

Dependencies

TaskScheduler by dahall.

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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment