LibraryService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 System.Xml;
  32. using OpenSim.Framework;
  33. using OpenSim.Services.Base;
  34. using OpenSim.Services.Interfaces;
  35. using log4net;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using PermissionMask = OpenSim.Framework.PermissionMask;
  39. namespace OpenSim.Services.InventoryService
  40. {
  41. /// <summary>
  42. /// Basically a hack to give us a Inventory library while we don't have a inventory server
  43. /// once the server is fully implemented then should read the data from that
  44. /// </summary>
  45. public class LibraryService : ServiceBase, ILibraryService
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private static readonly UUID libOwner = Constants.m_MrOpenSimID;
  49. private const string m_LibraryRootFolderIDstr = "00000112-000f-0000-0000-000100bba000";
  50. private static readonly UUID m_LibraryRootFolderID = new UUID(m_LibraryRootFolderIDstr);
  51. static private InventoryFolderImpl m_LibraryRootFolder;
  52. public InventoryFolderImpl LibraryRootFolder
  53. {
  54. get { return m_LibraryRootFolder; }
  55. }
  56. /// <summary>
  57. /// Holds the root library folder and all its descendents. This is really only used during inventory
  58. /// setup so that we don't have to repeatedly search the tree of library folders.
  59. /// </summary>
  60. static protected Dictionary<UUID, InventoryFolderImpl> libraryFolders
  61. = new Dictionary<UUID, InventoryFolderImpl>(32);
  62. static protected Dictionary<UUID, InventoryItemBase> m_items = new Dictionary<UUID, InventoryItemBase>(256);
  63. static LibraryService m_root;
  64. static object m_rootLock = new object();
  65. static readonly uint m_BasePermissions = (uint)PermissionMask.AllAndExport;
  66. static readonly uint m_EveryOnePermissions = (uint)PermissionMask.AllAndExportNoMod;
  67. static readonly uint m_CurrentPermissions = (uint)PermissionMask.AllAndExport;
  68. static readonly uint m_NextPermissions = (uint)PermissionMask.AllAndExport;
  69. static readonly uint m_GroupPermissions = 0;
  70. public LibraryService(IConfigSource config):base(config)
  71. {
  72. lock(m_rootLock)
  73. {
  74. if(m_root != null)
  75. return;
  76. m_root = this;
  77. }
  78. string pLibrariesLocation = Path.Combine("inventory", "Libraries.xml");
  79. string pLibName = "OpenSim Library";
  80. IConfig libConfig = config.Configs["LibraryService"];
  81. if (libConfig != null)
  82. {
  83. pLibrariesLocation = libConfig.GetString("DefaultLibrary", pLibrariesLocation);
  84. pLibName = libConfig.GetString("LibraryName", pLibName);
  85. }
  86. m_log.Debug("[LIBRARY]: Starting library service...");
  87. m_LibraryRootFolder = new InventoryFolderImpl();
  88. m_LibraryRootFolder.Owner = libOwner;
  89. m_LibraryRootFolder.ID = m_LibraryRootFolderID;
  90. m_LibraryRootFolder.Name = pLibName;
  91. m_LibraryRootFolder.ParentID = UUID.Zero;
  92. m_LibraryRootFolder.Type = 8;
  93. m_LibraryRootFolder.Version = 1;
  94. libraryFolders.Add(m_LibraryRootFolder.ID, m_LibraryRootFolder);
  95. LoadLibraries(pLibrariesLocation);
  96. }
  97. public InventoryItemBase CreateItem(UUID inventoryID, UUID assetID, string name, string description,
  98. int assetType, int invType, UUID parentFolderID)
  99. {
  100. InventoryItemBase item = new InventoryItemBase();
  101. item.Owner = libOwner;
  102. item.CreatorId = libOwner.ToString();
  103. item.ID = inventoryID;
  104. item.AssetID = assetID;
  105. item.Description = description;
  106. item.Name = name;
  107. item.AssetType = assetType;
  108. item.InvType = invType;
  109. item.Folder = parentFolderID;
  110. item.BasePermissions = m_BasePermissions;
  111. item.EveryOnePermissions = m_EveryOnePermissions;
  112. item.CurrentPermissions = m_CurrentPermissions;
  113. item.NextPermissions = m_NextPermissions;
  114. item.GroupPermissions = m_GroupPermissions;
  115. return item;
  116. }
  117. /// <summary>
  118. /// Use the asset set information at path to load assets
  119. /// </summary>
  120. /// <param name="path"></param>
  121. /// <param name="assets"></param>
  122. protected void LoadLibraries(string librariesControlPath)
  123. {
  124. m_log.InfoFormat("[LIBRARY INVENTORY]: Loading library control file {0}", librariesControlPath);
  125. LoadFromFile(librariesControlPath, "Libraries control", ReadLibraryFromConfig);
  126. }
  127. /// <summary>
  128. /// Read a library set from config
  129. /// </summary>
  130. /// <param name="config"></param>
  131. protected void ReadLibraryFromConfig(IConfig config, string path)
  132. {
  133. string basePath = Path.GetDirectoryName(path);
  134. if (config.Contains("RootVersion"))
  135. {
  136. m_LibraryRootFolder.Version = (ushort)config.GetInt("RootVersion", m_LibraryRootFolder.Version);
  137. return;
  138. }
  139. string foldersPath = Path.Combine(basePath, config.GetString("foldersFile", String.Empty));
  140. LoadFromFile(foldersPath, "Library folders", ReadFolderFromConfig);
  141. string itemsPath = Path.Combine( basePath, config.GetString("itemsFile", String.Empty));
  142. LoadFromFile(itemsPath, "Library items", ReadItemFromConfig);
  143. }
  144. /// <summary>
  145. /// Read a library inventory folder from a loaded configuration
  146. /// </summary>
  147. /// <param name="source"></param>
  148. private void ReadFolderFromConfig(IConfig config, string path)
  149. {
  150. InventoryFolderImpl folderInfo = new InventoryFolderImpl();
  151. folderInfo.ID = new UUID(config.GetString("folderID", m_LibraryRootFolderIDstr));
  152. folderInfo.Name = config.GetString("name", "unknown");
  153. folderInfo.ParentID = new UUID(config.GetString("parentFolderID", m_LibraryRootFolderIDstr));
  154. folderInfo.Type = (short)config.GetInt("type", 8);
  155. folderInfo.Version = (ushort)config.GetInt("version", 1);
  156. folderInfo.Owner = libOwner;
  157. if (libraryFolders.TryGetValue(folderInfo.ParentID, out InventoryFolderImpl parentFolder))
  158. {
  159. libraryFolders.Add(folderInfo.ID, folderInfo);
  160. parentFolder.AddChildFolder(folderInfo);
  161. //m_log.InfoFormat("[LIBRARY INVENTORY]: Adding folder {0} ({1})", folderInfo.name, folderInfo.folderID);
  162. }
  163. else
  164. {
  165. m_log.WarnFormat(
  166. "[LIBRARY INVENTORY]: Couldn't add folder {0} ({1}) since parent folder with ID {2} does not exist!",
  167. folderInfo.Name, folderInfo.ID, folderInfo.ParentID);
  168. }
  169. }
  170. /// <summary>
  171. /// Read a library inventory item metadata from a loaded configuration
  172. /// </summary>
  173. /// <param name="source"></param>
  174. private void ReadItemFromConfig(IConfig config, string path)
  175. {
  176. InventoryItemBase item = new InventoryItemBase();
  177. item.Owner = libOwner;
  178. item.CreatorId = libOwner.ToString();
  179. UUID itID = new UUID(config.GetString("inventoryID", m_LibraryRootFolderIDstr));
  180. item.ID = itID;
  181. item.AssetID = new UUID(config.GetString("assetID", item.ID.ToString()));
  182. item.Folder = new UUID(config.GetString("folderID", m_LibraryRootFolderIDstr));
  183. item.Name = config.GetString("name", String.Empty);
  184. item.Description = config.GetString("description", item.Name);
  185. item.InvType = config.GetInt("inventoryType", 0);
  186. item.AssetType = config.GetInt("assetType", item.InvType);
  187. item.CurrentPermissions = (uint)config.GetLong("currentPermissions", m_CurrentPermissions);
  188. item.NextPermissions = (uint)config.GetLong("nextPermissions", m_NextPermissions);
  189. item.EveryOnePermissions = (uint)config.GetLong("everyonePermissions", m_EveryOnePermissions);
  190. item.BasePermissions = (uint)config.GetLong("basePermissions", m_BasePermissions);
  191. item.GroupPermissions = (uint)config.GetLong("basePermissions", m_GroupPermissions);;
  192. item.Flags = (uint)config.GetInt("flags", 0);
  193. if (libraryFolders.TryGetValue(item.Folder, out InventoryFolderImpl parentFolder))
  194. {
  195. if(!parentFolder.Items.ContainsKey(itID))
  196. {
  197. parentFolder.Items.Add(itID, item);
  198. m_items[itID] = item;
  199. }
  200. else
  201. {
  202. m_log.WarnFormat("[LIBRARY INVENTORY] Item {1} [{0}] not added, duplicate item", item.ID, item.Name);
  203. }
  204. }
  205. else
  206. {
  207. m_log.WarnFormat(
  208. "[LIBRARY INVENTORY]: Couldn't add item {0} ({1}) since parent folder with ID {2} does not exist!",
  209. item.Name, item.ID, item.Folder);
  210. }
  211. }
  212. private delegate void ConfigAction(IConfig config, string path);
  213. /// <summary>
  214. /// Load the given configuration at a path and perform an action on each Config contained within it
  215. /// </summary>
  216. /// <param name="path"></param>
  217. /// <param name="fileDescription"></param>
  218. /// <param name="action"></param>
  219. private static void LoadFromFile(string path, string fileDescription, ConfigAction action)
  220. {
  221. if (File.Exists(path))
  222. {
  223. try
  224. {
  225. XmlConfigSource source = new XmlConfigSource(path);
  226. for (int i = 0; i < source.Configs.Count; i++)
  227. {
  228. action(source.Configs[i], path);
  229. }
  230. }
  231. catch (XmlException e)
  232. {
  233. m_log.ErrorFormat("[LIBRARY INVENTORY]: Error loading {0} : {1}", path, e);
  234. }
  235. }
  236. else
  237. {
  238. m_log.ErrorFormat("[LIBRARY INVENTORY]: {0} file {1} does not exist!", fileDescription, path);
  239. }
  240. }
  241. /// <summary>
  242. /// Looks like a simple getter, but is written like this for some consistency with the other Request
  243. /// methods in the superclass
  244. /// </summary>
  245. /// <returns></returns>
  246. public Dictionary<UUID, InventoryFolderImpl> GetAllFolders()
  247. {
  248. Dictionary<UUID, InventoryFolderImpl> fs = new Dictionary<UUID, InventoryFolderImpl>();
  249. fs.Add(m_LibraryRootFolderID, m_LibraryRootFolder);
  250. List<InventoryFolderImpl> fis = TraverseFolder(m_LibraryRootFolder);
  251. foreach (InventoryFolderImpl f in fis)
  252. {
  253. fs.Add(f.ID, f);
  254. }
  255. //return libraryFolders;
  256. return fs;
  257. }
  258. private List<InventoryFolderImpl> TraverseFolder(InventoryFolderImpl node)
  259. {
  260. List<InventoryFolderImpl> folders = node.RequestListOfFolderImpls();
  261. List<InventoryFolderImpl> subs = new List<InventoryFolderImpl>();
  262. foreach (InventoryFolderImpl f in folders)
  263. subs.AddRange(TraverseFolder(f));
  264. folders.AddRange(subs);
  265. return folders;
  266. }
  267. public InventoryItemBase GetItem(UUID itemID)
  268. {
  269. if(m_items.TryGetValue(itemID, out InventoryItemBase it))
  270. return it;
  271. return null;
  272. }
  273. public InventoryItemBase[] GetMultipleItems(UUID[] ids)
  274. {
  275. List<InventoryItemBase> items = new List<InventoryItemBase>();
  276. foreach (UUID id in ids)
  277. {
  278. if (m_items.TryGetValue(id, out InventoryItemBase it))
  279. items.Add(it);
  280. }
  281. if(items.Count == 0)
  282. return null;
  283. return items.ToArray();
  284. }
  285. }
  286. }