LibraryModule.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.Region.CoreModules.Avatar.Inventory.Archiver;
  34. using OpenSim.Region.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Server.Base;
  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. private ILibraryService m_Library;
  52. #region ISharedRegionModule
  53. public void Initialise(IConfigSource config)
  54. {
  55. m_Enabled = config.Configs["Modules"].GetBoolean("LibraryModule", m_Enabled);
  56. if (m_Enabled)
  57. {
  58. IConfig libConfig = config.Configs["LibraryService"];
  59. if (libConfig != null)
  60. {
  61. string dllName = libConfig.GetString("LocalServiceModule", string.Empty);
  62. m_log.Debug("[LIBRARY MODULE]: Library service dll is " + dllName);
  63. if (dllName != string.Empty)
  64. {
  65. Object[] args = new Object[] { config };
  66. m_Library = ServerUtils.LoadPlugin<ILibraryService>(dllName, args);
  67. }
  68. }
  69. }
  70. if (m_Library == null)
  71. {
  72. m_log.Warn("[LIBRARY MODULE]: No local library service. Module will be disabled.");
  73. m_Enabled = false;
  74. }
  75. }
  76. public bool IsSharedModule
  77. {
  78. get { return true; }
  79. }
  80. public string Name
  81. {
  82. get { return "Library Module"; }
  83. }
  84. public Type ReplaceableInterface
  85. {
  86. get { return null; }
  87. }
  88. public void AddRegion(Scene scene)
  89. {
  90. if (!m_Enabled)
  91. return;
  92. // Store only the first scene
  93. if (m_Scene == null)
  94. {
  95. m_Scene = scene;
  96. }
  97. scene.RegisterModuleInterface<ILibraryService>(m_Library);
  98. }
  99. public void RemoveRegion(Scene scene)
  100. {
  101. if (!m_Enabled)
  102. return;
  103. scene.UnregisterModuleInterface<ILibraryService>(m_Library);
  104. }
  105. public void RegionLoaded(Scene scene)
  106. {
  107. if (!m_Enabled)
  108. return;
  109. // This will never run more than once, even if the region is restarted
  110. if (!m_HasRunOnce)
  111. {
  112. LoadLibrariesFromArchives();
  113. //DumpLibrary();
  114. m_HasRunOnce = true;
  115. }
  116. }
  117. public void PostInitialise()
  118. {
  119. }
  120. public void Close()
  121. {
  122. m_Scene = null;
  123. }
  124. #endregion ISharedRegionModule
  125. #region LoadLibraries
  126. private string pathToLibraries = "Library";
  127. protected void LoadLibrariesFromArchives()
  128. {
  129. InventoryFolderImpl lib = m_Library.LibraryRootFolder;
  130. if (lib == null)
  131. {
  132. m_log.Debug("[LIBRARY MODULE]: No library. Ignoring Library Module");
  133. return;
  134. }
  135. RegionInfo regInfo = new RegionInfo();
  136. Scene m_MockScene = new Scene(regInfo);
  137. LocalInventoryService invService = new LocalInventoryService(lib);
  138. m_MockScene.RegisterModuleInterface<IInventoryService>(invService);
  139. m_MockScene.RegisterModuleInterface<IAssetService>(m_Scene.AssetService);
  140. UserAccount uinfo = new UserAccount(lib.Owner);
  141. uinfo.FirstName = "OpenSim";
  142. uinfo.LastName = "Library";
  143. uinfo.ServiceURLs = new Dictionary<string, object>();
  144. foreach (string iarFileName in Directory.GetFiles(pathToLibraries, "*.iar"))
  145. {
  146. string simpleName = Path.GetFileNameWithoutExtension(iarFileName);
  147. m_log.InfoFormat("[LIBRARY MODULE]: Loading library archive {0} ({1})...", iarFileName, simpleName);
  148. simpleName = GetInventoryPathFromName(simpleName);
  149. InventoryArchiveReadRequest archread = new InventoryArchiveReadRequest(m_MockScene, uinfo, simpleName, iarFileName);
  150. try
  151. {
  152. List<InventoryNodeBase> nodes = archread.Execute();
  153. if (nodes != null && nodes.Count == 0)
  154. {
  155. // didn't find the subfolder with the given name; place it on the top
  156. m_log.InfoFormat("[LIBRARY MODULE]: Didn't find {0} in library. Placing archive on the top level", simpleName);
  157. archread.Close();
  158. archread = new InventoryArchiveReadRequest(m_MockScene, uinfo, "/", iarFileName);
  159. archread.Execute();
  160. }
  161. foreach (InventoryNodeBase node in nodes)
  162. FixPerms(node);
  163. }
  164. catch (Exception e)
  165. {
  166. m_log.DebugFormat("[LIBRARY MODULE]: Exception when processing archive {0}: {1}", iarFileName, e.StackTrace);
  167. }
  168. finally
  169. {
  170. archread.Close();
  171. }
  172. }
  173. }
  174. private void FixPerms(InventoryNodeBase node)
  175. {
  176. if (node is InventoryItemBase)
  177. {
  178. InventoryItemBase item = (InventoryItemBase)node;
  179. item.BasePermissions = 0x7FFFFFFF;
  180. item.EveryOnePermissions = 0x7FFFFFFF;
  181. item.CurrentPermissions = 0x7FFFFFFF;
  182. item.NextPermissions = 0x7FFFFFFF;
  183. }
  184. }
  185. private void DumpLibrary()
  186. {
  187. InventoryFolderImpl lib = m_Library.LibraryRootFolder;
  188. m_log.DebugFormat(" - folder {0}", lib.Name);
  189. DumpFolder(lib);
  190. }
  191. //
  192. // private void DumpLibrary()
  193. // {
  194. // InventoryFolderImpl lib = m_Scene.CommsManager.UserProfileCacheService.LibraryRoot;
  195. //
  196. // m_log.DebugFormat(" - folder {0}", lib.Name);
  197. // DumpFolder(lib);
  198. // }
  199. private void DumpFolder(InventoryFolderImpl folder)
  200. {
  201. foreach (InventoryItemBase item in folder.Items.Values)
  202. {
  203. m_log.DebugFormat(" --> item {0}", item.Name);
  204. }
  205. foreach (InventoryFolderImpl f in folder.RequestListOfFolderImpls())
  206. {
  207. m_log.DebugFormat(" - folder {0}", f.Name);
  208. DumpFolder(f);
  209. }
  210. }
  211. private string GetInventoryPathFromName(string name)
  212. {
  213. string[] parts = name.Split(new char[] { ' ' });
  214. if (parts.Length == 3)
  215. {
  216. name = string.Empty;
  217. // cut the last part
  218. for (int i = 0; i < parts.Length - 1; i++)
  219. name = name + ' ' + parts[i];
  220. }
  221. return name;
  222. }
  223. #endregion LoadLibraries
  224. }
  225. }