SceneHelpers.cs 30 KB

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