using System; using System.Diagnostics; namespace Ubisoft { public class HtOSUtility { public static void ExecuteCommand(string command, bool asAdmin = false) { #if UNITY_EDITOR_WIN Cmd_ExecuteCommand(command, asAdmin); #else Bash_ExecuteCommand(command); #endif } public static string ExecuteGitCommand(string gitCommand) { // Set up our processInfo to run the git command and log to output and errorOutput. ProcessStartInfo processInfo = new ProcessStartInfo("git", @gitCommand) { CreateNoWindow = true, // We want no visible pop-ups UseShellExecute = false, // Allows us to redirect input, output and error streams RedirectStandardOutput = true, // Allows us to read the output stream RedirectStandardError = true // Allows us to read the error stream }; // Set up the Process Process process = new Process { StartInfo = processInfo }; try { _ = process.Start(); // Try to start it, catching any exceptions if it fails } catch (Exception e) { // For now just assume its failed cause it can't find git. Debug.LogError("Git is not set-up correctly, required to be on PATH, and to be a git project."); throw e; } // Strings that will catch the output from our process. // Read the results back from the process so we can get the output and check for errors string output = process.StandardOutput.ReadToEnd(); string errorOutput = process.StandardError.ReadToEnd(); process.WaitForExit(); // Make sure we wait till the process has fully finished. process.Close(); // Close the process ensuring it frees it resources. // Check for failure due to no git setup in the project itself or other fatal errors from git. if (output.Contains("fatal") || output == "no-git" /*|| output == ""*/) { throw new Exception("Command: git " + @gitCommand + " Failed\n" + output + errorOutput); } // Log any errors. if (errorOutput != "") { Debug.LogError("Git Error: " + errorOutput); } return output; // Return the output from git. } #if UNITY_EDITOR_WIN private static void Cmd_ExecuteCommand(string command, bool asAdmin) { var startInfo = new ProcessStartInfo { FileName = "CMD.exe", Arguments = "/C " + command, UseShellExecute = asAdmin, RedirectStandardError = !asAdmin, CreateNoWindow = true, }; if (asAdmin) { startInfo.Verb = "runas"; // Runs process in admin mode. See https://stackoverflow.com/questions/2532769/how-to-start-a-process-as-administrator-mode-in-c-sharp } var proc = new Process() { StartInfo = startInfo }; using (proc) { _ = proc.Start(); proc.WaitForExit(); if (!asAdmin && !proc.StandardError.EndOfStream) { Debug.EditorLogError(proc.StandardError.ReadToEnd()); } } } #else private static void Bash_ExecuteCommand(string command) { command = command.Replace("\"", "\"\""); var proc = new Process() { StartInfo = new ProcessStartInfo { FileName = "/bin/bash", Arguments = "-c \"" + command + "\"", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; using (proc) { bool result = proc.Start(); Debug.EditorLog($"Starting command {command} with result: {result}"); proc.WaitForExit(); if (!proc.StandardError.EndOfStream) { Debug.EditorLogError(proc.StandardError.ReadToEnd()); } } } #endif } }