Skip to content

Instantly share code, notes, and snippets.

@SoylentGraham
Created May 31, 2018 17:18
Show Gist options
  • Save SoylentGraham/6eed6d19d4eb92410701e5f541be12c6 to your computer and use it in GitHub Desktop.
Save SoylentGraham/6eed6d19d4eb92410701e5f541be12c6 to your computer and use it in GitHub Desktop.
Oculus auto VRC tester
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using System.Diagnostics;
using System.IO;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
public static class CliRunner
{
// returns the process exit code
public static int Run(string ExeFilename,string Arguments,System.Action<byte[]> OnStdOut,System.Action<byte[]> OnStdErr=null,System.Func<bool> Continue=null)
{
// fill in the auto-continue func if none provided
if ( Continue == null )
Continue = ()=>{return true; };
var info = new System.Diagnostics.ProcessStartInfo(ExeFilename, Arguments);
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.RedirectStandardInput = false;
info.RedirectStandardOutput = OnStdOut!=null;
info.RedirectStandardError = OnStdErr!=null;
var SubProcess = System.Diagnostics.Process.Start(info);
System.Action<BinaryReader,System.Action<byte[]>> ReadAllOfStream = (BinaryStream,OnRead) =>
{
if ( BinaryStream == null )
return;
if ( OnRead == null )
return;
var Buffer = new byte[1024*10];
// allow external abort
while ( Continue() )
{
var BytesRead = BinaryStream.Read(Buffer, 0, Buffer.Length);
if (BytesRead == 0)
break;
if ( BytesRead < Buffer.Length )
{
var SmallBuffer = new byte[BytesRead];
Array.Copy( Buffer, SmallBuffer, SmallBuffer.Length );
}
else
{
OnRead( Buffer );
}
}
};
var StdoutReader = OnStdOut!=null ? new BinaryReader(SubProcess.StandardOutput.BaseStream) : null;
var StderrReader = OnStdErr!=null ? new BinaryReader(SubProcess.StandardError.BaseStream) : null;
// run until done
try
{
while (!SubProcess.HasExited)
{
if ( !Continue() )
throw new System.Exception("Aborted");
ReadAllOfStream(StdoutReader, OnStdOut);
ReadAllOfStream(StderrReader, OnStdErr);
System.Threading.Thread.Sleep(1);
}
}
catch(System.Exception e)
{
UnityEngine.Debug.LogException(e);
SubProcess.Kill();
throw;
}
var ExitCode = SubProcess.ExitCode;
SubProcess.Close();
SubProcess.Dispose();
return ExitCode;
}
}
// has to be a monobehaviour for the menu items
public class OculusVRCTester
{
#if UNITY_EDITOR
[MenuItem("New Chromantics/Oculus/Verify Build with VRC")]
public static void Verify_MenuItem()
{
var ExePath = EditorUtility.OpenFilePanel("Oculus Exe build", Application.dataPath, "exe");
if ( string.IsNullOrEmpty(ExePath) )
throw new System.Exception("User cancelled");
Verify( ExePath );
}
#endif
const string VrcFilename = @"C:\Program Files\Oculus\Support\oculus-diagnostics\OculusVRCValidator.exe";
public static void Verify(string AppExeFilename)
{
var StdOut = new List<byte>();
System.Action<byte[]> EnumStdOut = (NewBytes) =>
{
StdOut.AddRange(NewBytes);
};
// https://developer.oculus.com/documentation/pcsdk/latest/concepts/dg-vrcvalidator/
// gr: oculus guide says "cd into directory, then run", but it's not explicit. lets see if we can get away with not
var Arguments = "--path ";
// gr: need to escape this?
Arguments += AppExeFilename;
var ExitCode = CliRunner.Run( VrcFilename, Arguments, EnumStdOut );
Debug.Log("Exit code: " + ExitCode);
var StdOutStr = System.Text.ASCIIEncoding.ASCII.GetString( StdOut.ToArray() );
Debug.Log("stdout: " + StdOutStr );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment