From 1ad5d3676f45797c59fa83ad0e888db7c0aad12e Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 7 Mar 2016 02:39:55 +0100 Subject: [PATCH] Add GlobalDatabase --- ArchiSteamFarm/ArchiSteamFarm.csproj | 1 + ArchiSteamFarm/Bot.cs | 8 ++- ArchiSteamFarm/BotDatabase.cs | 8 +++ ArchiSteamFarm/GlobalDatabase.cs | 80 ++++++++++++++++++++++++++++ ArchiSteamFarm/Program.cs | 19 +++++-- 5 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 ArchiSteamFarm/GlobalDatabase.cs diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index c927126c0..3e3f968ce 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -102,6 +102,7 @@ + diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 6a00775ff..42a8ad5f5 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -74,12 +74,12 @@ namespace ArchiSteamFarm { return null; } - internal static async Task RefreshCMs() { + internal static async Task RefreshCMs(uint cellID) { bool initialized = false; for (byte i = 0; i < 3 && !initialized; i++) { try { Logging.LogGenericInfo("Refreshing list of CMs..."); - await SteamDirectory.Initialize().ConfigureAwait(false); + await SteamDirectory.Initialize(cellID).ConfigureAwait(false); initialized = true; } catch (Exception e) { Logging.LogGenericException(e); @@ -1152,6 +1152,10 @@ namespace ArchiSteamFarm { case EResult.OK: Logging.LogGenericInfo("Successfully logged on!", BotName); + if (callback.CellID != 0) { + Program.GlobalDatabase.CellID = callback.CellID; + } + if (BotConfig.UseAsfAsMobileAuthenticator && TwoFactorAuth == null && BotDatabase.SteamGuardAccount == null) { LinkMobileAuthenticator(); } diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index 29cbe80d4..00b1e72d9 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -34,6 +34,10 @@ namespace ArchiSteamFarm { return _LoginKey; } set { + if (_LoginKey == value) { + return; + } + _LoginKey = value; Save(); } @@ -44,6 +48,10 @@ namespace ArchiSteamFarm { return _SteamGuardAccount; } set { + if (_SteamGuardAccount == value) { + return; + } + _SteamGuardAccount = value; Save(); } diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs new file mode 100644 index 000000000..0834322d6 --- /dev/null +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -0,0 +1,80 @@ +/* + _ _ _ ____ _ _____ + / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ + / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ + / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| + + Copyright 2015-2016 Ł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. + +*/ + +using Newtonsoft.Json; +using SteamAuth; +using System; +using System.IO; + +namespace ArchiSteamFarm { + internal sealed class GlobalDatabase { + private static readonly string FilePath = Path.Combine(Program.ConfigDirectory, Program.GlobalDatabaseFile); + + internal uint CellID { + get { + return _CellID; + } + set { + if (_CellID == value) { + return; + } + + _CellID = value; + Save(); + } + } + + [JsonProperty(Required = Required.DisallowNull)] + private uint _CellID = 0; + + internal static GlobalDatabase Load() { + if (!File.Exists(FilePath)) { + return new GlobalDatabase(); + } + + GlobalDatabase globalDatabase; + try { + globalDatabase = JsonConvert.DeserializeObject(File.ReadAllText(FilePath)); + } catch (Exception e) { + Logging.LogGenericException(e); + return null; + } + + return globalDatabase; + } + + // This constructor is used only by deserializer + private GlobalDatabase() { } + + private void Save() { + lock (FilePath) { + try { + File.WriteAllText(FilePath, JsonConvert.SerializeObject(this)); + } catch (Exception e) { + Logging.LogGenericException(e); + } + } + } + } +} diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index f07ce9357..c62905ebe 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -49,9 +49,12 @@ namespace ArchiSteamFarm { } private const string LatestGithubReleaseURL = "https://api.github.com/repos/JustArchi/ArchiSteamFarm/releases/latest"; + + internal const string ASF = "ASF"; internal const string ConfigDirectory = "config"; internal const string LogFile = "log.txt"; - internal const string GlobalConfigFile = "ASF.json"; + internal const string GlobalConfigFile = ASF + ".json"; + internal const string GlobalDatabaseFile = ASF + ".db"; private static readonly object ConsoleLock = new object(); private static readonly SemaphoreSlim SteamSemaphore = new SemaphoreSlim(1); @@ -64,6 +67,7 @@ namespace ArchiSteamFarm { internal static readonly string Version = Assembly.GetName().Version.ToString(); internal static GlobalConfig GlobalConfig { get; private set; } + internal static GlobalDatabase GlobalDatabase { get; private set; } internal static bool ConsoleIsBusy { get; private set; } = false; private static EMode Mode = EMode.Normal; @@ -175,6 +179,13 @@ namespace ArchiSteamFarm { Exit(1); } + GlobalDatabase = GlobalDatabase.Load(); + if (GlobalDatabase == null) { + Logging.LogGenericError("Global database could not be loaded!"); + Thread.Sleep(5000); + Exit(1); + } + ArchiWebHandler.Init(); WebBrowser.Init(); WCF.Init(); @@ -274,13 +285,11 @@ namespace ArchiSteamFarm { Task.Run(async () => await CheckForUpdate().ConfigureAwait(false)).Wait(); // Before attempting to connect, initialize our list of CMs - Bot.RefreshCMs().Wait(); - - string globalConfigName = GlobalConfigFile.Substring(0, GlobalConfigFile.LastIndexOf('.')); + Bot.RefreshCMs(GlobalDatabase.CellID).Wait(); foreach (var configFile in Directory.EnumerateFiles(ConfigDirectory, "*.json")) { string botName = Path.GetFileNameWithoutExtension(configFile); - if (botName.Equals(globalConfigName)) { + if (botName.Equals(ASF)) { continue; }