OSSL_ApiNpcTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.Reflection;
  30. using System.Text;
  31. using log4net;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenMetaverse;
  35. using OpenMetaverse.Assets;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.CoreModules.Avatar.Attachments;
  39. using OpenSim.Region.CoreModules.Avatar.AvatarFactory;
  40. using OpenSim.Region.OptionalModules.World.NPC;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Region.ScriptEngine.Shared;
  43. using OpenSim.Region.ScriptEngine.Shared.Api;
  44. using OpenSim.Region.ScriptEngine.Shared.Instance;
  45. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  46. using OpenSim.Services.Interfaces;
  47. using OpenSim.Tests.Common;
  48. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  49. {
  50. /// <summary>
  51. /// Tests for OSSL NPC API
  52. /// </summary>
  53. [TestFixture]
  54. public class OSSL_NpcApiAppearanceTest : OpenSimTestCase
  55. {
  56. protected Scene m_scene;
  57. protected XEngine.XEngine m_engine;
  58. [SetUp]
  59. public override void SetUp()
  60. {
  61. base.SetUp();
  62. IConfigSource initConfigSource = new IniConfigSource();
  63. IConfig config = initConfigSource.AddConfig("XEngine");
  64. config.Set("Enabled", "true");
  65. config.Set("AllowOSFunctions", "true");
  66. config.Set("OSFunctionThreatLevel", "Severe");
  67. config = initConfigSource.AddConfig("NPC");
  68. config.Set("Enabled", "true");
  69. m_scene = new SceneHelpers().SetupScene();
  70. SceneHelpers.SetupSceneModules(
  71. m_scene, initConfigSource, new AvatarFactoryModule(), new AttachmentsModule(), new NPCModule());
  72. m_engine = new XEngine.XEngine();
  73. m_engine.Initialise(initConfigSource);
  74. m_engine.AddRegion(m_scene);
  75. }
  76. /// <summary>
  77. /// Test creation of an NPC where the appearance data comes from a notecard
  78. /// </summary>
  79. [Test]
  80. public void TestOsNpcCreateUsingAppearanceFromNotecard()
  81. {
  82. TestHelpers.InMethod();
  83. // Store an avatar with a different height from default in a notecard.
  84. UUID userId = TestHelpers.ParseTail(0x1);
  85. float newHeight = 1.9f;
  86. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  87. sp.Appearance.AvatarHeight = newHeight;
  88. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  89. SceneObjectPart part = so.RootPart;
  90. m_scene.AddSceneObject(so);
  91. OSSL_Api osslApi = new OSSL_Api();
  92. osslApi.Initialize(m_engine, part, null, null);
  93. string notecardName = "appearanceNc";
  94. osslApi.osOwnerSaveAppearance(notecardName);
  95. // Try creating a bot using the appearance in the notecard.
  96. string npcRaw = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), notecardName);
  97. Assert.That(npcRaw, Is.Not.Null);
  98. UUID npcId = new UUID(npcRaw);
  99. ScenePresence npc = m_scene.GetScenePresence(npcId);
  100. Assert.That(npc, Is.Not.Null);
  101. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(newHeight));
  102. }
  103. [Test]
  104. public void TestOsNpcCreateNotExistingNotecard()
  105. {
  106. TestHelpers.InMethod();
  107. UUID userId = TestHelpers.ParseTail(0x1);
  108. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  109. m_scene.AddSceneObject(so);
  110. OSSL_Api osslApi = new OSSL_Api();
  111. osslApi.Initialize(m_engine, so.RootPart, null, null);
  112. bool gotExpectedException = false;
  113. try
  114. {
  115. osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), "not existing notecard name");
  116. }
  117. catch (ScriptException)
  118. {
  119. gotExpectedException = true;
  120. }
  121. Assert.That(gotExpectedException, Is.True);
  122. }
  123. /// <summary>
  124. /// Test creation of an NPC where the appearance data comes from an avatar already in the region.
  125. /// </summary>
  126. [Test]
  127. public void TestOsNpcCreateUsingAppearanceFromAvatar()
  128. {
  129. TestHelpers.InMethod();
  130. // TestHelpers.EnableLogging();
  131. // Store an avatar with a different height from default in a notecard.
  132. UUID userId = TestHelpers.ParseTail(0x1);
  133. float newHeight = 1.9f;
  134. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  135. sp.Appearance.AvatarHeight = newHeight;
  136. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  137. SceneObjectPart part = so.RootPart;
  138. m_scene.AddSceneObject(so);
  139. OSSL_Api osslApi = new OSSL_Api();
  140. osslApi.Initialize(m_engine, part, null, null);
  141. string notecardName = "appearanceNc";
  142. osslApi.osOwnerSaveAppearance(notecardName);
  143. // Try creating a bot using the existing avatar's appearance
  144. string npcRaw = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), sp.UUID.ToString());
  145. Assert.That(npcRaw, Is.Not.Null);
  146. UUID npcId = new UUID(npcRaw);
  147. ScenePresence npc = m_scene.GetScenePresence(npcId);
  148. Assert.That(npc, Is.Not.Null);
  149. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(newHeight));
  150. }
  151. [Test]
  152. public void TestOsNpcLoadAppearance()
  153. {
  154. TestHelpers.InMethod();
  155. //TestHelpers.EnableLogging();
  156. // Store an avatar with a different height from default in a notecard.
  157. UUID userId = TestHelpers.ParseTail(0x1);
  158. float firstHeight = 1.9f;
  159. float secondHeight = 2.1f;
  160. string firstAppearanceNcName = "appearanceNc1";
  161. string secondAppearanceNcName = "appearanceNc2";
  162. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  163. sp.Appearance.AvatarHeight = firstHeight;
  164. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  165. SceneObjectPart part = so.RootPart;
  166. m_scene.AddSceneObject(so);
  167. OSSL_Api osslApi = new OSSL_Api();
  168. osslApi.Initialize(m_engine, part, null, null);
  169. osslApi.osOwnerSaveAppearance(firstAppearanceNcName);
  170. string npcRaw
  171. = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), firstAppearanceNcName);
  172. // Create a second appearance notecard with a different height
  173. sp.Appearance.AvatarHeight = secondHeight;
  174. osslApi.osOwnerSaveAppearance(secondAppearanceNcName);
  175. osslApi.osNpcLoadAppearance(npcRaw, secondAppearanceNcName);
  176. UUID npcId = new UUID(npcRaw);
  177. ScenePresence npc = m_scene.GetScenePresence(npcId);
  178. Assert.That(npc, Is.Not.Null);
  179. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(secondHeight));
  180. }
  181. [Test]
  182. public void TestOsNpcLoadAppearanceNotExistingNotecard()
  183. {
  184. TestHelpers.InMethod();
  185. // Store an avatar with a different height from default in a notecard.
  186. UUID userId = TestHelpers.ParseTail(0x1);
  187. float firstHeight = 1.9f;
  188. // float secondHeight = 2.1f;
  189. string firstAppearanceNcName = "appearanceNc1";
  190. string secondAppearanceNcName = "appearanceNc2";
  191. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  192. sp.Appearance.AvatarHeight = firstHeight;
  193. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  194. SceneObjectPart part = so.RootPart;
  195. m_scene.AddSceneObject(so);
  196. OSSL_Api osslApi = new OSSL_Api();
  197. osslApi.Initialize(m_engine, part, null, null);
  198. osslApi.osOwnerSaveAppearance(firstAppearanceNcName);
  199. string npcRaw
  200. = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), firstAppearanceNcName);
  201. bool gotExpectedException = false;
  202. try
  203. {
  204. osslApi.osNpcLoadAppearance(npcRaw, secondAppearanceNcName);
  205. }
  206. catch (ScriptException)
  207. {
  208. gotExpectedException = true;
  209. }
  210. Assert.That(gotExpectedException, Is.True);
  211. UUID npcId = new UUID(npcRaw);
  212. ScenePresence npc = m_scene.GetScenePresence(npcId);
  213. Assert.That(npc, Is.Not.Null);
  214. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(firstHeight));
  215. }
  216. /// <summary>
  217. /// Test removal of an owned NPC.
  218. /// </summary>
  219. [Test]
  220. public void TestOsNpcRemoveOwned()
  221. {
  222. TestHelpers.InMethod();
  223. // Store an avatar with a different height from default in a notecard.
  224. UUID userId = TestHelpers.ParseTail(0x1);
  225. UUID otherUserId = TestHelpers.ParseTail(0x2);
  226. float newHeight = 1.9f;
  227. SceneHelpers.AddScenePresence(m_scene, otherUserId);
  228. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  229. sp.Appearance.AvatarHeight = newHeight;
  230. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  231. SceneObjectPart part = so.RootPart;
  232. m_scene.AddSceneObject(so);
  233. SceneObjectGroup otherSo = SceneHelpers.CreateSceneObject(1, otherUserId, 0x20);
  234. SceneObjectPart otherPart = otherSo.RootPart;
  235. m_scene.AddSceneObject(otherSo);
  236. OSSL_Api osslApi = new OSSL_Api();
  237. osslApi.Initialize(m_engine, part, null, null);
  238. OSSL_Api otherOsslApi = new OSSL_Api();
  239. otherOsslApi.Initialize(m_engine, otherPart, null, null);
  240. string notecardName = "appearanceNc";
  241. osslApi.osOwnerSaveAppearance(notecardName);
  242. string npcRaw
  243. = osslApi.osNpcCreate(
  244. "Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), notecardName, ScriptBaseClass.OS_NPC_CREATOR_OWNED);
  245. otherOsslApi.osNpcRemove(npcRaw);
  246. // Should still be around
  247. UUID npcId = new UUID(npcRaw);
  248. ScenePresence npc = m_scene.GetScenePresence(npcId);
  249. Assert.That(npc, Is.Not.Null);
  250. osslApi.osNpcRemove(npcRaw);
  251. npc = m_scene.GetScenePresence(npcId);
  252. // Now the owner deleted it and it's gone
  253. Assert.That(npc, Is.Null);
  254. }
  255. /// <summary>
  256. /// Test removal of an unowned NPC.
  257. /// </summary>
  258. [Test]
  259. public void TestOsNpcRemoveUnowned()
  260. {
  261. TestHelpers.InMethod();
  262. // log4net.Config.XmlConfigurator.Configure();
  263. // Store an avatar with a different height from default in a notecard.
  264. UUID userId = TestHelpers.ParseTail(0x1);
  265. float newHeight = 1.9f;
  266. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  267. sp.Appearance.AvatarHeight = newHeight;
  268. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  269. SceneObjectPart part = so.RootPart;
  270. m_scene.AddSceneObject(so);
  271. OSSL_Api osslApi = new OSSL_Api();
  272. osslApi.Initialize(m_engine, part, null, null);
  273. string notecardName = "appearanceNc";
  274. osslApi.osOwnerSaveAppearance(notecardName);
  275. string npcRaw
  276. = osslApi.osNpcCreate(
  277. "Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), notecardName, ScriptBaseClass.OS_NPC_NOT_OWNED);
  278. osslApi.osNpcRemove(npcRaw);
  279. UUID npcId = new UUID(npcRaw);
  280. ScenePresence npc = m_scene.GetScenePresence(npcId);
  281. Assert.That(npc, Is.Null);
  282. }
  283. }
  284. }