11using System ;
2+ using System . Collections . Generic ;
23using System . Diagnostics ;
34using System . IO ;
45using System . Runtime . InteropServices ;
@@ -9,14 +10,17 @@ namespace OpenVRStartup
910{
1011 class Program
1112 {
12- static string LOG_FILE_PATH = "./OpenVRStartup.log" ;
13+ static readonly string PATH_LOGFILE = "./OpenVRStartup.log" ;
14+ static readonly string PATH_STARTFOLDER = "./start/" ;
15+ static readonly string PATH_STOPFOLDER = "./stop/" ;
16+ static readonly string FILE_PATTERN = "*.cmd" ;
1317
1418 [ DllImport ( "user32.dll" ) ]
1519 public static extern bool ShowWindow ( IntPtr hWnd , int nCmdShow ) ;
1620 public const int SW_SHOWMINIMIZED = 2 ;
1721 private volatile static bool _isReady = false ;
1822
19- static void Main ( string [ ] args )
23+ static void Main ( string [ ] _ )
2024 {
2125 // Starting worker
2226 var t = new Thread ( Worker ) ;
@@ -25,7 +29,7 @@ static void Main(string[] args)
2529 else LogUtils . WriteLineToCache ( "Error: Could not start worker thread" ) ;
2630
2731 // Check if first run, if so do NOT minimize but write instructions.
28- if ( LogUtils . LogFileExists ( LOG_FILE_PATH ) )
32+ if ( LogUtils . LogFileExists ( PATH_LOGFILE ) )
2933 {
3034 _isReady = true ;
3135 IntPtr winHandle = Process . GetCurrentProcess ( ) . MainWindowHandle ;
@@ -36,7 +40,8 @@ static void Main(string[] args)
3640 Utils . PrintInfo ( " First Run Instructions " ) ;
3741 Utils . PrintInfo ( "========================" ) ;
3842 Utils . Print ( "\n This app automatically sets itself to auto-launch with SteamVR." ) ;
39- Utils . Print ( "\n When it runs it will in turn run all .cmd files in the same folder." ) ;
43+ Utils . Print ( $ "\n When it runs it will in turn run all { FILE_PATTERN } files in the { PATH_STARTFOLDER } folder.") ;
44+ Utils . Print ( $ "\n If there are { FILE_PATTERN } files in { PATH_STOPFOLDER } it will stay and run those on shutdown.") ;
4045 Utils . Print ( "\n This message is only shown once, to see it again delete the log file." ) ;
4146 Utils . Print ( "\n Press [Enter] in this window to continue execution." ) ;
4247 Console . ReadLine ( ) ;
@@ -64,24 +69,24 @@ private static void Worker()
6469 _isConnected = InitVR ( ) ;
6570 }
6671 else if ( _isReady )
67- {
68- RunStartupScripts ( ) ;
69- WaitForQuit ( ) ;
72+ {
73+ RunScripts ( PATH_STARTFOLDER ) ;
74+ if ( WeHaveScripts ( PATH_STOPFOLDER ) ) WaitForQuit ( ) ;
7075 OpenVR . System . AcknowledgeQuit_Exiting ( ) ;
7176 OpenVR . Shutdown ( ) ;
72- RunShutdownScripts ( ) ;
77+ RunScripts ( PATH_STOPFOLDER ) ;
7378 shouldRun = false ;
7479 }
7580 if ( ! shouldRun )
7681 {
7782 LogUtils . WriteLineToCache ( "Application exiting, writing log" ) ;
78- LogUtils . WriteCacheToLogFile ( LOG_FILE_PATH , 100 ) ;
83+ LogUtils . WriteCacheToLogFile ( PATH_LOGFILE , 100 ) ;
7984 Environment . Exit ( 0 ) ;
8085 }
8186 }
82- }
83-
84- // Initializing connection to OpenVR
87+ }
88+
89+ // Initializing connection to OpenVR
8590 private static bool InitVR ( )
8691 {
8792 var error = EVRInitError . None ;
@@ -112,38 +117,15 @@ private static bool InitVR()
112117 }
113118
114119 // Scripts
115- private static void RunStartupScripts ( )
116- {
117- try {
118- var files = Directory . GetFiles ( "./" , "*.cmd" ) ;
119- LogUtils . WriteLineToCache ( $ "Found: { files . Length } script(s)") ;
120- foreach ( var file in files )
121- {
122- LogUtils . WriteLineToCache ( $ "Executing: { file } ") ;
123- var path = Path . Combine ( Environment . CurrentDirectory , file ) ;
124- Process p = new Process ( ) ;
125- p . StartInfo . CreateNoWindow = true ;
126- p . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
127- p . StartInfo . FileName = Path . Combine ( Environment . SystemDirectory , "cmd.exe" ) ;
128- p . StartInfo . Arguments = $ "/C \" { path } \" ";
129- p . Start ( ) ;
130- }
131- if ( files . Length == 0 ) LogUtils . WriteLineToCache ( "Did not find any .cmd files to execute." ) ;
132- } catch ( Exception e )
133- {
134- LogUtils . WriteLineToCache ( $ "Error: Could not load scripts: { e . Message } ") ;
135- }
136- }
137-
138- private static void RunShutdownScripts ( )
139- {
120+ private static void RunScripts ( string folder ) {
140121 try
141122 {
142- var files = Directory . GetFiles ( "./shutdown/" , "*.cmd" ) ;
143- LogUtils . WriteLineToCache ( $ "Found: { files . Length } script(s)") ;
123+ if ( ! Directory . Exists ( folder ) ) Directory . CreateDirectory ( folder ) ;
124+ var files = Directory . GetFiles ( folder , FILE_PATTERN ) ;
125+ LogUtils . WriteLineToCache ( $ "Found: { files . Length } script(s) in { folder } ") ;
144126 foreach ( var file in files )
145127 {
146- LogUtils . WriteLineToCache ( $ "Executing: { file } ") ;
128+ LogUtils . WriteLineToCache ( $ "Executing: { file } from { folder } ") ;
147129 var path = Path . Combine ( Environment . CurrentDirectory , file ) ;
148130 Process p = new Process ( ) ;
149131 p . StartInfo . CreateNoWindow = true ;
@@ -152,25 +134,49 @@ private static void RunShutdownScripts()
152134 p . StartInfo . Arguments = $ "/C \" { path } \" ";
153135 p . Start ( ) ;
154136 }
155- if ( files . Length == 0 ) LogUtils . WriteLineToCache ( "Did not find any .cmd files to execute. " ) ;
137+ if ( files . Length == 0 ) LogUtils . WriteLineToCache ( $ "Did not find any .cmd files to execute in { folder } ") ;
156138 }
157139 catch ( Exception e )
158140 {
159- LogUtils . WriteLineToCache ( $ "Error: Could not load scripts: { e . Message } ") ;
141+ LogUtils . WriteLineToCache ( $ "Error: Could not load scripts from { folder } : { e . Message } ") ;
160142 }
161143 }
162144
163145 private static void WaitForQuit ( )
164146 {
165- VREvent_t ev = new VREvent_t ( ) ;
166-
167- var eventExists = OpenVR . System . PollNextEvent ( ref ev , Utils . SizeOf ( ev ) ) ;
168-
169- while ( ! eventExists || ( EVREventType ) ev . eventType != EVREventType . VREvent_Quit )
147+ var shouldRun = true ;
148+ while ( shouldRun )
170149 {
171- Thread . Sleep ( 100 ) ;
172- eventExists = OpenVR . System . PollNextEvent ( ref ev , Utils . SizeOf ( ev ) ) ;
150+ var vrEvents = new List < VREvent_t > ( ) ;
151+ var vrEvent = new VREvent_t ( ) ;
152+ uint eventSize = ( uint ) Marshal . SizeOf ( vrEvent ) ;
153+ try
154+ {
155+ while ( OpenVR . System . PollNextEvent ( ref vrEvent , eventSize ) )
156+ {
157+ vrEvents . Add ( vrEvent ) ;
158+ }
159+ }
160+ catch ( Exception e )
161+ {
162+ Utils . PrintError ( $ "Could not get new events: { e . Message } ") ;
163+ }
164+
165+ foreach ( var e in vrEvents )
166+ {
167+ if ( ( EVREventType ) e . eventType == EVREventType . VREvent_Quit )
168+ {
169+ shouldRun = false ;
170+ }
171+ }
172+ Thread . Sleep ( 1000 ) ;
173173 }
174+ }
175+
176+ private static bool WeHaveScripts ( string folder )
177+ {
178+ if ( ! Directory . Exists ( folder ) ) Directory . CreateDirectory ( folder ) ;
179+ return Directory . GetFiles ( folder , FILE_PATTERN ) . Length > 0 ;
174180 }
175181 }
176182}
0 commit comments