SitStandCommandsModule.cs 8.1 KB

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