PhysicsParameters.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.Reflection;
  29. using System.Collections.Generic;
  30. using log4net;
  31. using Mono.Addins;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Region.CoreModules.Framework.InterfaceCommander;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Region.Physics.Manager;
  39. namespace OpenSim.Region.OptionalModules.PhysicsParameters
  40. {
  41. /// <summary>
  42. /// </summary>
  43. /// <remarks>
  44. /// </remarks>
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "PhysicsParameters")]
  46. public class PhysicsParameters : ISharedRegionModule
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. // private static string LogHeader = "[PHYSICS PARAMETERS]";
  50. private List<Scene> m_scenes = new List<Scene>();
  51. private static bool m_commandsLoaded = false;
  52. #region ISharedRegionModule
  53. public string Name { get { return "Runtime Physics Parameter Module"; } }
  54. public Type ReplaceableInterface { get { return null; } }
  55. public void Initialise(IConfigSource source)
  56. {
  57. // m_log.DebugFormat("{0}: INITIALIZED MODULE", LogHeader);
  58. }
  59. public void PostInitialise()
  60. {
  61. // m_log.DebugFormat("[{0}: POST INITIALIZED MODULE", LogHeader);
  62. InstallInterfaces();
  63. }
  64. public void Close()
  65. {
  66. // m_log.DebugFormat("{0}: CLOSED MODULE", LogHeader);
  67. }
  68. public void AddRegion(Scene scene)
  69. {
  70. // m_log.DebugFormat("{0}: REGION {1} ADDED", LogHeader, scene.RegionInfo.RegionName);
  71. m_scenes.Add(scene);
  72. }
  73. public void RemoveRegion(Scene scene)
  74. {
  75. // m_log.DebugFormat("{0}: REGION {1} REMOVED", LogHeader, scene.RegionInfo.RegionName);
  76. if (m_scenes.Contains(scene))
  77. m_scenes.Remove(scene);
  78. }
  79. public void RegionLoaded(Scene scene)
  80. {
  81. // m_log.DebugFormat("{0}: REGION {1} LOADED", LogHeader, scene.RegionInfo.RegionName);
  82. }
  83. #endregion INonSharedRegionModule
  84. private const string getInvocation = "physics get [<param>|ALL]";
  85. private const string setInvocation = "physics set <param> [<value>|TRUE|FALSE] [localID|ALL]";
  86. private const string listInvocation = "physics list";
  87. private void InstallInterfaces()
  88. {
  89. if (!m_commandsLoaded)
  90. {
  91. MainConsole.Instance.Commands.AddCommand(
  92. "Regions", false, "physics set",
  93. setInvocation,
  94. "Set physics parameter from currently selected region",
  95. ProcessPhysicsSet);
  96. MainConsole.Instance.Commands.AddCommand(
  97. "Regions", false, "physics get",
  98. getInvocation,
  99. "Get physics parameter from currently selected region",
  100. ProcessPhysicsGet);
  101. MainConsole.Instance.Commands.AddCommand(
  102. "Regions", false, "physics list",
  103. listInvocation,
  104. "List settable physics parameters",
  105. ProcessPhysicsList);
  106. m_commandsLoaded = true;
  107. }
  108. }
  109. // TODO: extend get so you can get a value from an individual localID
  110. private void ProcessPhysicsGet(string module, string[] cmdparms)
  111. {
  112. if (cmdparms.Length != 3)
  113. {
  114. WriteError("Parameter count error. Invocation: " + getInvocation);
  115. return;
  116. }
  117. string parm = cmdparms[2];
  118. if (SceneManager.Instance == null || SceneManager.Instance.CurrentScene == null)
  119. {
  120. WriteError("Error: no region selected. Use 'change region' to select a region.");
  121. return;
  122. }
  123. Scene scene = SceneManager.Instance.CurrentScene;
  124. IPhysicsParameters physScene = scene.PhysicsScene as IPhysicsParameters;
  125. if (physScene != null)
  126. {
  127. if (parm.ToLower() == "all")
  128. {
  129. foreach (PhysParameterEntry ppe in physScene.GetParameterList())
  130. {
  131. float val = 0.0f;
  132. if (physScene.GetPhysicsParameter(ppe.name, out val))
  133. {
  134. WriteOut(" {0}/{1} = {2}", scene.RegionInfo.RegionName, ppe.name, val);
  135. }
  136. else
  137. {
  138. WriteOut(" {0}/{1} = {2}", scene.RegionInfo.RegionName, ppe.name, "unknown");
  139. }
  140. }
  141. }
  142. else
  143. {
  144. float val = 0.0f;
  145. if (physScene.GetPhysicsParameter(parm, out val))
  146. {
  147. WriteOut(" {0}/{1} = {2}", scene.RegionInfo.RegionName, parm, val);
  148. }
  149. else
  150. {
  151. WriteError("Failed fetch of parameter '{0}' from region '{1}'", parm, scene.RegionInfo.RegionName);
  152. }
  153. }
  154. }
  155. else
  156. {
  157. WriteError("Region '{0}' physics engine has no gettable physics parameters", scene.RegionInfo.RegionName);
  158. }
  159. return;
  160. }
  161. private void ProcessPhysicsSet(string module, string[] cmdparms)
  162. {
  163. if (cmdparms.Length < 4 || cmdparms.Length > 5)
  164. {
  165. WriteError("Parameter count error. Invocation: " + getInvocation);
  166. return;
  167. }
  168. string parm = "xxx";
  169. float val = 0f;
  170. uint localID = (uint)PhysParameterEntry.APPLY_TO_NONE; // set default value
  171. try
  172. {
  173. parm = cmdparms[2];
  174. string valparm = cmdparms[3].ToLower();
  175. if (valparm == "true")
  176. val = PhysParameterEntry.NUMERIC_TRUE;
  177. else
  178. {
  179. if (valparm == "false")
  180. val = PhysParameterEntry.NUMERIC_FALSE;
  181. else
  182. val = float.Parse(valparm, Culture.NumberFormatInfo);
  183. }
  184. if (cmdparms.Length > 4)
  185. {
  186. if (cmdparms[4].ToLower() == "all")
  187. localID = (uint)PhysParameterEntry.APPLY_TO_ALL;
  188. else
  189. localID = uint.Parse(cmdparms[2], Culture.NumberFormatInfo);
  190. }
  191. }
  192. catch
  193. {
  194. WriteError(" Error parsing parameters. Invocation: " + setInvocation);
  195. return;
  196. }
  197. if (SceneManager.Instance == null || SceneManager.Instance.CurrentScene == null)
  198. {
  199. WriteError("Error: no region selected. Use 'change region' to select a region.");
  200. return;
  201. }
  202. Scene scene = SceneManager.Instance.CurrentScene;
  203. IPhysicsParameters physScene = scene.PhysicsScene as IPhysicsParameters;
  204. if (physScene != null)
  205. {
  206. if (!physScene.SetPhysicsParameter(parm, val, localID))
  207. {
  208. WriteError("Failed set of parameter '{0}' for region '{1}'", parm, scene.RegionInfo.RegionName);
  209. }
  210. }
  211. else
  212. {
  213. WriteOut("Region '{0}'s physics engine has no settable physics parameters", scene.RegionInfo.RegionName);
  214. }
  215. return;
  216. }
  217. private void ProcessPhysicsList(string module, string[] cmdparms)
  218. {
  219. if (SceneManager.Instance == null || SceneManager.Instance.CurrentScene == null)
  220. {
  221. WriteError("Error: no region selected. Use 'change region' to select a region.");
  222. return;
  223. }
  224. Scene scene = SceneManager.Instance.CurrentScene;
  225. IPhysicsParameters physScene = scene.PhysicsScene as IPhysicsParameters;
  226. if (physScene != null)
  227. {
  228. WriteOut("Available physics parameters:");
  229. PhysParameterEntry[] parms = physScene.GetParameterList();
  230. foreach (PhysParameterEntry ent in parms)
  231. {
  232. WriteOut(" {0}: {1}", ent.name, ent.desc);
  233. }
  234. }
  235. else
  236. {
  237. WriteError("Current regions's physics engine has no settable physics parameters");
  238. }
  239. return;
  240. }
  241. private void WriteOut(string msg, params object[] args)
  242. {
  243. m_log.InfoFormat(msg, args);
  244. // MainConsole.Instance.OutputFormat(msg, args);
  245. }
  246. private void WriteError(string msg, params object[] args)
  247. {
  248. m_log.ErrorFormat(msg, args);
  249. // MainConsole.Instance.OutputFormat(msg, args);
  250. }
  251. }
  252. }