-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Better.Extensions.Runtime; | ||
|
||
namespace Better.Locators.Runtime | ||
{ | ||
internal class InternalLocator<TItem> : ILocator<TItem> | ||
{ | ||
private readonly Dictionary<Type, TItem> _typeItemsMap; | ||
|
||
public InternalLocator() | ||
{ | ||
_typeItemsMap = new(); | ||
} | ||
|
||
public void Register<T>(T item) where T : TItem | ||
{ | ||
if (item == null) | ||
{ | ||
DebugUtility.LogException<ArgumentNullException>(nameof(item)); | ||
return; | ||
} | ||
|
||
var type = item.GetType(); | ||
if (HasRegistered<T>()) | ||
{ | ||
var message = $"{nameof(item)} of type {type} is already registered. Operation cancelled"; | ||
DebugUtility.LogException<InvalidOperationException>(message); | ||
return; | ||
} | ||
|
||
_typeItemsMap[type] = item; | ||
} | ||
|
||
public bool HasRegistered<T>() where T : TItem | ||
{ | ||
var type = typeof(T); | ||
return _typeItemsMap.ContainsKey(type); | ||
} | ||
|
||
public void Unregister<T>(T item) where T : TItem | ||
{ | ||
if (item == null) | ||
{ | ||
DebugUtility.LogException<ArgumentNullException>(nameof(item)); | ||
return; | ||
} | ||
|
||
var type = item.GetType(); | ||
if (!HasRegistered<T>()) | ||
{ | ||
var message = $"{nameof(item)} of type {type} is not registered. Operation cancelled"; | ||
DebugUtility.LogException<InvalidOperationException>(message); | ||
return; | ||
} | ||
|
||
_typeItemsMap.Remove(type); | ||
} | ||
|
||
public T Get<T>() 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<InvalidOperationException>(message); | ||
return default; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using UnityEngine; | ||
|
||
namespace Better.Locators.Runtime | ||
{ | ||
public abstract class MonoLocator<TItem> : MonoBehaviour, ILocator<TItem> | ||
{ | ||
protected ILocator<TItem> _internalLocator = new InternalLocator<TItem>(); | ||
|
||
#region ILocator<TItem> | ||
|
||
public virtual void Register<T>(T item) where T : TItem => _internalLocator.Register(item); | ||
public bool HasRegistered<T>() where T : TItem => _internalLocator.HasRegistered<T>(); | ||
public virtual void Unregister<T>(T item) where T : TItem => _internalLocator.Unregister(item); | ||
public virtual T Get<T>() where T : TItem => _internalLocator.Get<T>(); | ||
|
||
#endregion | ||
} | ||
|
||
public class MonoLocator : MonoLocator<MonoBehaviour> | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Better.Locators.Runtime | ||
{ | ||
public abstract class PocoLocator<TItem> : ILocator<TItem> | ||
{ | ||
protected ILocator<TItem> _internalLocator; | ||
|
||
protected PocoLocator() | ||
{ | ||
_internalLocator = new InternalLocator<TItem>(); | ||
} | ||
|
||
#region ILocator<TItem> | ||
|
||
public virtual void Register<T>(T item) where T : TItem => _internalLocator.Register(item); | ||
public virtual bool HasRegistered<T>() where T : TItem => _internalLocator.HasRegistered<T>(); | ||
public virtual void Unregister<T>(T item) where T : TItem => _internalLocator.Unregister(item); | ||
public virtual T Get<T>() where T : TItem => _internalLocator.Get<T>(); | ||
|
||
#endregion | ||
} | ||
|
||
public class PocoLocator : MonoLocator<object> | ||
{ | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#if BETTER_SERVICES | ||
using Better.Services.Runtime.Interfaces; | ||
using UnityEngine; | ||
|
||
namespace Better.Locators.Runtime | ||
{ | ||
public static class ServiceLocator | ||
{ | ||
private static readonly ILocator<IService> _internalLocator; | ||
|
||
static ServiceLocator() | ||
{ | ||
_internalLocator = new InternalLocator<IService>(); | ||
} | ||
|
||
public static void Register<T>(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<T>() where T : IService | ||
{ | ||
return _internalLocator.HasRegistered<T>(); | ||
} | ||
|
||
public static void Unregister<T>(T service) where T : IService | ||
{ | ||
_internalLocator.Unregister(service); | ||
} | ||
|
||
public static T Get<T>() where T : IService | ||
{ | ||
return _internalLocator.Get<T>(); | ||
} | ||
} | ||
} | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Better.Locators.Runtime | ||
{ | ||
public interface ILocator<in TItem> | ||
{ | ||
void Register<T>(T item) where T : TItem; | ||
bool HasRegistered<T>() where T : TItem; | ||
void Unregister<T>(T item) where T : TItem; | ||
T Get<T>() where T : TItem; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#if BETTER_SERVICES | ||
using Better.Services.Runtime.Interfaces; | ||
|
||
namespace Better.Locators.Runtime | ||
{ | ||
public sealed class ServiceProperty<T> where T : IService | ||
{ | ||
private T _cachedService; | ||
|
||
public T CachedService | ||
{ | ||
get | ||
{ | ||
if (_cachedService == null) | ||
{ | ||
_cachedService = ServiceLocator.Get<T>(); | ||
} | ||
|
||
return _cachedService; | ||
} | ||
} | ||
|
||
public bool IsRegistered => ServiceLocator.HasRegistered<T>(); | ||
} | ||
} | ||
#endif |
This file was deleted.
This file was deleted.