123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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<string>("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<dynamic>(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<Character> GetCharacterList(string accountId)
- {
- var characterListFilePath = GetCharacterListFilePath(accountId);
- if (!System.IO.File.Exists(characterListFilePath))
- {
- var newCharacterData = new List<Character>() { 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<List<Character>>(json);
- }
- }
- public class Character
- {
- public string Name { get; }
- public string Channel { get; }
- public Character(string name, string channel)
- {
- Name = name;
- Channel = channel;
- }
- }
- }
|