Added SZ singleton and services
This commit is contained in:
@@ -8,5 +8,6 @@ public class SzDataset
|
||||
|
||||
public required string DataObjectType {get; set;}
|
||||
public required string DataObjectTemplateId {get; set;}
|
||||
public Guid? DataObjectTemplateUuid {get; set;}
|
||||
public Dictionary<string, SzDataObject> DataObjects {get; set;} = [];
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ namespace SzCore.DataObjects;
|
||||
|
||||
public class SzTemplateField
|
||||
{
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public required SzFieldType FieldType { get; set; }
|
||||
public bool IsList {get; set;} = false;
|
||||
public string DefaultValue {get; set;} = "";
|
||||
public bool IsSpecialType {get; set;} = false;
|
||||
|
||||
51
SzCore/Defaults/DefaultLocalFileManager.cs
Normal file
51
SzCore/Defaults/DefaultLocalFileManager.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
namespace SzCore.Defaults;
|
||||
|
||||
public class DefaultLocalFileManager : ISzFileManager
|
||||
{
|
||||
public string DataPath
|
||||
{
|
||||
get
|
||||
{
|
||||
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
public string DatasetsPath => Path.Combine(DataPath, "datasets");
|
||||
|
||||
public bool SaveFile(string path, string fileContent)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dir = Path.GetDirectoryName(path);
|
||||
if (dir is null) return false;
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
File.WriteAllText(path, fileContent);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error saving file: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public string? LoadFile(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return File.ReadAllText(path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error loading file: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
107
SzCore/SZ.cs
Normal file
107
SzCore/SZ.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace SzCore;
|
||||
|
||||
public static class SZ
|
||||
{
|
||||
private static ISzFileManager? _fileManager;
|
||||
private static ISzLogger? _logger;
|
||||
|
||||
public static bool IsInitalized { get; private set; } = false;
|
||||
|
||||
public static ISzFileManager LocalFileManager
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckInitialization();
|
||||
if (_fileManager is null) throw new Exception("SZ._fileManager is null");
|
||||
return _fileManager;
|
||||
}
|
||||
}
|
||||
|
||||
public static ISzLogger Logger
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckInitialization();
|
||||
if (_logger is null) throw new Exception("SZ._logger is null");
|
||||
return _logger;
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<Type, object> _services = [];
|
||||
private static List<Type> _protectedServices = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initalizes the SZ singleton in SzCore
|
||||
/// </summary>
|
||||
public static void Init(ISzFileManager fileManager, ISzLogger logger)
|
||||
{
|
||||
if (IsInitalized) throw new Exception("Cannot initalize SZ more than once.");
|
||||
|
||||
_logger = logger;
|
||||
_fileManager = fileManager;
|
||||
|
||||
if (_logger is null)
|
||||
{
|
||||
throw new Exception("SZ.Init failed: Logger was null");
|
||||
}
|
||||
|
||||
if (_fileManager is null)
|
||||
{
|
||||
throw new Exception("SZ.Init failed: LocalFileManager was null");
|
||||
}
|
||||
|
||||
AddService(fileManager, true);
|
||||
AddService(logger, true);
|
||||
|
||||
IsInitalized = true;
|
||||
|
||||
logger.Log(" -- SZ CORE INITALIZED -- ");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to add a service
|
||||
/// </summary>
|
||||
/// <param name="instance">The object instance.</param>
|
||||
/// <param name="isProtected">Sets if the service should be protected (not able to be deleted).</param>
|
||||
/// <returns>True if the service was successfully added. False otherwise.</returns>
|
||||
/// <exception cref="Exception">Throws if SZ.CheckInitialization() fails</exception>
|
||||
public static bool AddService(object instance, bool isProtected = false)
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
var result = _services.TryAdd(instance.GetType(), instance);
|
||||
if (!result) return false;
|
||||
|
||||
if (isProtected)
|
||||
{
|
||||
if (_protectedServices.Contains(instance.GetType())) return false;
|
||||
|
||||
_protectedServices.Add(instance.GetType());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static T? GetService<T>()
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
if (!_services.ContainsKey(typeof(T))) return default(T);
|
||||
return (T)_services[typeof(T)];
|
||||
}
|
||||
|
||||
public static bool RemoveService(Type type)
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
if (_protectedServices.Contains(type)) return false;
|
||||
return _services.Remove(type);
|
||||
}
|
||||
|
||||
private static void CheckInitialization()
|
||||
{
|
||||
if (!IsInitalized) throw new Exception("SZ.Init has not been called.");
|
||||
}
|
||||
}
|
||||
6
SzCore/SzDataHandler.cs
Normal file
6
SzCore/SzDataHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace SzCore;
|
||||
|
||||
public static class SzDataHandler
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user