Skip to content

Commit

Permalink
Implement update check on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Oct 29, 2015
1 parent abe3bea commit a0ba148
Show file tree
Hide file tree
Showing 21 changed files with 53,431 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ArchiSteamFarm/ArchiSteamFarm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<HintPath>..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.1-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="protobuf-net, Version=2.0.0.668, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
<HintPath>..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll</HintPath>
<Private>True</Private>
Expand Down
3 changes: 1 addition & 2 deletions ArchiSteamFarm/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal class Bot {
internal Trading Trading { get; private set; }

// Config variables
private bool Enabled { get { return bool.Parse(Config["Enabled"]); } }
internal bool Enabled { get { return bool.Parse(Config["Enabled"]); } }
private string SteamLogin { get { return Config["SteamLogin"]; } }
private string SteamPassword { get { return Config["SteamPassword"]; } }
private string SteamNickname { get { return Config["SteamNickname"]; } }
Expand All @@ -76,7 +76,6 @@ internal Bot(string botName) {
ReadConfig();

if (!Enabled) {
Logging.LogGenericInfo(BotName, "Not starting this instance, because it's disabled in config file");
return;
}

Expand Down
4 changes: 4 additions & 0 deletions ArchiSteamFarm/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ internal static void LogGenericInfo(string botName, string message, [CallerMembe
Log("[*] INFO: " + previousMethodName + "() <" + botName + "> " + message);
}

internal static void LogGenericNotice(string botName, string message, [CallerMemberName] string previousMethodName = "") {
Log("[*] NOTICE: " + previousMethodName + "() <" + botName + "> " + message);
}

[Conditional("DEBUG")]
internal static void LogGenericDebug(string botName, string message, [CallerMemberName] string previousMethodName = "") {
Log("[#] DEBUG: " + previousMethodName + "() <" + botName + "> " + message);
Expand Down
38 changes: 37 additions & 1 deletion ArchiSteamFarm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ limitations under the License.
*/

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace ArchiSteamFarm {
internal static class Program {
Expand All @@ -39,8 +42,33 @@ internal enum EUserInputType {

internal const ulong ArchiSCFarmGroup = 103582791440160998;
internal const string ConfigDirectoryPath = "config";
private const string LatestGithubReleaseURL = "https://api.github.com/repos/JustArchi/ArchiSteamFarm/releases/latest";

private static readonly HashSet<Bot> Bots = new HashSet<Bot>();
internal static readonly object ConsoleLock = new object();
internal static string Version { get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } }

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;

if (localVersion.CompareTo(remoteVersion) < 0) {
Logging.LogGenericNotice("", "New version is available!");
Logging.LogGenericNotice("", "Local version: " + localVersion);
Logging.LogGenericNotice("", "Remote version: " + remoteVersion);
Logging.LogGenericNotice("", "Consider updating yourself!");
Thread.Sleep(5000);
}
}

internal static void Exit(int exitCode = 0) {
ShutdownAllBots();
Expand Down Expand Up @@ -83,6 +111,10 @@ private static void ShutdownAllBots() {
}

private static void Main(string[] args) {
Logging.LogGenericInfo("Main", "Archi's Steam Farm, version " + Version);

Task.Run(async () => await CheckForUpdate().ConfigureAwait(false)).Wait();

// Config directory may not be in the same directory as the .exe, check maximum of 3 levels lower
for (var i = 0; i < 4 && !Directory.Exists(ConfigDirectoryPath); i++) {
Directory.SetCurrentDirectory("..");
Expand All @@ -97,7 +129,11 @@ private static void Main(string[] args) {
lock (Bots) {
foreach (var configFile in Directory.EnumerateFiles(ConfigDirectoryPath, "*.xml")) {
string botName = Path.GetFileNameWithoutExtension(configFile);
Bots.Add(new Bot(botName));
Bot bot = new Bot(botName);
Bots.Add(bot);
if (!bot.Enabled) {
Logging.LogGenericInfo(botName, "Not starting this instance because it's disabled in config file");
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions ArchiSteamFarm/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("0.4.0.0")]
[assembly: AssemblyFileVersion("0.4.0.0")]
30 changes: 29 additions & 1 deletion ArchiSteamFarm/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
*/

using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -62,6 +63,7 @@ internal static async Task<HttpResponseMessage> UrlToHttpResponse(string website
using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) {
using (HttpClient client = new HttpClient(clientHandler)) {
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0");
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, websiteAddress);
if (cookieVariables != null) {
StringBuilder cookie = new StringBuilder();
Expand Down Expand Up @@ -122,7 +124,8 @@ internal static async Task<bool> UrlPostRequest(string request, Dictionary<strin
try {
using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) {
using (HttpClient client = new HttpClient(clientHandler)) {
client.Timeout = TimeSpan.FromSeconds(15);
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0");
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, request);
requestMessage.Content = new FormUrlEncodedContent(postData);
if (cookieVariables != null && cookieVariables.Count > 0) {
Expand Down Expand Up @@ -159,6 +162,7 @@ internal static async Task<HttpResponseMessage> UrlPostRequestWithResponse(strin
using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) {
using (HttpClient client = new HttpClient(clientHandler)) {
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0");
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, request);
requestMessage.Content = new FormUrlEncodedContent(postData);
if (cookieVariables != null && cookieVariables.Count > 0) {
Expand All @@ -183,5 +187,29 @@ internal static async Task<HttpResponseMessage> UrlPostRequestWithResponse(strin

return result;
}

internal static async Task<JObject> UrlToJObject(string request) {
if (string.IsNullOrEmpty(request)) {
return null;
}

HttpResponseMessage httpResponseMessage = await UrlToHttpResponse(request, null).ConfigureAwait(false);
if (httpResponseMessage == null) {
return null;
}

string source = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
if (string.IsNullOrEmpty(source)) {
return null;
}

JObject result = null;
try {
result = JObject.Parse(source);
} catch {
}

return result;
}
}
}
7 changes: 4 additions & 3 deletions ArchiSteamFarm/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="protobuf-net" version="2.0.0.668" targetFramework="net45" />
<package id="SteamKit2" version="1.6.5" targetFramework="net45" />
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.1-beta1" targetFramework="net45" />
<package id="protobuf-net" version="2.0.0.668" targetFramework="net45" />
<package id="SteamKit2" version="1.6.5" targetFramework="net45" />
</packages>
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a0ba148

Please sign in to comment.