OSSL_ApiNpcTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 = initConfigSource.AddConfig("NPC");
  66. config.Set("Enabled", "true");
  67. config = initConfigSource.AddConfig("OSSL");
  68. config.Set("DebuggerSafe", false);
  69. config.Set("AllowOSFunctions", "true");
  70. config.Set("OSFunctionThreatLevel", "Severe");
  71. m_scene = new SceneHelpers().SetupScene();
  72. SceneHelpers.SetupSceneModules(
  73. m_scene, initConfigSource, new AvatarFactoryModule(), new AttachmentsModule(), new NPCModule());
  74. m_engine = new XEngine.XEngine();
  75. m_engine.Initialise(initConfigSource);
  76. m_engine.AddRegion(m_scene);
  77. }
  78. /// <summary>
  79. /// Test creation of an NPC where the appearance data comes from a notecard
  80. /// </summary>
  81. [Test]
  82. public void TestOsNpcCreateUsingAppearanceFromNotecard()
  83. {
  84. TestHelpers.InMethod();
  85. // Store an avatar with a different height from default in a notecard.
  86. UUID userId = TestHelpers.ParseTail(0x1);
  87. float newHeight = 1.9f;
  88. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  89. sp.Appearance.AvatarHeight = newHeight;
  90. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  91. SceneObjectPart part = so.RootPart;
  92. m_scene.AddSceneObject(so);
  93. OSSL_Api osslApi = new OSSL_Api();
  94. osslApi.Initialize(m_engine, part, null);
  95. string notecardName = "appearanceNc";
  96. osslApi.osOwnerSaveAppearance(notecardName);
  97. // Try creating a bot using the appearance in the notecard.
  98. string npcRaw = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), notecardName);
  99. Assert.That(npcRaw, Is.Not.Null);
  100. UUID npcId = new UUID(npcRaw);
  101. ScenePresence npc = m_scene.GetScenePresence(npcId);
  102. Assert.That(npc, Is.Not.Null);
  103. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(newHeight));
  104. }
  105. [Test]
  106. public void TestOsNpcCreateNotExistingNotecard()
  107. {
  108. TestHelpers.InMethod();
  109. UUID userId = TestHelpers.ParseTail(0x1);
  110. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  111. m_scene.AddSceneObject(so);
  112. OSSL_Api osslApi = new OSSL_Api();
  113. osslApi.Initialize(m_engine, so.RootPart, null);
  114. bool gotExpectedException = false;
  115. try
  116. {
  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. //TestHelpers.EnableLogging();
  158. // Store an avatar with a different height from default in a notecard.
  159. UUID userId = TestHelpers.ParseTail(0x1);
  160. float firstHeight = 1.9f;
  161. float secondHeight = 2.1f;
  162. string firstAppearanceNcName = "appearanceNc1";
  163. string secondAppearanceNcName = "appearanceNc2";
  164. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  165. sp.Appearance.AvatarHeight = firstHeight;
  166. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  167. SceneObjectPart part = so.RootPart;
  168. m_scene.AddSceneObject(so);
  169. OSSL_Api osslApi = new OSSL_Api();
  170. osslApi.Initialize(m_engine, part, null);
  171. osslApi.osOwnerSaveAppearance(firstAppearanceNcName);
  172. string npcRaw
  173. = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), firstAppearanceNcName);
  174. // Create a second appearance notecard with a different height
  175. sp.Appearance.AvatarHeight = secondHeight;
  176. osslApi.osOwnerSaveAppearance(secondAppearanceNcName);
  177. osslApi.osNpcLoadAppearance(npcRaw, secondAppearanceNcName);
  178. UUID npcId = new UUID(npcRaw);
  179. ScenePresence npc = m_scene.GetScenePresence(npcId);
  180. Assert.That(npc, Is.Not.Null);
  181. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(secondHeight));
  182. }
  183. [Test]
  184. public void TestOsNpcLoadAppearanceNotExistingNotecard()
  185. {
  186. TestHelpers.InMethod();
  187. // Store an avatar with a different height from default in a notecard.
  188. UUID userId = TestHelpers.ParseTail(0x1);
  189. float firstHeight = 1.9f;
  190. // float secondHeight = 2.1f;
  191. string firstAppearanceNcName = "appearanceNc1";
  192. string secondAppearanceNcName = "appearanceNc2";
  193. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  194. sp.Appearance.AvatarHeight = firstHeight;
  195. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  196. SceneObjectPart part = so.RootPart;
  197. m_scene.AddSceneObject(so);
  198. OSSL_Api osslApi = new OSSL_Api();
  199. osslApi.Initialize(m_engine, part, null);
  200. osslApi.osOwnerSaveAppearance(firstAppearanceNcName);
  201. string npcRaw
  202. = osslApi.osNpcCreate("Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), firstAppearanceNcName);
  203. bool gotExpectedException = false;
  204. try
  205. {
  206. osslApi.osNpcLoadAppearance(npcRaw, secondAppearanceNcName);
  207. }
  208. catch (ScriptException)
  209. {
  210. gotExpectedException = true;
  211. }
  212. Assert.That(gotExpectedException, Is.True);
  213. UUID npcId = new UUID(npcRaw);
  214. ScenePresence npc = m_scene.GetScenePresence(npcId);
  215. Assert.That(npc, Is.Not.Null);
  216. Assert.That(npc.Appearance.AvatarHeight, Is.EqualTo(firstHeight));
  217. }
  218. /// <summary>
  219. /// Test removal of an owned NPC.
  220. /// </summary>
  221. [Test]
  222. public void TestOsNpcRemoveOwned()
  223. {
  224. TestHelpers.InMethod();
  225. // Store an avatar with a different height from default in a notecard.
  226. UUID userId = TestHelpers.ParseTail(0x1);
  227. UUID otherUserId = TestHelpers.ParseTail(0x2);
  228. float newHeight = 1.9f;
  229. SceneHelpers.AddScenePresence(m_scene, otherUserId);
  230. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  231. sp.Appearance.AvatarHeight = newHeight;
  232. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  233. SceneObjectPart part = so.RootPart;
  234. m_scene.AddSceneObject(so);
  235. SceneObjectGroup otherSo = SceneHelpers.CreateSceneObject(1, otherUserId, 0x20);
  236. SceneObjectPart otherPart = otherSo.RootPart;
  237. m_scene.AddSceneObject(otherSo);
  238. OSSL_Api osslApi = new OSSL_Api();
  239. osslApi.Initialize(m_engine, part, null);
  240. OSSL_Api otherOsslApi = new OSSL_Api();
  241. otherOsslApi.Initialize(m_engine, otherPart, null);
  242. string notecardName = "appearanceNc";
  243. osslApi.osOwnerSaveAppearance(notecardName);
  244. string npcRaw
  245. = osslApi.osNpcCreate(
  246. "Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), notecardName, ScriptBaseClass.OS_NPC_CREATOR_OWNED);
  247. otherOsslApi.osNpcRemove(npcRaw);
  248. // Should still be around
  249. UUID npcId = new UUID(npcRaw);
  250. ScenePresence npc = m_scene.GetScenePresence(npcId);
  251. Assert.That(npc, Is.Not.Null);
  252. osslApi.osNpcRemove(npcRaw);
  253. npc = m_scene.GetScenePresence(npcId);
  254. // Now the owner deleted it and it's gone
  255. Assert.That(npc, Is.Null);
  256. }
  257. /// <summary>
  258. /// Test removal of an unowned NPC.
  259. /// </summary>
  260. [Test]
  261. public void TestOsNpcRemoveUnowned()
  262. {
  263. TestHelpers.InMethod();
  264. // log4net.Config.XmlConfigurator.Configure();
  265. // Store an avatar with a different height from default in a notecard.
  266. UUID userId = TestHelpers.ParseTail(0x1);
  267. float newHeight = 1.9f;
  268. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  269. sp.Appearance.AvatarHeight = newHeight;
  270. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, 0x10);
  271. SceneObjectPart part = so.RootPart;
  272. m_scene.AddSceneObject(so);
  273. OSSL_Api osslApi = new OSSL_Api();
  274. osslApi.Initialize(m_engine, part, null);
  275. string notecardName = "appearanceNc";
  276. osslApi.osOwnerSaveAppearance(notecardName);
  277. string npcRaw
  278. = osslApi.osNpcCreate(
  279. "Jane", "Doe", new LSL_Types.Vector3(128, 128, 128), notecardName, ScriptBaseClass.OS_NPC_NOT_OWNED);
  280. osslApi.osNpcRemove(npcRaw);
  281. UUID npcId = new UUID(npcRaw);
  282. ScenePresence npc = m_scene.GetScenePresence(npcId);
  283. Assert.That(npc, Is.Null);
  284. }
  285. }
  286. }