/***************************************************************************** MIT License Copyright (c) 2023 Calvin Rien Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *****************************************************************************/ using System; using System.Collections.Generic; using UnityEngine; public interface IService { } public static class ServiceLocator { private static readonly Dictionary<Type, IService> k_Services = new Dictionary<Type, IService>(); private static readonly Dictionary<Type, Action<IService>> k_Callbacks = new(); private static readonly Dictionary<Delegate, Action<IService>> k_RegisteredCallbacks = new(); public static bool Register<T>(T service) where T : class, IService { var type = typeof(T); if (!k_Services.TryAdd(type, service)) { // Throw exception or just warn and return false? Debug.LogWarning($"Service of type {type.Name} already registered"); return false; } if (k_Callbacks.TryGetValue(type, out var serviceCallback)) { serviceCallback?.Invoke(service); } return true; } public static bool Unregister<T>(T service) where T : class, IService { var type = typeof(T); if (k_Services.TryGetValue(type, out var temp) && service == temp) { k_Services.Remove(type); return true; } Debug.LogWarning($"This instance of {type.Name} is not registered"); return false; } public static T GetService<T>() where T : class, IService { if (k_Services.TryGetValue(typeof(T), out var service)) { return (T)service; } return null; } public static bool TryGetService<T>(out T result) where T : class, IService { if (k_Services.TryGetValue(typeof(T), out var service)) { result = (T)service; return true; } result = default; return false; } public static void AddServiceListener<T>(Action<T> serviceCallback) where T : class, IService { if (k_RegisteredCallbacks.ContainsKey(serviceCallback)) { Debug.LogWarning("double registered"); return; } if (TryGetService<T>(out var service)) { serviceCallback?.Invoke(service); return; } var type = typeof(T); Action<IService> newAction = (e) => serviceCallback((T)e); if (k_Callbacks.TryGetValue(type, out var result)) { k_Callbacks[type] = result += newAction; } else { k_Callbacks[type] = newAction; } k_RegisteredCallbacks.Add(serviceCallback, newAction); } public static void RemoveServiceListener<T>(Action<T> serviceCallback) where T : class, IService { if (!k_RegisteredCallbacks.TryGetValue(serviceCallback, out var action)) { return; } var type = typeof(T); if (k_Callbacks.TryGetValue(type, out var tempAction)) { tempAction -= action; if (tempAction == null) { k_Callbacks.Remove(type); } else { k_Callbacks[type] = tempAction; } } k_RegisteredCallbacks.Remove(serviceCallback); } }