From 1f4a4cc6b7bfb9e98f90879995dc49ef2bd62307 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 27 Jun 2016 18:44:48 +0200 Subject: [PATCH] Database saving hardening, #265 --- ArchiSteamFarm/BotDatabase.cs | 36 +++++++++++++++++++++----------- ArchiSteamFarm/GlobalDatabase.cs | 36 +++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index 09b48c1ab..369da081e 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -26,6 +26,7 @@ using Newtonsoft.Json; using System; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Threading.Tasks; namespace ArchiSteamFarm { internal sealed class BotDatabase { @@ -42,7 +43,7 @@ namespace ArchiSteamFarm { } _LoginKey = value; - Save(); + Save().Wait(); } } @@ -59,7 +60,7 @@ namespace ArchiSteamFarm { } _MobileAuthenticator = value; - Save(); + Save().Wait(); } } @@ -77,7 +78,7 @@ namespace ArchiSteamFarm { } _SteamGuardAccount = value; - Save(); + Save().Wait(); } } @@ -118,21 +119,32 @@ namespace ArchiSteamFarm { } FilePath = filePath; - Save(); + Save().Wait(); } // This constructor is used only by deserializer [SuppressMessage("ReSharper", "UnusedMember.Local")] private BotDatabase() { } - internal void Save() { - lock (FilePath) { - try { - File.WriteAllText(FilePath, JsonConvert.SerializeObject(this)); - } catch (Exception e) { - Logging.LogGenericException(e); - } - } + internal async Task Save() { + string json = JsonConvert.SerializeObject(this); + if (string.IsNullOrEmpty(json)) { + Logging.LogNullError(nameof(json)); + return; + } + + for (byte i = 0; i < 5; i++) { + lock (FilePath) { + try { + File.WriteAllText(FilePath, json); + break; + } catch (Exception e) { + Logging.LogGenericException(e); + } + } + + await Task.Delay(1000).ConfigureAwait(false); + } } } } diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index edfffdb9a..50909ee94 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -26,6 +26,7 @@ using Newtonsoft.Json; using System; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Threading.Tasks; namespace ArchiSteamFarm { internal sealed class GlobalDatabase { @@ -39,7 +40,7 @@ namespace ArchiSteamFarm { } _CellID = value; - Save(); + Save().Wait(); } } @@ -83,21 +84,32 @@ namespace ArchiSteamFarm { } FilePath = filePath; - Save(); + Save().Wait(); } // This constructor is used only by deserializer [SuppressMessage("ReSharper", "UnusedMember.Local")] private GlobalDatabase() { } - private void Save() { - lock (FilePath) { - try { - File.WriteAllText(FilePath, JsonConvert.SerializeObject(this)); - } catch (Exception e) { - Logging.LogGenericException(e); - } - } - } - } + private async Task Save() { + string json = JsonConvert.SerializeObject(this); + if (string.IsNullOrEmpty(json)) { + Logging.LogNullError(nameof(json)); + return; + } + + for (byte i = 0; i < 5; i++) { + lock (FilePath) { + try { + File.WriteAllText(FilePath, json); + break; + } catch (Exception e) { + Logging.LogGenericException(e); + } + } + + await Task.Delay(1000).ConfigureAwait(false); + } + } + } }