Files
ArchiSteamFarm/ConfigGenerator/ASFConfig.cs

64 lines
1.6 KiB
C#
Raw Normal View History

2016-03-20 06:41:12 +01:00
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace ConfigGenerator {
internal class ASFConfig {
internal static List<ASFConfig> ASFConfigs = new List<ASFConfig>();
internal string FilePath { get; set; }
protected ASFConfig() {
ASFConfigs.Add(this);
}
2016-03-20 08:29:27 +01:00
protected ASFConfig(string filePath) {
2016-03-20 06:41:12 +01:00
FilePath = filePath;
2016-03-20 08:29:27 +01:00
ASFConfigs.Add(this);
2016-03-20 06:41:12 +01:00
}
internal virtual void Save() {
lock (FilePath) {
try {
File.WriteAllText(FilePath, JsonConvert.SerializeObject(this, Formatting.Indented));
} catch (Exception e) {
Logging.LogGenericException(e);
}
}
}
2016-03-20 08:29:27 +01:00
internal virtual void Remove() {
string queryPath = Path.GetFileNameWithoutExtension(FilePath);
lock (FilePath) {
foreach (var configFile in Directory.EnumerateFiles(Program.ConfigDirectory, queryPath + ".*")) {
try {
File.Delete(configFile);
} catch (Exception e) {
Logging.LogGenericException(e);
}
}
}
ASFConfigs.Remove(this);
}
internal virtual void Rename(string botName) {
if (string.IsNullOrEmpty(botName)) {
return;
}
string queryPath = Path.GetFileNameWithoutExtension(FilePath);
lock (FilePath) {
foreach (var file in Directory.EnumerateFiles(Program.ConfigDirectory, queryPath + ".*")) {
try {
File.Move(file, Path.Combine(Program.ConfigDirectory, botName + Path.GetExtension(file)));
} catch (Exception e) {
Logging.LogGenericException(e);
}
}
FilePath = Path.Combine(Program.ConfigDirectory, botName + ".json");
}
}
2016-03-20 06:41:12 +01:00
}
}