Added SZ singleton and services
This commit is contained in:
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.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user