Skip to content

Commit a0ba148

Browse files
committed
Implement update check on startup
1 parent abe3bea commit a0ba148

File tree

21 files changed

+53431
-9
lines changed

21 files changed

+53431
-9
lines changed

ArchiSteamFarm/ArchiSteamFarm.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
<HintPath>..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
5757
<Private>True</Private>
5858
</Reference>
59+
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
60+
<HintPath>..\packages\Newtonsoft.Json.8.0.1-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
61+
<Private>True</Private>
62+
</Reference>
5963
<Reference Include="protobuf-net, Version=2.0.0.668, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
6064
<HintPath>..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll</HintPath>
6165
<Private>True</Private>

ArchiSteamFarm/Bot.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal class Bot {
5555
internal Trading Trading { get; private set; }
5656

5757
// Config variables
58-
private bool Enabled { get { return bool.Parse(Config["Enabled"]); } }
58+
internal bool Enabled { get { return bool.Parse(Config["Enabled"]); } }
5959
private string SteamLogin { get { return Config["SteamLogin"]; } }
6060
private string SteamPassword { get { return Config["SteamPassword"]; } }
6161
private string SteamNickname { get { return Config["SteamNickname"]; } }
@@ -76,7 +76,6 @@ internal Bot(string botName) {
7676
ReadConfig();
7777

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

ArchiSteamFarm/Logging.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ internal static void LogGenericInfo(string botName, string message, [CallerMembe
5050
Log("[*] INFO: " + previousMethodName + "() <" + botName + "> " + message);
5151
}
5252

53+
internal static void LogGenericNotice(string botName, string message, [CallerMemberName] string previousMethodName = "") {
54+
Log("[*] NOTICE: " + previousMethodName + "() <" + botName + "> " + message);
55+
}
56+
5357
[Conditional("DEBUG")]
5458
internal static void LogGenericDebug(string botName, string message, [CallerMemberName] string previousMethodName = "") {
5559
Log("[#] DEBUG: " + previousMethodName + "() <" + botName + "> " + message);

ArchiSteamFarm/Program.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ limitations under the License.
2222
2323
*/
2424

25+
using Newtonsoft.Json.Linq;
2526
using System;
2627
using System.Collections.Generic;
2728
using System.IO;
29+
using System.Reflection;
2830
using System.Threading;
31+
using System.Threading.Tasks;
2932

3033
namespace ArchiSteamFarm {
3134
internal static class Program {
@@ -39,8 +42,33 @@ internal enum EUserInputType {
3942

4043
internal const ulong ArchiSCFarmGroup = 103582791440160998;
4144
internal const string ConfigDirectoryPath = "config";
45+
private const string LatestGithubReleaseURL = "https://api.github.com/repos/JustArchi/ArchiSteamFarm/releases/latest";
46+
4247
private static readonly HashSet<Bot> Bots = new HashSet<Bot>();
4348
internal static readonly object ConsoleLock = new object();
49+
internal static string Version { get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } }
50+
51+
private static async Task CheckForUpdate() {
52+
JObject response = await Utilities.UrlToJObject(LatestGithubReleaseURL).ConfigureAwait(false);
53+
if (response == null) {
54+
return;
55+
}
56+
57+
string remoteVersion = response["tag_name"].ToString();
58+
if (string.IsNullOrEmpty(remoteVersion)) {
59+
return;
60+
}
61+
62+
string localVersion = Version;
63+
64+
if (localVersion.CompareTo(remoteVersion) < 0) {
65+
Logging.LogGenericNotice("", "New version is available!");
66+
Logging.LogGenericNotice("", "Local version: " + localVersion);
67+
Logging.LogGenericNotice("", "Remote version: " + remoteVersion);
68+
Logging.LogGenericNotice("", "Consider updating yourself!");
69+
Thread.Sleep(5000);
70+
}
71+
}
4472

4573
internal static void Exit(int exitCode = 0) {
4674
ShutdownAllBots();
@@ -83,6 +111,10 @@ private static void ShutdownAllBots() {
83111
}
84112

85113
private static void Main(string[] args) {
114+
Logging.LogGenericInfo("Main", "Archi's Steam Farm, version " + Version);
115+
116+
Task.Run(async () => await CheckForUpdate().ConfigureAwait(false)).Wait();
117+
86118
// Config directory may not be in the same directory as the .exe, check maximum of 3 levels lower
87119
for (var i = 0; i < 4 && !Directory.Exists(ConfigDirectoryPath); i++) {
88120
Directory.SetCurrentDirectory("..");
@@ -97,7 +129,11 @@ private static void Main(string[] args) {
97129
lock (Bots) {
98130
foreach (var configFile in Directory.EnumerateFiles(ConfigDirectoryPath, "*.xml")) {
99131
string botName = Path.GetFileNameWithoutExtension(configFile);
100-
Bots.Add(new Bot(botName));
132+
Bot bot = new Bot(botName);
133+
Bots.Add(bot);
134+
if (!bot.Enabled) {
135+
Logging.LogGenericInfo(botName, "Not starting this instance because it's disabled in config file");
136+
}
101137
}
102138
}
103139

ArchiSteamFarm/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("0.4.0.0")]
36+
[assembly: AssemblyFileVersion("0.4.0.0")]

ArchiSteamFarm/Utilities.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ limitations under the License.
2323
*/
2424

2525
using HtmlAgilityPack;
26+
using Newtonsoft.Json.Linq;
2627
using System;
2728
using System.Collections.Generic;
2829
using System.Globalization;
@@ -62,6 +63,7 @@ internal static async Task<HttpResponseMessage> UrlToHttpResponse(string website
6263
using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) {
6364
using (HttpClient client = new HttpClient(clientHandler)) {
6465
client.Timeout = TimeSpan.FromSeconds(10);
66+
client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0");
6567
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, websiteAddress);
6668
if (cookieVariables != null) {
6769
StringBuilder cookie = new StringBuilder();
@@ -122,7 +124,8 @@ internal static async Task<bool> UrlPostRequest(string request, Dictionary<strin
122124
try {
123125
using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) {
124126
using (HttpClient client = new HttpClient(clientHandler)) {
125-
client.Timeout = TimeSpan.FromSeconds(15);
127+
client.Timeout = TimeSpan.FromSeconds(10);
128+
client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0");
126129
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, request);
127130
requestMessage.Content = new FormUrlEncodedContent(postData);
128131
if (cookieVariables != null && cookieVariables.Count > 0) {
@@ -159,6 +162,7 @@ internal static async Task<HttpResponseMessage> UrlPostRequestWithResponse(strin
159162
using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) {
160163
using (HttpClient client = new HttpClient(clientHandler)) {
161164
client.Timeout = TimeSpan.FromSeconds(10);
165+
client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0");
162166
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, request);
163167
requestMessage.Content = new FormUrlEncodedContent(postData);
164168
if (cookieVariables != null && cookieVariables.Count > 0) {
@@ -183,5 +187,29 @@ internal static async Task<HttpResponseMessage> UrlPostRequestWithResponse(strin
183187

184188
return result;
185189
}
190+
191+
internal static async Task<JObject> UrlToJObject(string request) {
192+
if (string.IsNullOrEmpty(request)) {
193+
return null;
194+
}
195+
196+
HttpResponseMessage httpResponseMessage = await UrlToHttpResponse(request, null).ConfigureAwait(false);
197+
if (httpResponseMessage == null) {
198+
return null;
199+
}
200+
201+
string source = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
202+
if (string.IsNullOrEmpty(source)) {
203+
return null;
204+
}
205+
206+
JObject result = null;
207+
try {
208+
result = JObject.Parse(source);
209+
} catch {
210+
}
211+
212+
return result;
213+
}
186214
}
187215
}

ArchiSteamFarm/packages.config

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
4-
<package id="protobuf-net" version="2.0.0.668" targetFramework="net45" />
5-
<package id="SteamKit2" version="1.6.5" targetFramework="net45" />
3+
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
4+
<package id="Newtonsoft.Json" version="8.0.1-beta1" targetFramework="net45" />
5+
<package id="protobuf-net" version="2.0.0.668" targetFramework="net45" />
6+
<package id="SteamKit2" version="1.6.5" targetFramework="net45" />
67
</packages>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)