LibraryModule.cs 9.5 KB

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