AssetLoaderFileSystem.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /// <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, sbyte type)
  44. {
  45. AssetBase asset = new AssetBase(new UUID(assetIdStr), name, type);
  46. if (!String.IsNullOrEmpty(path))
  47. {
  48. //m_log.InfoFormat("[ASSETS]: Loading: [{0}][{1}]", name, path);
  49. LoadAsset(asset, path);
  50. }
  51. else
  52. {
  53. m_log.InfoFormat("[ASSETS]: Instantiated: [{0}]", name);
  54. }
  55. return asset;
  56. }
  57. protected static void LoadAsset(AssetBase info, string path)
  58. {
  59. // bool image =
  60. // (info.Type == (sbyte)AssetType.Texture ||
  61. // info.Type == (sbyte)AssetType.TextureTGA ||
  62. // info.Type == (sbyte)AssetType.ImageJPEG ||
  63. // info.Type == (sbyte)AssetType.ImageTGA);
  64. FileInfo fInfo = new FileInfo(path);
  65. long numBytes = fInfo.Length;
  66. if (fInfo.Exists)
  67. {
  68. FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read);
  69. byte[] idata = new byte[numBytes];
  70. BinaryReader br = new BinaryReader(fStream);
  71. idata = br.ReadBytes((int)numBytes);
  72. br.Close();
  73. fStream.Close();
  74. info.Data = idata;
  75. //info.loaded=true;
  76. }
  77. else
  78. {
  79. m_log.ErrorFormat("[ASSETS]: file: [{0}] not found !", path);
  80. }
  81. }
  82. public void ForEachDefaultXmlAsset(string assetSetFilename, Action<AssetBase> action)
  83. {
  84. List<AssetBase> assets = new List<AssetBase>();
  85. if (File.Exists(assetSetFilename))
  86. {
  87. string assetSetPath = "ERROR";
  88. string assetRootPath = "";
  89. try
  90. {
  91. XmlConfigSource source = new XmlConfigSource(assetSetFilename);
  92. assetRootPath = Path.GetFullPath(source.SavePath);
  93. assetRootPath = Path.GetDirectoryName(assetRootPath);
  94. for (int i = 0; i < source.Configs.Count; i++)
  95. {
  96. assetSetPath = source.Configs[i].GetString("file", String.Empty);
  97. LoadXmlAssetSet(Path.Combine(assetRootPath, assetSetPath), assets);
  98. }
  99. }
  100. catch (XmlException e)
  101. {
  102. m_log.ErrorFormat("[ASSETS]: Error loading {0} : {1}", assetSetPath, e);
  103. }
  104. }
  105. else
  106. {
  107. m_log.ErrorFormat("[ASSETS]: Asset set control file {0} does not exist! No assets loaded.", assetSetFilename);
  108. }
  109. assets.ForEach(action);
  110. }
  111. /// <summary>
  112. /// Use the asset set information at path to load assets
  113. /// </summary>
  114. /// <param name="assetSetPath"></param>
  115. /// <param name="assets"></param>
  116. protected static void LoadXmlAssetSet(string assetSetPath, List<AssetBase> assets)
  117. {
  118. //m_log.InfoFormat("[ASSETS]: Loading asset set {0}", assetSetPath);
  119. if (File.Exists(assetSetPath))
  120. {
  121. try
  122. {
  123. XmlConfigSource source = new XmlConfigSource(assetSetPath);
  124. String dir = Path.GetDirectoryName(assetSetPath);
  125. for (int i = 0; i < source.Configs.Count; i++)
  126. {
  127. string assetIdStr = source.Configs[i].GetString("assetID", UUID.Random().ToString());
  128. string name = source.Configs[i].GetString("name", String.Empty);
  129. sbyte type = (sbyte)source.Configs[i].GetInt("assetType", 0);
  130. string assetPath = Path.Combine(dir, source.Configs[i].GetString("fileName", String.Empty));
  131. AssetBase newAsset = CreateAsset(assetIdStr, name, assetPath, type);
  132. newAsset.Type = type;
  133. assets.Add(newAsset);
  134. }
  135. }
  136. catch (XmlException e)
  137. {
  138. m_log.ErrorFormat("[ASSETS]: Error loading {0} : {1}", assetSetPath, e);
  139. }
  140. }
  141. else
  142. {
  143. m_log.ErrorFormat("[ASSETS]: Asset set file {0} does not exist!", assetSetPath);
  144. }
  145. }
  146. }
  147. }