Files
ArchiSteamFarm/ArchiSteamFarm/Program.cs

170 lines
6.2 KiB
C#
Raw Normal View History

2015-10-28 19:21:27 +01:00
/*
_ _ _ ____ _ _____
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
Copyright 2015 Łukasz "JustArchi" Domeradzki
Contact: JustArchi@JustArchi.net
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2015-10-29 17:36:16 +01:00
using Newtonsoft.Json.Linq;
2015-10-28 19:21:27 +01:00
using System;
2015-10-25 06:16:50 +01:00
using System.IO;
2015-10-29 17:36:16 +01:00
using System.Reflection;
2015-10-25 06:16:50 +01:00
using System.Threading;
2015-10-29 17:36:16 +01:00
using System.Threading.Tasks;
2015-10-25 06:16:50 +01:00
namespace ArchiSteamFarm {
internal static class Program {
internal enum EUserInputType {
Login,
Password,
SteamGuard,
SteamParentalPIN,
TwoFactorAuthentication,
}
2015-10-29 16:38:16 +01:00
internal const ulong ArchiSCFarmGroup = 103582791440160998;
2015-10-25 06:16:50 +01:00
internal const string ConfigDirectoryPath = "config";
2015-10-29 17:36:16 +01:00
private const string LatestGithubReleaseURL = "https://api.github.com/repos/JustArchi/ArchiSteamFarm/releases/latest";
2015-10-25 06:16:50 +01:00
internal static readonly object ConsoleLock = new object();
2015-11-04 04:31:27 +01:00
private static readonly SemaphoreSlim SteamSemaphore = new SemaphoreSlim(1);
2015-10-31 06:09:03 +01:00
private static readonly ManualResetEvent ShutdownResetEvent = new ManualResetEvent(false);
2015-11-01 02:04:44 +01:00
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
private static readonly string ExecutablePath = Assembly.Location;
2015-11-01 02:08:41 +01:00
private static readonly AssemblyName AssemblyName = Assembly.GetName();
2015-10-31 06:09:03 +01:00
private static readonly string ExeName = AssemblyName.Name + ".exe";
private static readonly string Version = AssemblyName.Version.ToString();
2015-10-29 17:36:16 +01:00
private static async Task CheckForUpdate() {
JObject response = await Utilities.UrlToJObject(LatestGithubReleaseURL).ConfigureAwait(false);
if (response == null) {
return;
}
string remoteVersion = response["tag_name"].ToString();
if (string.IsNullOrEmpty(remoteVersion)) {
return;
}
string localVersion = Version;
2015-10-31 06:09:03 +01:00
Logging.LogGenericNotice("", "Local version: " + localVersion);
Logging.LogGenericNotice("", "Remote version: " + remoteVersion);
int comparisonResult = localVersion.CompareTo(remoteVersion);
2015-10-29 17:36:16 +01:00
if (localVersion.CompareTo(remoteVersion) < 0) {
Logging.LogGenericNotice("", "New version is available!");
Logging.LogGenericNotice("", "Consider updating yourself!");
2015-11-01 02:04:44 +01:00
await Utilities.SleepAsync(5000).ConfigureAwait(false);
2015-10-31 05:27:30 +01:00
} else if (localVersion.CompareTo(remoteVersion) > 0) {
Logging.LogGenericNotice("", "You're currently using pre-release version!");
Logging.LogGenericNotice("", "Be careful!");
2015-10-29 17:36:16 +01:00
}
}
2015-10-25 06:16:50 +01:00
2015-10-31 05:27:30 +01:00
internal static async Task Exit(int exitCode = 0) {
await Bot.ShutdownAllBots().ConfigureAwait(false);
2015-10-25 06:16:50 +01:00
Environment.Exit(exitCode);
}
2015-11-01 02:04:44 +01:00
internal static async Task Restart() {
await Bot.ShutdownAllBots().ConfigureAwait(false);
System.Diagnostics.Process.Start(ExecutablePath);
Environment.Exit(0);
}
2015-11-04 04:31:27 +01:00
internal static async Task LimitSteamRequestsAsync() {
await SteamSemaphore.WaitAsync().ConfigureAwait(false);
2015-11-04 18:30:38 +01:00
await Utilities.SleepAsync(5 * 1000).ConfigureAwait(false); // We must add some delay to not get caught by Steam anty-DoS
2015-11-04 04:31:27 +01:00
SteamSemaphore.Release();
}
internal static string GetUserInput(string botLogin, EUserInputType userInputType) {
2015-10-28 20:01:43 +01:00
string result;
2015-10-25 06:16:50 +01:00
lock (ConsoleLock) {
switch (userInputType) {
case EUserInputType.Login:
Console.Write("<" + botLogin + "> Please enter your login: ");
break;
case EUserInputType.Password:
Console.Write("<" + botLogin + "> Please enter your password: ");
break;
case EUserInputType.SteamGuard:
Console.Write("<" + botLogin + "> Please enter the auth code sent to your email: ");
break;
case EUserInputType.SteamParentalPIN:
Console.Write("<" + botLogin + "> Please enter steam parental PIN: ");
break;
case EUserInputType.TwoFactorAuthentication:
Console.Write("<" + botLogin + "> Please enter your 2 factor auth code from your authenticator app: ");
break;
2015-10-25 06:16:50 +01:00
}
2015-10-28 20:01:43 +01:00
result = Console.ReadLine();
Console.Clear(); // For security purposes
2015-10-25 06:16:50 +01:00
}
2015-11-04 18:18:04 +01:00
result = result.Trim(); // Get rid of all whitespace characters
2015-10-28 20:01:43 +01:00
return result;
2015-10-25 06:16:50 +01:00
}
2015-11-01 02:04:44 +01:00
internal static async void OnBotShutdown(Bot bot) {
if (Bot.GetRunningBotsCount() == 0) {
Logging.LogGenericInfo("Main", "No bots are running, exiting");
2015-11-01 02:04:44 +01:00
await Utilities.SleepAsync(5000).ConfigureAwait(false); // This might be the only message user gets, consider giving him some time
ShutdownResetEvent.Set();
2015-10-25 06:16:50 +01:00
}
}
private static void Main(string[] args) {
2015-10-29 17:36:16 +01:00
Logging.LogGenericInfo("Main", "Archi's Steam Farm, version " + Version);
Task.Run(async () => await CheckForUpdate().ConfigureAwait(false)).Wait();
2015-11-01 02:04:44 +01:00
// 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;
}
}
}
2015-10-25 06:16:50 +01:00
if (!Directory.Exists(ConfigDirectoryPath)) {
Logging.LogGenericError("Main", "Config directory doesn't exist!");
Console.ReadLine();
2015-10-31 05:27:30 +01:00
Task.Run(async () => await Exit(1).ConfigureAwait(false)).Wait();
2015-10-31 06:09:03 +01:00
}
2015-10-25 06:16:50 +01:00
foreach (var configFile in Directory.EnumerateFiles(ConfigDirectoryPath, "*.xml")) {
string botName = Path.GetFileNameWithoutExtension(configFile);
Bot bot = new Bot(botName);
if (!bot.Enabled) {
Logging.LogGenericInfo(botName, "Not starting this instance because it's disabled in config file");
2015-10-25 06:16:50 +01:00
}
}
// Check if we got any bots running
OnBotShutdown(null);
ShutdownResetEvent.WaitOne();
2015-10-25 06:16:50 +01:00
}
}
}