General code review

This commit is contained in:
JustArchi
2015-11-18 14:28:46 +01:00
parent 7c3f5beb3a
commit 750c34189a
4 changed files with 41 additions and 42 deletions

View File

@@ -52,10 +52,12 @@ namespace ArchiSteamFarm {
PurchaseResult = (EPurchaseResult) body.purchase_result_details;
using (MemoryStream ms = new MemoryStream(body.purchase_receipt_info)) {
if (ReceiptInfo.TryReadAsBinary(ms)) {
foreach (KeyValue lineItem in ReceiptInfo["lineitems"].Children) {
Items.Add((uint) lineItem["PackageID"].AsUnsignedLong(), lineItem["ItemDescription"].AsString());
}
if (!ReceiptInfo.TryReadAsBinary(ms)) {
return;
}
foreach (KeyValue lineItem in ReceiptInfo["lineitems"].Children) {
Items.Add((uint) lineItem["PackageID"].AsUnsignedLong(), lineItem["ItemDescription"].AsString());
}
}
}
@@ -91,11 +93,13 @@ namespace ArchiSteamFarm {
internal void PlayGames(params ulong[] gameIDs) {
var request = new ClientMsgProtobuf<CMsgClientGamesPlayed>(EMsg.ClientGamesPlayed);
foreach (ulong gameID in gameIDs) {
if (gameID != 0) {
request.Body.games_played.Add(new CMsgClientGamesPlayed.GamePlayed {
game_id = new GameID(gameID),
});
if (gameID == 0) {
continue;
}
request.Body.games_played.Add(new CMsgClientGamesPlayed.GamePlayed {
game_id = new GameID(gameID),
});
}
Client.Send(request);
}
@@ -108,15 +112,17 @@ namespace ArchiSteamFarm {
}
public sealed override void HandleMsg(IPacketMsg packetMsg) {
if (packetMsg != null) {
switch (packetMsg.MsgType) {
case EMsg.ClientPurchaseResponse:
HandlePurchaseResponse(packetMsg);
break;
case EMsg.ClientUserNotifications:
HandleUserNotifications(packetMsg);
break;
}
if (packetMsg == null) {
return;
}
switch (packetMsg.MsgType) {
case EMsg.ClientPurchaseResponse:
HandlePurchaseResponse(packetMsg);
break;
case EMsg.ClientUserNotifications:
HandleUserNotifications(packetMsg);
break;
}
}

View File

@@ -24,6 +24,7 @@
using SteamKit2;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
@@ -34,7 +35,7 @@ namespace ArchiSteamFarm {
internal class Bot {
private const ushort CallbackSleep = 500; // In miliseconds
private static readonly Dictionary<string, Bot> Bots = new Dictionary<string, Bot>();
private static readonly ConcurrentDictionary<string, Bot> Bots = new ConcurrentDictionary<string, Bot>();
private readonly string ConfigFile;
private readonly string SentryFile;
@@ -67,19 +68,13 @@ namespace ArchiSteamFarm {
internal bool Statistics { get; private set; } = true;
internal static int GetRunningBotsCount() {
int result;
lock (Bots) {
result = Bots.Count;
}
return result;
}
return Bots.Count;
}
internal static async Task ShutdownAllBots() {
List<Task> tasks = new List<Task>();
lock (Bots) {
foreach (Bot bot in Bots.Values) {
tasks.Add(Task.Run(async () => await bot.Shutdown().ConfigureAwait(false)));
}
foreach (Bot bot in Bots.Values) {
tasks.Add(Task.Run(async () => await bot.Shutdown().ConfigureAwait(false)));
}
await Task.WhenAll(tasks).ConfigureAwait(false);
}
@@ -102,9 +97,7 @@ namespace ArchiSteamFarm {
return;
}
lock (Bots) {
Bots.Add(BotName, this);
}
Bots.AddOrUpdate(BotName, this, (key, value) => this);
// Initialize
SteamClient = new SteamClient();
@@ -203,7 +196,7 @@ namespace ArchiSteamFarm {
}
}
}
} catch (XmlException e) {
} catch (Exception e) {
Logging.LogGenericException(BotName, e);
Logging.LogGenericError(BotName, "Your config for this bot instance is invalid, it won't run!");
return false;
@@ -243,9 +236,7 @@ namespace ArchiSteamFarm {
}
await botToShutdown.Stop().ConfigureAwait(false);
lock (Bots) {
Bots.Remove(botNameToShutdown);
}
Bots.TryRemove(botNameToShutdown, out botToShutdown);
Program.OnBotShutdown(botToShutdown);
return true;

View File

@@ -106,7 +106,7 @@ namespace ArchiSteamFarm {
}
Logging.LogGenericInfo(Bot.BotName, "Farming in progress...");
NowFarming = true;
NowFarming = appIDs.Count > 0;
Semaphore.Release();
// Start farming

View File

@@ -39,19 +39,21 @@ namespace ArchiSteamFarm {
TwoFactorAuthentication,
}
internal const ulong ArchiSCFarmGroup = 103582791440160998;
internal const string ConfigDirectoryPath = "config";
private const string LatestGithubReleaseURL = "https://api.github.com/repos/JustArchi/ArchiSteamFarm/releases/latest";
internal static readonly object ConsoleLock = new object();
internal const ulong ArchiSCFarmGroup = 103582791440160998;
internal const string ConfigDirectoryPath = "config";
private static readonly SemaphoreSlim SteamSemaphore = new SemaphoreSlim(1);
private static readonly ManualResetEvent ShutdownResetEvent = new ManualResetEvent(false);
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
private static readonly string ExecutablePath = Assembly.Location;
private static readonly AssemblyName AssemblyName = Assembly.GetName();
private static readonly string ExeName = AssemblyName.Name + ".exe";
//private static readonly string ExeName = AssemblyName.Name + ".exe";
private static readonly string Version = AssemblyName.Version.ToString();
internal static readonly object ConsoleLock = new object();
private static async Task CheckForUpdate() {
JObject response = await Utilities.UrlToJObject(LatestGithubReleaseURL).ConfigureAwait(false);
if (response == null) {
@@ -69,11 +71,11 @@ namespace ArchiSteamFarm {
Logging.LogGenericNotice("", "Remote version: " + remoteVersion);
int comparisonResult = localVersion.CompareTo(remoteVersion);
if (localVersion.CompareTo(remoteVersion) < 0) {
if (comparisonResult < 0) {
Logging.LogGenericNotice("", "New version is available!");
Logging.LogGenericNotice("", "Consider updating yourself!");
await Utilities.SleepAsync(5000).ConfigureAwait(false);
} else if (localVersion.CompareTo(remoteVersion) > 0) {
} else if (comparisonResult > 0) {
Logging.LogGenericNotice("", "You're currently using pre-release version!");
Logging.LogGenericNotice("", "Be careful!");
}