OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
AssetLoaderFileSystem.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Xml;
33 using log4net;
34 using Nini.Config;
35 using OpenMetaverse;
36 
40 namespace OpenSim.Framework.AssetLoader.Filesystem
41 {
43  {
44  private static readonly UUID LIBRARY_OWNER_ID = new UUID("11111111-1111-0000-0000-000100bba000");
45  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46 
47  protected static AssetBase CreateAsset(string assetIdStr, string name, string path, sbyte type)
48  {
49  AssetBase asset = new AssetBase(new UUID(assetIdStr), name, type, LIBRARY_OWNER_ID.ToString());
50 
51  if (!String.IsNullOrEmpty(path))
52  {
53  //m_log.InfoFormat("[ASSETS]: Loading: [{0}][{1}]", name, path);
54 
55  LoadAsset(asset, path);
56  }
57  else
58  {
59  m_log.InfoFormat("[ASSETS]: Instantiated: [{0}]", name);
60  }
61 
62  return asset;
63  }
64 
65  protected static void LoadAsset(AssetBase info, string path)
66  {
67 // bool image =
68 // (info.Type == (sbyte)AssetType.Texture ||
69 // info.Type == (sbyte)AssetType.TextureTGA ||
70 // info.Type == (sbyte)AssetType.ImageJPEG ||
71 // info.Type == (sbyte)AssetType.ImageTGA);
72 
73  FileInfo fInfo = new FileInfo(path);
74  long numBytes = fInfo.Length;
75  if (fInfo.Exists)
76  {
77  FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read);
78  byte[] idata = new byte[numBytes];
79  BinaryReader br = new BinaryReader(fStream);
80  idata = br.ReadBytes((int)numBytes);
81  br.Close();
82  fStream.Close();
83  info.Data = idata;
84  //info.loaded=true;
85  }
86  else
87  {
88  m_log.ErrorFormat("[ASSETS]: file: [{0}] not found !", path);
89  }
90  }
91 
92  public void ForEachDefaultXmlAsset(string assetSetFilename, Action<AssetBase> action)
93  {
94  List<AssetBase> assets = new List<AssetBase>();
95  if (File.Exists(assetSetFilename))
96  {
97  string assetSetPath = "ERROR";
98  string assetRootPath = "";
99  try
100  {
101  XmlConfigSource source = new XmlConfigSource(assetSetFilename);
102  assetRootPath = Path.GetFullPath(source.SavePath);
103  assetRootPath = Path.GetDirectoryName(assetRootPath);
104 
105  for (int i = 0; i < source.Configs.Count; i++)
106  {
107  assetSetPath = source.Configs[i].GetString("file", String.Empty);
108 
109  LoadXmlAssetSet(Path.Combine(assetRootPath, assetSetPath), assets);
110  }
111  }
112  catch (XmlException e)
113  {
114  m_log.ErrorFormat("[ASSETS]: Error loading {0} : {1}", assetSetPath, e);
115  }
116  }
117  else
118  {
119  m_log.ErrorFormat("[ASSETS]: Asset set control file {0} does not exist! No assets loaded.", assetSetFilename);
120  }
121 
122  assets.ForEach(action);
123  }
124 
130  protected static void LoadXmlAssetSet(string assetSetPath, List<AssetBase> assets)
131  {
132  //m_log.InfoFormat("[ASSETS]: Loading asset set {0}", assetSetPath);
133 
134  if (File.Exists(assetSetPath))
135  {
136  try
137  {
138  XmlConfigSource source = new XmlConfigSource(assetSetPath);
139  String dir = Path.GetDirectoryName(assetSetPath);
140 
141  for (int i = 0; i < source.Configs.Count; i++)
142  {
143  string assetIdStr = source.Configs[i].GetString("assetID", UUID.Random().ToString());
144  string name = source.Configs[i].GetString("name", String.Empty);
145  sbyte type = (sbyte)source.Configs[i].GetInt("assetType", 0);
146  string assetPath = Path.Combine(dir, source.Configs[i].GetString("fileName", String.Empty));
147 
148  AssetBase newAsset = CreateAsset(assetIdStr, name, assetPath, type);
149 
150  newAsset.Type = type;
151  assets.Add(newAsset);
152  }
153  }
154  catch (XmlException e)
155  {
156  m_log.ErrorFormat("[ASSETS]: Error loading {0} : {1}", assetSetPath, e);
157  }
158  }
159  else
160  {
161  m_log.ErrorFormat("[ASSETS]: Asset set file {0} does not exist!", assetSetPath);
162  }
163  }
164  }
165 }
void ForEachDefaultXmlAsset(string assetSetFilename, Action< AssetBase > action)
static void LoadXmlAssetSet(string assetSetPath, List< AssetBase > assets)
Use the asset set information at path to load assets
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
static AssetBase CreateAsset(string assetIdStr, string name, string path, sbyte type)
Interactive OpenSim region server
Definition: OpenSim.cs:55