diff --git a/SzCli/Program.cs b/SzCli/Program.cs index 775e722..dc9cf40 100644 --- a/SzCli/Program.cs +++ b/SzCli/Program.cs @@ -11,20 +11,28 @@ public class Program public static void Main(string[] args) { - var parser = new SzParser(_fileManager, _logger); + SZ.Init(_fileManager, _logger); - _logger.Log("Creating test dataset..."); - var dataset = Test.CreateTestDataset(); - _logger.Log($"Successfully created test dataset with id {dataset.Id}"); + // _logger.Log("Creating test dataset..."); + // var dataset = Test.CreateTestDataset(); + // _logger.Log($"Successfully created test dataset with id {dataset.Id}"); - _logger.Log("Attempting to save dataset..."); - var saved = parser.SaveDataset(dataset); - _logger.Log($"Successfully saved dataset? {saved}"); + // _logger.Log("Attempting to save dataset..."); + // var saved = parser.SaveDataset(dataset); + // _logger.Log($"Successfully saved dataset? {saved}"); - _logger.Log("Attempting to load dataset..."); - var loadedDataset = parser.LoadDataset(dataset.Id); - _logger.Log($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}"); + // _logger.Log("Attempting to load dataset..."); + // var loadedDataset = parser.LoadDataset(dataset.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"); } } \ No newline at end of file diff --git a/SzCore/SZ.cs b/SzCore/SZ.cs index 731fba0..1ffcac2 100644 --- a/SzCore/SZ.cs +++ b/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 /// /// Initalizes the SZ singleton in SzCore /// + /// An ISzFileManager instance. + /// An ISzLogger instance. + /// /// Throws if SZ has already been initalized 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() { 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() diff --git a/SzCore/SzDataHandler.cs b/SzCore/SzDataHandler.cs index 755cb95..cfd1e95 100644 --- a/SzCore/SzDataHandler.cs +++ b/SzCore/SzDataHandler.cs @@ -1,6 +1,6 @@ namespace SzCore; -public static class SzDataHandler +public class SzDataHandler { } \ No newline at end of file diff --git a/SzCore/SzParser.cs b/SzCore/SzParser.cs index fca25e5..a6f9de7 100644 --- a/SzCore/SzParser.cs +++ b/SzCore/SzParser.cs @@ -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); } }