SitStandCommandsModule.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.OutputFormat(
  124. "Sitting {0} on {1} {2} in {3}",
  125. sp.Name, sitPart.ParentGroup.Name, sitPart.ParentGroup.UUID, m_scene.Name);
  126. sp.HandleAgentRequestSit(sp.ControllingClient, sp.UUID, sitPart.UUID, Vector3.Zero);
  127. sp.HandleAgentSit(sp.ControllingClient, sp.UUID);
  128. }
  129. else
  130. {
  131. MainConsole.Instance.OutputFormat(
  132. "Could not find any unoccupied set seat on which to sit {0} in {1}. Aborting",
  133. sp.Name, m_scene.Name);
  134. break;
  135. }
  136. }
  137. }
  138. private void HandleStandUserNameCommand(string module, string[] cmd)
  139. {
  140. if (MainConsole.Instance.ConsoleScene != m_scene && MainConsole.Instance.ConsoleScene != null)
  141. return;
  142. if (cmd.Length < 5)
  143. {
  144. MainConsole.Instance.Output("Usage: stand user name [--regex] <first-name> <last-name>");
  145. return;
  146. }
  147. List<ScenePresence> scenePresences = GetScenePresences(cmd);
  148. foreach (ScenePresence sp in scenePresences)
  149. {
  150. if (sp.SitGround || sp.IsSatOnObject)
  151. {
  152. MainConsole.Instance.OutputFormat("Standing {0} in {1}", sp.Name, m_scene.Name);
  153. sp.StandUp();
  154. }
  155. }
  156. }
  157. private List<ScenePresence> GetScenePresences(string[] cmdParams)
  158. {
  159. bool useRegex = false;
  160. OptionSet options = new OptionSet().Add("regex", v=> useRegex = v != null );
  161. List<string> mainParams = options.Parse(cmdParams);
  162. string firstName = mainParams[3];
  163. string lastName = mainParams[4];
  164. List<ScenePresence> scenePresencesMatched = new List<ScenePresence>();
  165. if (useRegex)
  166. {
  167. Regex nameRegex = new Regex(string.Format("{0} {1}", firstName, lastName));
  168. List<ScenePresence> scenePresences = m_scene.GetScenePresences();
  169. foreach (ScenePresence sp in scenePresences)
  170. {
  171. if (!sp.IsChildAgent && nameRegex.IsMatch(sp.Name))
  172. scenePresencesMatched.Add(sp);
  173. }
  174. }
  175. else
  176. {
  177. ScenePresence sp = m_scene.GetScenePresence(firstName, lastName);
  178. if (sp != null && !sp.IsChildAgent)
  179. scenePresencesMatched.Add(sp);
  180. }
  181. return scenePresencesMatched;
  182. }
  183. }
  184. }