AssetLoaderFileSystem.cs 6.7 KB

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