PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Aurora/Framework/Services/ClassHelpers/Other/AuroraEventManager.cs

https://bitbucket.org/VirtualReality/aurora-sim
C# | 151 lines | 51 code | 6 blank | 94 comment | 6 complexity | ea4bca7294d10df74b55c18ae3a37827 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.Collections.Generic;
  28. using System.Linq;
  29. namespace Aurora.Framework
  30. {
  31. /// <summary>
  32. /// Delegate to fire when a generic event comes in
  33. /// </summary>
  34. /// <param name = "FunctionName">Name of the event being fired</param>
  35. /// <param name = "parameters">Parameters that the event has, can be null</param>
  36. public delegate object OnGenericEventHandler(string FunctionName, object parameters);
  37. /// <summary>
  38. /// A generic event manager that fires one event for many generic events
  39. /// </summary>
  40. public class AuroraEventManager
  41. {
  42. private readonly Dictionary<string, List<OnGenericEventHandler>> m_events =
  43. new Dictionary<string, List<OnGenericEventHandler>>();
  44. /// <summary>
  45. /// Events so far:
  46. ///
  47. /// DrawDistanceChanged - Changed Draw Distance
  48. /// param is a ScenePresence
  49. /// BanUser - Added a new banned user to the estate bans
  50. /// param is a UUID of an agent
  51. /// UnBanUser - Removed a banned user from the estate bans
  52. /// param is a UUID of an agent
  53. /// SignficantCameraMovement - The Camera has moved a distance that has triggered this update
  54. /// param is a ScenePresence
  55. /// ObjectSelected - An object has been selected
  56. /// param is a SceneObjectPart
  57. /// ObjectDeselected - An object has been selected
  58. /// param is a SceneObjectPart
  59. /// ObjectChangedOwner - An object's owner was changed
  60. /// param is a SceneObjectGroup
  61. /// ObjectChangedPhysicalStatus - An object's physical status has changed
  62. /// param is a SceneObjectGroup
  63. /// ObjectEnteringNewParcel - An object has entered a new parcel
  64. /// param is an object[], with o[0] a SceneObjectGroup, o[1] the new parcel UUID, and o[2] the old parcel UUID
  65. /// RegionRegistered - New Region has been registered
  66. /// param is a GridRegion
  67. /// UserStatusChange - User's status (logged in/out) has changed
  68. /// param is a object[], with o[0] the UUID of the user (as a string), o[1] whether they are logging in, o[2] the region they are entering (if logging in)
  69. /// PreRegisterRegion - A region is about to be registered
  70. /// param is a GridRegion
  71. /// NewUserConnection - A new user has been added to the scene (child or root)
  72. /// param is an object[], with o[0] OSDMap that will be returned to the server, and o[1] the AgentCircuitData that will be added to the region
  73. /// EstateUpdated - An estate has been updated
  74. /// param is the EstateSettings of the changed estate
  75. /// GridRegionRegistered - New Region has been registered with remote grid service
  76. /// param is an object[], with o[0] a GridRegion, o[1] the OSDMap response from the server
  77. /// ObjectAddedFlag - An object in the Scene has added a prim flag
  78. /// param is an object[], with o[0] a ISceneChildEntity and o[1] the flag that was changed
  79. /// ObjectRemovedFlag - An object in the Scene has removed a prim flag
  80. /// param is an object[], with o[0] a ISceneChildEntity and o[1] the flag that was changed
  81. /// SetAppearance - An avatar has updated their appearance
  82. /// param is an object[], with o[0] the UUID of the avatar and o[1] the AvatarData that is to be updated
  83. /// GridRegionSuccessfullyRegistered - Aurora.Server, A region has registered with the grid service
  84. /// param is an object[], with o[0] the OSDMap which will be sent to the new region, o[1] the SessionID, o[2] the GridRegion that registered
  85. /// Backup - The 'backup' console command was triggered, everything should backup
  86. /// no params
  87. /// AssetRequested - This is fired when the asset is requested by the client
  88. /// params are IClientAPI and TransferRequestPacket
  89. /// RegionInfoChanged - The RegionInfo changed for a region (Aurora.exe only)
  90. /// params are the old RegionInfo and the new RegionInfo
  91. /// DeleteUserInformation - The user is being deleted, remove all of their information from all databases
  92. /// params are the user's UUID
  93. /// CreateUserInformation - The user account is being created
  94. /// params are the user's UUID
  95. /// UpdateUserInformation - The user account is being updated
  96. /// params are the user's UUID
  97. /// </summary>
  98. public void RegisterEventHandler(string functionName, OnGenericEventHandler handler)
  99. {
  100. lock (m_events)
  101. {
  102. if (!m_events.ContainsKey(functionName))
  103. m_events.Add(functionName, new List<OnGenericEventHandler>());
  104. m_events[functionName].Add(handler);
  105. }
  106. }
  107. public void UnregisterEventHandler(string functionName, OnGenericEventHandler handler)
  108. {
  109. lock (m_events)
  110. {
  111. if (!m_events.ContainsKey(functionName))
  112. return;
  113. m_events[functionName].Remove(handler);
  114. }
  115. }
  116. /// <summary>
  117. /// Fire a generic event for all modules hooking onto it
  118. /// </summary>
  119. /// <param name = "FunctionName">Name of event to trigger</param>
  120. /// <param name = "Param">Any parameters to pass along with the event</param>
  121. public List<object> FireGenericEventHandler(string FunctionName, object Param)
  122. {
  123. List<object> retVal = new List<object>();
  124. lock (m_events)
  125. {
  126. //If not null, fire for all
  127. List<OnGenericEventHandler> events;
  128. if (m_events.TryGetValue(FunctionName, out events))
  129. {
  130. #if (!ISWIN)
  131. foreach (OnGenericEventHandler handler in new List<OnGenericEventHandler>(events))
  132. {
  133. object param = handler(FunctionName, Param);
  134. if (param != null)
  135. retVal.Add(param);
  136. }
  137. #else
  138. retVal.AddRange(new List<OnGenericEventHandler>(events).Select(handler => handler(FunctionName, Param)).Where(param => param != null));
  139. #endif
  140. }
  141. }
  142. return retVal;
  143. }
  144. }
  145. }