Work work

This commit is contained in:
JustArchi
2015-11-01 02:04:44 +01:00
parent 337c397505
commit 164240641b
8 changed files with 233 additions and 85 deletions

View File

@@ -113,6 +113,9 @@
</ItemGroup>
<ItemGroup>
<Content Include="cirno.ico" />
<Content Include="config\example.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
@@ -129,6 +132,14 @@ if $(ConfigurationName) == Release (
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PostBuildEvent>if $(ConfigurationName) == Release (
mkdir "$(TargetDir)out" "$(TargetDir)out\config"
copy "$(TargetDir)config\example.xml" "$(TargetDir)out\config"
"$(SolutionDir)tools\ILMerge.exe" /out:"$(TargetDir)out\ASF.exe" "$(TargetDir)$(TargetName).exe" "$(TargetDir)*.dll" /target:exe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework64\v4.0.30319 /wildcards
del "$(TargetDir)out\ASF.pdb"
)</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@@ -27,7 +27,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
@@ -35,7 +34,7 @@ namespace ArchiSteamFarm {
internal class Bot {
private const ushort CallbackSleep = 500; // In miliseconds
private static readonly HashSet<Bot> Bots = new HashSet<Bot>();
private static readonly Dictionary<string, Bot> Bots = new Dictionary<string, Bot>();
private readonly string ConfigFile;
private readonly string SentryFile;
@@ -78,7 +77,7 @@ namespace ArchiSteamFarm {
internal static async Task ShutdownAllBots() {
List<Task> tasks = new List<Task>();
lock (Bots) {
foreach (Bot bot in Bots) {
foreach (Bot bot in Bots.Values) {
tasks.Add(Task.Run(async () => await bot.Shutdown().ConfigureAwait(false)));
}
}
@@ -86,19 +85,25 @@ namespace ArchiSteamFarm {
}
internal Bot(string botName) {
if (Bots.ContainsKey(botName)) {
return;
}
BotName = botName;
ConfigFile = Path.Combine(Program.ConfigDirectoryPath, BotName + ".xml");
SentryFile = Path.Combine(Program.ConfigDirectoryPath, BotName + ".bin");
ReadConfig();
if (!ReadConfig()) {
return;
}
if (!Enabled) {
return;
}
lock (Bots) {
Bots.Add(this);
Bots.Add(BotName, this);
}
// Initialize
@@ -133,7 +138,11 @@ namespace ArchiSteamFarm {
Start();
}
private void ReadConfig() {
private bool ReadConfig() {
if (!File.Exists(ConfigFile)) {
return false;
}
using (XmlReader reader = XmlReader.Create(ConfigFile)) {
while (reader.Read()) {
if (reader.NodeType != XmlNodeType.Element) {
@@ -192,6 +201,8 @@ namespace ArchiSteamFarm {
}
}
}
return true;
}
internal void Start() {
@@ -215,12 +226,23 @@ namespace ArchiSteamFarm {
IsRunning = false;
}
internal async Task Shutdown() {
await Stop().ConfigureAwait(false);
lock (Bots) {
Bots.Remove(this);
private async Task<bool> Shutdown(string botNameToShutdown) {
Bot botToShutdown;
if (!Bots.TryGetValue(botNameToShutdown, out botToShutdown)) {
return false;
}
Program.OnBotShutdown(this);
await botToShutdown.Stop().ConfigureAwait(false);
lock (Bots) {
Bots.Remove(botNameToShutdown);
}
Program.OnBotShutdown(botToShutdown);
return true;
}
internal async Task<bool> Shutdown() {
return await Shutdown(BotName).ConfigureAwait(false);
}
internal async Task OnFarmingFinished() {
@@ -240,6 +262,63 @@ namespace ArchiSteamFarm {
}
}
private void SendMessageToUser(ulong steamID, string message) {
if (steamID == 0 || string.IsNullOrEmpty(message)) {
return;
}
SteamFriends.SendChatMessage(steamID, EChatEntryType.ChatMsg, message);
}
private void ResponseStatus(ulong steamID) {
if (steamID == 0) {
return;
}
SendMessageToUser(steamID, "Currently " + Bots.Count + " bots are running");
}
private void ResponseStart(ulong steamID, string botNameToStart) {
if (steamID == 0 || string.IsNullOrEmpty(botNameToStart)) {
return;
}
if (Bots.ContainsKey(botNameToStart)) {
SendMessageToUser(steamID, "That bot instance is already running!");
return;
}
new Bot(botNameToStart);
if (Bots.ContainsKey(botNameToStart)) {
SendMessageToUser(steamID, "Done!");
} else {
SendMessageToUser(steamID, "That bot instance failed to start, make sure that " + botNameToStart + ".xml config exists and bot is active!");
}
}
private async Task ResponseStop(ulong steamID, string botNameToShutdown) {
if (steamID == 0 || string.IsNullOrEmpty(botNameToShutdown)) {
return;
}
if (!Bots.ContainsKey(botNameToShutdown)) {
SendMessageToUser(steamID, "That bot instance is already inactive!");
return;
}
if (await Shutdown(botNameToShutdown).ConfigureAwait(false)) {
SendMessageToUser(steamID, "Done!");
} else {
SendMessageToUser(steamID, "That bot instance failed to shutdown!");
}
}
private void OnConnected(SteamClient.ConnectedCallback callback) {
if (callback == null) {
return;
@@ -275,17 +354,21 @@ namespace ArchiSteamFarm {
});
}
private void OnDisconnected(SteamClient.DisconnectedCallback callback) {
private async void OnDisconnected(SteamClient.DisconnectedCallback callback) {
if (callback == null) {
return;
}
if (callback.UserInitiated) {
return;
}
if (SteamClient == null) {
return;
}
Logging.LogGenericWarning(BotName, "Disconnected from Steam, reconnecting...");
Thread.Sleep(TimeSpan.FromMilliseconds(CallbackSleep));
await Utilities.SleepAsync(CallbackSleep).ConfigureAwait(false);
SteamClient.Connect();
}
@@ -336,15 +419,42 @@ namespace ArchiSteamFarm {
if (message.Length == 17 && message[5] == '-' && message[11] == '-') {
ArchiHandler.RedeemKey(message);
return;
}
switch (message) {
case "!farm":
await CardsFarmer.StartFarming().ConfigureAwait(false);
break;
case "!exit":
await Shutdown().ConfigureAwait(false);
break;
if (!message.StartsWith("!")) {
return;
}
if (!message.Contains(" ")) {
switch (message) {
case "!exit":
await ShutdownAllBots().ConfigureAwait(false);
break;
case "!farm":
await CardsFarmer.StartFarming().ConfigureAwait(false);
SendMessageToUser(steamID, "Done!");
break;
case "!restart":
await Program.Restart().ConfigureAwait(false);
break;
case "!status":
ResponseStatus(steamID);
break;
case "!stop":
await Shutdown().ConfigureAwait(false);
break;
}
} else {
string[] args = message.Split(' ');
switch (args[0]) {
case "!start":
ResponseStart(steamID, args[1]);
break;
case "!stop":
await ResponseStop(steamID, args[1]).ConfigureAwait(false);
break;
}
}
}
@@ -418,7 +528,7 @@ namespace ArchiSteamFarm {
case EResult.TryAnotherCM:
Logging.LogGenericWarning(BotName, "Unable to login to Steam: " + callback.Result + " / " + callback.ExtendedResult + ", retrying...");
await Stop().ConfigureAwait(false);
Thread.Sleep(5000);
await Utilities.SleepAsync(CallbackSleep).ConfigureAwait(false);
Start();
break;
default:
@@ -484,7 +594,7 @@ namespace ArchiSteamFarm {
}
var purchaseResult = callback.PurchaseResult;
SteamFriends.SendChatMessage(SteamMasterID, EChatEntryType.ChatMsg, "Status: " + purchaseResult);
SendMessageToUser(SteamMasterID, "Status: " + purchaseResult);
if (purchaseResult == ArchiHandler.PurchaseResponseCallback.EPurchaseResult.OK) {
await CardsFarmer.StartFarming().ConfigureAwait(false);

View File

@@ -132,7 +132,7 @@ namespace ArchiSteamFarm {
FarmResetEvent.Set();
while (NowFarming) {
Logging.LogGenericInfo(Bot.BotName, "Waiting for reaction...");
Thread.Sleep(1000);
await Utilities.SleepAsync(1000).ConfigureAwait(false);
}
FarmResetEvent.Reset();
Logging.LogGenericInfo(Bot.BotName, "Farming stopped!");

View File

@@ -26,7 +26,8 @@ using System.Diagnostics;
namespace ArchiSteamFarm {
internal static class Debugging {
internal static bool IsReleaseBuild { get; private set; } = true;
internal static bool IsDebugBuild { get; private set; } = false;
internal static bool IsReleaseBuild { get; private set; } = !IsDebugBuild;
static Debugging() {
MarkIfDebug();
@@ -34,7 +35,8 @@ namespace ArchiSteamFarm {
[Conditional("DEBUG")]
private static void MarkIfDebug() {
IsReleaseBuild = false;
IsDebugBuild = true;
IsReleaseBuild = !IsDebugBuild;
}
}
}

View File

@@ -45,7 +45,9 @@ namespace ArchiSteamFarm {
internal static readonly object ConsoleLock = new object();
private static readonly ManualResetEvent ShutdownResetEvent = new ManualResetEvent(false);
private static readonly AssemblyName AssemblyName = Assembly.GetExecutingAssembly().GetName();
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 Version = AssemblyName.Version.ToString();
@@ -69,7 +71,7 @@ namespace ArchiSteamFarm {
if (localVersion.CompareTo(remoteVersion) < 0) {
Logging.LogGenericNotice("", "New version is available!");
Logging.LogGenericNotice("", "Consider updating yourself!");
Thread.Sleep(5000);
await Utilities.SleepAsync(5000).ConfigureAwait(false);
} else if (localVersion.CompareTo(remoteVersion) > 0) {
Logging.LogGenericNotice("", "You're currently using pre-release version!");
Logging.LogGenericNotice("", "Be careful!");
@@ -81,6 +83,12 @@ namespace ArchiSteamFarm {
Environment.Exit(exitCode);
}
internal static async Task Restart() {
await Bot.ShutdownAllBots().ConfigureAwait(false);
System.Diagnostics.Process.Start(ExecutablePath);
Environment.Exit(0);
}
internal static string GetUserInput(string botLogin, EUserInputType userInputType) {
string result;
lock (ConsoleLock) {
@@ -107,10 +115,10 @@ namespace ArchiSteamFarm {
return result;
}
internal static void OnBotShutdown(Bot bot) {
internal static async void OnBotShutdown(Bot bot) {
if (Bot.GetRunningBotsCount() == 0) {
Logging.LogGenericInfo("Main", "No bots are running, exiting");
Thread.Sleep(5000); // This might be the only message user gets, consider giving him some time
await Utilities.SleepAsync(5000).ConfigureAwait(false); // This might be the only message user gets, consider giving him some time
ShutdownResetEvent.Set();
}
}
@@ -120,6 +128,16 @@ namespace ArchiSteamFarm {
Task.Run(async () => await CheckForUpdate().ConfigureAwait(false)).Wait();
// Allow loading configs from source tree if it's a debug build
if (Debugging.IsDebugBuild) {
for (var i = 0; i < 4; i++) {
Directory.SetCurrentDirectory("..");
if (Directory.Exists(ConfigDirectoryPath)) {
break;
}
}
}
if (!Directory.Exists(ConfigDirectoryPath)) {
Logging.LogGenericError("Main", "Config directory doesn't exist!");
Console.ReadLine();

View File

@@ -36,33 +36,32 @@ namespace ArchiSteamFarm {
Bot = bot;
}
internal void CheckTrades() {
internal async void CheckTrades() {
if (ParsingTasks < 2) {
ParsingTasks++;
Task.Run(() => ParseActiveTrades());
await Semaphore.WaitAsync().ConfigureAwait(false);
await ParseActiveTrades().ConfigureAwait(false);
Semaphore.Release();
ParsingTasks--;
}
}
private async Task ParseActiveTrades() {
await Semaphore.WaitAsync().ConfigureAwait(false);
List<SteamTradeOffer> tradeOffers = Bot.ArchiWebHandler.GetTradeOffers();
if (tradeOffers != null) {
List<Task> tasks = new List<Task>();
foreach (SteamTradeOffer tradeOffer in tradeOffers) {
if (tradeOffer.trade_offer_state == SteamTradeOffer.ETradeOfferState.Active) {
Task task = Task.Run(async () => {
await ParseTrade(tradeOffer).ConfigureAwait(false);
});
tasks.Add(task);
}
}
await Task.WhenAll(tasks).ConfigureAwait(false);
if (tradeOffers == null) {
return;
}
ParsingTasks--;
Semaphore.Release();
List<Task> tasks = new List<Task>();
foreach (SteamTradeOffer tradeOffer in tradeOffers) {
if (tradeOffer.trade_offer_state == SteamTradeOffer.ETradeOfferState.Active) {
tasks.Add(Task.Run(async () => await ParseTrade(tradeOffer).ConfigureAwait(false)));
}
}
await Task.WhenAll(tasks).ConfigureAwait(false);
}
private async Task ParseTrade(SteamTradeOffer tradeOffer) {

View File

@@ -35,6 +35,10 @@ using System.Threading.Tasks;
namespace ArchiSteamFarm {
internal static class Utilities {
internal static async Task SleepAsync(int miliseconds) {
await Task.Delay(miliseconds).ConfigureAwait(false);
}
internal static ulong OnlyNumbers(string inputString) {
if (string.IsNullOrEmpty(inputString)) {
return 0;

View File

@@ -1,53 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Every bot should have it's own unique .xml configuration file, this is example on which you can base on -->
<!-- Every bot should have it's own unique .xml configuration file, this is example on which you can base on -->
<!-- Master switch to turn account on and off, set to "true" after you're done -->
<!-- TIP: This bot instance won't run unless below switch is set to "true" -->
<Enabled type="bool" value="false"/>
<!-- Notice, if you use special characters reserved for XML, you should escape them -->
<!-- Escape table: [& - &amp;] | [" - &quot;] | [' - &apos;] | [< - &lt;] | [> - &gt;] -->
<!-- So e.g. if your SteamPassword is "foo&" you should write value="foo&amp;" -->
<!-- This is your steam login, the one you use for logging in to steam -->
<!-- TIP: You can use "null" if you wish to enter login on every startup -->
<SteamLogin type="string" value="null"/>
<!-- Master switch to turn account on and off, set to "true" after you're done -->
<!-- TIP: This bot instance won't run unless below switch is set to "true" -->
<Enabled type="bool" value="false"/>
<!-- This is your steam password, the one you use for logging in to steam -->
<!-- TIP: You can use "null" if you wish to enter password on every startup -->
<SteamPassword type="string" value="null"/>
<!-- This is your steam login, the one you use for logging in to steam -->
<!-- TIP: You can use "null" if you wish to enter login on every startup -->
<SteamLogin type="string" value="null"/>
<!-- This is steam nickname, the one you want to use for bot. Can be anything up to 32 characters -->
<!-- TIP: You can use "null" if you wish to preserve your actual nickname -->
<SteamNickname type="string" value="null"/>
<!-- This is your steam password, the one you use for logging in to steam -->
<!-- TIP: You can use "null" if you wish to enter password on every startup -->
<SteamPassword type="string" value="null"/>
<!-- This is your bot's API key, get one at https://steamcommunity.com/dev/apikey while logged in as bot, domain doesn't matter -->
<!-- TIP: You can use "null", but it will disable all API-based functionalities such as trading -->
<SteamApiKey type="string" value="null"/>
<!-- This is steam nickname, the one you want to use for bot. Can be anything up to 32 characters -->
<!-- TIP: You can use "null" if you wish to preserve your actual nickname -->
<SteamNickname type="string" value="null"/>
<!-- This is your parental PIN if you use steam parental functionality -->
<!-- TIP: Most likely you don't want to change it. You can use "null" if you wish to enter PIN on every startup, 0 means there is no PIN -->
<SteamParentalPIN type="string" value="0"/>
<!-- This is your bot's API key, get one at https://steamcommunity.com/dev/apikey while logged in as bot, domain doesn't matter -->
<!-- TIP: You can use "null", but it will disable all API-based functionalities such as trading -->
<SteamApiKey type="string" value="null"/>
<!-- This is steamID of the bot-master - you, in steamID64 format -->
<!-- TIP: You can use "0", but bot won't accept steam cd-keys or trades from anybody" -->
<SteamMasterID type="ulong" value="76561198006963719"/>
<!-- This is your parental PIN if you use steam parental functionality -->
<!-- TIP: Most likely you don't want to change it. You can use "null" if you wish to enter PIN on every startup, 0 means there is no PIN -->
<SteamParentalPIN type="string" value="0"/>
<!-- This defines clan of the master, bot will join chatroom of that clan automatically after logging in -->
<!-- TIP: Most likely you don't want to change it -->
<SteamMasterClanID type="ulong" value="0"/>
<!-- This is steamID of the bot-master - you, in steamID64 format -->
<!-- TIP: You can use "0", but bot won't accept steam cd-keys or trades from anybody" -->
<SteamMasterID type="ulong" value="76561198006963719"/>
<!-- This switch defines if bot should disconnect once farming is finished -->
<!-- When no bots are active, ASF will shutdown as well -->
<!-- Some people may want to keep their bots 24/7, other disconnect them after job is done -->
<!-- Choose yourself what you prefer -->
<ShutdownOnFarmingFinished type="bool" value="false"/>
<!-- This defines clan of the master, bot will join chatroom of that clan automatically after logging in -->
<!-- TIP: Most likely you don't want to change it -->
<SteamMasterClanID type="ulong" value="0"/>
<!-- Comma-separated list of IDs that should not be considered for farming -->
<!-- TIP: Most likely you don't want to change it -->
<Blacklist type="HashSet(uint)" value="368020"/>
<!-- This switch defines if bot should disconnect once farming is finished -->
<!-- When no bots are active, ASF will shutdown as well -->
<!-- Some people may want to keep their bots 24/7, other disconnect them after job is done -->
<!-- Choose yourself what you prefer -->
<ShutdownOnFarmingFinished type="bool" value="false"/>
<!-- This enables statistics for me - bot will join Archi's SC Farm group and chat -->
<!-- Consider leaving it at "true", this way I can check how many running bots are active -->
<!-- TIP: Group link is http://steamcommunity.com/groups/ascfarm -->
<Statistics type="bool" value="true"/>
<!-- Comma-separated list of IDs that should not be considered for farming -->
<!-- TIP: Most likely you don't want to change it -->
<Blacklist type="HashSet(uint)" value="368020"/>
<!-- This enables statistics for me - bot will join Archi's SC Farm group and chat -->
<!-- Consider leaving it at "true", this way I can check how many running bots are active -->
<!-- TIP: Group link is http://steamcommunity.com/groups/ascfarm -->
<Statistics type="bool" value="true"/>
</configuration>