#if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX
#define UNITY_EDITOR_OSX
using System.Linq ;
using UnityEditor ;
using UnityEditor.Perception.Visualizer ;
using Debug = UnityEngine . Debug ;
namespace UnityEditor.Perception.Visualizer
{
//This files stores entries as ProjectDataPath,PythonPID,Port,VisualizerPID
//It keeps a record of the instances of visualizer opened so that we don't open a new one everytime
private static readonly string _filename_streamlit_instances = "Unity/streamlit_instances.csv" ;
private static string PathToStreamlitInstances
const string k_FilenameStreamlitInstances = "Unity/streamlit_instances.csv" ;
static string PathToStreamlitInstances
return Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , _filename_streamlit_instances ) ;
return Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , k_FilenameStreamlitInstances ) ;
_filename_streamlit_instances ) ;
k_FilenameStreamlitInstances ) ;
private static readonly string nameOfVisualizerProcess
#if UNITY_EDITOR_WIN
const string k_NameOfVisualizerProcess
#if UNITY_EDITOR_OSX
= "bash" ;
#elif UNITY_EDITOR_WIN
#elif UNITY_EDITOR_OSX
= "bash" ;
#else
= "" ;
// ReSharper disable Unity.PerformanceAnalysis
/// <summary>
/// Install visualizer (Assumes python3 and pip3 are already installed)
/// - installs virtualenv if it is not already installed
{
string project = Application . dataPath ;
var project = Application . dataPath ;
( int pythonPID , int port , int visualizerPID ) = ReadEntry ( project ) ;
var ( pythonPid , port , visualizerPid ) = ReadEntry ( project ) ;
if ( pythonPID ! = - 1 & & ProcessAlive ( pythonPID , port , visualizerPID ) )
if ( pythonPid ! = - 1 & & ProcessAlive ( pythonPid , port , visualizerPid ) )
{
if ( EditorUtility . DisplayDialog ( "Kill visualizer?" ,
"The visualizer tool can't be running while you setup, would you like to kill the current instance?" ,
Process . GetProcessById ( pythonPID + 1 ) . Kill ( ) ;
Process . GetProcessById ( pythonPid + 1 ) . Kill ( ) ;
}
else
{
int steps = 3 ;
int E xitCode = 0 ;
const int steps = 3 ;
var e xitCode = 0 ;
string packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
var packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
#elif UNITY_EDITOR_OSX
string packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/bin" ) ) ;
#endif
EditorUtility . DisplayProgressBar ( "Setting up the Visualizer" , "Installing Visualizer (This may take a few minutes - this only happens once)" , 2.5f / steps ) ;
#if UNITY_EDITOR_WIN
ExecuteCMD ( $"\" { packagesPath } \ "\\pip3.bat install --upgrade --no-warn-script-location unity-cv-datasetvisualizer" , ref E xitCode, ref output , waitForExit : - 1 ) ;
ExecuteCmd ( $"\" { packagesPath } \ "\\pip3.bat install --upgrade --no-warn-script-location unity-cv-datasetvisualizer" , ref e xitCode, ref output , waitForExit : - 1 ) ;
ExecuteCMD ( $"cd \'{packagesPath}\'; ./python3.7 -m pip install --upgrade unity-cv-datasetvisualizer" , ref E xitCode, ref output , waitForExit : - 1 ) ;
ExecuteCmd ( $"cd \'{packagesPath}\'; ./python3.7 -m pip install --upgrade unity-cv-datasetvisualizer" , ref e xitCode, ref output , waitForExit : - 1 ) ;
if ( E xitCode ! = 0 ) {
if ( e xitCode ! = 0 ) {
UnityEngine . Debug . Log ( "Successfully installed visualizer" ) ;
Debug . Log ( "Successfully installed visualizer" ) ;
}
/// <summary>
/// <param name="exitCode">int reference to get exit code</param >
/// <param name="output">string reference to save output</param>
/// <returns></returns >
private static int ExecuteCMD ( string command , ref int ExitCode , ref string output , int waitForExit = 0 , bool displayWindow = false , bool getOutput = false )
/// <param name="getOutput">Whether or not to get output</param>
/// <returns>PID of process that started</returns>
static int ExecuteCmd ( string command , ref int exitCode , ref string output , int waitForExit = 0 , bool displayWindow = false , bool getOutput = false )
string shell = "" ;
string argument = "" ;
#if UNITY_EDITOR_WIN
shell = "cmd.exe" ;
argument = $"/c \" { command } \ "" ;
#elif UNITY_EDITOR_OSX
shell = "/bin/bash ";
argument = $"-c \" { command } \ "" ;
#endif
#if UNITY_EDITOR_WIN
const string shell = "cmd.exe" ;
var argument = $"/c \" { command } \ " ";
#elif UNITY_EDITOR_OSX
const string shell = "/bin/bash" ;
var argument = $"-c \" { command } \ "" ;
#endif
ProcessStartInfo info = new ProcessStartInfo ( shell , argument ) ;
var info = new ProcessStartInfo ( shell , argument ) ;
info . CreateNoWindow = ! displayWindow ;
info . UseShellExecute = false ;
Process cmd = Process . Start ( info ) ;
if ( waitForExit = = 0 )
var cmd = Process . Start ( info ) ;
switch ( waitForExit )
return cmd . Id ;
case 0 :
if ( cmd ! = null ) return cmd . Id ;
break ;
case - 1 :
cmd ? . WaitForExit ( ) ;
break ;
default :
{
if ( waitForExit > 0 )
{
cmd ? . WaitForExit ( waitForExit ) ;
}
break ;
}
if ( waitForExit = = - 1 )
{
cmd . WaitForExit ( ) ;
}
else if ( waitForExit > 0 )
{
cmd . WaitForExit ( waitForExit ) ;
}
output = cmd . StandardOutput . ReadToEnd ( ) ;
output = cmd ? . StandardOutput . ReadToEnd ( ) ;
if ( cmd . HasExited ) {
ExitCode = cmd . ExitCode ;
if ( ExitCode ! = 0 )
if ( cmd is { HasExited : true } ) {
exitCode = cmd . ExitCode ;
if ( exitCode ! = 0 )
UnityEngine . Debug . LogError ( $"Error - {ExitCode} - Failed to execute: {command} - {cmd.StandardError.ReadToEnd()}" ) ;
Debug . LogError ( $"Error - {exitCode} - Failed to execute: {command} - {cmd.StandardError.ReadToEnd()}" ) ;
cmd . Close ( ) ;
cmd ? . Close ( ) ;
// ReSharper disable Unity.PerformanceAnalysis
/// <summary>
/// If an instance is already running for this project it opens the browser at the correct port
/// If no instance is found it launches a new process
{
if ( ! checkIfVisualizerInstalled ( ) )
{
if ( ! CheckIfVisualizerInstalled ( ) )
string project = Application . dataPath ;
var project = Application . dataPath ;
( int pythonPID , int port , int visualizerPID ) = ReadEntry ( project ) ;
var ( pythonPid , port , visualizerPid ) = ReadEntry ( project ) ;
if ( pythonPID ! = - 1 & & ProcessAlive ( pythonPID , port , visualizerPID ) )
if ( pythonPid ! = - 1 & & ProcessAlive ( pythonPid , port , visualizerPid ) )
{
LaunchBrowser ( port ) ;
}
DeleteEntry ( project ) ;
Process [ ] before = Process . GetProcesses ( ) ;
var before = Process . GetProcesses ( ) ;
int errorCode = ExecuteVisualizer ( ) ;
var errorCode = ExecuteVisualizer ( ) ;
UnityEngine . Debug . LogError ( "Could not launch visualizer tool" ) ;
Debug . LogError ( "Could not launch visualizer tool" ) ;
Process [ ] after = null ;
Process [ ] after ;
int maxAttempts = 2 0 ;
const int maxAttempts = 5 ;
int newVisualizerPID = - 1 ;
int attempts = 0 ;
while ( newVisualizerPID = = - 1 )
var newVisualizerPid = - 1 ;
var attempts = 0 ;
while ( newVisualizerPid = = - 1 )
Thread . Sleep ( 2 0 0 ) ;
Thread . Sleep ( 1 0 0 0 ) ;
newVisualizerPID = GetNewProcessID ( before , after , n ameOfVisualizerProcess) ;
newVisualizerPid = GetNewProcessID ( before , after , k_N ameOfVisualizerProcess) ;
UnityEngine . Debug . LogError ( "Failed to get visualizer ID" ) ;
Debug . LogError ( "Failed to get visualizer ID" ) ;
return ;
}
attempts + + ;
int newPythonPID = - 1 ;
var newPythonPid = - 1 ;
while ( newPythonPID = = - 1 )
while ( newPythonPid = = - 1 )
Thread . Sleep ( 2 0 0 ) ;
Thread . Sleep ( 1 0 0 0 ) ;
newPythonPID = GetNewProcessID ( before , after , "python" ) ;
newPythonPid = GetNewProcessID ( before , after , "python" ) ;
UnityEngine . Debug . LogError ( "Failed to get python ID" ) ;
Debug . LogError ( "Failed to get python ID" ) ;
return ;
}
attempts + + ;
int newPort = - 1 ;
var newPort = - 1 ;
Thread . Sleep ( 2 0 0 ) ;
newPort = GetPortForPID ( newPythonPID ) ;
Thread . Sleep ( 1 0 0 0 ) ;
newPort = GetPortForPid ( newPythonPid ) ;
UnityEngine . Debug . LogError ( "Failed to get PORT" ) ;
Debug . LogError ( "Failed to get PORT" ) ;
return ;
}
attempts + + ;
WriteEntry ( project , newPythonPID , newPort , newVisualizerPID ) ;
WriteEntry ( project , newPythonPid , newPort , newVisualizerPid ) ;
//For convinience if the user wants to force a new one to open they can press on "manually open"
//For convenience if the user wants to force a new one to open they can press on "manually open"
/ * if ( EditorUtility . DisplayDialog ( "Opening Visualizer Tool" ,
$"The visualizer tool should open shortly in your default browser at http://localhost:{newPort}.\n\nIf this is not the case after a few seconds you may open it manually" ,
"Manually Open" ,
/// </summary>
static int ExecuteVisualizer ( )
{
string path = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "" ) ) ;
string packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
var packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
string packagesPath = Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/bin" ) ;
var packagesPath = Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/bin" ) ;
string pathToData = PlayerPrefs . GetString ( SimulationState . latestOutputDirectoryKey ) ;
var pathToData = PlayerPrefs . GetString ( SimulationState . latestOutputDirectoryKey ) ;
path = path . Replace ( "/" , "\\" ) ;
string command = "" ;
command = $"cd \" { pathToData } \ " && \"{packagesPath}\\datasetvisualizer.exe\" --data=\".\"" ;
var command = $"cd \" { pathToData } \ " && \"{packagesPath}\\datasetvisualizer.exe\" --data=\".\"" ;
command = $"cd \'{pathToData}\'; \'{packagesPath}/datasetvisualizer\' --data=\'.\'" ;
var command = $"cd \'{pathToData}\'; \'{packagesPath}/datasetvisualizer\' --data=\'.\'" ;
int ExitCode = 0 ;
int PID = ExecuteCMD ( command , ref ExitCode , ref output , waitForExit : 0 , displayWindow : false ) ;
if ( ExitCode ! = 0 )
var exitCode = 0 ;
var pid = ExecuteCmd ( command , ref exitCode , ref output , waitForExit : 0 , displayWindow : false ) ;
if ( exitCode ! = 0 )
UnityEngine . Debug . LogError ( "Problem occured when launching the visualizer - Exit Code: " + ExitCode ) ;
Debug . LogError ( "Problem occured when launching the visualizer - Exit Code: " + exitCode ) ;
return PID ;
return pid ;
private static ( int pythonPID , int port , int visualizerPID ) ReadEntry ( string project )
static ( int pythonPID , int port , int visualizerPID ) ReadEntry ( string project )
string path = PathToStreamlitInstances ;
if ( ! Directory . Exists ( PathToStreamlitInstances ) )
if ( ! File . Exists ( path ) )
if ( ! Directory . Exists ( PathToStreamlitInstances ) | | ! File . Exists ( PathToStreamlitInstances ) )
using ( StreamReader sr = File . OpenText ( path ) )
using var sr = File . OpenText ( PathToStreamlitInstances ) ;
string line ;
while ( ( line = sr . ReadLine ( ) ) ! = null )
string line ;
while ( ( line = sr . ReadLine ( ) ) ! = null )
var entry = line . TrimEnd ( ) . Split ( ',' ) ;
if ( entry [ 0 ] = = project )
string [ ] entry = line . TrimEnd ( ) . Split ( ',' ) ;
if ( entry [ 0 ] = = project )
{
//The -1 on ports is because the System.Diagnosis.Process API starts at 0 where as the PID in Windows and Mac start at 1
return ( int . Parse ( entry [ 1 ] ) - 1 , int . Parse ( entry [ 2 ] ) , int . Parse ( entry [ 3 ] ) - 1 ) ;
}
//The -1 on ports is because the System.Diagnosis.Process API starts at 0 where as the PID in Windows and Mac start at 1
return ( int . Parse ( entry [ 1 ] ) - 1 , int . Parse ( entry [ 2 ] ) , int . Parse ( entry [ 3 ] ) - 1 ) ;
private static void WriteEntry ( string project , int pythonId , int port , int visualizerId )
static void WriteEntry ( string project , int pythonId , int port , int visualizerId )
string path = PathToStreamlitInstances ;
using ( StreamWriter sw = File . AppendText ( path ) )
{
sw . WriteLine ( $"{project},{pythonId},{port},{visualizerId}" ) ;
}
var path = PathToStreamlitInstances ;
using var sw = File . AppendText ( path ) ;
sw . WriteLine ( $"{project},{pythonId},{port},{visualizerId}" ) ;
private static void DeleteEntry ( string project )
static void DeleteEntry ( string project )
string path = PathToStreamlitInstances ;
var path = PathToStreamlitInstances ;
List < string > entries = new List < string > ( File . ReadAllLines ( path ) ) ;
var entries = new List < string > ( File . ReadAllLines ( path ) ) ;
using ( StreamWriter sw = File . CreateText ( path ) )
using var sw = File . CreateText ( path ) ;
foreach ( var entry in entries )
foreach ( string entry in entries )
{
sw . WriteLine ( entry . TrimEnd ( ) ) ;
}
sw . WriteLine ( entry . TrimEnd ( ) ) ;
}
}
/// <param name="after"></param>
/// <param name="name"></param>
/// <returns></returns>
private static int GetNewProcessID ( Process [ ] before , Process [ ] after , string name )
static int GetNewProcessID ( Process [ ] before , Process [ ] after , string name )
foreach ( Process p in after )
foreach ( var p in after )
bool isNew = true ;
var isNew = true ;
foreach ( Process q in before )
foreach ( var q in before )
{
if ( p . Id = = q . Id )
{
}
}
}
catch { }
catch
{
// ignored
}
}
return - 1 ;
}
/// </summary>
/// <param name="PID "></param>
/// <param name="pid "></param>
private static int GetPortForPID ( int PID )
static int GetPortForPid ( int pid )
foreach ( ProcessPort p in ProcessPorts . ProcessPortMap )
foreach ( var p in ProcessPorts . ProcessPortMap )
if ( p . ProcessId = = PID )
if ( p . ProcessId = = pid )
{
return p . PortNumber ;
}
/// Launches browser at localhost:port
/// </summary>
/// <param name="port"></param>
private static void LaunchBrowser ( int port )
static void LaunchBrowser ( int port )
{
Process . Start ( $"http://localhost:{port}" ) ;
}
/// </summary>
/// <param name="pythonPID "></param>
/// <param name="pythonPid "></param>
/// <param name="visualizerPID "></param>
/// <param name="visualizerPid "></param>
private static bool ProcessAlive ( int pythonPID , int port , int visualizerPID )
static bool ProcessAlive ( int pythonPid , int port , int visualizerPid )
return PIDExists ( pythonPID ) & &
checkProcessName ( pythonPID , "python" ) & &
ProcessListensToPort ( pythonPID , port ) & &
PIDExists ( visualizerPID ) & &
checkProcessName ( visualizerPID , n ameOfVisualizerProcess) ;
return PidExists ( pythonPid ) & &
CheckProcessName ( pythonPid , "python" ) & &
ProcessListensToPort ( pythonPid , port ) & &
PidExists ( visualizerPid ) & &
CheckProcessName ( visualizerPid , k_N ameOfVisualizerProcess) ;
/// <param name="PID "></param>
/// <param name="pid "></param>
private static bool PIDExists ( int PID )
static bool PidExists ( int pid )
Process proc = Process . GetProcessById ( PID + 1 ) ;
var proc = Process . GetProcessById ( pid + 1 ) ;
if ( proc . HasExited )
{
return false ;
/// <summary>
/// Check if process with PID has a name that contains "name"
/// </summary>
/// <param name="PID "></param>
/// <param name="pid "></param>
private static bool checkProcessName ( int PID , string name )
static bool CheckProcessName ( int pid , string name )
Process proc = Process . GetProcessById ( PID + 1 ) ;
var proc = Process . GetProcessById ( pid + 1 ) ;
return proc . ProcessName . ToLower ( ) . Contains ( name ) ;
}
/// <param name="PID"></param>
/// <param name="pid"></param>
private static bool ProcessListensToPort ( int PID , int port )
static bool ProcessListensToPort ( int pid , int port )
List < ProcessPort > processes = ProcessPorts . ProcessPortMap . FindAll (
x = > x . ProcessId = = PID + 1 & & x . PortNumber = = port
var processes = ProcessPorts . ProcessPortMap . FindAll (
x = > x . ProcessId = = pid + 1 & & x . PortNumber = = port
) ;
return processes . Count > = 1 ;
}
/// </summary>
private static class ProcessPorts
static class ProcessPorts
/// A list of ProcesesPorts that contain the mapping of processes and the ports that the process uses.
/// A list of ProcessPorts that contain the mapping of processes and the ports that the process uses.
public static List < ProcessPort > ProcessPortMap
{
get
{
return GetNetStatPorts ( ) ;
}
}
public static List < ProcessPort > ProcessPortMap = > GetNetStatPorts ( ) ;
/// <summary>
/// This method distills the output from Windows: netstat -a -n -o or OSX: netstat -v -a into a list of ProcessPorts that provide a mapping between
private static List < ProcessPort > GetNetStatPorts ( )
static List < ProcessPort > GetNetStatPorts ( )
List < ProcessPort > ProcessPorts = new List < ProcessPort > ( ) ;
var processPorts = new List < ProcessPort > ( ) ;
using ( Process Proc = new Process ( ) )
{
ProcessStartInfo StartInfo = new ProcessStartInfo ( ) ;
#if UNITY_EDITOR_WIN
StartInfo . FileName = "netstat.exe" ;
StartInfo . Arguments = "-a -n -o" ;
#elif UNITY_EDITOR_OSX
StartInfo . FileName = "netstat" ;
StartInfo . Arguments = "-n -v -a" ;
#endif
StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
StartInfo . UseShellExecute = false ;
StartInfo . RedirectStandardInput = true ;
StartInfo . RedirectStandardOutput = true ;
StartInfo . RedirectStandardError = true ;
using var proc = new Process ( ) ;
var startInfo = new ProcessStartInfo ( ) ;
#if UNITY_EDITOR_WIN
startInfo . FileName = "netstat.exe" ;
startInfo . Arguments = "-a -n -o" ;
#elif UNITY_EDITOR_OSX
startInfo . FileName = "netstat" ;
startInfo . Arguments = "-n -v -a" ;
#endif
startInfo . WindowStyle = ProcessWindowStyle . Hidden ;
startInfo . UseShellExecute = false ;
startInfo . RedirectStandardInput = true ;
startInfo . RedirectStandardOutput = true ;
startInfo . RedirectStandardError = true ;
Proc . StartInfo = StartInfo ;
Proc . Start ( ) ;
#if UNITY_EDITOR_OSX
Proc . WaitForExit ( 2 5 0 0 ) ;
#endif
proc . StartInfo = startInfo ;
proc . Start ( ) ;
#if UNITY_EDITOR_OSX
proc . WaitForExit ( 2 5 0 0 ) ;
#endif
StreamReader StandardOutput = Proc . StandardOutput ;
StreamReader StandardError = Proc . StandardError ;
var standardOutput = proc . StandardOutput ;
var standardError = proc . StandardError ;
string NetStatContent = StandardOutput . ReadToEnd ( ) + StandardError . ReadToEnd ( ) ;
string NetStatExitStatus = Proc . ExitCode . ToString ( ) ;
var netStatContent = standardOutput . ReadToEnd ( ) + standardError . ReadToEnd ( ) ;
var netStatExitStatus = proc . ExitCode . ToString ( ) ;
if ( NetStatExitStatus ! = "0" )
{
UnityEngine . Debug . LogError ( "NetStat command failed. This may require elevated permissions." ) ;
}
if ( netStatExitStatus ! = "0" )
{
Debug . LogError ( "NetStat command failed. This may require elevated permissions." ) ;
}
string [ ] NetStatRows = null ;
#if UNITY_EDITOR_WIN
NetStatRows = Regex . Split ( NetStatContent , "\r\n" ) ;
#elif UNITY_EDITOR_OSX
NetStatRows = Regex . Split ( NetStatContent , "\n" ) ;
#endif
#if UNITY_EDITOR_WIN
var netStatRows = Regex . Split ( netStatContent , "\r\n" ) ;
#elif UNITY_EDITOR_OSX
var netStatRows = Regex . Split ( netStatContent , "\n" ) ;
#endif
foreach ( string NetStatRow in NetStatRows )
foreach ( var netStatRow in netStatRows )
{
var tokens = Regex . Split ( netStatRow , "\\s+" ) ;
#if UNITY_EDITOR_WIN
if ( tokens . Length > 4 & & ( tokens [ 1 ] . Equals ( "UDP" ) | | tokens [ 1 ] . Equals ( "TCP" ) ) )
string [ ] Tokens = Regex . Split ( NetStatRow , "\\s+" ) ;
#if UNITY_EDITOR_WIN
if ( Tokens . Length > 4 & & ( Tokens [ 1 ] . Equals ( "UDP" ) | | Tokens [ 1 ] . Equals ( "TCP" ) ) )
var ipAddress = Regex . Replace ( tokens [ 2 ] , @"\[(.*?)\]" , "1.1.1.1" ) ;
try
string IpAddress = Regex . Replace ( Tokens [ 2 ] , @"\[(.*?)\]" , "1.1.1.1" ) ;
try
{
ProcessPorts . Add ( new ProcessPort (
Tokens [ 1 ] = = "UDP" ? GetProcessName ( Convert . ToInt16 ( Tokens [ 4 ] ) ) : GetProcessName ( Convert . ToInt32 ( Tokens [ 5 ] ) ) ,
Tokens [ 1 ] = = "UDP" ? Convert . ToInt32 ( Tokens [ 4 ] ) : Convert . ToInt32 ( Tokens [ 5 ] ) ,
IpAddress . Contains ( "1.1.1.1" ) ? String . Format ( "{0}v6" , Tokens [ 1 ] ) : String . Format ( "{0}v4" , Tokens [ 1 ] ) ,
Convert . ToInt32 ( IpAddress . Split ( ':' ) [ 1 ] )
) ) ;
}
catch
{
UnityEngine . Debug . LogError ( "Could not convert the following NetStat row to a Process to Port mapping." ) ;
UnityEngine . Debug . LogError ( NetStatRow ) ;
}
processPorts . Add ( new ProcessPort (
tokens [ 1 ] = = "UDP" ? Convert . ToInt32 ( tokens [ 4 ] ) : Convert . ToInt32 ( tokens [ 5 ] ) ,
Convert . ToInt32 ( ipAddress . Split ( ':' ) [ 1 ] )
) ) ;
}
catch
{
Debug . LogError ( "Could not convert the following NetStat row to a Process to Port mapping." ) ;
Debug . LogError ( netStatRow ) ;
else
}
else
{
if ( ! netStatRow . Trim ( ) . StartsWith ( "Proto" ) & & ! netStatRow . Trim ( ) . StartsWith ( "Active" ) & & ! String . IsNullOrWhiteSpace ( netStatRow ) )
if ( ! NetStatRow . Trim ( ) . StartsWith ( "Proto" ) & & ! NetStatRow . Trim ( ) . StartsWith ( "Active" ) & & ! String . IsNullOrWhiteSpace ( NetStatRow ) )
{
UnityEngine . Debug . LogError ( "Unrecognized NetStat row to a Process to Port mapping." ) ;
UnityEngine . Debug . LogError ( NetStatRow ) ;
}
Debug . LogError ( "Unrecognized NetStat row to a Process to Port mapping." ) ;
Debug . LogError ( netStatRow ) ;
#elif UNITY_EDITOR_OSX
if ( Tokens . Length = = 1 2 & & Tokens [ 0 ] . Equals ( "tcp4" ) & ( Tokens [ 3 ] . Contains ( "localhost" ) | | Tokens [ 3 ] . Contains ( "*." ) ) )
}
#elif UNITY_EDITOR_OSX
if ( tokens . Length = = 1 2 & & tokens [ 0 ] . Equals ( "tcp4" ) & ( tokens [ 3 ] . Contains ( "localhost" ) | | tokens [ 3 ] . Contains ( "*." ) ) )
if ( Tokens [ 5 ] ! = "CLOSED" ) {
ProcessPorts . Add ( new ProcessPort (
GetProcessName ( Convert . ToInt32 ( Tokens [ 8 ] ) ) ,
Convert . ToInt32 ( Tokens [ 8 ] ) ,
"tcp4" ,
Convert . ToInt32 ( Tokens [ 3 ] . Split ( '.' ) [ 1 ] )
if ( tokens [ 5 ] ! = "CLOSED" ) {
processPorts . Add ( new ProcessPort (
Convert . ToInt32 ( tokens [ 8 ] ) ,
Convert . ToInt32 ( tokens [ 3 ] . Split ( '.' ) [ 1 ] )
{
continue ;
}
catch
{
}
catch
UnityEngine . Debug . LogError ( "Could not convert the following NetStat row to a Process to Port mapping." ) ;
UnityEngine . Debug . LogError ( NetStatRow ) ;
Debug . LogError ( "Could not convert the following NetStat row to a Process to Port mapping." ) ;
Debug . LogError ( netStatRow ) ;
#endif
}
#endif
UnityEngine . Debug . LogError ( ex . Message ) ;
Debug . LogError ( ex . Message ) ;
return ProcessPorts ;
}
/// <summary>
/// Private method that handles pulling the process name (if one exists) from the process id.
/// </summary>
/// <param name="ProcessId"></param>
/// <returns></returns>
private static string GetProcessName ( int ProcessId )
{
string procName = "UNKNOWN" ;
try
{
procName = Process . GetProcessById ( ProcessId ) . ProcessName ;
}
catch { }
return procName ;
return processPorts ;
}
}
private class ProcessPort
class ProcessPort
private string _ProcessName = String . Empty ;
private int _ProcessId = 0 ;
private string _Protocol = String . Empty ;
private int _PortNumber = 0 ;
/// <param name="ProcessName">Name of process to be </param>
/// <param name="ProcessId"></param>
/// <param name="Protocol"></param>
/// <param name="PortNumber"></param>
internal ProcessPort ( string ProcessName , int ProcessId , string Protocol , int PortNumber )
/// <param name="processId"></param>
/// <param name="portNumber"></param>
internal ProcessPort ( int processId , int portNumber )
_ProcessName = ProcessName ;
_ProcessId = ProcessId ;
_Protocol = Protocol ;
_PortNumber = PortNumber ;
ProcessId = processId ;
PortNumber = portNumber ;
public string ProcessPortDescription
{
get
{
return String . Format ( "{0} ({1} port {2} pid {3})" , _ProcessName , _Protocol , _PortNumber , _ProcessId ) ;
}
}
public string ProcessName
{
get { return _ProcessName ; }
}
public int ProcessId
{
get { return _ProcessId ; }
}
public string Protocol
{
get { return _Protocol ; }
}
public int PortNumber
{
get { return _PortNumber ; }
}
public int ProcessId { get ; }
public int PortNumber { get ; }
private static void CheckForUpdates ( )
static void CheckForUpdates ( )
if ( ! checkIfVisualizerInstalled ( ) )
if ( ! CheckIfVisualizerInstalled ( ) )
{
if ( EditorUtility . DisplayDialog ( "Visualizer not Installed" ,
$"The visualizer is not yet installed, do you wish to install it?" ,
}
return ;
}
string latestVersion = Task . Run ( PipAPI . GetLatestVersionNumber ) . Result ;
var latestVersion = Task . Run ( PipAPI . GetLatestVersionNumber ) . Result ;
string packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
var packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
#elif UNITY_EDITOR_OSX
string packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/bin" ) ) ;
#endif
#endif
int ExitCode = - 1 ;
var exitCode = - 1 ;
ExecuteCMD ( $"\" { packagesPath } \ "\\pip3.bat show unity-cv-datasetvisualizer" , ref ExitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
ExecuteCmd ( $"\" { packagesPath } \ "\\pip3.bat show unity-cv-datasetvisualizer" , ref exitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
ExecuteCMD ( $"cd \'{packagesPath}\'; ./python3.7 -m pip show unity-cv-datasetvisualizer" , ref ExitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
ExecuteCmd ( $"cd \'{packagesPath}\'; ./python3.7 -m pip show unity-cv-datasetvisualizer" , ref exitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
if ( ExitCode ! = 0 ) {
UnityEngine . Debug . LogError ( "Could not get the version of the current install of the visualizer tool" ) ;
if ( exitCode ! = 0 ) {
Debug . LogError ( "Could not get the version of the current install of the visualizer tool" ) ;
string currentVersion = null ;
string [ ] outputLines = output . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
for ( int i = 0 ; i < outputLines . Length ; i + + )
{
if ( outputLines [ i ] . StartsWith ( "Version: " ) )
{
currentVersion = outputLines [ i ] . Substring ( "Version: " . Length ) ;
break ;
}
}
var outputLines = output . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
var currentVersion = ( from t in outputLines where t . StartsWith ( "Version: " ) select t . Substring ( "Version: " . Length ) ) . FirstOrDefault ( ) ;
UnityEngine . Debug . LogError ( "Could not parse the version of the current install of the visualizer tool" ) ;
Debug . LogError ( "Could not parse the version of the current install of the visualizer tool" ) ;
if ( PipAPI . compareVersions ( latestVersion , currentVersion ) > 0 )
if ( PipAPI . CompareVersions ( latestVersion , currentVersion ) > 0 )
{
if ( EditorUtility . DisplayDialog ( "Update Found for Visualizer" ,
$"An update was found for the Visualizer" ,
}
}
private static bool checkIfVisualizerInstalled ( )
static bool CheckIfVisualizerInstalled ( )
string packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
var packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/Scripts" ) ) ;
string packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/bin" ) ) ;
var packagesPath = Path . GetFullPath ( Application . dataPath . Replace ( "/Assets" , "/Library/PythonInstall/bin" ) ) ;
int ExitCode = 0 ;
var exitCode = 0 ;
ExecuteCMD ( $"\" { packagesPath } \ "\\pip3.bat list" , ref ExitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
ExecuteCmd ( $"\" { packagesPath } \ "\\pip3.bat list" , ref exitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
ExecuteCMD ( $"cd \'{packagesPath}\'; ./python3.7 -m pip list" , ref ExitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
ExecuteCmd ( $"cd \'{packagesPath}\'; ./python3.7 -m pip list" , ref exitCode , ref output , waitForExit : 1 5 0 0 , getOutput : true ) ;
if ( ExitCode ! = 0 ) {
UnityEngine . Debug . LogError ( "Could not list pip packages" ) ;
if ( exitCode ! = 0 ) {
Debug . LogError ( "Could not list pip packages" ) ;
string [ ] outputLines = output . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
for ( int i = 0 ; i < outputLines . Length ; i + + )
{
if ( outputLines [ i ] . StartsWith ( "unity-cv-datasetvisualizer" ) )
{
return true ;
}
}
return false ;
var outputLines = output . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
return outputLines . Any ( t = > t . StartsWith ( "unity-cv-datasetvisualizer" ) ) ;
}
}
}