PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Aurora/Framework/Utils/RegistryCore.cs

https://bitbucket.org/VirtualReality/aurora-sim
C# | 169 lines | 104 code | 20 blank | 45 comment | 8 complexity | 328497e053d68b171ebb35ffa352372d 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. namespace Aurora.Framework
  31. {
  32. public class RegistryCore : IRegistryCore
  33. {
  34. /// <value>
  35. /// The module interfaces available from this scene.
  36. /// </value>
  37. protected Dictionary<Type, List<object>> ModuleInterfaces = new Dictionary<Type, List<object>>();
  38. #region IRegistryCore Members
  39. /// <summary>
  40. /// Register an interface to a region module. This allows module methods to be called directly as
  41. /// well as via events. If there is already a module registered for this interface, it is not replaced
  42. /// (is this the best behaviour?)
  43. /// </summary>
  44. /// <param name = "mod"></param>
  45. public void RegisterModuleInterface<T>(T mod)
  46. {
  47. // MainConsole.Instance.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
  48. List<Object> l = null;
  49. if (!ModuleInterfaces.TryGetValue(typeof (T), out l))
  50. {
  51. l = new List<Object>();
  52. ModuleInterfaces.Add(typeof (T), l);
  53. }
  54. if (l.Count > 0)
  55. l.Clear();
  56. l.Add(mod);
  57. }
  58. public void AddModuleInterfaces(Dictionary<Type, List<object>> dictionary)
  59. {
  60. foreach (KeyValuePair<Type, List<object>> kvp in dictionary)
  61. {
  62. if (!ModuleInterfaces.ContainsKey(kvp.Key))
  63. ModuleInterfaces.Add(kvp.Key, kvp.Value);
  64. else
  65. ModuleInterfaces[kvp.Key].AddRange(kvp.Value);
  66. }
  67. }
  68. public void UnregisterModuleInterface<T>(T mod)
  69. {
  70. List<Object> l;
  71. if (ModuleInterfaces.TryGetValue(typeof (T), out l))
  72. {
  73. l.Remove(mod);
  74. }
  75. }
  76. public void StackModuleInterface<T>(T mod)
  77. {
  78. List<Object> l;
  79. l = ModuleInterfaces.ContainsKey(typeof (T)) ? ModuleInterfaces[typeof (T)] : new List<Object>();
  80. if (l.Contains(mod))
  81. return;
  82. l.Add(mod);
  83. ModuleInterfaces[typeof (T)] = l;
  84. }
  85. /// <summary>
  86. /// For the given interface, retrieve the region module which implements it.
  87. /// </summary>
  88. /// <returns>null if there is no registered module implementing that interface</returns>
  89. public T RequestModuleInterface<T>()
  90. {
  91. if (ModuleInterfaces.ContainsKey(typeof (T)) &&
  92. (ModuleInterfaces[typeof (T)].Count > 0))
  93. return (T) ModuleInterfaces[typeof (T)][0];
  94. else
  95. return default(T);
  96. }
  97. public bool TryRequestModuleInterface<T>(out T iface)
  98. {
  99. iface = default(T);
  100. if (ModuleInterfaces.ContainsKey(typeof (T)) &&
  101. (ModuleInterfaces[typeof (T)].Count > 0))
  102. {
  103. iface = (T) ModuleInterfaces[typeof (T)][0];
  104. return true;
  105. }
  106. else
  107. return false;
  108. }
  109. /// <summary>
  110. /// For the given interface, retrieve an array of region modules that implement it.
  111. /// </summary>
  112. /// <returns>an empty array if there are no registered modules implementing that interface</returns>
  113. public T[] RequestModuleInterfaces<T>()
  114. {
  115. if (ModuleInterfaces.ContainsKey(typeof (T)))
  116. {
  117. #if (!ISWIN)
  118. List<T> ret = new List<T>();
  119. foreach (Object o in ModuleInterfaces[typeof(T)])
  120. ret.Add((T)o);
  121. return ret.ToArray();
  122. #else
  123. return ModuleInterfaces[typeof (T)].Select(o => (T) o).ToArray();
  124. #endif
  125. }
  126. else
  127. {
  128. return new[] {default(T)};
  129. }
  130. }
  131. public Dictionary<Type, List<object>> GetInterfaces()
  132. {
  133. //Flatten the array
  134. #if (!ISWIN)
  135. Dictionary<Type, List<object>> dictionary = new Dictionary<Type, List<object>>();
  136. foreach (KeyValuePair<Type, List<object>> @interface in ModuleInterfaces)
  137. dictionary.Add(@interface.Key, @interface.Value);
  138. return dictionary;
  139. #else
  140. return ModuleInterfaces.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
  141. #endif
  142. }
  143. public void RemoveAllInterfaces()
  144. {
  145. ModuleInterfaces.Clear();
  146. }
  147. #endregion
  148. }
  149. }