diff --git a/Assets/BetterLocators/Runtime/Locators/Extensions.meta b/Assets/BetterLocators/Runtime/Extensions.meta similarity index 77% rename from Assets/BetterLocators/Runtime/Locators/Extensions.meta rename to Assets/BetterLocators/Runtime/Extensions.meta index 8169772..5ef92f9 100644 --- a/Assets/BetterLocators/Runtime/Locators/Extensions.meta +++ b/Assets/BetterLocators/Runtime/Extensions.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a718b9deb55d9ff42bec5d4fd9d6c1a6 +guid: bcf07a856dd86dc49b07b5bd4cc2e20f folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/BetterLocators/Runtime/Extensions/ILocatorExtensions.cs b/Assets/BetterLocators/Runtime/Extensions/ILocatorExtensions.cs new file mode 100644 index 0000000..abb5054 --- /dev/null +++ b/Assets/BetterLocators/Runtime/Extensions/ILocatorExtensions.cs @@ -0,0 +1,136 @@ +using System; +using Better.Commons.Runtime.Utility; +using Better.Locators.Runtime; + +public static class ILocatorExtensions +{ + public static bool TryAdd(this ILocator self, TElement element) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + if (element == null) + { + DebugUtility.LogException(nameof(element)); + return false; + } + + var key = element.GetType(); + return self.TryAdd(key, element); + } + + public static void Add(this ILocator self, TKey key, TElement element) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return; + } + + if (key == null) + { + DebugUtility.LogException(nameof(key)); + return; + } + + if (element == null) + { + DebugUtility.LogException(nameof(element)); + return; + } + + if (!self.TryAdd(key, element)) + { + var message = $"Invalid adding {nameof(element)} by {nameof(key)}({key})"; + DebugUtility.LogException(message); + } + } + + public static void Add(this ILocator self, TElement element) + { + if (element == null) + { + DebugUtility.LogException(nameof(element)); + return; + } + + var key = element.GetType(); + self.Add(key, element); + } + + public static bool ContainsKey(this ILocator self) + where TDerived : TElement + { + var key = typeof(TDerived); + return self.ContainsKey(key); + } + + public static bool TryGet(this ILocator self, TKey key, out TDerived element) + where TDerived : TElement + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + element = default; + return false; + } + + if (self.TryGet(key, out var located) + && located is TDerived casted) + { + element = casted; + return true; + } + + element = default; + return false; + } + + public static bool TryGet(this ILocator self, out TDerived element) + where TDerived : TElement + { + var key = typeof(TDerived); + return self.TryGet(key, out element); + } + + public static TElement Get(this ILocator self, TKey key) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return default; + } + + if (!self.TryGet(key, out var element)) + { + var message = $"Invalid getting {nameof(element)} by {nameof(key)}({key})"; + DebugUtility.LogException(message); + return default; + } + + return element; + } + + public static TDerived Get(this ILocator self) + where TDerived : TElement + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return default; + } + + var type = typeof(TDerived); + var element = (TDerived)self.Get(type); + if (element == null) + { + var message = $"Invalid getting {nameof(element)} by {nameof(type)}({type})"; + DebugUtility.LogException(message); + } + + return element; + } +} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Extensions/ILocatorExtensions.cs.meta b/Assets/BetterLocators/Runtime/Extensions/ILocatorExtensions.cs.meta new file mode 100644 index 0000000..00df96a --- /dev/null +++ b/Assets/BetterLocators/Runtime/Extensions/ILocatorExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2d6f8439e34d46329e43961000f9ad70 +timeCreated: 1715859721 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Implementations.meta b/Assets/BetterLocators/Runtime/Implementations.meta new file mode 100644 index 0000000..369050d --- /dev/null +++ b/Assets/BetterLocators/Runtime/Implementations.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e042a0327da340f9995447e6d7d6503a +timeCreated: 1715899735 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Implementations/Locator.cs b/Assets/BetterLocators/Runtime/Implementations/Locator.cs new file mode 100644 index 0000000..8f4021a --- /dev/null +++ b/Assets/BetterLocators/Runtime/Implementations/Locator.cs @@ -0,0 +1,147 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Better.Commons.Runtime.Utility; + +namespace Better.Locators.Runtime +{ + public class Locator : ILocator + { + public event Action Changed; + public event OnElementChanged ElementAdded; + public event OnElementChanged ElementRemoved; + + private Dictionary _sourceMap; + + public int Count => _sourceMap.Count; + + public Locator() + { + _sourceMap = new(); + } + + public delegate void OnElementChanged(TKey key, TElement element); + + public bool TryAdd(TKey key, TElement element) + { + if (element == null) + { + return false; + } + + if (ContainsElement(element)) + { + return false; + } + + _sourceMap.Add(key, element); + OnAdded(key, element); + return true; + } + + protected virtual void OnAdded(TKey key, TElement element) + { + ElementAdded?.Invoke(key, element); + OnChanged(); + } + + public bool ContainsKey(TKey key) + { + if (key == null) + { + DebugUtility.LogException(nameof(key)); + return false; + } + + return _sourceMap.ContainsKey(key); + } + + public bool ContainsElement(TElement element) + { + if (element == null) + { + DebugUtility.LogException(nameof(element)); + return false; + } + + return _sourceMap.ContainsValue(element); + } + + public TKey[] GetKeys() + { + return _sourceMap.Keys.ToArray(); + } + + public TElement[] GetElements() + { + return _sourceMap.Values.ToArray(); + } + + public bool TryGet(TKey key, out TElement element) + { + if (key == null) + { + DebugUtility.LogException(nameof(key)); + element = default; + return false; + } + + return _sourceMap.TryGetValue(key, out element); + } + + public bool Remove(TKey key) + { + if (key == null) + { + DebugUtility.LogException(nameof(key)); + return false; + } + + var removed = _sourceMap.Remove(key, out var element); + if (removed) + { + OnRemoved(key, element); + } + + return removed; + } + + public bool Remove(TElement element) + { + if (element == null) + { + DebugUtility.LogException(nameof(element)); + return false; + } + + var removed = true; TKey key = default; //_sourceMap.Remove(element, out var key); TODO: Commons DictionaryExtensions dependency + if (removed) + { + OnRemoved(key, element); + } + + return removed; + } + + protected virtual void OnRemoved(TKey key, TElement element) + { + ElementRemoved?.Invoke(key, element); + OnChanged(); + } + + public void Clear() + { + _sourceMap.Clear(); + OnChanged(); + } + + protected virtual void OnChanged() + { + Changed?.Invoke(); + } + } + + public class Locator : Locator + { + } +} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Implementations/Locator.cs.meta b/Assets/BetterLocators/Runtime/Implementations/Locator.cs.meta new file mode 100644 index 0000000..84fcb80 --- /dev/null +++ b/Assets/BetterLocators/Runtime/Implementations/Locator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b346e9375c454edea460d8da6447fc34 +timeCreated: 1715856118 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Implementations/MonoLocator.cs b/Assets/BetterLocators/Runtime/Implementations/MonoLocator.cs new file mode 100644 index 0000000..b9eeb94 --- /dev/null +++ b/Assets/BetterLocators/Runtime/Implementations/MonoLocator.cs @@ -0,0 +1,96 @@ +using System; +using UnityEngine; + +namespace Better.Locators.Runtime +{ + public abstract class MonoLocator : MonoBehaviour, ILocator + { + public event Action Changed + { + add => Source.Changed += value; + remove => Source.Changed -= value; + } + + public event Locator.OnElementChanged ElementAdded + { + add => Source.ElementAdded += value; + remove => Source.ElementAdded -= value; + } + + public event Locator.OnElementChanged ElementRemoved + { + add => Source.ElementRemoved += value; + remove => Source.ElementRemoved -= value; + } + + private ILocator _source; + + public int Count => Source.Count; + + protected ILocator Source + { + get + { + if (_source == null) + { + _source = new Locator(); + } + + return _source; + } + } + + public bool TryAdd(TKey key, TElement element) + { + return Source.TryAdd(key, element); + } + + public bool ContainsKey(TKey key) + { + return Source.ContainsKey(key); + } + + public bool ContainsElement(TElement element) + { + return Source.ContainsElement(element); + } + + public TKey[] GetKeys() + { + return Source.GetKeys(); + } + + public TElement[] GetElements() + { + return Source.GetElements(); + } + + public bool TryGet(TKey key, out TElement element) + { + return Source.TryGet(key, out element); + } + + public bool Remove(TKey key) + { + return Source.Remove(key); + } + + public bool Remove(TElement element) + { + return Source.Remove(element); + } + + public void Clear() + { + Source.Clear(); + } + } + + public abstract class MonoLocator : MonoLocator + { + } + + public class MonoLocator : MonoLocator + { + } +} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/MonoLocator.cs.meta b/Assets/BetterLocators/Runtime/Implementations/MonoLocator.cs.meta similarity index 100% rename from Assets/BetterLocators/Runtime/Locators/Implementations/MonoLocator.cs.meta rename to Assets/BetterLocators/Runtime/Implementations/MonoLocator.cs.meta diff --git a/Assets/BetterLocators/Runtime/Implementations/ServiceLocator.cs b/Assets/BetterLocators/Runtime/Implementations/ServiceLocator.cs new file mode 100644 index 0000000..69cf984 --- /dev/null +++ b/Assets/BetterLocators/Runtime/Implementations/ServiceLocator.cs @@ -0,0 +1,98 @@ +#if BETTER_SERVICES +using System; +using Better.Commons.Runtime.Utility; +using Better.Services.Runtime.Interfaces; +using UnityEngine; + +namespace Better.Locators.Runtime +{ + public static class ServiceLocator + { + private static readonly Locator _source; + + static ServiceLocator() + { + _source = new Locator(); + } + + public static void Register(TService service) + where TService : IService + { + if (service == null) + { + DebugUtility.LogException(nameof(service)); + return; + } + + if (service is { Initialized: false }) + { + var type = service.GetType(); + var message = $"{nameof(service)} of {nameof(type)}{type} not initialized"; + Debug.LogWarning(message); + } + + if (HasRegistered()) + { + var type = typeof(TService); + var message = $"{nameof(service)} of {nameof(type)}({type}) already registered"; + DebugUtility.LogException(message); + + return; + } + + if (!_source.TryAdd(service)) + { + DebugUtility.LogException(); + } + } + + public static bool HasRegistered() + where TService : IService + { + return _source.ContainsKey(); + } + + public static bool TryGet(out TService service) + where TService : IService + { + return _source.TryGet(out service); + } + + public static TService Get() + where TService : IService + { + if (!TryGet(out TService service)) + { + DebugUtility.LogException(); + } + + return service; + } + + public static void Unregister(TService service) + where TService : IService + { + if (service == null) + { + DebugUtility.LogException(nameof(service)); + return; + } + + if (!HasRegistered()) + { + var type = typeof(TService); + var message = $"{nameof(service)} of {nameof(type)}({type}) not registered"; + DebugUtility.LogException(message); + + return; + } + + if (!_source.Remove(service)) + { + DebugUtility.LogException(); + } + } + } +} + +#endif \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/ServiceLocator.cs.meta b/Assets/BetterLocators/Runtime/Implementations/ServiceLocator.cs.meta similarity index 100% rename from Assets/BetterLocators/Runtime/Locators/Implementations/ServiceLocator.cs.meta rename to Assets/BetterLocators/Runtime/Implementations/ServiceLocator.cs.meta diff --git a/Assets/BetterLocators/Runtime/Installers/MonoLocatorInstaller.cs b/Assets/BetterLocators/Runtime/Installers/MonoLocatorInstaller.cs deleted file mode 100644 index 38e6a41..0000000 --- a/Assets/BetterLocators/Runtime/Installers/MonoLocatorInstaller.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using UnityEngine; - -namespace Better.Locators.Runtime.Installers -{ - [Serializable] - public class MonoLocatorInstaller : MonoLocatorInstaller - { - [SerializeField] private MonoBehaviour[] _items; - - protected override MonoBehaviour[] Items => _items; - } - - [Serializable] - public abstract class MonoLocatorInstaller : Installer where TDerived : class where TLocator : MonoLocator - { - [SerializeField] private TLocator _locator; - - protected abstract TDerived[] Items { get; } - - public override Task InstallBindingsAsync(CancellationToken cancellationToken) - { - if (cancellationToken.IsCancellationRequested) - { - Debug.LogError("Operation was cancelled"); - return Task.CompletedTask; - } - - for (int i = 0; i < Items.Length; i++) - { - _locator.Register(Items[i]); - } - - return Task.CompletedTask; - } - - public override void UninstallBindings() - { - for (int i = 0; i < Items.Length; i++) - { - _locator.Unregister(Items[i]); - } - } - } -} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Installers/MonoLocatorInstaller.cs.meta b/Assets/BetterLocators/Runtime/Installers/MonoLocatorInstaller.cs.meta deleted file mode 100644 index 6b6a3c1..0000000 --- a/Assets/BetterLocators/Runtime/Installers/MonoLocatorInstaller.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 14d59ae1de9745138ba0524618d8cb12 -timeCreated: 1707591034 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Interfaces.meta b/Assets/BetterLocators/Runtime/Interfaces.meta new file mode 100644 index 0000000..ef77f4e --- /dev/null +++ b/Assets/BetterLocators/Runtime/Interfaces.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e9d0a27b81414d7fbf043596c3838c6c +timeCreated: 1715978497 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Interfaces/ILocator.cs b/Assets/BetterLocators/Runtime/Interfaces/ILocator.cs new file mode 100644 index 0000000..61c6747 --- /dev/null +++ b/Assets/BetterLocators/Runtime/Interfaces/ILocator.cs @@ -0,0 +1,23 @@ +using System; + +namespace Better.Locators.Runtime +{ + public interface ILocator + { + public event Action Changed; + public event Locator.OnElementChanged ElementAdded; + public event Locator.OnElementChanged ElementRemoved; + + public int Count { get; } + + public bool TryAdd(TKey key, TElement element); + public bool ContainsKey(TKey key); + public bool ContainsElement(TElement element); + public TKey[] GetKeys(); + public TElement[] GetElements(); + public bool TryGet(TKey key, out TElement element); + public bool Remove(TKey key); + public bool Remove(TElement element); + public void Clear(); + } +} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Interfaces/ILocator.cs.meta b/Assets/BetterLocators/Runtime/Interfaces/ILocator.cs.meta new file mode 100644 index 0000000..78ae468 --- /dev/null +++ b/Assets/BetterLocators/Runtime/Interfaces/ILocator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c6db356b4a23467bbadfdc5d6253aee9 +timeCreated: 1715978490 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators.meta b/Assets/BetterLocators/Runtime/Locators.meta deleted file mode 100644 index b357ae2..0000000 --- a/Assets/BetterLocators/Runtime/Locators.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 0007d77e367c447498cec5523b39c0d0 -timeCreated: 1709661144 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Awaiters.meta b/Assets/BetterLocators/Runtime/Locators/Awaiters.meta deleted file mode 100644 index 0848c7f..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Awaiters.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 238d0417a044456bb2b85563114eabfa -timeCreated: 1713899439 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Awaiters/LocatorGetAwaiter.cs b/Assets/BetterLocators/Runtime/Locators/Awaiters/LocatorGetAwaiter.cs deleted file mode 100644 index 95314b7..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Awaiters/LocatorGetAwaiter.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Threading; -using Better.Commons.Runtime.Helpers.CompletionAwaiters; -using ThreadingTask = System.Threading.Tasks.Task; - -namespace Better.Locators.Runtime.Awaiters -{ - public class LocatorGetAwaiter : CompletionAwaiter, TValue> where TValue : TItem - { - public LocatorGetAwaiter(ILocator source, CancellationToken cancellationToken) : base(source, cancellationToken) - { - ProcessAsync(cancellationToken); - } - - private async void ProcessAsync(CancellationToken cancellationToken) - { - while (!Source.HasRegistered() && !cancellationToken.IsCancellationRequested) - { - await ThreadingTask.Yield(); - } - - if (cancellationToken.IsCancellationRequested) return; - var service = Source.Get(); - SetResult(service); - } - - protected override void OnCompleted(TValue result) - { - } - } -} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Awaiters/LocatorGetAwaiter.cs.meta b/Assets/BetterLocators/Runtime/Locators/Awaiters/LocatorGetAwaiter.cs.meta deleted file mode 100644 index 0df8fb0..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Awaiters/LocatorGetAwaiter.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 8e5317e8af344afab838ae2ff32e49f5 -timeCreated: 1713899453 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Extensions/ILocatorExtensions.cs b/Assets/BetterLocators/Runtime/Locators/Extensions/ILocatorExtensions.cs deleted file mode 100644 index 967d7f5..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Extensions/ILocatorExtensions.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Better.Commons.Runtime.Utility; -using Better.Locators.Runtime.Awaiters; - -namespace Better.Locators.Runtime -{ - public static class ILocatorExtensions - { - public static bool TryGet(this ILocator self, out TItem item) - where TItem : TBase - { - if (self == null) - { - DebugUtility.LogException(nameof(self)); - item = default; - return false; - } - - if (self.HasRegistered()) - { - item = self.Get(); - return true; - } - - item = default; - return false; - } - - public static Task GetAsync(this ILocator self, CancellationToken token = default) - where TItem : TBase - { - if (self == null) - { - DebugUtility.LogException(nameof(self)); - return default; - } - - var awaiter = new LocatorGetAwaiter(self, token); - return awaiter.Task; - } - } -} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Extensions/ILocatorExtensions.cs.meta b/Assets/BetterLocators/Runtime/Locators/Extensions/ILocatorExtensions.cs.meta deleted file mode 100644 index 87cacf7..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Extensions/ILocatorExtensions.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 698ba8fa840243645b0bb893184c6ec5 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations.meta b/Assets/BetterLocators/Runtime/Locators/Implementations.meta deleted file mode 100644 index 073f8c6..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Implementations.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: ef0eef3e041341d1a3102b4821adb9b9 -timeCreated: 1709661851 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/InternalLocator.cs b/Assets/BetterLocators/Runtime/Locators/Implementations/InternalLocator.cs deleted file mode 100644 index 8645a3f..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Implementations/InternalLocator.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.Collections.Generic; -using Better.Commons.Runtime.Utility; - -namespace Better.Locators.Runtime -{ - internal class InternalLocator : ILocator - { - private readonly Dictionary _typeItemsMap; - - public InternalLocator() - { - _typeItemsMap = new(); - } - - public void Register(T item) where T : TItem - { - if (item == null) - { - DebugUtility.LogException(nameof(item)); - return; - } - - var type = item.GetType(); - if (HasRegistered(type)) - { - var message = $"{nameof(item)} of type {type} is already registered. Operation cancelled"; - DebugUtility.LogException(message); - return; - } - - _typeItemsMap[type] = item; - } - - public bool HasRegistered() where T : TItem - { - var type = typeof(T); - return HasRegistered(type); - } - - private bool HasRegistered(Type type) - { - return _typeItemsMap.ContainsKey(type); - } - - public void Unregister(T item) where T : TItem - { - if (item == null) - { - DebugUtility.LogException(nameof(item)); - return; - } - - var type = item.GetType(); - if (!HasRegistered(type)) - { - var message = $"{nameof(item)} of type {type} is not registered. Operation cancelled"; - DebugUtility.LogException(message); - return; - } - - _typeItemsMap.Remove(type); - } - - public T Get() where T : TItem - { - var type = typeof(T); - if (_typeItemsMap.TryGetValue(type, out var item)) - { - return (T)item; - } - - var message = $"Element type {type} is not registered"; - DebugUtility.LogException(message); - return default; - } - } -} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/InternalLocator.cs.meta b/Assets/BetterLocators/Runtime/Locators/Implementations/InternalLocator.cs.meta deleted file mode 100644 index d1b541f..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Implementations/InternalLocator.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9fbb80dd2bbd4b08ba9eff8350d1d40d -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/MonoLocator.cs b/Assets/BetterLocators/Runtime/Locators/Implementations/MonoLocator.cs deleted file mode 100644 index f599a5b..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Implementations/MonoLocator.cs +++ /dev/null @@ -1,22 +0,0 @@ -using UnityEngine; - -namespace Better.Locators.Runtime -{ - public abstract class MonoLocator : MonoBehaviour, ILocator - { - protected ILocator _internalLocator = new InternalLocator(); - - #region ILocator - - public virtual void Register(T item) where T : TItem => _internalLocator.Register(item); - public bool HasRegistered() where T : TItem => _internalLocator.HasRegistered(); - public virtual void Unregister(T item) where T : TItem => _internalLocator.Unregister(item); - public virtual T Get() where T : TItem => _internalLocator.Get(); - - #endregion - } - - public class MonoLocator : MonoLocator - { - } -} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/PocoLocator.cs b/Assets/BetterLocators/Runtime/Locators/Implementations/PocoLocator.cs deleted file mode 100644 index 676c2fc..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Implementations/PocoLocator.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Better.Locators.Runtime -{ - public class PocoLocator : ILocator - { - protected ILocator _internalLocator; - - public PocoLocator() - { - _internalLocator = new InternalLocator(); - } - - #region ILocator - - public virtual void Register(T item) where T : TItem => _internalLocator.Register(item); - public virtual bool HasRegistered() where T : TItem => _internalLocator.HasRegistered(); - public virtual void Unregister(T item) where T : TItem => _internalLocator.Unregister(item); - public virtual T Get() where T : TItem => _internalLocator.Get(); - - #endregion - } - - public class PocoLocator : PocoLocator - { - } -} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/PocoLocator.cs.meta b/Assets/BetterLocators/Runtime/Locators/Implementations/PocoLocator.cs.meta deleted file mode 100644 index 1e5b487..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Implementations/PocoLocator.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: eeba4425126f425b9b58295f3c1b8047 -timeCreated: 1709663116 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Implementations/ServiceLocator.cs b/Assets/BetterLocators/Runtime/Locators/Implementations/ServiceLocator.cs deleted file mode 100644 index 1c5c27b..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Implementations/ServiceLocator.cs +++ /dev/null @@ -1,52 +0,0 @@ -#if BETTER_SERVICES -using System.Threading; -using System.Threading.Tasks; -using Better.Services.Runtime.Interfaces; -using UnityEngine; - -namespace Better.Locators.Runtime -{ - public static class ServiceLocator - { - private static readonly ILocator _internalLocator; - - static ServiceLocator() - { - _internalLocator = new InternalLocator(); - } - - public static void Register(T service) where T : IService - { - _internalLocator.Register(service); - - if (service is { Initialized: false }) - { - var type = service.GetType(); - var message = $"Service of type {type} not initialized"; - Debug.LogWarning(message); - } - } - - public static bool HasRegistered() where T : IService - { - return _internalLocator.HasRegistered(); - } - - public static void Unregister(T service) where T : IService - { - _internalLocator.Unregister(service); - } - - public static T Get() where T : IService - { - return _internalLocator.Get(); - } - - public static Task GetAsync(CancellationToken token = default) - where T : IService - { - return _internalLocator.GetAsync(token); - } - } -} -#endif \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Interfaces.meta b/Assets/BetterLocators/Runtime/Locators/Interfaces.meta deleted file mode 100644 index 556b7f2..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Interfaces.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 4664b40a13074a478fcc364381d3801d -timeCreated: 1709661862 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Interfaces/ILocator.cs b/Assets/BetterLocators/Runtime/Locators/Interfaces/ILocator.cs deleted file mode 100644 index 293eb24..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Interfaces/ILocator.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Better.Locators.Runtime -{ - public interface ILocator - { - public void Register(T item) where T : TItem; - public bool HasRegistered() where T : TItem; - public T Get() where T : TItem; - public void Unregister(T item) where T : TItem; - } -} \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Interfaces/ILocator.cs.meta b/Assets/BetterLocators/Runtime/Locators/Interfaces/ILocator.cs.meta deleted file mode 100644 index e20e9a8..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Interfaces/ILocator.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: ea305b6657a04fd89465ef7057c96088 -timeCreated: 1709661335 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Properties.meta b/Assets/BetterLocators/Runtime/Locators/Properties.meta deleted file mode 100644 index b06ecd2..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Properties.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 963fc87a01234401ac1c67255b4fd69b -timeCreated: 1709661833 \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Properties/ServiceProperty.cs b/Assets/BetterLocators/Runtime/Locators/Properties/ServiceProperty.cs deleted file mode 100644 index 65492d3..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Properties/ServiceProperty.cs +++ /dev/null @@ -1,26 +0,0 @@ -#if BETTER_SERVICES -using Better.Services.Runtime.Interfaces; - -namespace Better.Locators.Runtime -{ - public sealed class ServiceProperty where T : IService - { - private T _cachedService; - - public T CachedService - { - get - { - if (_cachedService == null) - { - _cachedService = ServiceLocator.Get(); - } - - return _cachedService; - } - } - - public bool IsRegistered => ServiceLocator.HasRegistered(); - } -} -#endif \ No newline at end of file diff --git a/Assets/BetterLocators/Runtime/Locators/Properties/ServiceProperty.cs.meta b/Assets/BetterLocators/Runtime/Locators/Properties/ServiceProperty.cs.meta deleted file mode 100644 index 959dd40..0000000 --- a/Assets/BetterLocators/Runtime/Locators/Properties/ServiceProperty.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 800533bddbd24dc5b6aed0bdb67ea4ea -timeCreated: 1707589334 \ No newline at end of file diff --git a/Assets/BetterLocators/package.json b/Assets/BetterLocators/package.json index ced0d96..26db90d 100644 --- a/Assets/BetterLocators/package.json +++ b/Assets/BetterLocators/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.locators", "displayName": "Better Locators", - "version": "0.1.7", + "version": "0.1.8", "unity": "2021.3", "description": " ", "dependencies": {