1
0

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.ScriptBase;
  45. using OpenSim.Services.Interfaces;
  46. using OpenSim.Tests.Common;
  47. using OpenSim.Tests.Common.Mock;
  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);
  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);
  112. string npcRaw;
  113. bool gotExpectedException = false;
  114. try
  115. {
  116. npcRaw
  117. = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), "not existing notecard name");
  118. }
  119. catch (ScriptException)
  120. {
  121. gotExpectedException = true;
  122. }
  123. Assert.That(gotExpectedException, Is.True);
  124. }
  125. /// <summary>
  126. /// Test creation of an NPC where the appearance data comes from an avatar already in the region.
  127. /// </summary>
  128. [Test]
  129. public void TestOsNpcCreateUsingAppearanceFromAvatar()
  130. {
  131. TestHelpers.InMethod();
  132. // TestHelpers.EnableLogging();
  133. // Store an avatar with a different height from default in a notecard.
  134. UUID userId = TestHelpers.ParseTail(0x1);
  135. float newHeight = 1.9f;
  136. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  137. sp.Appearance.AvatarHeight = newHeight;
  138. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  139. SceneObjectPart part = so.RootPart;
  140. m_scene.AddSceneObject(so);
  141. OSSL_Api osslApi = new OSSL_Api();
  142. osslApi.Initialize(m_engine, part, null);
  143. string notecardName = "appearanceNc";
  144. osslApi.osOwnerSaveAppearance(notecardName);
  145. // Try creating a bot using the existing avatar's appearance
  146. string npcRaw = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), sp.UUID.ToString());
  147. Assert.That(npcRaw, Is.Not.Null);
  148. UUID npcId = new UUID(npcRaw);
  149. ScenePresence npc = m_scene.GetScenePresence(npcId);
  150. Assert.That(npc, Is.Not.Null);
  151. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(newHeight));
  152. }
  153. [Test]
  154. public void TestOsNpcLoadAppearance()
  155. {
  156. TestHelpers.InMethod();
  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);
  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);
  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);
  239. OSSL_Api otherOsslApi = new OSSL_Api();
  240. otherOsslApi.Initialize(m_engine, otherPart, 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);
  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. }