132 lines
3.8 KiB
C#
132 lines
3.8 KiB
C#
using System.Net.NetworkInformation;
|
|
|
|
namespace SzCore;
|
|
|
|
public static class SZ
|
|
{
|
|
private static ISzFileManager? _fileManager;
|
|
private static ISzLogger? _logger;
|
|
private static ISzDatabaseHandler? _dbHandler;
|
|
|
|
public static bool IsInitalized { get; private set; } = false;
|
|
|
|
public static readonly SzParser SzParser = new();
|
|
public static readonly SzDataHandler SzDataHandler = new();
|
|
public static readonly SzEvaluator SzEvaluator = new();
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public static ISzDatabaseHandler DatabaseHandler
|
|
{
|
|
get
|
|
{
|
|
CheckInitialization();
|
|
if (_dbHandler is null) throw new Exception("SZ._dbHandler is null");
|
|
return _dbHandler;
|
|
}
|
|
}
|
|
|
|
private static readonly Dictionary<Type, object> _services = [];
|
|
private static readonly List<Type> _protectedServices = [];
|
|
|
|
/// <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, ISzDatabaseHandler databaseHandler)
|
|
{
|
|
if (IsInitalized) throw new Exception("Cannot initalize SZ more than once.");
|
|
|
|
_logger = logger;
|
|
_fileManager = fileManager;
|
|
_dbHandler = databaseHandler;
|
|
|
|
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");
|
|
}
|
|
|
|
if (_dbHandler is null)
|
|
{
|
|
throw new Exception("SZ.Init failed: DatabaseHandler was null");
|
|
}
|
|
|
|
IsInitalized = true;
|
|
|
|
AddService(fileManager, true);
|
|
AddService(logger, true);
|
|
AddService(databaseHandler, 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;
|
|
return (T)_services[typeof(T)];
|
|
}
|
|
|
|
public static bool RemoveService<T>()
|
|
{
|
|
CheckInitialization();
|
|
|
|
if (_protectedServices.Contains(typeof(T))) return false;
|
|
return _services.Remove(typeof(T));
|
|
}
|
|
|
|
private static void CheckInitialization()
|
|
{
|
|
if (!IsInitalized) throw new Exception("SZ.Init has not been called.");
|
|
}
|
|
} |