using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using UnrealBuildTool; // Example Command Lines // HTML5LaunchHelper -m -h -b /Applications/Safari.app localhost:8000 // HTML5LaunchHelper -m -h -b /Applications/Firefox.app http://localhost:8000 -silent -profile /Users/james.moran/webporfiles/nightly/UE4HTML5 --jsconsole // HTML5LaunchHelper -m -h -b /Applications/Google\ Chrome.app http://localhost:8000 --user-data-dir="/Users/james.moran/webporfiles/chrome/UE4HTML5" --enable-logging --no-first-run namespace HTML5LaunchHelper { class Program { static private bool bMultiprocessBrowser = false; static private bool bStartPythonServer = false; static private int PythonServerPort = 8000; static private string PythonPath = "python"; static private string BrowserPath = null; static private string ServerDirectory = null; static private string Url = null; static private string AdditionalArgs = ""; static private List ProcessesToKill = new List(); static private List ProcessesToWatch = new List(); static Process SpawnBrowserProcess(string bpath, string args) { var Result = new Process(); if (BuildHostPlatform.Current.Platform == UnrealTargetPlatform.Mac) { Result.StartInfo.FileName = "/usr/bin/open"; Result.StartInfo.UseShellExecute = false; Result.StartInfo.RedirectStandardOutput = true; Result.StartInfo.RedirectStandardInput = true; Result.StartInfo.Arguments = String.Format("-nW \"{0}\" --args {1}", bpath, args); Result.EnableRaisingEvents = true; } else { Result.StartInfo.FileName = bpath; Result.StartInfo.UseShellExecute = false; Result.StartInfo.RedirectStandardOutput = true; Result.StartInfo.RedirectStandardInput = true; Result.StartInfo.Arguments = args; Result.EnableRaisingEvents = true; } System.Console.WriteLine("Spawning Browser Process {0} with args {1}\n", bpath, args); return Result; } static int Main(string[] args) { if (!ParseArgs(args)) { // TODO: print error & options. System.Console.Write("Invalid args\n"); return -1; } if (bStartPythonServer) { BeginPythonServer(); // Give the server time to start. 1 sec should be enough System.Threading.Thread.Sleep(1000); } var bIsSafari = BrowserPath.Contains("Safari"); // Browsers can be multiprocess programs (Chrome, basically) // which we need to catch spawning of other child processes. The trick is // they aren't really child-processes. There appears to be no real binding between the two // so we kinda fudge it a bit here var PrevProcesses = Process.GetProcesses(); var FirstProcess = SpawnBrowserProcess(BrowserPath, !bIsSafari ? Url + " " + AdditionalArgs : ""); ProcessesToWatch.Add(FirstProcess); FirstProcess.Start(); var ProcName = FirstProcess.ProcessName; if (bIsSafari) { // Give Safari time to open... System.Threading.Thread.Sleep(2000); var Proc = new Process(); Proc.StartInfo.FileName = "/usr/bin/osascript"; Proc.StartInfo.UseShellExecute = false; Proc.StartInfo.RedirectStandardOutput = true; Proc.StartInfo.RedirectStandardInput = true; Proc.StartInfo.Arguments = String.Format("-e 'tell application \"Safari\" to open location \"{1}\"'", FirstProcess.Id, Url); Proc.EnableRaisingEvents = true; Proc.Start(); Proc.WaitForExit(); } // We should now have a list of processes to watch to exit. // Loop over the calling WaitForExit() until the list is empty. while (ProcessesToWatch.Count() > 0) { for (var i=0; i