using System; using System.Collections.Generic; using System.IO; using System.Text.Json; using Microsoft.Extensions.Configuration; namespace BubbleSocketCore.Library { public class JsonFileManager { private readonly SHA256HashGenerator sha256HashGenerator; private readonly IConfiguration Configuration; public JsonFileManager(IConfiguration configuration) { sha256HashGenerator = SHA256HashGenerator.GetInstance(); Configuration = configuration; } public string GetAccountFilePath(string username) { var filename = sha256HashGenerator.Get(username + Configuration.GetValue("ServerSalt")) + ".json"; string accountFilePath = Configuration["AccountFilePath"]; return Path.Combine(accountFilePath, filename); } public JsonElement ReadAccountFile(string username) { var json = System.IO.File.ReadAllText(GetAccountFilePath(username)); return JsonSerializer.Deserialize(json); } public void WriteAccountFile(string username, dynamic data) { var json = JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true }); System.IO.File.WriteAllText(GetAccountFilePath(username), json); } public bool DoesAccountAlreadyExist(string username) { return System.IO.File.Exists(GetAccountFilePath(username)); } public string GetCharacterListFilePath(string accountId) { var filename = sha256HashGenerator.Get(accountId) + ".json"; string accountFilePath = Configuration["DataStoreFilePath"]; return Path.Combine(accountFilePath, "characters" ,filename); } public List GetCharacterList(string accountId) { var characterListFilePath = GetCharacterListFilePath(accountId); if (!System.IO.File.Exists(characterListFilePath)) { var newCharacterData = new List() { new Character("demo", "default") }; var newCharacterJson = JsonSerializer.Serialize(newCharacterData, new JsonSerializerOptions { WriteIndented = true }); System.IO.File.WriteAllText(characterListFilePath, newCharacterJson); } var json = System.IO.File.ReadAllText(characterListFilePath); return JsonSerializer.Deserialize>(json); } } public class Character { public string Name { get; } public string Channel { get; } public Character(string name, string channel) { Name = name; Channel = channel; } } }