OSSL_ApiNpcTests.cs 13 KB

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