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 _services = []; private static List _protectedServices = []; /// /// Initalizes the SZ singleton in SzCore /// 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 -- "); } /// /// Tries to add a service /// /// The object instance. /// Sets if the service should be protected (not able to be deleted). /// True if the service was successfully added. False otherwise. /// Throws if SZ.CheckInitialization() fails 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() { 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."); } }