SceneHelpers.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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.Net;
  29. using System.Collections.Generic;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using OpenSim.Data.Null;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Region.PhysicsModules.SharedBase;
  39. using OpenSim.Region.Framework;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Region.CoreModules.Avatar.Gods;
  43. using OpenSim.Region.CoreModules.Asset;
  44. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset;
  45. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Authentication;
  46. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory;
  47. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid;
  48. using OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts;
  49. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Presence;
  50. using OpenSim.Services.Interfaces;
  51. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  52. namespace OpenSim.Tests.Common
  53. {
  54. /// <summary>
  55. /// Helpers for setting up scenes.
  56. /// </summary>
  57. public class SceneHelpers
  58. {
  59. /// <summary>
  60. /// We need a scene manager so that test clients can retrieve a scene when performing teleport tests.
  61. /// </summary>
  62. public SceneManager SceneManager { get; private set; }
  63. public ISimulationDataService SimDataService { get; private set; }
  64. private AgentCircuitManager m_acm = new AgentCircuitManager();
  65. private IEstateDataService m_estateDataService = null;
  66. private LocalAssetServicesConnector m_assetService;
  67. private LocalAuthenticationServicesConnector m_authenticationService;
  68. private LocalInventoryServicesConnector m_inventoryService;
  69. private LocalGridServicesConnector m_gridService;
  70. private LocalUserAccountServicesConnector m_userAccountService;
  71. private LocalPresenceServicesConnector m_presenceService;
  72. private CoreAssetCache m_cache;
  73. public SceneHelpers() : this(null) {}
  74. public SceneHelpers(CoreAssetCache cache)
  75. {
  76. SceneManager = new SceneManager();
  77. m_assetService = StartAssetService(cache);
  78. m_authenticationService = StartAuthenticationService();
  79. m_inventoryService = StartInventoryService();
  80. m_gridService = StartGridService();
  81. m_userAccountService = StartUserAccountService();
  82. m_presenceService = StartPresenceService();
  83. m_inventoryService.PostInitialise();
  84. m_assetService.PostInitialise();
  85. m_userAccountService.PostInitialise();
  86. m_presenceService.PostInitialise();
  87. m_cache = cache;
  88. SimDataService
  89. = OpenSim.Server.Base.ServerUtils.LoadPlugin<ISimulationDataService>("OpenSim.Tests.Common.dll", null);
  90. }
  91. /// <summary>
  92. /// Set up a test scene
  93. /// </summary>
  94. /// <remarks>
  95. /// Automatically starts services, as would the normal runtime.
  96. /// </remarks>
  97. /// <returns></returns>
  98. public TestScene SetupScene()
  99. {
  100. return SetupScene("Unit test region", UUID.Random(), 1000, 1000);
  101. }
  102. public TestScene SetupScene(string name, UUID id, uint x, uint y)
  103. {
  104. return SetupScene(name, id, x, y, new IniConfigSource());
  105. }
  106. public TestScene SetupScene(string name, UUID id, uint x, uint y, IConfigSource configSource)
  107. {
  108. return SetupScene(name, id, x, y, Constants.RegionSize, Constants.RegionSize, configSource);
  109. }
  110. /// <summary>
  111. /// Set up a scene.
  112. /// </summary>
  113. /// <param name="name">Name of the region</param>
  114. /// <param name="id">ID of the region</param>
  115. /// <param name="x">X co-ordinate of the region</param>
  116. /// <param name="y">Y co-ordinate of the region</param>
  117. /// <param name="sizeX">X size of scene</param>
  118. /// <param name="sizeY">Y size of scene</param>
  119. /// <param name="configSource"></param>
  120. /// <returns></returns>
  121. public TestScene SetupScene(
  122. string name, UUID id, uint x, uint y, uint sizeX, uint sizeY, IConfigSource configSource)
  123. {
  124. Console.WriteLine("Setting up test scene {0}", name);
  125. // We must set up a console otherwise setup of some modules may fail
  126. MainConsole.Instance = new MockConsole();
  127. RegionInfo regInfo = new RegionInfo(x, y, new IPEndPoint(IPAddress.Loopback, 9000), "127.0.0.1");
  128. regInfo.RegionName = name;
  129. regInfo.RegionID = id;
  130. regInfo.RegionSizeX = sizeX;
  131. regInfo.RegionSizeY = sizeY;
  132. PhysicsPluginManager physicsPluginManager = new PhysicsPluginManager();
  133. physicsPluginManager.LoadPluginsFromAssembly("Physics/OpenSim.Region.PhysicsModule.BasicPhysics.dll");
  134. Vector3 regionExtent = new Vector3( regInfo.RegionSizeX, regInfo.RegionSizeY, regInfo.RegionSizeZ);
  135. TestScene testScene = new TestScene(
  136. regInfo, m_acm, SimDataService, m_estateDataService, configSource, null);
  137. INonSharedRegionModule godsModule = new GodsModule();
  138. godsModule.Initialise(new IniConfigSource());
  139. godsModule.AddRegion(testScene);
  140. // Add scene to services
  141. m_assetService.AddRegion(testScene);
  142. if (m_cache != null)
  143. {
  144. m_cache.AddRegion(testScene);
  145. m_cache.RegionLoaded(testScene);
  146. testScene.AddRegionModule(m_cache.Name, m_cache);
  147. }
  148. m_assetService.RegionLoaded(testScene);
  149. testScene.AddRegionModule(m_assetService.Name, m_assetService);
  150. m_authenticationService.AddRegion(testScene);
  151. m_authenticationService.RegionLoaded(testScene);
  152. testScene.AddRegionModule(m_authenticationService.Name, m_authenticationService);
  153. m_inventoryService.AddRegion(testScene);
  154. m_inventoryService.RegionLoaded(testScene);
  155. testScene.AddRegionModule(m_inventoryService.Name, m_inventoryService);
  156. m_gridService.AddRegion(testScene);
  157. m_gridService.RegionLoaded(testScene);
  158. testScene.AddRegionModule(m_gridService.Name, m_gridService);
  159. m_userAccountService.AddRegion(testScene);
  160. m_userAccountService.RegionLoaded(testScene);
  161. testScene.AddRegionModule(m_userAccountService.Name, m_userAccountService);
  162. m_presenceService.AddRegion(testScene);
  163. m_presenceService.RegionLoaded(testScene);
  164. testScene.AddRegionModule(m_presenceService.Name, m_presenceService);
  165. testScene.RegionInfo.EstateSettings.EstateOwner = UUID.Random();
  166. testScene.SetModuleInterfaces();
  167. testScene.LandChannel = new TestLandChannel(testScene);
  168. testScene.LoadWorldMap();
  169. testScene.RegionInfo.EstateSettings = new EstateSettings();
  170. testScene.LoginsEnabled = true;
  171. testScene.RegisterRegionWithGrid();
  172. SceneManager.Add(testScene);
  173. return testScene;
  174. }
  175. private static LocalAssetServicesConnector StartAssetService(CoreAssetCache cache)
  176. {
  177. IConfigSource config = new IniConfigSource();
  178. config.AddConfig("Modules");
  179. config.Configs["Modules"].Set("AssetServices", "LocalAssetServicesConnector");
  180. config.AddConfig("AssetService");
  181. config.Configs["AssetService"].Set("LocalServiceModule", "OpenSim.Services.AssetService.dll:AssetService");
  182. config.Configs["AssetService"].Set("StorageProvider", "OpenSim.Tests.Common.dll");
  183. LocalAssetServicesConnector assetService = new LocalAssetServicesConnector();
  184. assetService.Initialise(config);
  185. if (cache != null)
  186. {
  187. IConfigSource cacheConfig = new IniConfigSource();
  188. cacheConfig.AddConfig("Modules");
  189. cacheConfig.Configs["Modules"].Set("AssetCaching", "CoreAssetCache");
  190. cacheConfig.AddConfig("AssetCache");
  191. cache.Initialise(cacheConfig);
  192. }
  193. return assetService;
  194. }
  195. private static LocalAuthenticationServicesConnector StartAuthenticationService()
  196. {
  197. IConfigSource config = new IniConfigSource();
  198. config.AddConfig("Modules");
  199. config.AddConfig("AuthenticationService");
  200. config.Configs["Modules"].Set("AuthenticationServices", "LocalAuthenticationServicesConnector");
  201. config.Configs["AuthenticationService"].Set(
  202. "LocalServiceModule", "OpenSim.Services.AuthenticationService.dll:PasswordAuthenticationService");
  203. config.Configs["AuthenticationService"].Set("StorageProvider", "OpenSim.Data.Null.dll");
  204. LocalAuthenticationServicesConnector service = new LocalAuthenticationServicesConnector();
  205. service.Initialise(config);
  206. return service;
  207. }
  208. private static LocalInventoryServicesConnector StartInventoryService()
  209. {
  210. IConfigSource config = new IniConfigSource();
  211. config.AddConfig("Modules");
  212. config.AddConfig("InventoryService");
  213. config.Configs["Modules"].Set("InventoryServices", "LocalInventoryServicesConnector");
  214. config.Configs["InventoryService"].Set("LocalServiceModule", "OpenSim.Services.InventoryService.dll:XInventoryService");
  215. config.Configs["InventoryService"].Set("StorageProvider", "OpenSim.Tests.Common.dll");
  216. LocalInventoryServicesConnector inventoryService = new LocalInventoryServicesConnector();
  217. inventoryService.Initialise(config);
  218. return inventoryService;
  219. }
  220. private static LocalGridServicesConnector StartGridService()
  221. {
  222. IConfigSource config = new IniConfigSource();
  223. config.AddConfig("Modules");
  224. config.AddConfig("GridService");
  225. config.Configs["Modules"].Set("GridServices", "LocalGridServicesConnector");
  226. config.Configs["GridService"].Set("StorageProvider", "OpenSim.Data.Null.dll:NullRegionData");
  227. config.Configs["GridService"].Set("LocalServiceModule", "OpenSim.Services.GridService.dll:GridService");
  228. config.Configs["GridService"].Set("ConnectionString", "!static");
  229. LocalGridServicesConnector gridService = new LocalGridServicesConnector();
  230. gridService.Initialise(config);
  231. return gridService;
  232. }
  233. /// <summary>
  234. /// Start a user account service
  235. /// </summary>
  236. /// <param name="testScene"></param>
  237. /// <returns></returns>
  238. private static LocalUserAccountServicesConnector StartUserAccountService()
  239. {
  240. IConfigSource config = new IniConfigSource();
  241. config.AddConfig("Modules");
  242. config.AddConfig("UserAccountService");
  243. config.Configs["Modules"].Set("UserAccountServices", "LocalUserAccountServicesConnector");
  244. config.Configs["UserAccountService"].Set("StorageProvider", "OpenSim.Data.Null.dll");
  245. config.Configs["UserAccountService"].Set(
  246. "LocalServiceModule", "OpenSim.Services.UserAccountService.dll:UserAccountService");
  247. LocalUserAccountServicesConnector userAccountService = new LocalUserAccountServicesConnector();
  248. userAccountService.Initialise(config);
  249. return userAccountService;
  250. }
  251. /// <summary>
  252. /// Start a presence service
  253. /// </summary>
  254. /// <param name="testScene"></param>
  255. private static LocalPresenceServicesConnector StartPresenceService()
  256. {
  257. // Unfortunately, some services share data via statics, so we need to null every time to stop interference
  258. // between tests.
  259. // This is a massive non-obvious pita.
  260. NullPresenceData.Instance = null;
  261. IConfigSource config = new IniConfigSource();
  262. config.AddConfig("Modules");
  263. config.AddConfig("PresenceService");
  264. config.Configs["Modules"].Set("PresenceServices", "LocalPresenceServicesConnector");
  265. config.Configs["PresenceService"].Set("StorageProvider", "OpenSim.Data.Null.dll");
  266. config.Configs["PresenceService"].Set(
  267. "LocalServiceModule", "OpenSim.Services.PresenceService.dll:PresenceService");
  268. LocalPresenceServicesConnector presenceService = new LocalPresenceServicesConnector();
  269. presenceService.Initialise(config);
  270. return presenceService;
  271. }
  272. /// <summary>
  273. /// Setup modules for a scene using their default settings.
  274. /// </summary>
  275. /// <param name="scene"></param>
  276. /// <param name="modules"></param>
  277. public static void SetupSceneModules(Scene scene, params object[] modules)
  278. {
  279. SetupSceneModules(scene, new IniConfigSource(), modules);
  280. }
  281. /// <summary>
  282. /// Setup modules for a scene.
  283. /// </summary>
  284. /// <remarks>
  285. /// If called directly, then all the modules must be shared modules.
  286. /// </remarks>
  287. /// <param name="scenes"></param>
  288. /// <param name="config"></param>
  289. /// <param name="modules"></param>
  290. public static void SetupSceneModules(Scene scene, IConfigSource config, params object[] modules)
  291. {
  292. SetupSceneModules(new Scene[] { scene }, config, modules);
  293. }
  294. /// <summary>
  295. /// Setup modules for a scene using their default settings.
  296. /// </summary>
  297. /// <param name="scenes"></param>
  298. /// <param name="modules"></param>
  299. public static void SetupSceneModules(Scene[] scenes, params object[] modules)
  300. {
  301. SetupSceneModules(scenes, new IniConfigSource(), modules);
  302. }
  303. /// <summary>
  304. /// Setup modules for scenes.
  305. /// </summary>
  306. /// <remarks>
  307. /// If called directly, then all the modules must be shared modules.
  308. ///
  309. /// We are emulating here the normal calls made to setup region modules
  310. /// (Initialise(), PostInitialise(), AddRegion, RegionLoaded()).
  311. /// TODO: Need to reuse normal runtime module code.
  312. /// </remarks>
  313. /// <param name="scenes"></param>
  314. /// <param name="config"></param>
  315. /// <param name="modules"></param>
  316. public static void SetupSceneModules(Scene[] scenes, IConfigSource config, params object[] modules)
  317. {
  318. List<IRegionModuleBase> newModules = new List<IRegionModuleBase>();
  319. foreach (object module in modules)
  320. {
  321. IRegionModuleBase m = (IRegionModuleBase)module;
  322. // Console.WriteLine("MODULE {0}", m.Name);
  323. m.Initialise(config);
  324. newModules.Add(m);
  325. }
  326. foreach (IRegionModuleBase module in newModules)
  327. {
  328. if (module is ISharedRegionModule) ((ISharedRegionModule)module).PostInitialise();
  329. }
  330. foreach (IRegionModuleBase module in newModules)
  331. {
  332. foreach (Scene scene in scenes)
  333. {
  334. module.AddRegion(scene);
  335. scene.AddRegionModule(module.Name, module);
  336. }
  337. }
  338. // RegionLoaded is fired after all modules have been appropriately added to all scenes
  339. foreach (IRegionModuleBase module in newModules)
  340. foreach (Scene scene in scenes)
  341. module.RegionLoaded(scene);
  342. foreach (Scene scene in scenes) { scene.SetModuleInterfaces(); }
  343. }
  344. /// <summary>
  345. /// Generate some standard agent connection data.
  346. /// </summary>
  347. /// <param name="agentId"></param>
  348. /// <returns></returns>
  349. public static AgentCircuitData GenerateAgentData(UUID agentId)
  350. {
  351. AgentCircuitData acd = GenerateCommonAgentData();
  352. acd.AgentID = agentId;
  353. acd.firstname = "testfirstname";
  354. acd.lastname = "testlastname";
  355. acd.ServiceURLs = new Dictionary<string, object>();
  356. return acd;
  357. }
  358. /// <summary>
  359. /// Generate some standard agent connection data.
  360. /// </summary>
  361. /// <param name="agentId"></param>
  362. /// <returns></returns>
  363. public static AgentCircuitData GenerateAgentData(UserAccount ua)
  364. {
  365. AgentCircuitData acd = GenerateCommonAgentData();
  366. acd.AgentID = ua.PrincipalID;
  367. acd.firstname = ua.FirstName;
  368. acd.lastname = ua.LastName;
  369. acd.ServiceURLs = ua.ServiceURLs;
  370. return acd;
  371. }
  372. private static AgentCircuitData GenerateCommonAgentData()
  373. {
  374. AgentCircuitData acd = new AgentCircuitData();
  375. // XXX: Sessions must be unique, otherwise one presence can overwrite another in NullPresenceData.
  376. acd.SessionID = UUID.Random();
  377. acd.SecureSessionID = UUID.Random();
  378. acd.circuitcode = 123;
  379. acd.BaseFolder = UUID.Zero;
  380. acd.InventoryFolder = UUID.Zero;
  381. acd.startpos = Vector3.Zero;
  382. acd.CapsPath = "http://wibble.com";
  383. acd.Appearance = new AvatarAppearance();
  384. return acd;
  385. }
  386. /// <summary>
  387. /// Add a root agent where the details of the agent connection (apart from the id) are unimportant for the test
  388. /// </summary>
  389. /// <remarks>
  390. /// XXX: Use the version of this method that takes the UserAccount structure wherever possible - this will
  391. /// make the agent circuit data (e.g. first, lastname) consistent with the user account data.
  392. /// </remarks>
  393. /// <param name="scene"></param>
  394. /// <param name="agentId"></param>
  395. /// <returns></returns>
  396. public static ScenePresence AddScenePresence(Scene scene, UUID agentId)
  397. {
  398. return AddScenePresence(scene, GenerateAgentData(agentId));
  399. }
  400. /// <summary>
  401. /// Add a root agent.
  402. /// </summary>
  403. /// <param name="scene"></param>
  404. /// <param name="ua"></param>
  405. /// <returns></returns>
  406. public static ScenePresence AddScenePresence(Scene scene, UserAccount ua)
  407. {
  408. return AddScenePresence(scene, GenerateAgentData(ua));
  409. }
  410. /// <summary>
  411. /// Add a root agent.
  412. /// </summary>
  413. /// <remarks>
  414. /// This function
  415. ///
  416. /// 1) Tells the scene that an agent is coming. Normally, the login service (local if standalone, from the
  417. /// userserver if grid) would give initial login data back to the client and separately tell the scene that the
  418. /// agent was coming.
  419. ///
  420. /// 2) Connects the agent with the scene
  421. ///
  422. /// This function performs actions equivalent with notifying the scene that an agent is
  423. /// coming and then actually connecting the agent to the scene. The one step missed out is the very first
  424. /// </remarks>
  425. /// <param name="scene"></param>
  426. /// <param name="agentData"></param>
  427. /// <returns></returns>
  428. public static ScenePresence AddScenePresence(Scene scene, AgentCircuitData agentData)
  429. {
  430. return AddScenePresence(scene, new TestClient(agentData, scene), agentData);
  431. }
  432. /// <summary>
  433. /// Add a root agent.
  434. /// </summary>
  435. /// <remarks>
  436. /// This function
  437. ///
  438. /// 1) Tells the scene that an agent is coming. Normally, the login service (local if standalone, from the
  439. /// userserver if grid) would give initial login data back to the client and separately tell the scene that the
  440. /// agent was coming.
  441. ///
  442. /// 2) Connects the agent with the scene
  443. ///
  444. /// This function performs actions equivalent with notifying the scene that an agent is
  445. /// coming and then actually connecting the agent to the scene. The one step missed out is the very first
  446. /// </remarks>
  447. /// <param name="scene"></param>
  448. /// <param name="agentData"></param>
  449. /// <returns></returns>
  450. public static ScenePresence AddScenePresence(
  451. Scene scene, IClientAPI client, AgentCircuitData agentData)
  452. {
  453. // We emulate the proper login sequence here by doing things in four stages
  454. // Stage 0: login
  455. // We need to punch through to the underlying service because scene will not, correctly, let us call it
  456. // through it's reference to the LPSC
  457. LocalPresenceServicesConnector lpsc = (LocalPresenceServicesConnector)scene.PresenceService;
  458. lpsc.m_PresenceService.LoginAgent(agentData.AgentID.ToString(), agentData.SessionID, agentData.SecureSessionID);
  459. // Stages 1 & 2
  460. ScenePresence sp = IntroduceClientToScene(scene, client, agentData, TeleportFlags.ViaLogin);
  461. // Stage 3: Complete the entrance into the region. This converts the child agent into a root agent.
  462. sp.CompleteMovement(sp.ControllingClient, true);
  463. return sp;
  464. }
  465. /// <summary>
  466. /// Introduce an agent into the scene by adding a new client.
  467. /// </summary>
  468. /// <returns>The scene presence added</returns>
  469. /// <param name='scene'></param>
  470. /// <param name='testClient'></param>
  471. /// <param name='agentData'></param>
  472. /// <param name='tf'></param>
  473. private static ScenePresence IntroduceClientToScene(
  474. Scene scene, IClientAPI client, AgentCircuitData agentData, TeleportFlags tf)
  475. {
  476. string reason;
  477. // Stage 1: tell the scene to expect a new user connection
  478. if (!scene.NewUserConnection(agentData, (uint)tf, null, out reason))
  479. Console.WriteLine("NewUserConnection failed: " + reason);
  480. // Stage 2: add the new client as a child agent to the scene
  481. scene.AddNewAgent(client, PresenceType.User);
  482. return scene.GetScenePresence(client.AgentId);
  483. }
  484. public static ScenePresence AddChildScenePresence(Scene scene, UUID agentId)
  485. {
  486. return AddChildScenePresence(scene, GenerateAgentData(agentId));
  487. }
  488. public static ScenePresence AddChildScenePresence(Scene scene, AgentCircuitData acd)
  489. {
  490. acd.child = true;
  491. // XXX: ViaLogin may not be correct for child agents
  492. TestClient client = new TestClient(acd, scene);
  493. return IntroduceClientToScene(scene, client, acd, TeleportFlags.ViaLogin);
  494. }
  495. /// <summary>
  496. /// Add a test object
  497. /// </summary>
  498. /// <param name="scene"></param>
  499. /// <returns></returns>
  500. public static SceneObjectGroup AddSceneObject(Scene scene)
  501. {
  502. return AddSceneObject(scene, "Test Object", UUID.Zero);
  503. }
  504. /// <summary>
  505. /// Add a test object
  506. /// </summary>
  507. /// <param name="scene"></param>
  508. /// <param name="name"></param>
  509. /// <param name="ownerId"></param>
  510. /// <returns></returns>
  511. public static SceneObjectGroup AddSceneObject(Scene scene, string name, UUID ownerId)
  512. {
  513. SceneObjectGroup so = new SceneObjectGroup(CreateSceneObjectPart(name, UUID.Random(), ownerId));
  514. //part.UpdatePrimFlags(false, false, true);
  515. //part.ObjectFlags |= (uint)PrimFlags.Phantom;
  516. scene.AddNewSceneObject(so, true);
  517. return so;
  518. }
  519. /// <summary>
  520. /// Add a test object
  521. /// </summary>
  522. /// <param name="scene"></param>
  523. /// <param name="parts">
  524. /// The number of parts that should be in the scene object
  525. /// </param>
  526. /// <param name="ownerId"></param>
  527. /// <param name="partNamePrefix">
  528. /// The prefix to be given to part names. This will be suffixed with "Part<part no>"
  529. /// (e.g. mynamePart1 for the root part)
  530. /// </param>
  531. /// <param name="uuidTail">
  532. /// The hexadecimal last part of the UUID for parts created. A UUID of the form "00000000-0000-0000-0000-{0:XD12}"
  533. /// will be given to the root part, and incremented for each part thereafter.
  534. /// </param>
  535. /// <returns></returns>
  536. public static SceneObjectGroup AddSceneObject(Scene scene, int parts, UUID ownerId, string partNamePrefix, int uuidTail)
  537. {
  538. SceneObjectGroup so = CreateSceneObject(parts, ownerId, partNamePrefix, uuidTail);
  539. scene.AddNewSceneObject(so, false);
  540. return so;
  541. }
  542. /// <summary>
  543. /// Create a scene object part.
  544. /// </summary>
  545. /// <param name="name"></param>
  546. /// <param name="id"></param>
  547. /// <param name="ownerId"></param>
  548. /// <returns></returns>
  549. public static SceneObjectPart CreateSceneObjectPart(string name, UUID id, UUID ownerId)
  550. {
  551. return new SceneObjectPart(
  552. ownerId, PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero)
  553. { Name = name, UUID = id, Scale = new Vector3(1, 1, 1) };
  554. }
  555. /// <summary>
  556. /// Create a scene object but do not add it to the scene.
  557. /// </summary>
  558. /// <remarks>
  559. /// UUID always starts at 00000000-0000-0000-0000-000000000001. For some purposes, (e.g. serializing direct
  560. /// to another object's inventory) we do not need a scene unique ID. So it would be better to add the
  561. /// UUID when we actually add an object to a scene rather than on creation.
  562. /// </remarks>
  563. /// <param name="parts">The number of parts that should be in the scene object</param>
  564. /// <param name="ownerId"></param>
  565. /// <returns></returns>
  566. public static SceneObjectGroup CreateSceneObject(int parts, UUID ownerId)
  567. {
  568. return CreateSceneObject(parts, ownerId, 0x1);
  569. }
  570. /// <summary>
  571. /// Create a scene object but do not add it to the scene.
  572. /// </summary>
  573. /// <param name="parts">The number of parts that should be in the scene object</param>
  574. /// <param name="ownerId"></param>
  575. /// <param name="uuidTail">
  576. /// The hexadecimal last part of the UUID for parts created. A UUID of the form "00000000-0000-0000-0000-{0:XD12}"
  577. /// will be given to the root part, and incremented for each part thereafter.
  578. /// </param>
  579. /// <returns></returns>
  580. public static SceneObjectGroup CreateSceneObject(int parts, UUID ownerId, int uuidTail)
  581. {
  582. return CreateSceneObject(parts, ownerId, "", uuidTail);
  583. }
  584. /// <summary>
  585. /// Create a scene object but do not add it to the scene.
  586. /// </summary>
  587. /// <param name="parts">
  588. /// The number of parts that should be in the scene object
  589. /// </param>
  590. /// <param name="ownerId"></param>
  591. /// <param name="partNamePrefix">
  592. /// The prefix to be given to part names. This will be suffixed with "Part<part no>"
  593. /// (e.g. mynamePart1 for the root part)
  594. /// </param>
  595. /// <param name="uuidTail">
  596. /// The hexadecimal last part of the UUID for parts created. A UUID of the form "00000000-0000-0000-0000-{0:XD12}"
  597. /// will be given to the root part, and incremented for each part thereafter.
  598. /// </param>
  599. /// <returns></returns>
  600. public static SceneObjectGroup CreateSceneObject(int parts, UUID ownerId, string partNamePrefix, int uuidTail)
  601. {
  602. string rawSogId = string.Format("00000000-0000-0000-0000-{0:X12}", uuidTail);
  603. SceneObjectGroup sog
  604. = new SceneObjectGroup(
  605. CreateSceneObjectPart(string.Format("{0}Part1", partNamePrefix), new UUID(rawSogId), ownerId));
  606. if (parts > 1)
  607. for (int i = 2; i <= parts; i++)
  608. sog.AddPart(
  609. CreateSceneObjectPart(
  610. string.Format("{0}Part{1}", partNamePrefix, i),
  611. new UUID(string.Format("00000000-0000-0000-0000-{0:X12}", uuidTail + i - 1)),
  612. ownerId));
  613. return sog;
  614. }
  615. }
  616. }