This commit is contained in:
JustArchi
2017-07-01 04:06:33 +02:00
parent 78bb754a6e
commit f2d527c0ec
8 changed files with 100 additions and 48 deletions

View File

@@ -26,6 +26,7 @@ using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Threading;
@@ -45,32 +46,47 @@ namespace ArchiSteamFarm {
private static FileSystemWatcher FileSystemWatcher;
internal static async Task CheckForUpdate(bool updateOverride = false) {
if (Debugging.IsDebugBuild && !updateOverride) {
if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.None) {
return;
}
string exeFile = Assembly.GetEntryAssembly().Location;
if (string.IsNullOrEmpty(exeFile)) {
ArchiLogger.LogNullError(nameof(exeFile));
string assemblyFile = Assembly.GetEntryAssembly().Location;
if (string.IsNullOrEmpty(assemblyFile)) {
ArchiLogger.LogNullError(nameof(assemblyFile));
return;
}
string oldExeFile = exeFile + ".old";
string oldAssemblyFile = assemblyFile + ".old";
// We booted successfully so we can now remove old exe file
if (File.Exists(oldExeFile)) {
if (File.Exists(oldAssemblyFile)) {
// It's entirely possible that old process is still running, allow at least a second before trying to remove the file
await Task.Delay(1000).ConfigureAwait(false);
try {
File.Delete(oldExeFile);
File.Delete(oldAssemblyFile);
} catch (Exception e) {
ArchiLogger.LogGenericException(e);
ArchiLogger.LogGenericError(string.Format(Strings.ErrorRemovingOldBinary, oldExeFile));
ArchiLogger.LogGenericError(string.Format(Strings.ErrorRemovingOldBinary, oldAssemblyFile));
}
}
if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.None) {
if (!File.Exists(SharedInfo.VersionFile)) {
ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsEmpty, SharedInfo.VersionFile));
return;
}
string version;
try {
version = await File.ReadAllTextAsync(SharedInfo.VersionFile).ConfigureAwait(false);
} catch (Exception e) {
ArchiLogger.LogGenericException(e);
return;
}
if (string.IsNullOrEmpty(version) || !IsVersionValid(version)) {
ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, SharedInfo.VersionFile));
return;
}
@@ -137,8 +153,8 @@ namespace ArchiSteamFarm {
return;
}
if (File.Exists(oldExeFile)) {
ArchiLogger.LogGenericError(string.Format(Strings.ErrorRemovingOldBinary, oldExeFile));
if (File.Exists(oldAssemblyFile)) {
ArchiLogger.LogGenericError(string.Format(Strings.ErrorRemovingOldBinary, oldAssemblyFile));
return;
}
@@ -148,11 +164,11 @@ namespace ArchiSteamFarm {
return;
}
string exeFileName = Path.GetFileName(exeFile);
GitHub.ReleaseResponse.Asset binaryAsset = releaseResponse.Assets.FirstOrDefault(asset => asset.Name.Equals(exeFileName, StringComparison.OrdinalIgnoreCase));
string targetFile = SharedInfo.ASF + "-" + version + ".zip";
GitHub.ReleaseResponse.Asset binaryAsset = releaseResponse.Assets.FirstOrDefault(asset => asset.Name.Equals(targetFile, StringComparison.OrdinalIgnoreCase));
if (binaryAsset == null) {
ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssetForThisBinary);
ArchiLogger.LogGenericWarning(Strings.ErrorUpdateNoAssetForThisVersion);
return;
}
@@ -168,42 +184,27 @@ namespace ArchiSteamFarm {
return;
}
string newExeFile = exeFile + ".new";
// Firstly we create new exec
try {
File.WriteAllBytes(newExeFile, result);
File.Move(assemblyFile, oldAssemblyFile);
} catch (Exception e) {
ArchiLogger.LogGenericException(e);
return;
}
// Now we move current -> old
try {
File.Move(exeFile, oldExeFile);
using (ZipArchive zipArchive = new ZipArchive(new MemoryStream(result))) {
UpdateFromArchive(zipArchive);
}
} catch (Exception e) {
ArchiLogger.LogGenericException(e);
try {
// Cleanup
File.Delete(newExeFile);
} catch {
// Ignored
}
return;
}
// Now we move new -> current
try {
File.Move(newExeFile, exeFile);
} catch (Exception e) {
ArchiLogger.LogGenericException(e);
try {
// Cleanup
File.Move(oldExeFile, exeFile);
File.Delete(newExeFile);
File.Move(oldAssemblyFile, assemblyFile);
} catch {
// Ignored
}
return;
}
@@ -272,6 +273,23 @@ namespace ArchiSteamFarm {
Bot.RegisterBot(botName);
}
private static bool IsVersionValid(string version) {
if (string.IsNullOrEmpty(version)) {
ArchiLogger.LogNullError(nameof(version));
return false;
}
switch (version) {
case "generic":
case "win-x64":
case "linux-x64":
case "osx-x64":
return true;
default:
return false;
}
}
private static async void OnChanged(object sender, FileSystemEventArgs e) {
if ((sender == null) || (e == null)) {
ArchiLogger.LogNullError(nameof(sender) + " || " + nameof(e));
@@ -412,6 +430,30 @@ namespace ArchiSteamFarm {
}
}
private static void UpdateFromArchive(ZipArchive archive) {
if (archive == null) {
ArchiLogger.LogNullError(nameof(archive));
return;
}
string targetDirectory = Directory.GetCurrentDirectory();
foreach (ZipArchiveEntry file in archive.Entries) {
string completeFileName = Path.Combine(targetDirectory, file.FullName);
string directory = Path.GetDirectoryName(completeFileName);
if (!Directory.Exists(directory)) {
Directory.CreateDirectory(directory);
}
if (string.IsNullOrEmpty(file.Name) || file.Name.Equals(SharedInfo.GlobalConfigFileName)) {
continue;
}
file.ExtractToFile(completeFileName, true);
}
}
internal sealed class BotConfigEventArgs : EventArgs {
internal readonly BotConfig BotConfig;

View File

@@ -955,11 +955,11 @@ namespace ArchiSteamFarm.Localization {
}
/// <summary>
/// Wyszukuje zlokalizowany ciąg podobny do ciągu Could not proceed with update because there is no asset that relates to currently running binary! Please ensure that your ASF binary is named appropriately!.
/// Wyszukuje zlokalizowany ciąg podobny do ciągu Could not proceed with update because there is no asset that relates to currently running version! Automatic update to that version is not possible..
/// </summary>
internal static string ErrorUpdateNoAssetForThisBinary {
internal static string ErrorUpdateNoAssetForThisVersion {
get {
return ResourceManager.GetString("ErrorUpdateNoAssetForThisBinary", resourceCulture);
return ResourceManager.GetString("ErrorUpdateNoAssetForThisVersion", resourceCulture);
}
}

View File

@@ -184,8 +184,8 @@ StackTrace:
<data name="ErrorUpdateCheckFailed" xml:space="preserve">
<value>Could not check latest version!</value>
</data>
<data name="ErrorUpdateNoAssetForThisBinary" xml:space="preserve">
<value>Could not proceed with update because there is no asset that relates to currently running binary! Please ensure that your ASF binary is named appropriately!</value>
<data name="ErrorUpdateNoAssetForThisVersion" xml:space="preserve">
<value>Could not proceed with update because there is no asset that relates to currently running version! Automatic update to that version is not possible.</value>
</data>
<data name="ErrorUpdateNoAssets" xml:space="preserve">
<value>Could not proceed with an update because that version doesn't include any assets!</value>

View File

@@ -181,7 +181,10 @@ namespace ArchiSteamFarm {
ParsePostInitArgs(args);
}
await ASF.CheckForUpdate().ConfigureAwait(false);
if (!Debugging.IsDebugBuild) {
await ASF.CheckForUpdate().ConfigureAwait(false);
}
await ASF.InitBots().ConfigureAwait(false);
ASF.InitEvents();
}

View File

@@ -30,15 +30,16 @@ namespace ArchiSteamFarm {
internal const ulong ArchiSteamID = 76561198006963719;
internal const string ASF = "ASF";
internal const ulong ASFGroupSteamID = 103582791440160998;
internal const string AssemblyName = "ArchiSteamFarm";
internal const string ConfigDirectory = "config";
internal const string DebugDirectory = "debug";
internal const string GithubReleaseURL = "https://api.github.com/repos/" + GithubRepo + "/releases"; // GitHub API is HTTPS only
internal const string GithubRepo = "JustArchi/ArchiSteamFarm";
internal const string GithubRepo = "JustArchi/" + AssemblyName;
internal const string GlobalConfigFileName = ASF + ".json";
internal const string GlobalDatabaseFileName = ASF + ".db";
internal const string LogFile = "log.txt";
internal const string ServiceName = "ArchiSteamFarm";
internal const string StatisticsServer = "asf.justarchi.net";
internal const string VersionFile = AssemblyName + ".version";
internal static readonly Version Version = Assembly.GetEntryAssembly().GetName().Version;
}

View File

@@ -58,7 +58,7 @@ namespace ArchiSteamFarm {
};
// Most web services expect that UserAgent is set, so we declare it globally
HttpClient.DefaultRequestHeaders.UserAgent.ParseAdd(SharedInfo.ServiceName + "/" + SharedInfo.Version);
HttpClient.DefaultRequestHeaders.UserAgent.ParseAdd(SharedInfo.AssemblyName + "/" + SharedInfo.Version);
}
internal static void Init() {