SitStandCommandsModule.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) Contributors, 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 OpenSimulator 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 System.Reflection;
  31. using System.Text;
  32. using System.Text.RegularExpressions;
  33. using log4net;
  34. using Mono.Addins;
  35. using NDesk.Options;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Console;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. namespace OpenSim.Region.OptionalModules.Avatar.SitStand
  43. {
  44. /// <summary>
  45. /// A module that just holds commands for changing avatar sitting and standing states.
  46. /// </summary>
  47. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AnimationsCommandModule")]
  48. public class SitStandCommandModule : INonSharedRegionModule
  49. {
  50. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private Scene m_scene;
  52. public string Name { get { return "SitStand Command Module"; } }
  53. public Type ReplaceableInterface { get { return null; } }
  54. public void Initialise(IConfigSource source)
  55. {
  56. // m_log.DebugFormat("[ANIMATIONS COMMAND MODULE]: INITIALIZED MODULE");
  57. }
  58. public void PostInitialise()
  59. {
  60. // m_log.DebugFormat("[ANIMATIONS COMMAND MODULE]: POST INITIALIZED MODULE");
  61. }
  62. public void Close()
  63. {
  64. // m_log.DebugFormat("[ANIMATIONS COMMAND MODULE]: CLOSED MODULE");
  65. }
  66. public void AddRegion(Scene scene)
  67. {
  68. // m_log.DebugFormat("[ANIMATIONS COMMAND MODULE]: REGION {0} ADDED", scene.RegionInfo.RegionName);
  69. }
  70. public void RemoveRegion(Scene scene)
  71. {
  72. // m_log.DebugFormat("[ATTACHMENTS COMMAND MODULE]: REGION {0} REMOVED", scene.RegionInfo.RegionName);
  73. }
  74. public void RegionLoaded(Scene scene)
  75. {
  76. // m_log.DebugFormat("[ANIMATIONS COMMAND MODULE]: REGION {0} LOADED", scene.RegionInfo.RegionName);
  77. m_scene = scene;
  78. scene.AddCommand(
  79. "Users", this, "sit user name",
  80. "sit user name [--regex] <first-name> <last-name>",
  81. "Sit the named user on an unoccupied object with a sit target.",
  82. "If there are no such objects then nothing happens.\n"
  83. + "If --regex is specified then the names are treated as regular expressions.",
  84. HandleSitUserNameCommand);
  85. scene.AddCommand(
  86. "Users", this, "stand user name",
  87. "stand user name [--regex] <first-name> <last-name>",
  88. "Stand the named user.",
  89. "If --regex is specified then the names are treated as regular expressions.",
  90. HandleStandUserNameCommand);
  91. }
  92. private void HandleSitUserNameCommand(string module, string[] cmd)
  93. {
  94. if (MainConsole.Instance.ConsoleScene != m_scene && MainConsole.Instance.ConsoleScene != null)
  95. return;
  96. if (cmd.Length < 5)
  97. {
  98. MainConsole.Instance.Output("Usage: sit user name [--regex] <first-name> <last-name>");
  99. return;
  100. }
  101. List<ScenePresence> scenePresences = GetScenePresences(cmd);
  102. foreach (ScenePresence sp in scenePresences)
  103. {
  104. if (sp.SitGround || sp.IsSatOnObject)
  105. continue;
  106. SceneObjectPart sitPart = null;
  107. List<SceneObjectGroup> sceneObjects = m_scene.GetSceneObjectGroups();
  108. foreach (SceneObjectGroup sceneObject in sceneObjects)
  109. {
  110. if (sceneObject.IsAttachment)
  111. continue;
  112. foreach (SceneObjectPart part in sceneObject.Parts)
  113. {
  114. if (part.IsSitTargetSet && part.SitTargetAvatar == UUID.Zero)
  115. {
  116. sitPart = part;
  117. break;
  118. }
  119. }
  120. }
  121. if (sitPart != null)
  122. {
  123. MainConsole.Instance.Output(
  124. "Sitting {0} on {1} {2} in {3}",
  125. null,
  126. sp.Name, sitPart.ParentGroup.Name, sitPart.ParentGroup.UUID, m_scene.Name);
  127. sp.HandleAgentRequestSit(sp.ControllingClient, sp.UUID, sitPart.UUID, Vector3.Zero);
  128. sp.HandleAgentSit(sp.ControllingClient, sp.UUID);
  129. }
  130. else
  131. {
  132. MainConsole.Instance.Output(
  133. "Could not find any unoccupied set seat on which to sit {0} in {1}. Aborting",
  134. null,
  135. sp.Name, m_scene.Name);
  136. break;
  137. }
  138. }
  139. }
  140. private void HandleStandUserNameCommand(string module, string[] cmd)
  141. {
  142. if (MainConsole.Instance.ConsoleScene != m_scene && MainConsole.Instance.ConsoleScene != null)
  143. return;
  144. if (cmd.Length < 5)
  145. {
  146. MainConsole.Instance.Output("Usage: stand user name [--regex] <first-name> <last-name>");
  147. return;
  148. }
  149. List<ScenePresence> scenePresences = GetScenePresences(cmd);
  150. foreach (ScenePresence sp in scenePresences)
  151. {
  152. if (sp.SitGround || sp.IsSatOnObject)
  153. {
  154. MainConsole.Instance.Output("Standing {0} in {1}", null, sp.Name, m_scene.Name);
  155. sp.StandUp();
  156. }
  157. }
  158. }
  159. private List<ScenePresence> GetScenePresences(string[] cmdParams)
  160. {
  161. bool useRegex = false;
  162. OptionSet options = new OptionSet().Add("regex", v=> useRegex = v != null );
  163. List<string> mainParams = options.Parse(cmdParams);
  164. string firstName = mainParams[3];
  165. string lastName = mainParams[4];
  166. List<ScenePresence> scenePresencesMatched = new List<ScenePresence>();
  167. if (useRegex)
  168. {
  169. Regex nameRegex = new Regex(string.Format("{0} {1}", firstName, lastName));
  170. List<ScenePresence> scenePresences = m_scene.GetScenePresences();
  171. foreach (ScenePresence sp in scenePresences)
  172. {
  173. if (!sp.IsChildAgent && nameRegex.IsMatch(sp.Name))
  174. scenePresencesMatched.Add(sp);
  175. }
  176. }
  177. else
  178. {
  179. ScenePresence sp = m_scene.GetScenePresence(firstName, lastName);
  180. if (sp != null && !sp.IsChildAgent)
  181. scenePresencesMatched.Add(sp);
  182. }
  183. return scenePresencesMatched;
  184. }
  185. }
  186. }