11using System ;
2- using System . Collections . Generic ;
32using System . Diagnostics ;
43using System . IO ;
54using System . Runtime . InteropServices ;
6- using System . Text ;
75using System . Threading ;
86using Valve . VR ;
97
108namespace OpenVRStartup
119{
1210 class Program
1311 {
14- // TODO: Write log files of which command files we execute every launch.
15- // TODO: If no log file, stop execution with instruction of SteamVR startup toggle.
12+ static string LOG_FILE_PATH = "./OpenVRStartup.log" ;
13+
14+ [ DllImport ( "user32.dll" ) ]
15+ public static extern bool ShowWindow ( IntPtr hWnd , int nCmdShow ) ;
16+ public const int SW_SHOWMINIMIZED = 2 ;
17+ private volatile static bool _isReady = false ;
1618
1719 static void Main ( string [ ] args )
1820 {
1921 // Starting worker
2022 var t = new Thread ( Worker ) ;
21- Utils . PrintDebug ( "Starting worker thread. ") ;
23+ LogUtils . WriteLineToCache ( $ "Application starting ( { Properties . Resources . Version } ) ") ;
2224 if ( ! t . IsAlive ) t . Start ( ) ;
23- else Utils . PrintError ( "Could not start worker thread." ) ;
25+ else LogUtils . WriteLineToCache ( "Error: Could not start worker thread" ) ;
26+
27+ // Check if first run, if so do NOT minimize but write instructions.
28+ if ( LogUtils . LogFileExists ( LOG_FILE_PATH ) )
29+ {
30+ _isReady = true ;
31+ IntPtr winHandle = Process . GetCurrentProcess ( ) . MainWindowHandle ;
32+ ShowWindow ( winHandle , SW_SHOWMINIMIZED ) ;
33+ }
34+ else {
35+ Utils . PrintInfo ( "\n ========================" ) ;
36+ Utils . PrintInfo ( " First Run Instructions " ) ;
37+ Utils . PrintInfo ( "========================\n " ) ;
38+ Utils . Print ( "To get this to launch every time you start SteamVR, do the following steps:" ) ;
39+ Utils . Print ( " 1. Launch SteamVR." ) ;
40+ Utils . Print ( " 2. Open [Settings] from the hamburger menu in the SteamVR status window." ) ;
41+ Utils . Print ( " 3. Select the [Startup / Shutdown] section in the menu to the left." ) ;
42+ Utils . Print ( " 4. Click [Choose startup overlay apps]." ) ;
43+ Utils . Print ( " 5. Locate OpenVRStartup and toggle the switch to [On]." ) ;
44+ Utils . Print ( "\n The next time this program runs it will be minimized and terminate as soon as scripts have been launched." ) ;
45+ Utils . Print ( "\n To see this message again, delete the log file that is in the same folder." ) ;
46+ Utils . Print ( "\n Press [Enter] in this window to continue execution." ) ;
47+ Console . ReadLine ( ) ;
48+ _isReady = true ;
49+ }
2450
2551 Console . ReadLine ( ) ;
2652 t . Abort ( ) ;
@@ -42,14 +68,16 @@ private static void Worker()
4268 Thread . Sleep ( 1000 ) ;
4369 _isConnected = InitVR ( ) ;
4470 }
45- else
46- {
71+ else if ( _isReady )
72+ {
4773 RunScripts ( ) ;
4874 OpenVR . Shutdown ( ) ;
4975 shouldRun = false ;
5076 }
5177 if ( ! shouldRun )
5278 {
79+ LogUtils . WriteLineToCache ( "Application exiting, writing log" ) ;
80+ LogUtils . WriteCacheToLogFile ( LOG_FILE_PATH , 100 ) ;
5381 Environment . Exit ( 0 ) ;
5482 }
5583 }
@@ -62,18 +90,16 @@ private static bool InitVR()
6290 OpenVR . Init ( ref error , EVRApplicationType . VRApplication_Overlay ) ;
6391 if ( error != EVRInitError . None )
6492 {
65- Utils . PrintError ( $ "OpenVR initialization errored : { Enum . GetName ( typeof ( EVRInitError ) , error ) } ") ;
93+ LogUtils . WriteLineToCache ( $ "Error: OpenVR init failed : { Enum . GetName ( typeof ( EVRInitError ) , error ) } ") ;
6694 return false ;
6795 }
6896 else
6997 {
70- Utils . PrintInfo ( "OpenVR initialized successfully. " ) ;
98+ LogUtils . WriteLineToCache ( "OpenVR init success " ) ;
7199
72- // Load app manifest, I think this is needed for the application to show up in the input bindings at all
73- Utils . PrintVerbose ( "Loading app.vrmanifest" ) ;
100+ // Load app manifest
74101 var appError = OpenVR . Applications . AddApplicationManifest ( Path . GetFullPath ( "./app.vrmanifest" ) , false ) ;
75- if ( appError != EVRApplicationError . None ) Utils . PrintError ( $ "Failed to load Application Manifest: { Enum . GetName ( typeof ( EVRApplicationError ) , appError ) } ") ;
76- else Utils . PrintInfo ( "Application manifest loaded successfully." ) ;
102+ if ( appError != EVRApplicationError . None ) LogUtils . WriteLineToCache ( $ "Error: Failed to load app manifest: { Enum . GetName ( typeof ( EVRApplicationError ) , appError ) } ") ;
77103 return true ;
78104 }
79105 }
@@ -83,21 +109,21 @@ private static void RunScripts()
83109 {
84110 try {
85111 var files = Directory . GetFiles ( "./" , "*.cmd" ) ;
86- Utils . Print ( $ "Found { files . Length } file (s)") ;
112+ LogUtils . WriteLineToCache ( $ "Found: { files . Length } script (s)") ;
87113 foreach ( var file in files )
88114 {
89- Utils . PrintInfo ( $ "Running { file } ") ;
115+ LogUtils . WriteLineToCache ( $ "Executing: { file } ") ;
90116 Process p = new Process ( ) ;
91117 p . StartInfo . CreateNoWindow = true ;
92118 p . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
93119 p . StartInfo . FileName = Path . Combine ( Environment . CurrentDirectory , file ) ;
94120 p . Start ( ) ;
95121 }
122+ if ( files . Length == 0 ) LogUtils . WriteLineToCache ( $ "Did not find any .cmd files to execute.") ;
96123 } catch ( Exception e )
97124 {
98- Utils . PrintError ( $ "Error loading scripts: { e . Message } ") ;
125+ LogUtils . WriteLineToCache ( $ "Error: Could not load scripts: { e . Message } ") ;
99126 }
100-
101127 }
102128 }
103129}
0 commit comments