Files
Firebird_2D/Firebird2D/Configuration/GameConfig.cs
T
2025-02-23 14:29:32 +01:00

145 lines
4.9 KiB
C#

using Firebird2D.Logger;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using SFML.Window;
using System.Text;
namespace Firebird2D.Configuration
{
public class GameConfig
{
public VideoMode VideoMode { get { return _videoMode; } set { _videoMode = value; updateConfig(); } }
public bool FullScreen { get { return _fullScreen; } set { _fullScreen = value; updateConfig(); } }
public bool LogActive { get { return _logActive; } set { _logActive = value; GameLogger.logActive = value; updateConfig(); } }
private static readonly string ConfigSchema = "Configuration/ConfigurationSchema/ConfigSchema.json";
private static readonly string ConfigPath = "Configuration/GameConfig.json";
private IConfigurationBuilder _configurationBuilder;
private IConfigurationSection _configurationSection;
private VideoMode _videoMode;
private bool _fullScreen;
private bool _logActive;
public GameConfig() {
CheckFile();
LoadConfig();
}
private void updateConfig()
{
if (_configurationBuilder != null)
{
_configurationSection["ScreenResolution:x"] = _videoMode.Width.ToString();
_configurationSection["ScreenResolution:y"] = _videoMode.Height.ToString();
_configurationSection["Fullscreen"] = _fullScreen.ToString();
_configurationSection["Log"] = _logActive.ToString();
}
}
private void LoadConfig()
{
_configurationBuilder = new ConfigurationBuilder().AddJsonFile(ConfigPath, false, true);
_configurationSection = _configurationBuilder.Build().GetSection("AppSettings");
_videoMode = new VideoMode();
if (_configurationSection != null)
{
try
{
_videoMode.Width = uint.Parse(_configurationSection["ScreenResolution:x"]);
_videoMode.Height = uint.Parse(_configurationSection["ScreenResolution:y"]);
_fullScreen = bool.Parse(_configurationSection["Fullscreen"]);
_logActive = bool.Parse(_configurationSection["Log"]);
GameLogger.logActive = _logActive;
}
catch(Exception ex)
{
GameLogger.Log("GameConfig", LogType.Error, ex.Message);
File.Delete(ConfigPath);
MakeConfigFile();
LoadConfig();
}
}
else
{
GameLogger.Log("GameConfig", LogType.Error, "The Config File Was Corrupted");
File.Delete(ConfigPath);
MakeConfigFile();
LoadConfig();
}
}
private void CheckFile()
{
if (File.Exists(ConfigPath))
{
string schemaJson = File.ReadAllText(ConfigSchema);
JSchema schema = JSchema.Parse(schemaJson);
string ConfigData = File.ReadAllText(ConfigPath);
JObject json = JObject.Parse(ConfigData);
IList<string> validationErrors = [];
if (schema != null)
{
if (!json.IsValid(schema,out validationErrors)){
File.Delete(ConfigPath);
MakeConfigFile();
GameLogger.Log("GameConfig", LogType.Error, "The Config File Was Corrupted");
foreach (string error in validationErrors)
{
GameLogger.Log("GameConfig", LogType.Error, error);
}
}
}
}
else
{
MakeConfigFile();
}
}
private void MakeConfigFile()
{
VideoMode desktopMode = VideoMode.DesktopMode;
using (FileStream stream = File.Create(ConfigPath))
{
AddText(stream, "{" +
" \"AppSettings\":{" +
" \"ScreenResolution\": {" +
string.Format(" \"x\": {0},", desktopMode.Width) +
string.Format(" \"y\": {0}", desktopMode.Height) +
" }," +
" \"Fullscreen\": false," +
" \"Log\": true," +
" }" +
"}"
);
stream.Close();
}
}
private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}
}