LibraryRootFolder.cs 13 KB

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