LibraryModule.cs 9.5 KB

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