Database saving hardening, #265

This commit is contained in:
JustArchi
2016-06-27 18:44:48 +02:00
parent 612ab87626
commit 1f4a4cc6b7
2 changed files with 48 additions and 24 deletions

View File

@@ -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);
}
}
}
}

View File

@@ -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);
}
}
}
}