@@ -22,12 +22,11 @@ static class Program
2222 private static readonly byte deactivateByte = 0x00 ;
2323 private static List < GattCharacteristic > listGattCharacteristics = new List < GattCharacteristic > ( ) ;
2424
25-
25+ private static readonly string logFileName = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Logs" ) ;
2626 public delegate void LogHandler ( object msg , LogType type ) ;
2727 public static event LogHandler OnLog ;
2828 private static Form1 app = null ;
2929 private static CVRSystem OVRSystem ;
30- private static bool isEnabledVR = false ;
3130 private static Thread onQuitThread ;
3231 private static CancellationTokenSource cancellationToken = new CancellationTokenSource ( ) ;
3332
@@ -38,58 +37,64 @@ static void Main(string[] args)
3837 Application . SetCompatibleTextRenderingDefault ( false ) ;
3938 Application . ApplicationExit += ( obj , e ) => cancellationToken . Cancel ( ) ;
4039 app = new Form1 ( ) ;
41- OnLog += ( msg , type ) => app . Log ( msg , type ) ;
40+ OnLog += ( msg , type ) =>
41+ {
42+ WriteToLogFileAsync ( msg , LogType . error ) ;
43+ if ( app != null )
44+ {
45+ app . Log ( msg , type ) ;
46+ }
47+ } ;
48+ LogFileStart ( ) ;
4249 if ( args . Length > 0 )
4350 {
4451 app . ShowInTaskbar = false ;
4552 app . WindowState = FormWindowState . Minimized ;
4653 UseArgumentsAsync ( args ) ;
4754 }
55+ Startup ( ) ;
56+
57+ Stack < EventHandler > eventHandlers = new Stack < EventHandler > ( ) ; //Buttons from the form.
58+ eventHandlers . Push ( new EventHandler ( ( obj , args ) => SendActiveStatus ( true ) ) ) ;
59+ eventHandlers . Push ( new EventHandler ( ( obj , args ) => SendActiveStatus ( false ) ) ) ;
60+ eventHandlers . Push ( new EventHandler ( ( obj , args ) => AppManifest ( WithManifestTask . add ) ) ) ;
61+ eventHandlers . Push ( new EventHandler ( ( obj , args ) => AppManifest ( WithManifestTask . rm ) ) ) ;
62+ foreach ( Button button in app . GetButtons ( ) )
63+ {
64+ button . Click += eventHandlers . Pop ( ) ;
65+ }
66+ Application . Run ( app ) ;
67+ }
68+
69+ private static async Task Startup ( )
70+ {
71+ await GetGattCharacteristicsAsync ( ) ;
72+ Log ( $ "lighthouses found: { listGattCharacteristics . Count } ;") ;
73+ if ( listGattCharacteristics . Count > 0 && app != null )
74+ {
75+ app . BtnActive ( true ) ;
76+ }
4877
4978 try
5079 {
5180 EVRInitError error = EVRInitError . None ;
52- OVRSystem = OpenVR . Init ( ref error , EVRApplicationType . VRApplication_Background ) ; //çàïóñêàåòñÿ è âûðóáàåòñÿ ñ SteamVR
53- if ( error == EVRInitError . Init_NoServerForBackgroundApp )
81+ OVRSystem = OpenVR . Init ( ref error , EVRApplicationType . VRApplication_Background ) ; //starts and shuts down with SteamVR.
82+ if ( error == EVRInitError . Init_NoServerForBackgroundApp || error != EVRInitError . None )
5483 {
5584 Log ( "Init without SteamVR;" ) ;
5685 }
57- else if ( error != EVRInitError . None )
58- {
59- LogError ( error ) ;
60- }
6186 else
6287 {
63- isEnabledVR = true ;
64- OVRSystem . AcknowledgeQuit_Exiting ( ) ;
88+ Log ( "Init with SteamVR;" ) ;
6589 onQuitThread = new Thread ( new ThreadStart ( QuitThreadChecker ) ) ;
6690 onQuitThread . Start ( ) ;
91+ await SendOnLighthouseAsync ( activateByte ) ;
6792 }
6893 }
6994 catch ( Exception e )
7095 {
7196 LogError ( e . Message ) ;
7297 }
73-
74- if ( isEnabledVR )
75- {
76- SendOnStart ( ) ;
77- }
78- else
79- {
80- GetGattCharacteristicsAsync ( ) ;
81- }
82-
83- Stack < EventHandler > eventHandlers = new Stack < EventHandler > ( ) ; //Ïðñîòî äëÿ óäîáíîãî íàçíà÷åíèÿ êíîïîê.
84- eventHandlers . Push ( new EventHandler ( ( obj , args ) => SendActiveStatus ( true ) ) ) ;
85- eventHandlers . Push ( new EventHandler ( ( obj , args ) => SendActiveStatus ( false ) ) ) ;
86- eventHandlers . Push ( new EventHandler ( ( obj , args ) => AppManifest ( WithManifestTask . add ) ) ) ;
87- eventHandlers . Push ( new EventHandler ( ( obj , args ) => AppManifest ( WithManifestTask . rm ) ) ) ;
88- foreach ( Button button in app . GetButtons ( ) )
89- {
90- button . Click += eventHandlers . Pop ( ) ;
91- }
92- Application . Run ( app ) ;
9398 }
9499
95100
@@ -115,6 +120,7 @@ private static void QuitThreadChecker()
115120 while ( ! cancellationToken . IsCancellationRequested )
116121 {
117122 OVRSystem . AcknowledgeQuit_Exiting ( ) ;
123+ Thread . Sleep ( 100 ) ;
118124 }
119125 Exit ( ) ;
120126 }
@@ -169,12 +175,6 @@ private static async Task GetGattCharacteristicsAsync()
169175 LogError ( $ "Sevices { result . Status } ;") ;
170176 }
171177 }
172-
173- Log ( $ "lighthouses found: { listGattCharacteristics . Count } ;") ;
174- if ( listGattCharacteristics . Count > 0 && app != null )
175- {
176- app . BtnActive ( true ) ;
177- }
178178 }
179179
180180 /// <summary>
@@ -208,28 +208,53 @@ public static void SendActiveStatus(bool status)
208208 {
209209 SendOnLighthouseAsync ( ( status ) ? activateByte : deactivateByte ) ;
210210 }
211+ #endregion
212+
213+ #region Logs
211214
212- public static async Task SendOnStart ( )
215+ private static void Log ( object msg )
213216 {
214- await GetGattCharacteristicsAsync ( ) ;
215- await SendOnLighthouseAsync ( activateByte ) ;
217+ OnLog . Invoke ( msg , LogType . log ) ;
218+ }
219+
220+ private static void LogError ( object msg )
221+ {
222+ OnLog . Invoke ( msg , LogType . error ) ;
223+ }
224+
225+ private static void LogFileStart ( )
226+ {
227+ File . WriteAllText ( logFileName , null ) ;
228+ }
229+
230+ private async static Task WriteToLogFileAsync ( object msg , LogType type = LogType . log )
231+ {
232+ string res = $ "[{ DateTime . Now } ]";
233+ res += "[" + ( ( type == LogType . log ) ? "Log" : "ERROR" ) + "] " ;
234+ res += msg . ToString ( ) ;
235+ await File . AppendAllTextAsync ( logFileName , res + "\n " ) ;
216236 }
237+
217238 #endregion
218239
240+ #region StartupArgs
219241 /// <summary>
220242 /// Processing arguments at the start of the application.
221243 /// </summary>
222244 /// <param name="args"></param>
223- private static async void UseArgumentsAsync ( string [ ] args )
245+ private static async void UseArgumentsAsync ( string [ ] args )
224246 {
225- await GetGattCharacteristicsAsync ( ) ;
226- if ( args [ 0 ] == "--powerOn" )
227- {
228- await SendOnLighthouseAsync ( activateByte ) ;
229- }
230- else if ( args [ 0 ] == "--powerOff" )
247+ if ( args [ 0 ] == "--powerOn" || args [ 0 ] == "--powerOff" )
231248 {
232- await SendOnLighthouseAsync ( deactivateByte ) ;
249+ await GetGattCharacteristicsAsync ( ) ;
250+ if ( args [ 0 ] == "--powerOn" )
251+ {
252+ await SendOnLighthouseAsync ( activateByte ) ;
253+ }
254+ else
255+ {
256+ await SendOnLighthouseAsync ( deactivateByte ) ;
257+ }
233258 }
234259 else if ( args [ 0 ] == "--reg" )
235260 {
@@ -242,18 +267,6 @@ private static async void UseArgumentsAsync(string[] args)
242267 Exit ( ) ;
243268 }
244269
245- private static void Log ( object msg )
246- {
247- if ( app == null ) return ;
248- OnLog . Invoke ( msg , LogType . log ) ;
249- }
250-
251- private static void LogError ( object msg )
252- {
253- if ( app == null ) return ;
254- OnLog . Invoke ( msg , LogType . error ) ;
255- }
256-
257270 private static void AppManifest ( WithManifestTask task )
258271 {
259272 EVRInitError evrInitError = EVRInitError . None ;
@@ -265,23 +278,26 @@ private static void AppManifest(WithManifestTask task)
265278 }
266279
267280 EVRApplicationError applicationError ;
281+ string manifestPath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "manifest.vrmanifest" ) ;
268282 if ( task == WithManifestTask . add )
269283 {
270- applicationError = OpenVR . Applications . AddApplicationManifest ( Directory . GetCurrentDirectory ( ) + @"\manifest.vrmanifest" , false ) ;
284+ applicationError = OpenVR . Applications . AddApplicationManifest ( manifestPath , false ) ;
271285 }
272286 else
273287 {
274- applicationError = OpenVR . Applications . RemoveApplicationManifest ( Directory . GetCurrentDirectory ( ) + @"\manifest.vrmanifest" ) ;
288+ applicationError = OpenVR . Applications . RemoveApplicationManifest ( manifestPath ) ;
275289 }
276290
277291 if ( applicationError != EVRApplicationError . None )
278292 {
279293 LogError ( applicationError ) ;
280294 return ;
281295 }
282- Log ( $ "Application manifest { ( ( task == WithManifestTask . add ) ? "registered" : "removed" ) } ") ;
296+ Log ( $ "Application manifest { ( ( task == WithManifestTask . add ) ? "registered; " : "removed; " ) } ") ;
283297 }
284298
299+ #endregion
300+
285301 private static void Exit ( )
286302 {
287303 OpenVR . Shutdown ( ) ;
0 commit comments