LibraryRootFolder.cs 13 KB

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