Skip to content

Commit

Permalink
R# cleanup, small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed May 19, 2019
1 parent ec95dc4 commit b98268a
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 296 deletions.
12 changes: 3 additions & 9 deletions ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void OnASFInit(IReadOnlyDictionary<string, JToken> additionalConfigProper

// ReSharper disable once UseDeconstruction - deconstruction is not available in .NET Framework
foreach (KeyValuePair<string, JToken> configProperty in additionalConfigProperties) {
// It's a good idea to prefix your custom properties with the name of your plugin, so there will be no possible conflict of ASF or other plugins using the same name, neither now or in the future
switch (configProperty.Key) {
// It's a good idea to prefix your custom properties with the name of your plugin, so there will be no possible conflict of ASF or other plugins using the same name, neither now or in the future
case nameof(ExamplePlugin) + "TestProperty" when configProperty.Value.Type == JTokenType.Boolean:
bool exampleBooleanValue = configProperty.Value.Value<bool>();
ASF.ArchiLogger.LogGenericInfo(nameof(ExamplePlugin) + "TestProperty boolean property has been found with a value of: " + exampleBooleanValue);
Expand All @@ -75,21 +75,15 @@ public void OnASFInit(IReadOnlyDictionary<string, JToken> additionalConfigProper
// If you do not recognize the command, just return null/empty and allow ASF to gracefully return "unknown command" to user on usual basis
public async Task<string> OnBotCommand(Bot bot, ulong steamID, string message, string[] args) {
// In comparison with OnBotMessage(), we're using asynchronous CatAPI call here, so we declare our method as async and return the message as usual
// Notice how we handle access here as well, it'll work only for FamilySharing+
switch (args[0].ToUpperInvariant()) {
// Notice how we handle access here as well, it'll work only for FamilySharing+
case "CAT" when bot.HasPermission(steamID, BotConfig.EPermission.FamilySharing):

// Notice how we can decide whether to use bot's AWH WebBrowser or ASF's one. For Steam-related requests, AWH's one should always be used, for third-party requests like those it doesn't really matter
// Still, it makes sense to pass AWH's one, so in case you get some errors or alike, you know from which bot instance they come from. It's similar to using Bot's ArchiLogger compared to ASF's one
string randomCatURL = await CatAPI.GetRandomCatURL(bot.ArchiWebHandler.WebBrowser).ConfigureAwait(false);

if (string.IsNullOrEmpty(randomCatURL)) {
return "God damn it, we're out of cats, care to notify my master? Thanks!";
}

return randomCatURL;
return !string.IsNullOrEmpty(randomCatURL) ? randomCatURL : "God damn it, we're out of cats, care to notify my master? Thanks!";
default:

return null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions ArchiSteamFarm.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ limitations under the License.</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EXml_002ECodeStyle_002EFormatSettingsUpgrade_002EXmlMoveToCommonFormatterSettingsUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Int64 x:Key="/Default/Environment/UnitTesting/ParallelProcessesCount/@EntryValue">8</s:Int64>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Archi/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=BGR_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=bot_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=broadcasted/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Domeradzki/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=splitted/@EntryIndexedValue">True</s:Boolean>
Expand Down
17 changes: 3 additions & 14 deletions ArchiSteamFarm/ASF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ internal static string GetFilePath(EFileType fileType) {

switch (fileType) {
case EFileType.Config:

return Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalConfigFileName);
case EFileType.Database:

return Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalDatabaseFileName);
default:
ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(fileType), fileType));
Expand Down Expand Up @@ -350,10 +348,8 @@ private static bool IsValidBotName(string botName) {

switch (botName) {
case SharedInfo.ASF:

return false;
default:

return true;
}
}
Expand Down Expand Up @@ -687,28 +683,22 @@ private static bool UpdateFromArchive(ZipArchive archive, string targetDirectory
ArchiLogger.LogNullError(nameof(relativeDirectoryName));

return false;

// No directory, root folder
case "":

// No directory, root folder
switch (fileName) {
// Files with those names in root directory we want to keep
case SharedInfo.LogFile:
case "NLog.config":

// Files with those names in root directory we want to keep
continue;
}

break;

// Files in those directories we want to keep in their current place
case SharedInfo.ConfigDirectory:
case SharedInfo.PluginsDirectory:
case SharedInfo.UpdateDirectory:

// Files in those directories we want to keep in their current place
continue;
default:

// Files in subdirectories of those directories we want to keep as well
if (Utilities.RelativeDirectoryStartsWith(relativeDirectoryName, SharedInfo.ConfigDirectory, SharedInfo.PluginsDirectory, SharedInfo.UpdateDirectory)) {
continue;
Expand Down Expand Up @@ -758,7 +748,6 @@ private static bool UpdateFromArchive(ZipArchive archive, string targetDirectory
// We're not interested in extracting placeholder files (but we still want directories created for them, done above)
switch (zipFile.Name) {
case ".gitkeep":

continue;
}
}
Expand Down
6 changes: 0 additions & 6 deletions ArchiSteamFarm/ArchiCryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ internal static string Decrypt(ECryptoMethod cryptoMethod, string encrypted) {

switch (cryptoMethod) {
case ECryptoMethod.PlainText:

return encrypted;
case ECryptoMethod.AES:

return DecryptAES(encrypted);
case ECryptoMethod.ProtectedDataForCurrentUser:

return DecryptProtectedDataForCurrentUser(encrypted);
default:
ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(cryptoMethod), cryptoMethod));
Expand All @@ -62,13 +59,10 @@ internal static string Encrypt(ECryptoMethod cryptoMethod, string decrypted) {

switch (cryptoMethod) {
case ECryptoMethod.PlainText:

return decrypted;
case ECryptoMethod.AES:

return EncryptAES(decrypted);
case ECryptoMethod.ProtectedDataForCurrentUser:

return EncryptProtectedDataForCurrentUser(decrypted);
default:
ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(cryptoMethod), cryptoMethod));
Expand Down
1 change: 0 additions & 1 deletion ArchiSteamFarm/ArchiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,6 @@ internal UserNotificationsCallback([NotNull] JobID jobID, [NotNull] CMsgClientUs
case EUserNotification.Items:
case EUserNotification.ModeratorMessages:
case EUserNotification.Trading:

break;
default:
ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(type), type));
Expand Down
9 changes: 0 additions & 9 deletions ArchiSteamFarm/ArchiWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2463,12 +2463,10 @@ private async Task<bool> RegisterApiKey() {

switch (result.State) {
case ESteamApiKeyState.AccessDenied:

// We succeeded in fetching API key, but it resulted in access denied
// Return empty result, API key is unavailable permanently
return (true, "");
case ESteamApiKeyState.NotRegisteredYet:

// We succeeded in fetching API key, and it resulted in no key registered yet
// Let's try to register a new key
if (!await RegisterApiKey().ConfigureAwait(false)) {
Expand All @@ -2491,16 +2489,13 @@ private async Task<bool> RegisterApiKey() {

goto case ESteamApiKeyState.Registered;
case ESteamApiKeyState.Registered:

// We succeeded in fetching API key, and it resulted in registered key
// Cache the result, this is the API key we want
return (true, result.Key);
case ESteamApiKeyState.Timeout:

// Request timed out, bad luck, we'll try again later
return (false, null);
default:

// We got an unhandled error, this should never happen
Bot.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(result.State), result.State));

Expand Down Expand Up @@ -2554,17 +2549,13 @@ private async Task<bool> RegisterApiKey() {
switch (userPrivacy.Settings.Profile) {
case Steam.UserPrivacy.PrivacySettings.EPrivacySetting.FriendsOnly:
case Steam.UserPrivacy.PrivacySettings.EPrivacySetting.Private:

return (true, false);
case Steam.UserPrivacy.PrivacySettings.EPrivacySetting.Public:

switch (userPrivacy.Settings.Inventory) {
case Steam.UserPrivacy.PrivacySettings.EPrivacySetting.FriendsOnly:
case Steam.UserPrivacy.PrivacySettings.EPrivacySetting.Private:

return (true, false);
case Steam.UserPrivacy.PrivacySettings.EPrivacySetting.Public:

return (true, true);
default:
Bot.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(userPrivacy.Settings.Inventory), userPrivacy.Settings.Inventory));
Expand Down
Loading

0 comments on commit b98268a

Please sign in to comment.