Helpers.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.IO;
  29. using System.Threading;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Runtime.Serialization;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.CoreModules;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Region.Framework.Interfaces;
  39. namespace OpenSim.Region.ScriptEngine.Shared
  40. {
  41. [Serializable]
  42. public class EventAbortException : Exception
  43. {
  44. public EventAbortException()
  45. {
  46. }
  47. protected EventAbortException(
  48. SerializationInfo info,
  49. StreamingContext context)
  50. {
  51. }
  52. }
  53. [Serializable]
  54. public class SelfDeleteException : Exception
  55. {
  56. public SelfDeleteException()
  57. {
  58. }
  59. protected SelfDeleteException(
  60. SerializationInfo info,
  61. StreamingContext context)
  62. {
  63. }
  64. }
  65. [Serializable]
  66. public class ScriptDeleteException : Exception
  67. {
  68. public ScriptDeleteException()
  69. {
  70. }
  71. protected ScriptDeleteException(
  72. SerializationInfo info,
  73. StreamingContext context)
  74. {
  75. }
  76. }
  77. /// <summary>
  78. /// Used to signal when the script is stopping in co-operation with the script engine
  79. /// (instead of through Thread.Abort()).
  80. /// </summary>
  81. [Serializable]
  82. public class ScriptCoopStopException : Exception
  83. {
  84. public ScriptCoopStopException()
  85. {
  86. }
  87. protected ScriptCoopStopException(
  88. SerializationInfo info,
  89. StreamingContext context)
  90. {
  91. }
  92. }
  93. public class DetectParams
  94. {
  95. public const int AGENT = 1;
  96. public const int ACTIVE = 2;
  97. public const int PASSIVE = 4;
  98. public const int SCRIPTED = 8;
  99. public const int OS_NPC = 0x01000000;
  100. public DetectParams()
  101. {
  102. Key = UUID.Zero;
  103. OffsetPos = new LSL_Types.Vector3();
  104. LinkNum = 0;
  105. Group = UUID.Zero;
  106. Name = String.Empty;
  107. Owner = UUID.Zero;
  108. Position = new LSL_Types.Vector3();
  109. Rotation = new LSL_Types.Quaternion();
  110. Type = 0;
  111. Velocity = new LSL_Types.Vector3();
  112. initializeSurfaceTouch();
  113. }
  114. public UUID Key;
  115. public LSL_Types.Vector3 OffsetPos;
  116. public int LinkNum;
  117. public UUID Group;
  118. public string Name;
  119. public UUID Owner;
  120. public LSL_Types.Vector3 Position;
  121. public LSL_Types.Quaternion Rotation;
  122. public int Type;
  123. public LSL_Types.Vector3 Velocity;
  124. private LSL_Types.Vector3 touchST;
  125. public LSL_Types.Vector3 TouchST { get { return touchST; } }
  126. private LSL_Types.Vector3 touchNormal;
  127. public LSL_Types.Vector3 TouchNormal { get { return touchNormal; } }
  128. private LSL_Types.Vector3 touchBinormal;
  129. public LSL_Types.Vector3 TouchBinormal { get { return touchBinormal; } }
  130. private LSL_Types.Vector3 touchPos;
  131. public LSL_Types.Vector3 TouchPos { get { return touchPos; } }
  132. private LSL_Types.Vector3 touchUV;
  133. public LSL_Types.Vector3 TouchUV { get { return touchUV; } }
  134. private int touchFace;
  135. public int TouchFace { get { return touchFace; } }
  136. // This can be done in two places including the constructor
  137. // so be carefull what gets added here
  138. private void initializeSurfaceTouch()
  139. {
  140. touchST = new LSL_Types.Vector3(-1.0, -1.0, 0.0);
  141. touchNormal = new LSL_Types.Vector3();
  142. touchBinormal = new LSL_Types.Vector3();
  143. touchPos = new LSL_Types.Vector3();
  144. touchUV = new LSL_Types.Vector3(-1.0, -1.0, 0.0);
  145. touchFace = -1;
  146. }
  147. /*
  148. * Set up the surface touch detected values
  149. */
  150. public SurfaceTouchEventArgs SurfaceTouchArgs
  151. {
  152. set
  153. {
  154. if (value == null)
  155. {
  156. // Initialise to defaults if no value
  157. initializeSurfaceTouch();
  158. }
  159. else
  160. {
  161. // Set the values from the touch data provided by the client
  162. touchST = new LSL_Types.Vector3(value.STCoord);
  163. touchUV = new LSL_Types.Vector3(value.UVCoord);
  164. touchNormal = new LSL_Types.Vector3(value.Normal);
  165. touchBinormal = new LSL_Types.Vector3(value.Binormal);
  166. touchPos = new LSL_Types.Vector3(value.Position);
  167. touchFace = value.FaceIndex;
  168. }
  169. }
  170. }
  171. public void Populate(Scene scene)
  172. {
  173. SceneObjectPart part = scene.GetSceneObjectPart(Key);
  174. if (part == null) // Avatar, maybe?
  175. {
  176. ScenePresence presence = scene.GetScenePresence(Key);
  177. if (presence == null)
  178. return;
  179. Name = presence.Firstname + " " + presence.Lastname;
  180. Owner = Key;
  181. Position = new LSL_Types.Vector3(presence.AbsolutePosition);
  182. Rotation = new LSL_Types.Quaternion(presence.Rotation);
  183. Velocity = new LSL_Types.Vector3(presence.Velocity);
  184. Type = 0x01; // Avatar
  185. if (presence.PresenceType == PresenceType.Npc)
  186. Type = 0x20;
  187. if (presence.Velocity != Vector3.Zero)
  188. Type |= ACTIVE;
  189. Group = presence.ControllingClient.ActiveGroupId;
  190. return;
  191. }
  192. part = part.ParentGroup.RootPart; // We detect objects only
  193. LinkNum = 0; // Not relevant
  194. Group = part.GroupID;
  195. Name = part.Name;
  196. Owner = part.OwnerID;
  197. if (part.Velocity.IsZero())
  198. Type = PASSIVE;
  199. else
  200. Type = ACTIVE;
  201. foreach (SceneObjectPart p in part.ParentGroup.Parts)
  202. {
  203. if (p.Inventory.ContainsScripts())
  204. {
  205. Type |= SCRIPTED; // Scripted
  206. break;
  207. }
  208. }
  209. Position = new LSL_Types.Vector3(part.AbsolutePosition);
  210. Quaternion wr = part.ParentGroup.GroupRotation;
  211. Rotation = new LSL_Types.Quaternion(wr.X, wr.Y, wr.Z, wr.W);
  212. Velocity = new LSL_Types.Vector3(part.Velocity);
  213. }
  214. public void Populate(Scene scene, DetectedObject obj)
  215. {
  216. if(obj.keyUUID.IsZero()) // land
  217. {
  218. Position = new LSL_Types.Vector3(obj.posVector);
  219. Rotation.s = 1.0;
  220. return;
  221. }
  222. if((obj.colliderType & 0x21) != 0) // avatar or npc
  223. {
  224. ScenePresence presence = scene.GetScenePresence(obj.keyUUID);
  225. if (presence == null)
  226. return;
  227. Name = obj.nameStr;
  228. Key = obj.keyUUID;
  229. Owner = obj.ownerUUID;
  230. Group = obj.groupUUID;
  231. Position = new LSL_Types.Vector3(obj.posVector);
  232. Rotation = new LSL_Types.Quaternion(obj.rotQuat);
  233. Velocity = new LSL_Types.Vector3(obj.velVector);
  234. LinkNum = obj.linkNumber;
  235. Type = obj.colliderType;
  236. return;
  237. }
  238. SceneObjectPart part = scene.GetSceneObjectPart(obj.keyUUID);
  239. if(part == null)
  240. return;
  241. Name = obj.nameStr;
  242. Key = obj.keyUUID;
  243. Owner = obj.ownerUUID;
  244. Group = obj.groupUUID;
  245. Position = new LSL_Types.Vector3(obj.posVector);
  246. Rotation = new LSL_Types.Quaternion(obj.rotQuat);
  247. Velocity = new LSL_Types.Vector3(obj.velVector);
  248. LinkNum = obj.linkNumber;
  249. if(obj.velVector.IsZero())
  250. Type = 4;
  251. else
  252. Type = 2;
  253. part = part.ParentGroup.RootPart;
  254. foreach (SceneObjectPart p in part.ParentGroup.Parts)
  255. {
  256. if (p.Inventory.ContainsScripts())
  257. {
  258. // at sl a physical prim is active also if has active scripts
  259. // assuming all scripts are in run state to save time
  260. if((part.Flags & PrimFlags.Physics) != 0 )
  261. Type = 10; // script + active
  262. else
  263. Type |= SCRIPTED; // Scripted
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. /// <summary>
  270. /// Holds all the data required to execute a scripting event.
  271. /// </summary>
  272. public class EventParams
  273. {
  274. public EventParams(string eventName, object[] eventParams, DetectParams[] detectParams)
  275. {
  276. EventName = eventName;
  277. Params = eventParams;
  278. DetectParams = detectParams;
  279. }
  280. public string EventName;
  281. public object[] Params;
  282. public DetectParams[] DetectParams;
  283. public static EventParams StateEntryParams = new EventParams("state_entry", new object[0], new DetectParams[0]);
  284. }
  285. }