25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

97 lines
2.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SUISS.Core
  5. {
  6. public class ServiceLocator
  7. {
  8. private static ServiceLocator Instance
  9. {
  10. get
  11. {
  12. if (ServiceLocator._instance == null)
  13. {
  14. ServiceLocator._instance = new ServiceLocator();
  15. }
  16. return ServiceLocator._instance;
  17. }
  18. }
  19. public static T Find<T>()
  20. {
  21. ServiceLocator instance = ServiceLocator.Instance;
  22. if (!instance._services.ContainsKey(typeof(T)))
  23. {
  24. UnityEngine.Debug.LogWarning("Could not find Service of type \"" + typeof(T).ToString() + "\", returning default.");
  25. return default(T);
  26. }
  27. object obj = instance._services[typeof(T)];
  28. if (ServiceLocator.CheckForHiddenNull(obj))
  29. {
  30. ServiceLocator.UnregisterServiceObject<T>();
  31. return default(T);
  32. }
  33. return (T)((object)obj);
  34. }
  35. public static void RegistorServiceObject<T>(T serviceObject)
  36. {
  37. ServiceLocator instance = ServiceLocator.Instance;
  38. if (serviceObject == null)
  39. {
  40. UnityEngine.Debug.LogError("Trying to bind null object to " + typeof(T).ToString() + "!");
  41. return;
  42. }
  43. if (instance._services.ContainsKey(typeof(T)))
  44. {
  45. object service = instance._services[typeof(T)];
  46. if (!ServiceLocator.CheckForHiddenNull(service))
  47. {
  48. UnityEngine.Debug.LogError("Interface " + typeof(T).ToString() + " already registored with service locator!");
  49. return;
  50. }
  51. UnityEngine.Debug.LogWarning(string.Format("Removing (hidden) 'null' UnityEngine.Object {0} from service registory", typeof(T).Name));
  52. ServiceLocator.UnregisterServiceObject<T>();
  53. }
  54. instance._services.Add(typeof(T), serviceObject);
  55. }
  56. public static void UnregisterServiceObject<T>()
  57. {
  58. ServiceLocator instance = ServiceLocator.Instance;
  59. if (!instance._services.ContainsKey(typeof(T)))
  60. {
  61. UnityEngine.Debug.LogWarning("Interface " + typeof(T).ToString() + " was not registered with service locator!");
  62. return;
  63. }
  64. instance._services.Remove(typeof(T));
  65. }
  66. public static bool WasRegistered<T>()
  67. {
  68. ServiceLocator instance = ServiceLocator.Instance;
  69. if (!instance._services.ContainsKey(typeof(T)))
  70. {
  71. return false;
  72. }
  73. object service = instance._services[typeof(T)];
  74. if (ServiceLocator.CheckForHiddenNull(service))
  75. {
  76. ServiceLocator.UnregisterServiceObject<T>();
  77. return false;
  78. }
  79. return true;
  80. }
  81. private static bool CheckForHiddenNull(object service)
  82. {
  83. return service == null || service.Equals(null);
  84. }
  85. private static ServiceLocator _instance;
  86. private Dictionary<Type, object> _services = new Dictionary<Type, object>();
  87. }
  88. }