Skip to content

Instantly share code, notes, and snippets.

@mrexodia
Created October 22, 2017 23:13
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 mrexodia/a74bbece4ecc76fedfc5d2ec00cb2e01 to your computer and use it in GitHub Desktop.
Save mrexodia/a74bbece4ecc76fedfc5d2ec00cb2e01 to your computer and use it in GitHub Desktop.
UnityCli
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace UnityCli
{
public static class Program
{
public static int Main(string[] args)
{
var unity = Environment.GetEnvironmentVariable("UNITY");
if (unity == null || !File.Exists(unity))
{
Console.Error.WriteLine("Incorrect UNITY environment variable...");
return 1;
}
args = args.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
var logFile = "";
for (var i = 0; i < args.Length; i++)
{
if (string.Compare(args[i], "-logFile", true) == 0 && i + 1 < args.Length)
{
logFile = args[i + 1];
break;
}
}
if (string.IsNullOrEmpty(logFile))
{
Console.Error.WriteLine("No/invalid '-logFile' parameter found...");
return 1;
}
Console.WriteLine("Log: " + logFile);
File.Delete(logFile);
var ar = string.Join(" ", args.Select(s => "\"" + Regex.Replace(s, @"(\\+)$", @"$1$1") + "\""));
Console.WriteLine("Command line: \"{0}\" " + ar, unity);
var p = Process.Start(unity, ar);
Console.WriteLine("UnityPipe!");
while (!File.Exists(logFile) && !p.HasExited)
Thread.Sleep(10);
if (p.HasExited)
return p.ExitCode;
Console.WriteLine("Log created!");
using (var reader = new StreamReader(File.Open(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
Console.WriteLine("Opened file!");
while (true)
{
var data = reader.ReadToEnd();
if(string.IsNullOrEmpty(data))
{
if (p.HasExited)
break;
Thread.Sleep(10);
continue;
}
Console.Write(data);
}
}
return p.ExitCode;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment