Skip to content

Commit f99d53a

Browse files
author
Casper
committed
Changes, bugfixes and error messages
Added better example Settings.json file and fixes some stuff and more responsive error messages
1 parent 016d72e commit f99d53a

3 files changed

Lines changed: 95 additions & 34 deletions

File tree

HourBoostr/BotClass.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ class BotClass
1717
/// <summary>
1818
/// Bot variables
1919
/// </summary>
20-
public List<int> _Games;
21-
private SteamClient _SteamClient;
22-
private SteamUser _SteamUser;
23-
private SteamUser.LogOnDetails _LogOnDetails;
24-
private CallbackManager _CBManager;
20+
public List<int> _Games;
21+
public SteamClient _SteamClient;
22+
private SteamUser _SteamUser;
23+
private SteamUser.LogOnDetails _LogOnDetails;
24+
private CallbackManager _CBManager;
2525

26-
private string _SentryPath;
27-
private string _AuthCode;
28-
private string _Nounce;
29-
public string _Username;
30-
private string _Password;
26+
private string _SentryPath;
27+
private string _AuthCode;
28+
private string _Nounce;
29+
public string _Username;
30+
private string _Password;
3131

32-
private bool _IsRunning;
33-
public bool _IsLoggedIn;
32+
public bool _IsRunning;
33+
public bool _IsLoggedIn;
3434

3535

3636
/// <summary>
3737
/// Thread variables
3838
/// </summary>
39-
private Thread _CBThread;
39+
private Thread _CBThread;
4040

4141

4242
/// <summary>
@@ -165,8 +165,7 @@ private void OnDisconnected(SteamClient.DisconnectedCallback callback)
165165
private void OnLoggedOn(SteamUser.LoggedOnCallback callback)
166166
{
167167
/*Fetch if SteamGuard is required*/
168-
bool isSteamGuard = callback.Result == EResult.AccountLogonDenied;
169-
if (isSteamGuard)
168+
if (callback.Result == EResult.AccountLogonDenied)
170169
{
171170
/*SteamGuard required, tell user to enter the code*/
172171
Log("This account is SteamGuard protected.");
@@ -178,15 +177,17 @@ private void OnLoggedOn(SteamUser.LoggedOnCallback callback)
178177
/*Something terrible has happened*/
179178
if(callback.Result != EResult.OK)
180179
{
180+
/*Incorrect password*/
181181
if(callback.Result == EResult.InvalidPassword)
182182
{
183-
Log("Incorrect password! Try again: ");
183+
Log(String.Format("{0} - Incorrect password! Try again:", callback.Result));
184184
_LogOnDetails.Password = Config.Password.ReadPassword();
185185
_SteamClient.Disconnect();
186186
return;
187187
}
188188

189-
Log("Something failed. Redo process please.");
189+
/*Something else happened that I didn't account for*/
190+
Log(String.Format("{0} - Something failed. Redo process please.", callback.Result));
190191
_IsLoggedIn = false;
191192
return;
192193
}
@@ -228,6 +229,7 @@ private void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback)
228229
/*Inform steam servers that we're accepting this sentry file*/
229230
_SteamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
230231
{
232+
/*Set the information recieved*/
231233
JobID = callback.JobID,
232234
FileName = callback.FileName,
233235

@@ -260,7 +262,7 @@ private void SetGamesPlaying()
260262
});
261263
}
262264

263-
/*Send the information*/
265+
/*Tell the client that we're playing these games*/
264266
_SteamClient.Send(gamesPlaying);
265267
}
266268
}

HourBoostr/Program.cs

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nGo 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.\nExiting.", "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
{

HourBoostr/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.2.0.0")]
36-
[assembly: AssemblyFileVersion("1.2.0.0")]
35+
[assembly: AssemblyVersion("1.3.0.0")]
36+
[assembly: AssemblyFileVersion("1.3.0.0")]

0 commit comments

Comments
 (0)