More SZ singleton integration
This commit is contained in:
@@ -11,20 +11,28 @@ public class Program
|
|||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var parser = new SzParser(_fileManager, _logger);
|
SZ.Init(_fileManager, _logger);
|
||||||
|
|
||||||
_logger.Log("Creating test dataset...");
|
// _logger.Log("Creating test dataset...");
|
||||||
var dataset = Test.CreateTestDataset();
|
// var dataset = Test.CreateTestDataset();
|
||||||
_logger.Log($"Successfully created test dataset with id {dataset.Id}");
|
// _logger.Log($"Successfully created test dataset with id {dataset.Id}");
|
||||||
|
|
||||||
_logger.Log("Attempting to save dataset...");
|
// _logger.Log("Attempting to save dataset...");
|
||||||
var saved = parser.SaveDataset(dataset);
|
// var saved = parser.SaveDataset(dataset);
|
||||||
_logger.Log($"Successfully saved dataset? {saved}");
|
// _logger.Log($"Successfully saved dataset? {saved}");
|
||||||
|
|
||||||
_logger.Log("Attempting to load dataset...");
|
// _logger.Log("Attempting to load dataset...");
|
||||||
var loadedDataset = parser.LoadDataset(dataset.Id);
|
// var loadedDataset = parser.LoadDataset(dataset.Id);
|
||||||
_logger.Log($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}");
|
// _logger.Log($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}");
|
||||||
|
|
||||||
_logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value ?? "Could not get value of test.value");
|
// _logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value ?? "Could not get value of test.value");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WeirdThing
|
||||||
|
{
|
||||||
|
public void Yuh()
|
||||||
|
{
|
||||||
|
SZ.Logger.Log("Service test success");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 bool IsInitalized { get; private set; } = false;
|
||||||
|
|
||||||
|
public static readonly SzParser SzParser = new();
|
||||||
|
public static readonly SzDataHandler SzDataHandler = new();
|
||||||
|
|
||||||
public static ISzFileManager LocalFileManager
|
public static ISzFileManager LocalFileManager
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -35,6 +38,9 @@ public static class SZ
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initalizes the SZ singleton in SzCore
|
/// Initalizes the SZ singleton in SzCore
|
||||||
/// </summary>
|
/// </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)
|
public static void Init(ISzFileManager fileManager, ISzLogger logger)
|
||||||
{
|
{
|
||||||
if (IsInitalized) throw new Exception("Cannot initalize SZ more than once.");
|
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");
|
throw new Exception("SZ.Init failed: LocalFileManager was null");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IsInitalized = true;
|
||||||
|
|
||||||
AddService(fileManager, true);
|
AddService(fileManager, true);
|
||||||
AddService(logger, true);
|
AddService(logger, true);
|
||||||
|
|
||||||
IsInitalized = true;
|
|
||||||
|
|
||||||
logger.Log(" -- SZ CORE INITALIZED -- ");
|
logger.Log(" -- SZ CORE INITALIZED -- ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,16 +94,16 @@ public static class SZ
|
|||||||
{
|
{
|
||||||
CheckInitialization();
|
CheckInitialization();
|
||||||
|
|
||||||
if (!_services.ContainsKey(typeof(T))) return default(T);
|
if (!_services.ContainsKey(typeof(T))) return default;
|
||||||
return (T)_services[typeof(T)];
|
return (T)_services[typeof(T)];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool RemoveService(Type type)
|
public static bool RemoveService<T>()
|
||||||
{
|
{
|
||||||
CheckInitialization();
|
CheckInitialization();
|
||||||
|
|
||||||
if (_protectedServices.Contains(type)) return false;
|
if (_protectedServices.Contains(typeof(T))) return false;
|
||||||
return _services.Remove(type);
|
return _services.Remove(typeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CheckInitialization()
|
private static void CheckInitialization()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace SzCore;
|
namespace SzCore;
|
||||||
|
|
||||||
public static class SzDataHandler
|
public class SzDataHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,9 @@ using SzCore.DataObjects;
|
|||||||
|
|
||||||
namespace SzCore;
|
namespace SzCore;
|
||||||
|
|
||||||
public class SzParser(ISzFileManager szFileManager, ISzLogger szLogger)
|
public class SzParser
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
||||||
|
|
||||||
public string SerializeDatasetToJson(SzDataset dataset)
|
public string SerializeDatasetToJson(SzDataset dataset)
|
||||||
@@ -29,29 +30,29 @@ public class SzParser(ISzFileManager szFileManager, ISzLogger szLogger)
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SaveDataset(SzDataset dataset)
|
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
|
try
|
||||||
{
|
{
|
||||||
return szFileManager.SaveFile(datasetPath, SerializeDatasetToJson(dataset));
|
return SZ.LocalFileManager.SaveFile(datasetPath, SerializeDatasetToJson(dataset));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
szLogger.LogError("Error saving dataset: " + e.Message);
|
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SzDataset? LoadDataset(string datasetId)
|
public SzDataset? LoadDataset(string datasetId)
|
||||||
{
|
{
|
||||||
var datasetPath = Path.Combine(szFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||||
var json = szFileManager.LoadFile(datasetPath);
|
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
||||||
return json is null ? null : DeserializeDataset(json);
|
return json is null ? null : DeserializeDataset(json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user