From 4a36345635b2b9dc493f95a280da9b7ab85daf92 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 12 Apr 2016 19:12:45 +0200 Subject: [PATCH] Code review --- ArchiSteamFarm/ArchiHandler.cs | 2 +- ArchiSteamFarm/ArchiWebHandler.cs | 2 +- ArchiSteamFarm/Bot.cs | 2 +- ArchiSteamFarm/BotDatabase.cs | 4 ++++ ArchiSteamFarm/CardsFarmer.cs | 2 +- ArchiSteamFarm/GlobalConfig.cs | 7 ++++-- ArchiSteamFarm/GlobalDatabase.cs | 31 ++++++++++++++++++++----- ArchiSteamFarm/Program.cs | 4 ++-- ArchiSteamFarm/Trading.cs | 3 ++- ArchiSteamFarm/WebBrowser.cs | 2 +- ConfigGenerator/ASFConfig.cs | 4 ++++ ConfigGenerator/BotConfig.cs | 5 +++- ConfigGenerator/EnhancedPropertyGrid.cs | 2 +- ConfigGenerator/GlobalConfig.cs | 5 +++- ConfigGenerator/MainForm.cs | 7 ++++++ ConfigGenerator/Tutorial.cs | 10 ++++---- 16 files changed, 68 insertions(+), 24 deletions(-) diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index 6e4c2e159..fe8bb3bd4 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -36,7 +36,7 @@ namespace ArchiSteamFarm { internal ArchiHandler(Bot bot) { if (bot == null) { - return; + throw new ArgumentNullException("bot"); } Bot = bot; diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index fb8c66568..45b5990be 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -56,7 +56,7 @@ namespace ArchiSteamFarm { internal ArchiWebHandler(Bot bot) { if (bot == null) { - return; + throw new ArgumentNullException("bot"); } Bot = bot; diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 754825b38..2d593fdb5 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -107,7 +107,7 @@ namespace ArchiSteamFarm { internal Bot(string botName) { if (string.IsNullOrEmpty(botName)) { - return; + throw new ArgumentNullException("botName"); } BotName = botName; diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index 52c909267..3deb31012 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -92,6 +92,10 @@ namespace ArchiSteamFarm { // This constructor is used when creating new database private BotDatabase(string filePath) { + if (string.IsNullOrEmpty(filePath)) { + throw new ArgumentNullException("filePath"); + } + FilePath = filePath; Save(); } diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index e1bc09dba..bacbde9e9 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -48,7 +48,7 @@ namespace ArchiSteamFarm { internal CardsFarmer(Bot bot) { if (bot == null) { - return; + throw new ArgumentNullException("bot"); } Bot = bot; diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index 2a1f5c2f1..02592184d 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -107,8 +107,11 @@ namespace ArchiSteamFarm { [JsonProperty(Required = Required.DisallowNull)] internal HashSet Blacklist { get; private set; } = new HashSet(GlobalBlacklist); - internal static GlobalConfig Load() { - string filePath = Path.Combine(Program.ConfigDirectory, Program.GlobalConfigFile); + internal static GlobalConfig Load(string filePath) { + if (string.IsNullOrEmpty(filePath)) { + return null; + } + if (!File.Exists(filePath)) { return null; } diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index a74fc362c..634186a00 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -28,8 +28,6 @@ using System.IO; namespace ArchiSteamFarm { internal sealed class GlobalDatabase { - private static readonly string FilePath = Path.Combine(Program.ConfigDirectory, Program.GlobalDatabaseFile); - internal uint CellID { get { return _CellID; @@ -47,22 +45,43 @@ namespace ArchiSteamFarm { [JsonProperty(Required = Required.DisallowNull)] private uint _CellID; - internal static GlobalDatabase Load() { - if (!File.Exists(FilePath)) { - return new GlobalDatabase(); + private string FilePath; + + internal static GlobalDatabase Load(string filePath) { + if (string.IsNullOrEmpty(filePath)) { + return null; + } + + if (!File.Exists(filePath)) { + return new GlobalDatabase(filePath); } GlobalDatabase globalDatabase; try { - globalDatabase = JsonConvert.DeserializeObject(File.ReadAllText(FilePath)); + globalDatabase = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { Logging.LogGenericException(e); return null; } + if (globalDatabase == null) { + return null; + } + + globalDatabase.FilePath = filePath; return globalDatabase; } + // This constructor is used when creating new database + private GlobalDatabase(string filePath) { + if (string.IsNullOrEmpty(filePath)) { + throw new ArgumentNullException("filePath"); + } + + FilePath = filePath; + Save(); + } + // This constructor is used only by deserializer private GlobalDatabase() { } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 7fcf82263..f12d370d9 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -353,14 +353,14 @@ namespace ArchiSteamFarm { } private static void InitServices() { - GlobalConfig = GlobalConfig.Load(); + GlobalConfig = GlobalConfig.Load(Path.Combine(ConfigDirectory, GlobalConfigFile)); if (GlobalConfig == null) { Logging.LogGenericError("Global config could not be loaded, please make sure that ASF.json exists and is valid!"); Thread.Sleep(5000); Exit(1); } - GlobalDatabase = GlobalDatabase.Load(); + GlobalDatabase = GlobalDatabase.Load(Path.Combine(ConfigDirectory, GlobalDatabaseFile)); if (GlobalDatabase == null) { Logging.LogGenericError("Global database could not be loaded!"); Thread.Sleep(5000); diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 38e8632ca..6b6891b35 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -23,6 +23,7 @@ */ using SteamAuth; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -49,7 +50,7 @@ namespace ArchiSteamFarm { internal Trading(Bot bot) { if (bot == null) { - return; + throw new ArgumentNullException("bot"); } Bot = bot; diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index f6346b87b..c265b7c83 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -65,7 +65,7 @@ namespace ArchiSteamFarm { internal WebBrowser(string identifier) { if (string.IsNullOrEmpty(identifier)) { - return; + throw new ArgumentNullException("identifier"); } Identifier = identifier; diff --git a/ConfigGenerator/ASFConfig.cs b/ConfigGenerator/ASFConfig.cs index edf8e1c14..d7c18d6e2 100644 --- a/ConfigGenerator/ASFConfig.cs +++ b/ConfigGenerator/ASFConfig.cs @@ -38,6 +38,10 @@ namespace ConfigGenerator { } protected ASFConfig(string filePath) : this() { + if (string.IsNullOrEmpty(filePath)) { + throw new ArgumentNullException("filePath"); + } + FilePath = filePath; } diff --git a/ConfigGenerator/BotConfig.cs b/ConfigGenerator/BotConfig.cs index 379f9c5c2..4327af510 100644 --- a/ConfigGenerator/BotConfig.cs +++ b/ConfigGenerator/BotConfig.cs @@ -128,7 +128,10 @@ namespace ConfigGenerator { private BotConfig() { } private BotConfig(string filePath) : base(filePath) { - FilePath = filePath; + if (string.IsNullOrEmpty(filePath)) { + throw new ArgumentNullException("filePath"); + } + GamesPlayedWhileIdle.Add(0); Save(); } diff --git a/ConfigGenerator/EnhancedPropertyGrid.cs b/ConfigGenerator/EnhancedPropertyGrid.cs index 3fae90b11..e77f0cfb1 100644 --- a/ConfigGenerator/EnhancedPropertyGrid.cs +++ b/ConfigGenerator/EnhancedPropertyGrid.cs @@ -31,7 +31,7 @@ namespace ConfigGenerator { internal EnhancedPropertyGrid(ASFConfig config) { if (config == null) { - return; + throw new ArgumentNullException("config"); } ASFConfig = config; diff --git a/ConfigGenerator/GlobalConfig.cs b/ConfigGenerator/GlobalConfig.cs index f15caec7b..161cde0a4 100644 --- a/ConfigGenerator/GlobalConfig.cs +++ b/ConfigGenerator/GlobalConfig.cs @@ -170,7 +170,10 @@ namespace ConfigGenerator { private GlobalConfig() { } private GlobalConfig(string filePath) : base(filePath) { - FilePath = filePath; + if (string.IsNullOrEmpty(filePath)) { + throw new ArgumentNullException("filePath"); + } + Blacklist.AddRange(GlobalBlacklist); Save(); } diff --git a/ConfigGenerator/MainForm.cs b/ConfigGenerator/MainForm.cs index 25b3e664b..a7523716f 100644 --- a/ConfigGenerator/MainForm.cs +++ b/ConfigGenerator/MainForm.cs @@ -26,6 +26,7 @@ using System; using System.ComponentModel; using System.Diagnostics; using System.IO; +using System.Text.RegularExpressions; using System.Windows.Forms; namespace ConfigGenerator { @@ -121,6 +122,9 @@ namespace ConfigGenerator { return; } + // Get rid of any potential whitespaces in bot name + input = Regex.Replace(input, @"\s+", ""); + configPage.ASFConfig.Rename(input); configPage.RefreshText(); } else if (e.TabPage == NewTab) { @@ -144,6 +148,9 @@ namespace ConfigGenerator { return; } + // Get rid of any potential whitespaces in bot name + input = Regex.Replace(input, @"\s+", ""); + foreach (ASFConfig config in ASFConfig.ASFConfigs) { if (Path.GetFileNameWithoutExtension(config.FilePath).Equals(input)) { Logging.LogGenericError("Bot with such name exists already!"); diff --git a/ConfigGenerator/Tutorial.cs b/ConfigGenerator/Tutorial.cs index 9aace47a8..d837db379 100644 --- a/ConfigGenerator/Tutorial.cs +++ b/ConfigGenerator/Tutorial.cs @@ -35,7 +35,7 @@ namespace ConfigGenerator { BotEnabled, BotReady, GlobalConfigOpened, - GlobalConfigReady, + GlobalConfigReady } internal static bool Enabled { get; set; } = true; @@ -61,20 +61,20 @@ namespace ConfigGenerator { Logging.LogGenericInfo("Please click the help button to continue."); break; case EPhase.Help: - Logging.LogGenericInfo("That's right! On ASF wiki you can find detailed help about every config property you're going to configure in a moment."); + Logging.LogGenericInfo("Well done! On ASF wiki you can find detailed help about every config property you're going to configure in a moment."); break; case EPhase.HelpFinished: Logging.LogGenericInfo("Alright, let's start configuring our ASF. Click on the plus [+] button to add your first steam account to ASF!"); break; case EPhase.BotNickname: - Logging.LogGenericInfo("That's right! You'll be asked for your bot name now. A good example would be a nickname that you're using for the steam account you're configuring right now, or any other name of your choice which will be easy for you to connect with bot instance that is being configured."); + Logging.LogGenericInfo("Good job! You'll be asked for your bot name now. A good example would be a nickname that you're using for the steam account you're configuring right now, or any other name of your choice which will be easy for you to connect with bot instance that is being configured. Please don't use spaces in the name."); break; case EPhase.BotNicknameFinished: Logging.LogGenericInfo("As you can see your bot config is now ready to configure!"); Logging.LogGenericInfo("First thing that you want to do is switching \"Enabled\" property from False to True, try it!"); break; case EPhase.BotEnabled: - Logging.LogGenericInfo("That's right! Now your bot instance is enabled. You need to configure at least 2 more config properties - \"SteamLogin\" and \"SteamPassword\". The tutorial will continue after you're done with it. Remember to visit ASF wiki by clicking the help icon if you're unsure how given property should be configured!"); + Logging.LogGenericInfo("Excellent! Now your bot instance is enabled. You need to configure at least 2 more config properties - \"SteamLogin\" and \"SteamPassword\". The tutorial will continue after you're done with it. Remember to visit ASF wiki by clicking the help icon if you're unsure how given property should be configured!"); break; case EPhase.BotReady: Logging.LogGenericInfo("If the data you put is proper, then your bot is ready to run! We need to do only one more thing now. Visit global ASF config, which is labelled as \"ASF\" on your config tab."); @@ -86,7 +86,7 @@ namespace ConfigGenerator { case EPhase.GlobalConfigReady: Logging.LogGenericInfo("Your ASF is now ready! Simply launch ASF process by double-clicking ASF.exe binary and if you did everything properly, you should now notice that ASF logs in on your account and starts farming. If you have SteamGuard or 2FA authorization enabled, ASF will ask you for that once"); Logging.LogGenericInfo("Congratulations! You've done everything that is needed in order to make ASF \"work\". I highly recommend reading the wiki now, as ASF offers some really neat features for you to configure, such as offline farming or deciding upon most efficient cards farming algorithm."); - Logging.LogGenericInfo("If you'd like to add another steam account for farming, simply click the plus [+] button and add another instance. You can also rename bots and remove them with 2 other buttons. Good luck!"); + Logging.LogGenericInfo("If you'd like to add another steam account for farming, simply click the plus [+] button and add another instance. You can also rename bots [~] and remove them [-]. Good luck!"); Enabled = false; break; }