99using System . Net ;
1010using Newtonsoft . Json ;
1111using System . Linq ;
12+ using System . Threading ;
1213
1314namespace AssistantComputerControl {
1415 class MainProgram {
15- public const string softwareVersion = "1.0.1 " ,
16- releaseDate = "2018-07-20 16:59 " ,
16+ public const string softwareVersion = "1.0.2 " ,
17+ releaseDate = "2018-07-21 21:19 " ,
1718 appName = "AssistantComputerControl" ;
1819 static public bool debug = true ,
1920 unmuteVolumeChange = true ,
2021 isPerformingAction = false ,
22+ isCheckingForUpdate = false ,
2123
2224 testingAction = false ;
2325
@@ -28,6 +30,8 @@ public enum TestStatus {
2830 ongoing
2931 }
3032
33+ private static FileSystemWatcher watcher ;
34+
3135 static public string currentLocationFull = Assembly . GetEntryAssembly ( ) . Location ,
3236 defaultActionFolder = CheckPath ( ) ,
3337
@@ -47,9 +51,13 @@ public enum TestStatus {
4751 [ STAThread ]
4852 [ PermissionSet ( SecurityAction . Demand , Name = "FullTrust" ) ]
4953 static void Main ( string [ ] args ) {
54+ AppDomain . CurrentDomain . UnhandledException += new UnhandledExceptionEventHandler ( CurrentDomain_UnhandledException ) ;
55+
5056 SetupDataFolder ( ) ;
5157 if ( File . Exists ( logFilePath ) )
5258 File . WriteAllText ( logFilePath , string . Empty ) ;
59+ else
60+ CreateLogFile ( ) ;
5361
5462 //Check if software already runs, if so kill this instance
5563 if ( Process . GetProcessesByName ( Path . GetFileNameWithoutExtension ( Assembly . GetEntryAssembly ( ) . Location ) ) . Length > 1 ) {
@@ -63,12 +71,15 @@ static void Main(string[] args) {
6371
6472 if ( Properties . Settings . Default . CheckForUpdates ) {
6573 if ( HasInternet ( ) ) {
66- new ACC_Updater ( ) . Check ( ) ;
74+ new Thread ( ( ) => {
75+ new ACC_Updater ( ) . Check ( ) ;
76+ } ) . Start ( ) ;
6777 } else {
6878 DoDebug ( "Couldn't check for new update as PC does not have access to the internet" ) ;
6979 }
7080 }
71- if ( File . Exists ( Path . Combine ( dataFolderLocation , "updated.txt" ) ) ) {
81+
82+ if ( File . Exists ( Path . Combine ( dataFolderLocation , "updated.txt" ) ) ) {
7283 File . Delete ( Path . Combine ( dataFolderLocation , "updated.txt" ) ) ;
7384 new AboutVersion ( ) . Show ( ) ;
7485 }
@@ -94,20 +105,13 @@ static void Main(string[] args) {
94105 }
95106
96107 //Delete all old action files
97- foreach ( string file in Directory . GetFiles ( CheckPath ( ) , "*." + Properties . Settings . Default . ActionFileExtension ) ) {
98- ClearFile ( file ) ;
108+ if ( Directory . Exists ( CheckPath ( ) ) ) {
109+ foreach ( string file in Directory . GetFiles ( CheckPath ( ) , "*." + Properties . Settings . Default . ActionFileExtension ) ) {
110+ ClearFile ( file ) ;
111+ }
99112 }
100-
101- //WATCHER
102- FileSystemWatcher watcher = new FileSystemWatcher ( ) {
103- Path = CheckPath ( ) ,
104- NotifyFilter = NotifyFilters . LastAccess | NotifyFilters . LastWrite
105- | NotifyFilters . FileName | NotifyFilters . DirectoryName ,
106- Filter = "*." + Properties . Settings . Default . ActionFileExtension ,
107- EnableRaisingEvents = true
108- } ;
109- watcher . Changed += new FileSystemEventHandler ( ActionChecker . FileFound ) ;
110- //END WATCHER
113+
114+ SetupListener ( ) ;
111115
112116 DoDebug ( "\n [" + messageBoxTitle + "] Initiated. \n Listening in: \" " + CheckPath ( ) + "\" for \" ." + Properties . Settings . Default . ActionFileExtension + "\" extensions" ) ;
113117
@@ -138,11 +142,51 @@ static void Main(string[] args) {
138142 Application . Run ( ) ;
139143 }
140144
145+ private static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs args ) {
146+ Exception e = ( Exception ) args . ExceptionObject ;
147+ string errorLogLoc = Path . Combine ( dataFolderLocation , "error_log.txt" ) ;
148+
149+ if ( ! File . Exists ( errorLogLoc ) ) {
150+ using ( var tw = new StreamWriter ( errorLogLoc , true ) ) {
151+ tw . WriteLine ( e ) ;
152+ tw . Close ( ) ;
153+ }
154+ }
155+
156+ File . AppendAllText ( errorLogLoc , DateTime . Now . ToString ( ) + ": " + e + Environment . NewLine ) ;
157+ if ( debug ) {
158+ Console . WriteLine ( e ) ;
159+ }
160+ MessageBox . Show ( "An unhandled critical error occurred. Please contact the developer (https://github.com/AlbertMN/AssistantComputerControl/issues)" ) ;
161+ }
162+
163+ private static void CreateLogFile ( ) {
164+ using ( var tw = new StreamWriter ( logFilePath , true ) ) {
165+ tw . WriteLine ( string . Empty ) ;
166+ tw . Close ( ) ;
167+ }
168+ }
169+
170+ public static void SetupListener ( ) {
171+ if ( Directory . Exists ( CheckPath ( ) ) ) {
172+ watcher = new FileSystemWatcher ( ) {
173+ Path = CheckPath ( ) ,
174+ NotifyFilter = NotifyFilters . LastAccess | NotifyFilters . LastWrite
175+ | NotifyFilters . FileName | NotifyFilters . DirectoryName ,
176+ Filter = "*." + Properties . Settings . Default . ActionFileExtension ,
177+ EnableRaisingEvents = true
178+ } ;
179+ watcher . Changed += new FileSystemEventHandler ( ActionChecker . FileFound ) ;
180+ }
181+ }
182+
141183 public static void SetCheckFolder ( string setTo ) {
142184 SetRegKey ( "ActionFolder" , setTo ) ;
143185
144186 Properties . Settings . Default . ActionFilePath = setTo ;
145187 Properties . Settings . Default . Save ( ) ;
188+
189+ SetupListener ( ) ;
146190 }
147191 public static void SetCheckExtension ( string setTo ) {
148192 SetRegKey ( "ActionExtension" , setTo ) ;
@@ -151,7 +195,7 @@ public static void SetCheckExtension(string setTo) {
151195 Properties . Settings . Default . Save ( ) ;
152196 }
153197
154- private static void SetRegKey ( string theKey , string setTo ) {
198+ public static void SetRegKey ( string theKey , string setTo ) {
155199 RegistryKey key = Registry . CurrentUser . OpenSubKey ( "Software" , true ) ;
156200 if ( Registry . GetValue ( key . Name + "\\ AssistantComputerControl" , theKey , null ) == null ) {
157201 key . CreateSubKey ( "AssistantComputerControl" ) ;
@@ -292,11 +336,16 @@ public static string CheckPath() {
292336 }
293337
294338 public static void DoDebug ( string str ) {
295- if ( str != null && File . Exists ( logFilePath ) ) {
339+ try {
340+ if ( ! File . Exists ( logFilePath ) ) {
341+ CreateLogFile ( ) ;
342+ }
296343 File . AppendAllText ( logFilePath , DateTime . Now . ToString ( ) + ": " + str + Environment . NewLine ) ;
297344 if ( debug ) {
298345 Console . WriteLine ( str ) ;
299346 }
347+ } catch ( Exception e ) {
348+ Console . WriteLine ( "Failed to write to log, exception; " + e ) ;
300349 }
301350 }
302351
0 commit comments