More SZ singleton integration
This commit is contained in:
18
SzCore/SZ.cs
18
SzCore/SZ.cs
@@ -9,6 +9,9 @@ public static class SZ
|
||||
|
||||
public static bool IsInitalized { get; private set; } = false;
|
||||
|
||||
public static readonly SzParser SzParser = new();
|
||||
public static readonly SzDataHandler SzDataHandler = new();
|
||||
|
||||
public static ISzFileManager LocalFileManager
|
||||
{
|
||||
get
|
||||
@@ -35,6 +38,9 @@ public static class SZ
|
||||
/// <summary>
|
||||
/// Initalizes the SZ singleton in SzCore
|
||||
/// </summary>
|
||||
/// <param name="fileManager">An ISzFileManager instance.</param>
|
||||
/// <param name="logger">An ISzLogger instance.</param>
|
||||
/// /// <exception cref="Exception">Throws if SZ has already been initalized</exception>
|
||||
public static void Init(ISzFileManager fileManager, ISzLogger logger)
|
||||
{
|
||||
if (IsInitalized) throw new Exception("Cannot initalize SZ more than once.");
|
||||
@@ -52,11 +58,11 @@ public static class SZ
|
||||
throw new Exception("SZ.Init failed: LocalFileManager was null");
|
||||
}
|
||||
|
||||
IsInitalized = true;
|
||||
|
||||
AddService(fileManager, true);
|
||||
AddService(logger, true);
|
||||
|
||||
IsInitalized = true;
|
||||
|
||||
logger.Log(" -- SZ CORE INITALIZED -- ");
|
||||
}
|
||||
|
||||
@@ -88,16 +94,16 @@ public static class SZ
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
if (!_services.ContainsKey(typeof(T))) return default(T);
|
||||
if (!_services.ContainsKey(typeof(T))) return default;
|
||||
return (T)_services[typeof(T)];
|
||||
}
|
||||
|
||||
public static bool RemoveService(Type type)
|
||||
public static bool RemoveService<T>()
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
if (_protectedServices.Contains(type)) return false;
|
||||
return _services.Remove(type);
|
||||
if (_protectedServices.Contains(typeof(T))) return false;
|
||||
return _services.Remove(typeof(T));
|
||||
}
|
||||
|
||||
private static void CheckInitialization()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace SzCore;
|
||||
|
||||
public static class SzDataHandler
|
||||
public class SzDataHandler
|
||||
{
|
||||
|
||||
}
|
||||
@@ -3,8 +3,9 @@ using SzCore.DataObjects;
|
||||
|
||||
namespace SzCore;
|
||||
|
||||
public class SzParser(ISzFileManager szFileManager, ISzLogger szLogger)
|
||||
public class SzParser
|
||||
{
|
||||
|
||||
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
||||
|
||||
public string SerializeDatasetToJson(SzDataset dataset)
|
||||
@@ -29,29 +30,29 @@ public class SzParser(ISzFileManager szFileManager, ISzLogger szLogger)
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
szLogger.LogError("Could not deserialize JSON to type SzDataset: " + e.Message);
|
||||
SZ.Logger.LogError("Could not deserialize JSON to type SzDataset: " + e.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool SaveDataset(SzDataset dataset)
|
||||
{
|
||||
var datasetPath = Path.Combine(szFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
||||
try
|
||||
{
|
||||
return szFileManager.SaveFile(datasetPath, SerializeDatasetToJson(dataset));
|
||||
return SZ.LocalFileManager.SaveFile(datasetPath, SerializeDatasetToJson(dataset));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
szLogger.LogError("Error saving dataset: " + e.Message);
|
||||
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? LoadDataset(string datasetId)
|
||||
{
|
||||
var datasetPath = Path.Combine(szFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||
var json = szFileManager.LoadFile(datasetPath);
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
||||
return json is null ? null : DeserializeDataset(json);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user