PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Aurora/Framework/Utilities/RegistryCore.cs

https://bitbucket.org/VirtualReality/async-sim-testing
C# | 155 lines | 91 code | 19 blank | 45 comment | 8 complexity | e3438b8363f9908f808a6a8e37499807 MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Aurora-Sim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using Aurora.Framework.Modules;
  31. namespace Aurora.Framework.Utilities
  32. {
  33. public class RegistryCore : IRegistryCore
  34. {
  35. /// <value>
  36. /// The module interfaces available from this scene.
  37. /// </value>
  38. protected Dictionary<Type, List<object>> ModuleInterfaces = new Dictionary<Type, List<object>>();
  39. #region IRegistryCore Members
  40. /// <summary>
  41. /// Register an interface to a region module. This allows module methods to be called directly as
  42. /// well as via events. If there is already a module registered for this interface, it is not replaced
  43. /// (is this the best behaviour?)
  44. /// </summary>
  45. /// <param name="mod"></param>
  46. public void RegisterModuleInterface<T>(T mod)
  47. {
  48. // MainConsole.Instance.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
  49. List<Object> l = null;
  50. if (!ModuleInterfaces.TryGetValue(typeof (T), out l))
  51. {
  52. l = new List<Object>();
  53. ModuleInterfaces.Add(typeof (T), l);
  54. }
  55. if (l.Count > 0)
  56. l.Clear();
  57. l.Add(mod);
  58. }
  59. public void AddModuleInterfaces(Dictionary<Type, List<object>> dictionary)
  60. {
  61. foreach (KeyValuePair<Type, List<object>> kvp in dictionary)
  62. {
  63. if (!ModuleInterfaces.ContainsKey(kvp.Key))
  64. ModuleInterfaces.Add(kvp.Key, kvp.Value);
  65. else
  66. ModuleInterfaces[kvp.Key].AddRange(kvp.Value);
  67. }
  68. }
  69. public void UnregisterModuleInterface<T>(T mod)
  70. {
  71. List<Object> l;
  72. if (ModuleInterfaces.TryGetValue(typeof (T), out l))
  73. {
  74. l.Remove(mod);
  75. }
  76. }
  77. public void StackModuleInterface<T>(T mod)
  78. {
  79. List<Object> l;
  80. l = ModuleInterfaces.ContainsKey(typeof (T)) ? ModuleInterfaces[typeof (T)] : new List<Object>();
  81. if (l.Contains(mod))
  82. return;
  83. l.Add(mod);
  84. ModuleInterfaces[typeof (T)] = l;
  85. }
  86. /// <summary>
  87. /// For the given interface, retrieve the region module which implements it.
  88. /// </summary>
  89. /// <returns>null if there is no registered module implementing that interface</returns>
  90. public T RequestModuleInterface<T>()
  91. {
  92. if (ModuleInterfaces.ContainsKey(typeof (T)) &&
  93. (ModuleInterfaces[typeof (T)].Count > 0))
  94. return (T) ModuleInterfaces[typeof (T)][0];
  95. else
  96. return default(T);
  97. }
  98. public bool TryRequestModuleInterface<T>(out T iface)
  99. {
  100. iface = default(T);
  101. if (ModuleInterfaces.ContainsKey(typeof (T)) &&
  102. (ModuleInterfaces[typeof (T)].Count > 0))
  103. {
  104. iface = (T) ModuleInterfaces[typeof (T)][0];
  105. return true;
  106. }
  107. else
  108. return false;
  109. }
  110. /// <summary>
  111. /// For the given interface, retrieve an array of region modules that implement it.
  112. /// </summary>
  113. /// <returns>an empty array if there are no registered modules implementing that interface</returns>
  114. public T[] RequestModuleInterfaces<T>()
  115. {
  116. if (ModuleInterfaces.ContainsKey(typeof (T)))
  117. {
  118. return ModuleInterfaces[typeof (T)].Select(o => (T) o).ToArray();
  119. }
  120. else
  121. {
  122. return new[] {default(T)};
  123. }
  124. }
  125. public Dictionary<Type, List<object>> GetInterfaces()
  126. {
  127. //Flatten the array
  128. return ModuleInterfaces.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
  129. }
  130. public void RemoveAllInterfaces()
  131. {
  132. ModuleInterfaces.Clear();
  133. }
  134. #endregion
  135. }
  136. }