LibraryRootFolder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 OpenSim 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 libsecondlife;
  33. using log4net;
  34. using Nini.Config;
  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 LLUUID libOwner = new LLUUID("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<LLUUID, InventoryFolderImpl> libraryFolders
  50. = new Dictionary<LLUUID, InventoryFolderImpl>();
  51. public LibraryRootFolder()
  52. {
  53. m_log.Info("[LIBRARY INVENTORY]: Loading library inventory");
  54. Owner = libOwner;
  55. ID = new LLUUID("00000112-000f-0000-0000-000100bba000");
  56. Name = "OpenSim Library";
  57. ParentID = LLUUID.Zero;
  58. Type = (short) 8;
  59. Version = (ushort) 1;
  60. libraryFolders.Add(ID, this);
  61. LoadLibraries(Path.Combine(Util.inventoryDir(), "Libraries.xml"));
  62. // CreateLibraryItems();
  63. }
  64. /// <summary>
  65. /// Hardcoded item creation. Please don't add any more items here - future items should be created
  66. /// in the xml in the bin/inventory folder.
  67. /// </summary>
  68. ///
  69. /// Commented the following out due to sending it all through xml, remove this section once this is provin to work stable.
  70. ///
  71. //private void CreateLibraryItems()
  72. //{
  73. // InventoryItemBase item =
  74. // CreateItem(new LLUUID("66c41e39-38f9-f75a-024e-585989bfaba9"),
  75. // new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"), "Default Shape", "Default Shape",
  76. // (int) AssetType.Bodypart, (int) InventoryType.Wearable, folderID);
  77. // item.inventoryCurrentPermissions = 0;
  78. // item.inventoryNextPermissions = 0;
  79. // Items.Add(item.inventoryID, item);
  80. // item =
  81. // CreateItem(new LLUUID("77c41e39-38f9-f75a-024e-585989bfabc9"),
  82. // new LLUUID("77c41e39-38f9-f75a-024e-585989bbabbb"), "Default Skin", "Default Skin",
  83. // (int) AssetType.Bodypart, (int) InventoryType.Wearable, folderID);
  84. // item.inventoryCurrentPermissions = 0;
  85. // item.inventoryNextPermissions = 0;
  86. // Items.Add(item.inventoryID, item);
  87. // item =
  88. // CreateItem(new LLUUID("77c41e39-38f9-f75a-0000-585989bf0000"),
  89. // new LLUUID("00000000-38f9-1111-024e-222222111110"), "Default Shirt", "Default Shirt",
  90. // (int) AssetType.Clothing, (int) InventoryType.Wearable, folderID);
  91. // item.inventoryCurrentPermissions = 0;
  92. // item.inventoryNextPermissions = 0;
  93. // Items.Add(item.inventoryID, item);
  94. // item =
  95. // CreateItem(new LLUUID("77c41e39-38f9-f75a-0000-5859892f1111"),
  96. // new LLUUID("00000000-38f9-1111-024e-222222111120"), "Default Pants", "Default Pants",
  97. // (int) AssetType.Clothing, (int) InventoryType.Wearable, folderID);
  98. // item.inventoryCurrentPermissions = 0;
  99. // item.inventoryNextPermissions = 0;
  100. // Items.Add(item.inventoryID, item);
  101. //}
  102. public InventoryItemBase CreateItem(LLUUID inventoryID, LLUUID assetID, string name, string description,
  103. int assetType, int invType, LLUUID parentFolderID)
  104. {
  105. InventoryItemBase item = new InventoryItemBase();
  106. item.Owner = libOwner;
  107. item.Creator = libOwner;
  108. item.ID = inventoryID;
  109. item.AssetID = assetID;
  110. item.Description = description;
  111. item.Name = name;
  112. item.AssetType = assetType;
  113. item.InvType = invType;
  114. item.Folder = parentFolderID;
  115. item.BasePermissions = 0x7FFFFFFF;
  116. item.EveryOnePermissions = 0x7FFFFFFF;
  117. item.CurrentPermissions = 0x7FFFFFFF;
  118. item.NextPermissions = 0x7FFFFFFF;
  119. return item;
  120. }
  121. /// <summary>
  122. /// Use the asset set information at path to load assets
  123. /// </summary>
  124. /// <param name="path"></param>
  125. /// <param name="assets"></param>
  126. protected void LoadLibraries(string librariesControlPath)
  127. {
  128. m_log.InfoFormat(
  129. "[LIBRARY INVENTORY]: Loading libraries control file {0}", librariesControlPath);
  130. LoadFromFile(librariesControlPath, "Libraries control", ReadLibraryFromConfig);
  131. }
  132. /// <summary>
  133. /// Read a library set from config
  134. /// </summary>
  135. /// <param name="config"></param>
  136. protected void ReadLibraryFromConfig(IConfig config)
  137. {
  138. string foldersPath
  139. = Path.Combine(
  140. Util.inventoryDir(), config.GetString("foldersFile", String.Empty));
  141. LoadFromFile(foldersPath, "Library folders", ReadFolderFromConfig);
  142. string itemsPath
  143. = Path.Combine(
  144. Util.inventoryDir(), config.GetString("itemsFile", String.Empty));
  145. LoadFromFile(itemsPath, "Library items", ReadItemFromConfig);
  146. }
  147. /// <summary>
  148. /// Read a library inventory folder from a loaded configuration
  149. /// </summary>
  150. /// <param name="source"></param>
  151. private void ReadFolderFromConfig(IConfig config)
  152. {
  153. InventoryFolderImpl folderInfo = new InventoryFolderImpl();
  154. folderInfo.ID = new LLUUID(config.GetString("folderID", ID.ToString()));
  155. folderInfo.Name = config.GetString("name", "unknown");
  156. folderInfo.ParentID = new LLUUID(config.GetString("parentFolderID", ID.ToString()));
  157. folderInfo.Type = (short)config.GetInt("type", 8);
  158. folderInfo.Owner = libOwner;
  159. folderInfo.Version = 1;
  160. if (libraryFolders.ContainsKey(folderInfo.ParentID))
  161. {
  162. InventoryFolderImpl parentFolder = libraryFolders[folderInfo.ParentID];
  163. libraryFolders.Add(folderInfo.ID, folderInfo);
  164. parentFolder.SubFolders.Add(folderInfo.ID, folderInfo);
  165. // m_log.InfoFormat("[LIBRARY INVENTORY]: Adding folder {0} ({1})", folderInfo.name, folderInfo.folderID);
  166. }
  167. else
  168. {
  169. m_log.WarnFormat(
  170. "[LIBRARY INVENTORY]: Couldn't add folder {0} ({1}) since parent folder with ID {2} does not exist!",
  171. folderInfo.Name, folderInfo.ID, folderInfo.ParentID);
  172. }
  173. }
  174. /// <summary>
  175. /// Read a library inventory item metadata from a loaded configuration
  176. /// </summary>
  177. /// <param name="source"></param>
  178. private void ReadItemFromConfig(IConfig config)
  179. {
  180. InventoryItemBase item = new InventoryItemBase();
  181. item.Owner = libOwner;
  182. item.Creator = libOwner;
  183. item.ID = new LLUUID(config.GetString("inventoryID", ID.ToString()));
  184. item.AssetID = new LLUUID(config.GetString("assetID", item.ID.ToString()));
  185. item.Folder = new LLUUID(config.GetString("folderID", ID.ToString()));
  186. item.Name = config.GetString("name", String.Empty);
  187. item.Description = config.GetString("description", item.Name);
  188. item.InvType = config.GetInt("inventoryType", 0);
  189. item.AssetType = config.GetInt("assetType", item.InvType);
  190. item.CurrentPermissions = (uint)config.GetLong("currentPermissions", 0x7FFFFFFF);
  191. item.NextPermissions = (uint)config.GetLong("nextPermissions", 0x7FFFFFFF);
  192. item.EveryOnePermissions = (uint)config.GetLong("everyonePermissions", 0x7FFFFFFF);
  193. item.BasePermissions = (uint)config.GetLong("basePermissions", 0x7FFFFFFF);
  194. if (libraryFolders.ContainsKey(item.Folder))
  195. {
  196. InventoryFolderImpl parentFolder = libraryFolders[item.Folder];
  197. parentFolder.Items.Add(item.ID, item);
  198. }
  199. else
  200. {
  201. m_log.WarnFormat(
  202. "[LIBRARY INVENTORY]: Couldn't add item {0} ({1}) since parent folder with ID {2} does not exist!",
  203. item.Name, item.ID, item.Folder);
  204. }
  205. }
  206. private delegate void ConfigAction(IConfig config);
  207. /// <summary>
  208. /// Load the given configuration at a path and perform an action on each Config contained within it
  209. /// </summary>
  210. /// <param name="path"></param>
  211. /// <param name="fileDescription"></param>
  212. /// <param name="action"></param>
  213. private static void LoadFromFile(string path, string fileDescription, ConfigAction action)
  214. {
  215. if (File.Exists(path))
  216. {
  217. try
  218. {
  219. XmlConfigSource source = new XmlConfigSource(path);
  220. for (int i = 0; i < source.Configs.Count; i++)
  221. {
  222. action(source.Configs[i]);
  223. }
  224. }
  225. catch (XmlException e)
  226. {
  227. m_log.ErrorFormat("[LIBRARY INVENTORY]: Error loading {0} : {1}", path, e);
  228. }
  229. }
  230. else
  231. {
  232. m_log.ErrorFormat("[LIBRARY INVENTORY]: {0} file {1} does not exist!", fileDescription, path);
  233. }
  234. }
  235. /// <summary>
  236. /// Looks like a simple getter, but is written like this for some consistency with the other Request
  237. /// methods in the superclass
  238. /// </summary>
  239. /// <returns></returns>
  240. public Dictionary<LLUUID, InventoryFolderImpl> RequestSelfAndDescendentFolders()
  241. {
  242. return libraryFolders;
  243. }
  244. }
  245. }