1
0

AssetLoaderFileSystem.cs 7.0 KB

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