@@ -17,13 +17,16 @@ namespace HourBoostr
1717 class Program
1818 {
1919 /// <summary>
20- /// DllImports for hiding/showing window
20+ /// DllImports
2121 /// </summary>
2222 /// <returns></returns>
2323 [ DllImport ( "kernel32.dll" ) ]
2424 static extern IntPtr GetConsoleWindow ( ) ;
2525 [ DllImport ( "user32.dll" ) ]
2626 static extern bool ShowWindow ( IntPtr hWnd , int nCmdShow ) ;
27+ private delegate bool ConsoleEventDelegate ( int eventType ) ;
28+ [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
29+ private static extern bool SetConsoleCtrlHandler ( ConsoleEventDelegate callback , bool add ) ;
2730
2831
2932 /// <summary>
@@ -54,20 +57,20 @@ static private void Initialize()
5457 string _filePath = Path . Combine ( Application . StartupPath , "Settings.json" ) ;
5558 Console . Title = _Title ;
5659
57- /*Check if Json settings file exist*/
60+ /*Check if Json Settings file exist, else print a new one */
5861 if ( ! File . Exists ( _filePath ) )
5962 {
6063 /*Construct new classes to print an example settings file*/
6164 MessageBox . Show ( "Writing a new Settings.json\n Go and edit it!" , "Wizard" ) ;
6265 Config . SettingsInfo SettingsInfo = new Config . SettingsInfo ( )
6366 {
6467 Username = "" ,
65- Games = new List < int > { 730 }
68+ Games = new List < int > { 730 , 10 }
6669 } ;
6770 Config . Settings Settings = new Config . Settings ( )
6871 {
69- /*Print two json entries as an example*/
70- Account = new List < Config . SettingsInfo > { SettingsInfo , SettingsInfo }
72+ /*Print three json entries as an example*/
73+ Account = new List < Config . SettingsInfo > { SettingsInfo , SettingsInfo , SettingsInfo }
7174 } ;
7275
7376 /*Write the serialized class to file*/
@@ -82,7 +85,34 @@ static private void Initialize()
8285 else
8386 {
8487 /*Parse the Settings.json file*/
85- JObject Settings = JObject . Parse ( File . ReadAllText ( _filePath ) ) ;
88+ JObject Settings = null ;
89+ try
90+ {
91+ /*Try to parse the file*/
92+ Settings = JObject . Parse ( File . ReadAllText ( _filePath ) ) ;
93+ }
94+ catch ( JsonException jEx )
95+ {
96+ /*Error parsing the file. User messed up the syntax*/
97+ MessageBox . Show (
98+ "There was an error parsing Settings.json\n "
99+ + "You probably set it up incorrectly.\n \n "
100+ + "You can delete the Settings.json and run the program again\n "
101+ + "and it will spawn a new example file.\n \n "
102+ + "Error message:\n "
103+ + jEx . Message , "Json error" ) ;
104+
105+ /*Exit*/
106+ Environment . Exit ( 1 ) ;
107+ }
108+
109+ /*Fool check if Settings isn't null*/
110+ if ( Settings == null )
111+ {
112+ /*Don't know how this would normally proc, but never underestimate users*/
113+ MessageBox . Show ( "Settings is null.\n Exiting." , "Oops." ) ;
114+ Environment . Exit ( 1 ) ;
115+ }
86116
87117 /*Loop through all accounts set*/
88118 foreach ( var Account in Settings [ "Account" ] . Select ( ( value , i ) => new { i , value } ) )
@@ -102,7 +132,7 @@ static private void Initialize()
102132 } ;
103133
104134 /*If not empty*/
105- if ( User . Username . Length > 0 && User . Games != null )
135+ if ( User . Username . Length > 0 )
106136 {
107137 /*Let user type in password to account*/
108138 Console . WriteLine ( "Enter the password for the account '{0}'." , User . Username ) ;
@@ -111,20 +141,16 @@ static private void Initialize()
111141 /*Run a new bot with the information*/
112142 BotClass Bot = new BotClass ( User ) ;
113143
114- /*Add bot to active list*/
144+ /*Add bot to active bot list*/
115145 _ActiveBots . Add ( Bot ) ;
116146
117147 /*Wait for bot to log in fully until we initialize the next account*/
118148 while ( ! Bot . _IsLoggedIn ) { Thread . Sleep ( 2500 ) ; }
119149 }
120- else
121- {
122- /*Some information is missing, throw and error and exit app*/
123- Console . WriteLine ( String . Format ( "Account #{0} has invalid/missing information - skipping" , ( Account . i + 1 ) ) ) ;
124- }
125150 }
126151
127152 /*Done loading all bots*/
153+ /*Print some ascii stuff and information about bots*/
128154 Console . Clear ( ) ;
129155 Console . WriteLine ( "\n _____ _ _ " ) ;
130156 Console . WriteLine ( " | | |___ _ _ ___| |_ ___ ___ ___| |_ ___ " ) ;
@@ -151,6 +177,8 @@ static private void CheckStatus()
151177 {
152178 while ( true )
153179 {
180+ /*Get the current time then subtract the time when all bots were done initializing*/
181+ /*This will give us an idea of how long the bot has been running*/
154182 TimeSpan span = DateTime . Now . Subtract ( _InitializedTime ) ;
155183 Console . Title = String . Format ( "{0} | Online for: {1} Hours" , _Title , span . Hours ) ;
156184 Thread . Sleep ( 1000 ) ;
@@ -165,19 +193,21 @@ static private void CheckStatus()
165193 /// </summary>
166194 static private void ToTray ( )
167195 {
196+ /*Set TrayIcon information*/
168197 _TrayIcon . Text = String . Format ( "HourBoostr | {0} Bots" , _ActiveBots . Count ) ;
169198 _TrayIcon . Icon = Properties . Resources . icon ;
170199 _TrayIcon . Click += new EventHandler ( _TrayIcon_Click ) ;
171200 _TrayIcon . Visible = true ;
172201 Application . Run ( ) ;
173202
203+ /*Keep the form thread running, otherwise the TrayIcon will dissapear*/
174204 while ( true ) { Thread . Sleep ( 100 ) ; }
175205 }
176206
177207
178208 /// <summary>
179209 /// TrayIcon click event
180- /// Show/Hide the window depending on its' state
210+ /// Show/Hide the window depending on its state
181211 /// </summary>
182212 /// <param name="sender"></param>
183213 /// <param name="e"></param>
@@ -206,6 +236,30 @@ static private void ShowConsole(bool b)
206236 }
207237
208238
239+ /// <summary>
240+ /// Catch exit event
241+ /// Realistically we have 4-5 seconds to preform this
242+ /// action before windows forces the program to close
243+ /// </summary>
244+ /// <param name="eventType"></param>
245+ /// <returns></returns>
246+ static bool ConsoleEventCallback ( int eventType )
247+ {
248+ if ( eventType == 2 )
249+ {
250+ /*Disconnect all clients*/
251+ foreach ( var Bot in _ActiveBots )
252+ {
253+ /*Disconnect bot*/
254+ Bot . _IsRunning = false ;
255+ Bot . _SteamClient . Disconnect ( ) ;
256+ }
257+ }
258+ return false ;
259+ }
260+ static ConsoleEventDelegate handler ;
261+
262+
209263 /// <summary>
210264 /// Main function
211265 /// Too many comments
@@ -227,6 +281,7 @@ static void Main(string[] args)
227281 /*Hide console*/
228282 if ( _ActiveBots . Count > 0 )
229283 {
284+ /*Hide the program to Tray*/
230285 Console . WriteLine ( " Hiding console to Tray in 3s...\n \n " ) ;
231286 Thread . Sleep ( 3000 ) ;
232287 ShowConsole ( false ) ;
@@ -240,6 +295,10 @@ static void Main(string[] args)
240295 Environment . Exit ( 1 ) ;
241296 }
242297
298+ /*Set exit events*/
299+ handler = new ConsoleEventDelegate ( ConsoleEventCallback ) ;
300+ SetConsoleCtrlHandler ( handler , true ) ;
301+
243302 /*Keep it alive*/
244303 while ( true )
245304 {
0 commit comments