From ef52e076d371458f5b40e5af15405141d7cee8bd Mon Sep 17 00:00:00 2001 From: JustArchi Date: Thu, 6 May 2021 20:28:25 +0200 Subject: [PATCH] Cleanup round --- .../GlobalCache.cs | 15 +++-- ArchiSteamFarm/ASF.cs | 56 +++++++++---------- ArchiSteamFarm/BotConfig.cs | 15 +++-- ArchiSteamFarm/BotDatabase.cs | 6 +- ArchiSteamFarm/Commands.cs | 3 +- ArchiSteamFarm/GlobalDatabase.cs | 8 ++- ArchiSteamFarm/IPC/ArchiKestrel.cs | 15 +++-- ArchiSteamFarm/IPC/Startup.cs | 8 ++- ArchiSteamFarm/NLog/ArchiLogger.cs | 5 +- ArchiSteamFarm/OS.cs | 1 + ArchiSteamFarm/SharedInfo.cs | 3 +- ArchiSteamFarm/Web/WebBrowser.cs | 2 +- 12 files changed, 70 insertions(+), 67 deletions(-) diff --git a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs index 5c0f2ea91..3abc83a04 100644 --- a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs +++ b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/GlobalCache.cs @@ -19,6 +19,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if NETFRAMEWORK +using ArchiSteamFarm.RuntimeCompatibility; +using File = System.IO.File; +using Path = System.IO.Path; +#else +using System.IO; +#endif using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -30,14 +37,6 @@ using ArchiSteamFarm.Localization; using Newtonsoft.Json; using SteamKit2; -#if NETFRAMEWORK -using ArchiSteamFarm.RuntimeCompatibility; -using File = System.IO.File; -using Path = System.IO.Path; -#else -using System.IO; -#endif - namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper { internal sealed class GlobalCache : SerializableFile { private static string SharedFilePath => Path.Combine(ArchiSteamFarm.SharedInfo.ConfigDirectory, nameof(SteamTokenDumper) + ".cache"); diff --git a/ArchiSteamFarm/ASF.cs b/ArchiSteamFarm/ASF.cs index c353da338..a8cb597d0 100644 --- a/ArchiSteamFarm/ASF.cs +++ b/ArchiSteamFarm/ASF.cs @@ -43,8 +43,6 @@ using ArchiSteamFarm.Web; using JetBrains.Annotations; using SteamKit2; using SteamKit2.Discovery; -using File = ArchiSteamFarm.RuntimeCompatibility.File; -using Path = ArchiSteamFarm.RuntimeCompatibility.Path; namespace ArchiSteamFarm { public static class ASF { @@ -94,8 +92,8 @@ namespace ArchiSteamFarm { } return fileType switch { - EFileType.Config => System.IO.Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalConfigFileName), - EFileType.Database => System.IO.Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalDatabaseFileName), + EFileType.Config => Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalConfigFileName), + EFileType.Database => Path.Combine(SharedInfo.ConfigDirectory, SharedInfo.GlobalDatabaseFileName), _ => throw new ArgumentOutOfRangeException(nameof(fileType)) }; } @@ -236,7 +234,7 @@ namespace ArchiSteamFarm { try { // If backup directory from previous update exists, it's a good idea to purge it now - string backupDirectory = System.IO.Path.Combine(SharedInfo.HomeDirectory, SharedInfo.UpdateDirectory); + string backupDirectory = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.UpdateDirectory); if (Directory.Exists(backupDirectory)) { ArchiLogger.LogGenericInfo(Strings.UpdateCleanup); @@ -364,9 +362,9 @@ namespace ArchiSteamFarm { } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - string executable = System.IO.Path.Combine(SharedInfo.HomeDirectory, SharedInfo.AssemblyName); + string executable = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.AssemblyName); - if (System.IO.File.Exists(executable)) { + if (File.Exists(executable)) { OS.UnixSetFileAccess(executable, OS.EUnixPermission.Combined755); } } @@ -508,7 +506,7 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(fullPath)); } - string extension = System.IO.Path.GetExtension(name); + string extension = Path.GetExtension(name); switch (extension) { case SharedInfo.JsonConfigExtension: @@ -564,7 +562,7 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(fullPath)); } - string extension = System.IO.Path.GetExtension(name); + string extension = Path.GetExtension(name); switch (extension) { case SharedInfo.IPCConfigExtension: @@ -587,7 +585,7 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(fullPath)); } - string extension = System.IO.Path.GetExtension(name); + string extension = Path.GetExtension(name); switch (extension) { case SharedInfo.JsonConfigExtension: @@ -615,7 +613,7 @@ namespace ArchiSteamFarm { throw new InvalidOperationException(nameof(Bot.Bots)); } - string botName = System.IO.Path.GetFileNameWithoutExtension(name); + string botName = Path.GetFileNameWithoutExtension(name); if (string.IsNullOrEmpty(botName) || (botName[0] == '.')) { return; @@ -660,7 +658,7 @@ namespace ArchiSteamFarm { throw new InvalidOperationException(nameof(Bot.Bots)); } - string botName = System.IO.Path.GetFileNameWithoutExtension(name); + string botName = Path.GetFileNameWithoutExtension(name); if (string.IsNullOrEmpty(botName) || (botName[0] == '.')) { return; @@ -706,7 +704,7 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(fullPath)); } - string extension = System.IO.Path.GetExtension(name); + string extension = Path.GetExtension(name); switch (extension) { case SharedInfo.IPCConfigExtension: @@ -729,7 +727,7 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(fullPath)); } - string extension = System.IO.Path.GetExtension(name); + string extension = Path.GetExtension(name); switch (extension) { case SharedInfo.JsonConfigExtension: @@ -753,7 +751,7 @@ namespace ArchiSteamFarm { throw new InvalidOperationException(nameof(Bot.Bots)); } - string botName = System.IO.Path.GetFileNameWithoutExtension(name); + string botName = Path.GetFileNameWithoutExtension(name); if (string.IsNullOrEmpty(botName)) { return; @@ -764,7 +762,7 @@ namespace ArchiSteamFarm { } if (botName.Equals(SharedInfo.ASF, StringComparison.OrdinalIgnoreCase)) { - if (System.IO.File.Exists(fullPath)) { + if (File.Exists(fullPath)) { return; } @@ -772,7 +770,7 @@ namespace ArchiSteamFarm { // If that's the case, we wait for maximum of 5 seconds before shutting down await Task.Delay(5000).ConfigureAwait(false); - if (System.IO.File.Exists(fullPath)) { + if (File.Exists(fullPath)) { return; } @@ -857,7 +855,7 @@ namespace ArchiSteamFarm { HashSet botNames; try { - botNames = Directory.EnumerateFiles(SharedInfo.ConfigDirectory, "*" + SharedInfo.JsonConfigExtension).Select(System.IO.Path.GetFileNameWithoutExtension).Where(botName => !string.IsNullOrEmpty(botName) && IsValidBotName(botName)).ToHashSet(Bot.BotsComparer)!; + botNames = Directory.EnumerateFiles(SharedInfo.ConfigDirectory, "*" + SharedInfo.JsonConfigExtension).Select(Path.GetFileNameWithoutExtension).Where(botName => !string.IsNullOrEmpty(botName) && IsValidBotName(botName)).ToHashSet(Bot.BotsComparer)!; } catch (Exception e) { ArchiLogger.LogGenericException(e); @@ -926,10 +924,10 @@ namespace ArchiSteamFarm { } // Firstly we'll move all our existing files to a backup directory - string backupDirectory = System.IO.Path.Combine(targetDirectory, SharedInfo.UpdateDirectory); + string backupDirectory = Path.Combine(targetDirectory, SharedInfo.UpdateDirectory); foreach (string file in Directory.EnumerateFiles(targetDirectory, "*", SearchOption.AllDirectories)) { - string fileName = System.IO.Path.GetFileName(file); + string fileName = Path.GetFileName(file); if (string.IsNullOrEmpty(fileName)) { ArchiLogger.LogNullError(nameof(fileName)); @@ -937,7 +935,7 @@ namespace ArchiSteamFarm { return false; } - string relativeFilePath = Path.GetRelativePath(targetDirectory, file); + string relativeFilePath = RuntimeCompatibility.Path.GetRelativePath(targetDirectory, file); if (string.IsNullOrEmpty(relativeFilePath)) { ArchiLogger.LogNullError(nameof(relativeFilePath)); @@ -945,7 +943,7 @@ namespace ArchiSteamFarm { return false; } - string? relativeDirectoryName = System.IO.Path.GetDirectoryName(relativeFilePath); + string? relativeDirectoryName = Path.GetDirectoryName(relativeFilePath); switch (relativeDirectoryName) { case null: @@ -978,11 +976,11 @@ namespace ArchiSteamFarm { break; } - string targetBackupDirectory = relativeDirectoryName.Length > 0 ? System.IO.Path.Combine(backupDirectory, relativeDirectoryName) : backupDirectory; + string targetBackupDirectory = relativeDirectoryName.Length > 0 ? Path.Combine(backupDirectory, relativeDirectoryName) : backupDirectory; Directory.CreateDirectory(targetBackupDirectory); - string targetBackupFile = System.IO.Path.Combine(targetBackupDirectory, fileName); - File.Move(file, targetBackupFile, true); + string targetBackupFile = Path.Combine(targetBackupDirectory, fileName); + RuntimeCompatibility.File.Move(file, targetBackupFile, true); } // We can now get rid of directories that are empty @@ -994,17 +992,17 @@ namespace ArchiSteamFarm { // Now enumerate over files in the zip archive, skip directory entries that we're not interested in (we can create them ourselves if needed) foreach (ZipArchiveEntry zipFile in archive.Entries.Where(zipFile => !string.IsNullOrEmpty(zipFile.Name))) { - string file = System.IO.Path.Combine(targetDirectory, zipFile.FullName); + string file = Path.Combine(targetDirectory, zipFile.FullName); - if (System.IO.File.Exists(file)) { + if (File.Exists(file)) { // This is possible only with files that we decided to leave in place during our backup function string targetBackupFile = file + ".bak"; - File.Move(file, targetBackupFile, true); + RuntimeCompatibility.File.Move(file, targetBackupFile, true); } // Check if this file requires its own folder if (zipFile.Name != zipFile.FullName) { - string? directory = System.IO.Path.GetDirectoryName(file); + string? directory = Path.GetDirectoryName(file); if (string.IsNullOrEmpty(directory)) { ArchiLogger.LogNullError(nameof(directory)); diff --git a/ArchiSteamFarm/BotConfig.cs b/ArchiSteamFarm/BotConfig.cs index 526536b5f..ffb13e0b8 100644 --- a/ArchiSteamFarm/BotConfig.cs +++ b/ArchiSteamFarm/BotConfig.cs @@ -19,6 +19,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if NETFRAMEWORK +using ArchiSteamFarm.RuntimeCompatibility; + +using File = System.IO.File; +#else +using System.IO; +#endif using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -34,14 +41,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SteamKit2; -#if NETFRAMEWORK -using ArchiSteamFarm.RuntimeCompatibility; - -using File = System.IO.File; -#else -using System.IO; -#endif - namespace ArchiSteamFarm { [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] public sealed class BotConfig { diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index 7f4516057..6623f981d 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -23,13 +23,13 @@ using System; using System.Collections; using System.Collections.Specialized; using System.Globalization; +using System.IO; using System.Linq; using System.Threading.Tasks; using ArchiSteamFarm.Collections; using ArchiSteamFarm.Helpers; using ArchiSteamFarm.Localization; using Newtonsoft.Json; -using File = ArchiSteamFarm.RuntimeCompatibility.File; namespace ArchiSteamFarm { internal sealed class BotDatabase : SerializableFile { @@ -141,14 +141,14 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(filePath)); } - if (!System.IO.File.Exists(filePath)) { + if (!File.Exists(filePath)) { return new BotDatabase(filePath); } BotDatabase? botDatabase; try { - string json = await File.ReadAllTextAsync(filePath).ConfigureAwait(false); + string json = await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false); if (string.IsNullOrEmpty(json)) { ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json))); diff --git a/ArchiSteamFarm/Commands.cs b/ArchiSteamFarm/Commands.cs index 2110d825e..b2b1bfc9f 100644 --- a/ArchiSteamFarm/Commands.cs +++ b/ArchiSteamFarm/Commands.cs @@ -31,11 +31,10 @@ using ArchiSteamFarm.Callbacks; using ArchiSteamFarm.Json; using ArchiSteamFarm.Localization; using ArchiSteamFarm.Plugins; +using ArchiSteamFarm.RuntimeCompatibility; using JetBrains.Annotations; using SteamKit2; -using ArchiSteamFarm.RuntimeCompatibility; - namespace ArchiSteamFarm { public sealed class Commands { private const ushort SteamTypingStatusDelay = 10 * 1000; // Steam client broadcasts typing status each 10 seconds diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index 61132b9b2..a2c8f8e11 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -32,10 +32,12 @@ using ArchiSteamFarm.Localization; using ArchiSteamFarm.SteamKit2; using JetBrains.Annotations; using Newtonsoft.Json; -using File = ArchiSteamFarm.RuntimeCompatibility.File; #if NETFRAMEWORK using ArchiSteamFarm.RuntimeCompatibility; +using File = System.IO.File; +#else +using System.IO; #endif namespace ArchiSteamFarm { @@ -122,7 +124,7 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(filePath)); } - if (!System.IO.File.Exists(filePath)) { + if (!File.Exists(filePath)) { GlobalDatabase result = new(filePath); Utilities.InBackground(result.Save); @@ -133,7 +135,7 @@ namespace ArchiSteamFarm { GlobalDatabase? globalDatabase; try { - string json = await File.ReadAllTextAsync(filePath).ConfigureAwait(false); + string json = await RuntimeCompatibility.File.ReadAllTextAsync(filePath).ConfigureAwait(false); if (string.IsNullOrEmpty(json)) { ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json))); diff --git a/ArchiSteamFarm/IPC/ArchiKestrel.cs b/ArchiSteamFarm/IPC/ArchiKestrel.cs index eb9b08bf0..65ec24507 100644 --- a/ArchiSteamFarm/IPC/ArchiKestrel.cs +++ b/ArchiSteamFarm/IPC/ArchiKestrel.cs @@ -19,6 +19,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if NETFRAMEWORK +using ArchiSteamFarm.RuntimeCompatibility; +using File = System.IO.File; +using Path = System.IO.Path; +#else +using Microsoft.Extensions.Hosting; +#endif using System; using System.IO; using System.Threading.Tasks; @@ -32,14 +39,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NLog.Web; -#if NETFRAMEWORK -using ArchiSteamFarm.RuntimeCompatibility; -using File = System.IO.File; -using Path = System.IO.Path; -#else -using Microsoft.Extensions.Hosting; -#endif - namespace ArchiSteamFarm.IPC { internal static class ArchiKestrel { internal static HistoryTarget? HistoryTarget { get; private set; } diff --git a/ArchiSteamFarm/IPC/Startup.cs b/ArchiSteamFarm/IPC/Startup.cs index 37228b295..a38d7ff0a 100644 --- a/ArchiSteamFarm/IPC/Startup.cs +++ b/ArchiSteamFarm/IPC/Startup.cs @@ -42,6 +42,10 @@ using Newtonsoft.Json.Serialization; #if NETFRAMEWORK using ArchiSteamFarm.RuntimeCompatibility; using Newtonsoft.Json.Converters; +using File = System.IO.File; +using Path = System.IO.Path; +#else +using System.IO; #endif namespace ArchiSteamFarm.IPC { @@ -229,9 +233,9 @@ namespace ArchiSteamFarm.IPC { } ); - string xmlDocumentationFile = System.IO.Path.Combine(AppContext.BaseDirectory, SharedInfo.AssemblyDocumentation); + string xmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, SharedInfo.AssemblyDocumentation); - if (System.IO.File.Exists(xmlDocumentationFile)) { + if (File.Exists(xmlDocumentationFile)) { options.IncludeXmlComments(xmlDocumentationFile); } } diff --git a/ArchiSteamFarm/NLog/ArchiLogger.cs b/ArchiSteamFarm/NLog/ArchiLogger.cs index 699c6c3fb..fc31b845c 100644 --- a/ArchiSteamFarm/NLog/ArchiLogger.cs +++ b/ArchiSteamFarm/NLog/ArchiLogger.cs @@ -25,6 +25,7 @@ using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using ArchiSteamFarm.Localization; +using ArchiSteamFarm.RuntimeCompatibility; using JetBrains.Annotations; using NLog; using SteamKit2; @@ -173,7 +174,7 @@ namespace ArchiSteamFarm.NLog { string message = string.Format(CultureInfo.CurrentCulture, DateTime.Now + " " + Strings.ErrorEarlyFatalExceptionInfo, SharedInfo.Version) + Environment.NewLine; try { - await RuntimeCompatibility.File.WriteAllTextAsync(SharedInfo.LogFile, message).ConfigureAwait(false); + await File.WriteAllTextAsync(SharedInfo.LogFile, message).ConfigureAwait(false); } catch { // Ignored, we can't do anything about this } @@ -188,7 +189,7 @@ namespace ArchiSteamFarm.NLog { message = string.Format(CultureInfo.CurrentCulture, Strings.ErrorEarlyFatalExceptionPrint, previousMethodName, exception.Message, exception.StackTrace) + Environment.NewLine; try { - await RuntimeCompatibility.File.AppendAllTextAsync(SharedInfo.LogFile, message).ConfigureAwait(false); + await File.AppendAllTextAsync(SharedInfo.LogFile, message).ConfigureAwait(false); } catch { // Ignored, we can't do anything about this } diff --git a/ArchiSteamFarm/OS.cs b/ArchiSteamFarm/OS.cs index 0f690bd36..bb42814e4 100644 --- a/ArchiSteamFarm/OS.cs +++ b/ArchiSteamFarm/OS.cs @@ -193,6 +193,7 @@ namespace ArchiSteamFarm { _ => false }; #else + // This is .NET Core build, we support all scenarios return true; #endif diff --git a/ArchiSteamFarm/SharedInfo.cs b/ArchiSteamFarm/SharedInfo.cs index 765d02e77..83b7c42d4 100644 --- a/ArchiSteamFarm/SharedInfo.cs +++ b/ArchiSteamFarm/SharedInfo.cs @@ -20,6 +20,7 @@ // limitations under the License. using System; +using System.IO; using System.Reflection; using ArchiSteamFarm.Plugins; using JetBrains.Annotations; @@ -74,7 +75,7 @@ namespace ArchiSteamFarm { // We can't just return our base directory since it could lead to the (wrong) temporary directory of extracted files in a single-publish scenario // If the path goes to our own binary, the user is using OS-specific build, single-file or not, we'll use path to location of that binary then // Otherwise, this path goes to some third-party binary, likely dotnet/mono, the user is using our generic build or other custom binary, we need to trust our base directory then - CachedHomeDirectory = System.IO.Path.GetFileNameWithoutExtension(OS.ProcessFileName) == AssemblyName ? System.IO.Path.GetDirectoryName(OS.ProcessFileName) ?? AppContext.BaseDirectory : AppContext.BaseDirectory; + CachedHomeDirectory = Path.GetFileNameWithoutExtension(OS.ProcessFileName) == AssemblyName ? Path.GetDirectoryName(OS.ProcessFileName) ?? AppContext.BaseDirectory : AppContext.BaseDirectory; return CachedHomeDirectory; } diff --git a/ArchiSteamFarm/Web/WebBrowser.cs b/ArchiSteamFarm/Web/WebBrowser.cs index a5fc57ed6..c83ad7e2b 100644 --- a/ArchiSteamFarm/Web/WebBrowser.cs +++ b/ArchiSteamFarm/Web/WebBrowser.cs @@ -31,9 +31,9 @@ using System.Threading.Tasks; using System.Xml; using ArchiSteamFarm.Localization; using ArchiSteamFarm.NLog; +using ArchiSteamFarm.RuntimeCompatibility; using JetBrains.Annotations; using Newtonsoft.Json; -using ArchiSteamFarm.RuntimeCompatibility; namespace ArchiSteamFarm.Web { public sealed class WebBrowser : IDisposable {