1
0

LibraryRootFolder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. namespace OpenSim.Framework.Communications.Cache
  36. {
  37. /// <summary>
  38. /// Basically a hack to give us a Inventory library while we don't have a inventory server
  39. /// once the server is fully implemented then should read the data from that
  40. /// </summary>
  41. public class LibraryRootFolder : InventoryFolderImpl
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private UUID libOwner = new UUID("11111111-1111-0000-0000-000100bba000");
  45. /// <summary>
  46. /// Holds the root library folder and all its descendents. This is really only used during inventory
  47. /// setup so that we don't have to repeatedly search the tree of library folders.
  48. /// </summary>
  49. protected Dictionary<UUID, InventoryFolderImpl> libraryFolders
  50. = new Dictionary<UUID, InventoryFolderImpl>();
  51. public LibraryRootFolder(string pLibrariesLocation)
  52. {
  53. Owner = libOwner;
  54. ID = new UUID("00000112-000f-0000-0000-000100bba000");
  55. Name = "OpenSim Library";
  56. ParentID = UUID.Zero;
  57. Type = (short) 8;
  58. Version = (ushort) 1;
  59. libraryFolders.Add(ID, this);
  60. LoadLibraries(pLibrariesLocation);
  61. }
  62. public InventoryItemBase CreateItem(UUID inventoryID, UUID assetID, string name, string description,
  63. int assetType, int invType, UUID parentFolderID)
  64. {
  65. InventoryItemBase item = new InventoryItemBase();
  66. item.Owner = libOwner;
  67. item.CreatorId = libOwner.ToString();
  68. item.ID = inventoryID;
  69. item.AssetID = assetID;
  70. item.Description = description;
  71. item.Name = name;
  72. item.AssetType = assetType;
  73. item.InvType = invType;
  74. item.Folder = parentFolderID;
  75. item.BasePermissions = 0x7FFFFFFF;
  76. item.EveryOnePermissions = 0x7FFFFFFF;
  77. item.CurrentPermissions = 0x7FFFFFFF;
  78. item.NextPermissions = 0x7FFFFFFF;
  79. return item;
  80. }
  81. /// <summary>
  82. /// Use the asset set information at path to load assets
  83. /// </summary>
  84. /// <param name="path"></param>
  85. /// <param name="assets"></param>
  86. protected void LoadLibraries(string librariesControlPath)
  87. {
  88. m_log.InfoFormat("[LIBRARY INVENTORY]: Loading library control file {0}", librariesControlPath);
  89. LoadFromFile(librariesControlPath, "Libraries control", ReadLibraryFromConfig);
  90. }
  91. /// <summary>
  92. /// Read a library set from config
  93. /// </summary>
  94. /// <param name="config"></param>
  95. protected void ReadLibraryFromConfig(IConfig config, string path)
  96. {
  97. string basePath = Path.GetDirectoryName(path);
  98. string foldersPath
  99. = Path.Combine(
  100. basePath, config.GetString("foldersFile", String.Empty));
  101. LoadFromFile(foldersPath, "Library folders", ReadFolderFromConfig);
  102. string itemsPath
  103. = Path.Combine(
  104. basePath, config.GetString("itemsFile", String.Empty));
  105. LoadFromFile(itemsPath, "Library items", ReadItemFromConfig);
  106. }
  107. /// <summary>
  108. /// Read a library inventory folder from a loaded configuration
  109. /// </summary>
  110. /// <param name="source"></param>
  111. private void ReadFolderFromConfig(IConfig config, string path)
  112. {
  113. InventoryFolderImpl folderInfo = new InventoryFolderImpl();
  114. folderInfo.ID = new UUID(config.GetString("folderID", ID.ToString()));
  115. folderInfo.Name = config.GetString("name", "unknown");
  116. folderInfo.ParentID = new UUID(config.GetString("parentFolderID", ID.ToString()));
  117. folderInfo.Type = (short)config.GetInt("type", 8);
  118. folderInfo.Owner = libOwner;
  119. folderInfo.Version = 1;
  120. if (libraryFolders.ContainsKey(folderInfo.ParentID))
  121. {
  122. InventoryFolderImpl parentFolder = libraryFolders[folderInfo.ParentID];
  123. libraryFolders.Add(folderInfo.ID, folderInfo);
  124. parentFolder.AddChildFolder(folderInfo);
  125. // m_log.InfoFormat("[LIBRARY INVENTORY]: Adding folder {0} ({1})", folderInfo.name, folderInfo.folderID);
  126. }
  127. else
  128. {
  129. m_log.WarnFormat(
  130. "[LIBRARY INVENTORY]: Couldn't add folder {0} ({1}) since parent folder with ID {2} does not exist!",
  131. folderInfo.Name, folderInfo.ID, folderInfo.ParentID);
  132. }
  133. }
  134. /// <summary>
  135. /// Read a library inventory item metadata from a loaded configuration
  136. /// </summary>
  137. /// <param name="source"></param>
  138. private void ReadItemFromConfig(IConfig config, string path)
  139. {
  140. InventoryItemBase item = new InventoryItemBase();
  141. item.Owner = libOwner;
  142. item.CreatorId = libOwner.ToString();
  143. item.ID = new UUID(config.GetString("inventoryID", ID.ToString()));
  144. item.AssetID = new UUID(config.GetString("assetID", item.ID.ToString()));
  145. item.Folder = new UUID(config.GetString("folderID", ID.ToString()));
  146. item.Name = config.GetString("name", String.Empty);
  147. item.Description = config.GetString("description", item.Name);
  148. item.InvType = config.GetInt("inventoryType", 0);
  149. item.AssetType = config.GetInt("assetType", item.InvType);
  150. item.CurrentPermissions = (uint)config.GetLong("currentPermissions", 0x7FFFFFFF);
  151. item.NextPermissions = (uint)config.GetLong("nextPermissions", 0x7FFFFFFF);
  152. item.EveryOnePermissions = (uint)config.GetLong("everyonePermissions", 0x7FFFFFFF);
  153. item.BasePermissions = (uint)config.GetLong("basePermissions", 0x7FFFFFFF);
  154. item.Flags = (uint)config.GetInt("flags", 0);
  155. if (libraryFolders.ContainsKey(item.Folder))
  156. {
  157. InventoryFolderImpl parentFolder = libraryFolders[item.Folder];
  158. try
  159. {
  160. parentFolder.Items.Add(item.ID, item);
  161. }
  162. catch (Exception)
  163. {
  164. m_log.WarnFormat("[LIBRARY INVENTORY] Item {1} [{0}] not added, duplicate item", item.ID, item.Name);
  165. }
  166. }
  167. else
  168. {
  169. m_log.WarnFormat(
  170. "[LIBRARY INVENTORY]: Couldn't add item {0} ({1}) since parent folder with ID {2} does not exist!",
  171. item.Name, item.ID, item.Folder);
  172. }
  173. }
  174. private delegate void ConfigAction(IConfig config, string path);
  175. /// <summary>
  176. /// Load the given configuration at a path and perform an action on each Config contained within it
  177. /// </summary>
  178. /// <param name="path"></param>
  179. /// <param name="fileDescription"></param>
  180. /// <param name="action"></param>
  181. private static void LoadFromFile(string path, string fileDescription, ConfigAction action)
  182. {
  183. if (File.Exists(path))
  184. {
  185. try
  186. {
  187. XmlConfigSource source = new XmlConfigSource(path);
  188. for (int i = 0; i < source.Configs.Count; i++)
  189. {
  190. action(source.Configs[i], path);
  191. }
  192. }
  193. catch (XmlException e)
  194. {
  195. m_log.ErrorFormat("[LIBRARY INVENTORY]: Error loading {0} : {1}", path, e);
  196. }
  197. }
  198. else
  199. {
  200. m_log.ErrorFormat("[LIBRARY INVENTORY]: {0} file {1} does not exist!", fileDescription, path);
  201. }
  202. }
  203. /// <summary>
  204. /// Looks like a simple getter, but is written like this for some consistency with the other Request
  205. /// methods in the superclass
  206. /// </summary>
  207. /// <returns></returns>
  208. public Dictionary<UUID, InventoryFolderImpl> RequestSelfAndDescendentFolders()
  209. {
  210. Dictionary<UUID, InventoryFolderImpl> fs = new Dictionary<UUID, InventoryFolderImpl>();
  211. fs.Add(ID, this);
  212. List<InventoryFolderImpl> fis = TraverseFolder(this);
  213. foreach (InventoryFolderImpl f in fis)
  214. {
  215. fs.Add(f.ID, f);
  216. }
  217. //return libraryFolders;
  218. return fs;
  219. }
  220. private List<InventoryFolderImpl> TraverseFolder(InventoryFolderImpl node)
  221. {
  222. List<InventoryFolderImpl> folders = node.RequestListOfFolderImpls();
  223. List<InventoryFolderImpl> subs = new List<InventoryFolderImpl>();
  224. foreach (InventoryFolderImpl f in folders)
  225. subs.AddRange(TraverseFolder(f));
  226. folders.AddRange(subs);
  227. return folders;
  228. }
  229. }
  230. }