LibraryModule.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.IO;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Communications;
  33. using OpenSim.Framework.Communications.Cache;
  34. using OpenSim.Region.CoreModules.Avatar.Inventory.Archiver;
  35. using OpenSim.Region.Framework;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Services.Interfaces;
  39. using OpenMetaverse;
  40. using log4net;
  41. using Nini.Config;
  42. namespace OpenSim.Region.CoreModules.Framework.Library
  43. {
  44. public class LibraryModule : ISharedRegionModule
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private static bool m_HasRunOnce = false;
  48. private bool m_Enabled = false;
  49. private string m_LibraryName = "OpenSim Library";
  50. private Scene m_Scene;
  51. #region ISharedRegionModule
  52. public void Initialise(IConfigSource config)
  53. {
  54. m_Enabled = config.Configs["Modules"].GetBoolean("LibraryModule", m_Enabled);
  55. if (m_Enabled)
  56. {
  57. IConfig libConfig = config.Configs["LibraryModule"];
  58. if (libConfig != null)
  59. m_LibraryName = libConfig.GetString("LibraryName", m_LibraryName);
  60. }
  61. }
  62. public bool IsSharedModule
  63. {
  64. get { return true; }
  65. }
  66. public string Name
  67. {
  68. get { return "Library Module"; }
  69. }
  70. public Type ReplaceableInterface
  71. {
  72. get { return null; }
  73. }
  74. public void AddRegion(Scene scene)
  75. {
  76. if (!m_Enabled)
  77. return;
  78. // Store only the first scene
  79. if (m_Scene == null)
  80. {
  81. m_Scene = scene;
  82. }
  83. }
  84. public void RemoveRegion(Scene scene)
  85. {
  86. }
  87. public void RegionLoaded(Scene scene)
  88. {
  89. if (!m_Enabled)
  90. return;
  91. // This will never run more than once, even if the region is restarted
  92. if (!m_HasRunOnce)
  93. {
  94. LoadLibrariesFromArchives();
  95. //DumpLibrary();
  96. m_HasRunOnce = true;
  97. }
  98. }
  99. public void PostInitialise()
  100. {
  101. }
  102. public void Close()
  103. {
  104. m_Scene = null;
  105. }
  106. #endregion ISharedRegionModule
  107. #region LoadLibraries
  108. private string pathToLibraries = "Library";
  109. protected void LoadLibrariesFromArchives()
  110. {
  111. InventoryFolderImpl lib = m_Scene.CommsManager.UserProfileCacheService.LibraryRoot;
  112. if (lib == null)
  113. {
  114. m_log.Debug("[LIBRARY MODULE]: No library.");
  115. return;
  116. }
  117. lib.Name = m_LibraryName;
  118. RegionInfo regInfo = new RegionInfo();
  119. Scene m_MockScene = new Scene(regInfo);
  120. m_MockScene.CommsManager = m_Scene.CommsManager;
  121. LocalInventoryService invService = new LocalInventoryService((LibraryRootFolder)lib);
  122. m_MockScene.RegisterModuleInterface<IInventoryService>(invService);
  123. m_MockScene.RegisterModuleInterface<IAssetService>(m_Scene.AssetService);
  124. UserProfileData profile = new UserProfileData();
  125. profile.FirstName = "OpenSim";
  126. profile.ID = lib.Owner;
  127. profile.SurName = "Library";
  128. CachedUserInfo uinfo = new CachedUserInfo(invService, profile);
  129. foreach (string iarFileName in Directory.GetFiles(pathToLibraries, "*.iar"))
  130. {
  131. string simpleName = Path.GetFileNameWithoutExtension(iarFileName);
  132. m_log.InfoFormat("[LIBRARY MODULE]: Loading library archive {0} ({1})...", iarFileName, simpleName);
  133. simpleName = GetInventoryPathFromName(simpleName);
  134. try
  135. {
  136. InventoryArchiveReadRequest archread = new InventoryArchiveReadRequest(m_MockScene, uinfo, simpleName, iarFileName);
  137. List<InventoryNodeBase> nodes = archread.Execute();
  138. if (nodes.Count == 0)
  139. {
  140. // didn't find the subfolder with the given name; place it on the top
  141. m_log.InfoFormat("[LIBRARY MODULE]: Didn't find {0} in library. Placing archive on the top level", simpleName);
  142. archread.Close();
  143. archread = new InventoryArchiveReadRequest(m_MockScene, uinfo, "/", iarFileName);
  144. archread.Execute();
  145. }
  146. archread.Close();
  147. }
  148. catch (Exception e)
  149. {
  150. m_log.DebugFormat("[LIBRARY MODULE]: Exception when processing archive {0}: {1}", iarFileName, e.Message);
  151. }
  152. }
  153. }
  154. private void DumpLibrary()
  155. {
  156. InventoryFolderImpl lib = m_Scene.CommsManager.UserProfileCacheService.LibraryRoot;
  157. m_log.DebugFormat(" - folder {0}", lib.Name);
  158. DumpFolder(lib);
  159. }
  160. private void DumpFolder(InventoryFolderImpl folder)
  161. {
  162. foreach (InventoryItemBase item in folder.Items.Values)
  163. {
  164. m_log.DebugFormat(" --> item {0}", item.Name);
  165. }
  166. foreach (InventoryFolderImpl f in folder.RequestListOfFolderImpls())
  167. {
  168. m_log.DebugFormat(" - folder {0}", f.Name);
  169. DumpFolder(f);
  170. }
  171. }
  172. private string GetInventoryPathFromName(string name)
  173. {
  174. string[] parts = name.Split(new char[] { ' ' });
  175. if (parts.Length == 3)
  176. {
  177. name = string.Empty;
  178. // cut the last part
  179. for (int i = 0; i < parts.Length - 1; i++)
  180. name = name + ' ' + parts[i];
  181. }
  182. return name;
  183. }
  184. #endregion LoadLibraries
  185. }
  186. }