AssetLoaderFileSystem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /// <summary>
  36. /// Loads assets from the filesystem location. Not yet a plugin, though it should be.
  37. /// </summary>
  38. namespace OpenSim.Framework.AssetLoader.Filesystem
  39. {
  40. public class AssetLoaderFileSystem : IAssetLoader
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. protected static AssetBase CreateAsset(string assetIdStr, string name, string path, bool isImage)
  44. {
  45. AssetBase asset = new AssetBase(
  46. new LLUUID(assetIdStr),
  47. name
  48. );
  49. if (!String.IsNullOrEmpty(path))
  50. {
  51. m_log.InfoFormat("[ASSETS]: Loading: [{0}][{1}]", name, path);
  52. LoadAsset(asset, isImage, path);
  53. }
  54. else
  55. {
  56. m_log.InfoFormat("[ASSETS]: Instantiated: [{0}]", name);
  57. }
  58. return asset;
  59. }
  60. protected static void LoadAsset(AssetBase info, bool image, string path)
  61. {
  62. FileInfo fInfo = new FileInfo(path);
  63. long numBytes = fInfo.Length;
  64. FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read);
  65. byte[] idata = new byte[numBytes];
  66. BinaryReader br = new BinaryReader(fStream);
  67. idata = br.ReadBytes((int) numBytes);
  68. br.Close();
  69. fStream.Close();
  70. info.Data = idata;
  71. //info.loaded=true;
  72. }
  73. public void ForEachDefaultXmlAsset(Action<AssetBase> action)
  74. {
  75. string assetSetFilename = Path.Combine(Util.assetsDir(), "AssetSets.xml");
  76. ForEachDefaultXmlAsset(assetSetFilename, action);
  77. }
  78. public void ForEachDefaultXmlAsset(string assetSetFilename, Action<AssetBase> action)
  79. {
  80. List<AssetBase> assets = new List<AssetBase>();
  81. if (File.Exists(assetSetFilename))
  82. {
  83. string assetSetPath = "ERROR";
  84. try
  85. {
  86. XmlConfigSource source = new XmlConfigSource(assetSetFilename);
  87. for (int i = 0; i < source.Configs.Count; i++)
  88. {
  89. assetSetPath = source.Configs[i].GetString("file", String.Empty);
  90. LoadXmlAssetSet(Path.Combine(Util.assetsDir(), assetSetPath), assets);
  91. }
  92. }
  93. catch (XmlException e)
  94. {
  95. m_log.ErrorFormat("[ASSETS]: Error loading {0} : {1}", assetSetPath, e);
  96. }
  97. }
  98. else
  99. {
  100. m_log.Error("[ASSETS]: Asset set control file assets/AssetSets.xml does not exist! No assets loaded.");
  101. }
  102. assets.ForEach(action);
  103. }
  104. /// <summary>
  105. /// Use the asset set information at path to load assets
  106. /// </summary>
  107. /// <param name="assetSetPath"></param>
  108. /// <param name="assets"></param>
  109. protected static void LoadXmlAssetSet(string assetSetPath, List<AssetBase> assets)
  110. {
  111. m_log.InfoFormat("[ASSETS]: Loading asset set {0}", assetSetPath);
  112. if (File.Exists(assetSetPath))
  113. {
  114. try
  115. {
  116. XmlConfigSource source = new XmlConfigSource(assetSetPath);
  117. String dir = Path.GetDirectoryName(assetSetPath);
  118. for (int i = 0; i < source.Configs.Count; i++)
  119. {
  120. string assetIdStr = source.Configs[i].GetString("assetID", LLUUID.Random().ToString());
  121. string name = source.Configs[i].GetString("name", String.Empty);
  122. sbyte type = (sbyte) source.Configs[i].GetInt("assetType", 0);
  123. sbyte invType = (sbyte) source.Configs[i].GetInt("inventoryType", 0);
  124. string assetPath = Path.Combine(dir, source.Configs[i].GetString("fileName", String.Empty));
  125. AssetBase newAsset = CreateAsset(assetIdStr, name, assetPath, false);
  126. newAsset.Type = type;
  127. newAsset.InvType = invType;
  128. assets.Add(newAsset);
  129. }
  130. }
  131. catch (XmlException e)
  132. {
  133. m_log.ErrorFormat("[ASSETS]: Error loading {0} : {1}", assetSetPath, e);
  134. }
  135. }
  136. else
  137. {
  138. m_log.ErrorFormat("[ASSETS]: Asset set file {0} does not exist!", assetSetPath);
  139. }
  140. }
  141. }
  142. }