AssetLoaderFileSystem.cs 6.7 KB

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