Added Base Project
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"AppSettings": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"ScreenResolution": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": { "type": "integer" },
|
||||
"y": { "type": "integer" }
|
||||
},
|
||||
"required": [ "x", "y" ]
|
||||
},
|
||||
"Fullscreen": { "type": "boolean" },
|
||||
"Log" : {"type": "boolean"}
|
||||
},
|
||||
"Required": [ "ScreenResolution", "Fullscreen", "Log" ]
|
||||
}
|
||||
},
|
||||
"Required" : ["AppSettings"]
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user