AssetBase.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.Xml.Serialization;
  29. using System.Reflection;
  30. using log4net;
  31. using OpenMetaverse;
  32. namespace OpenSim.Framework
  33. {
  34. /// <summary>
  35. /// Asset class. All Assets are reference by this class or a class derived from this class
  36. /// </summary>
  37. [Serializable]
  38. public class AssetBase
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// Data of the Asset
  43. /// </summary>
  44. private byte[] m_data;
  45. /// <summary>
  46. /// Meta Data of the Asset
  47. /// </summary>
  48. private AssetMetadata m_metadata;
  49. // This is needed for .NET serialization!!!
  50. // Do NOT "Optimize" away!
  51. public AssetBase()
  52. {
  53. m_metadata = new AssetMetadata();
  54. m_metadata.FullID = UUID.Zero;
  55. m_metadata.ID = UUID.Zero.ToString();
  56. m_metadata.Type = (sbyte)AssetType.Unknown;
  57. }
  58. public AssetBase(UUID assetID, string name, sbyte assetType)
  59. {
  60. if (assetType == (sbyte)AssetType.Unknown)
  61. {
  62. System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);
  63. m_log.ErrorFormat("[ASSETBASE]: Creating asset '{0}' ({1}) with an unknown asset type\n{2}",
  64. name, assetID, trace.ToString());
  65. }
  66. m_metadata = new AssetMetadata();
  67. m_metadata.FullID = assetID;
  68. m_metadata.Name = name;
  69. m_metadata.Type = assetType;
  70. }
  71. public AssetBase(string assetID, string name, sbyte assetType)
  72. {
  73. if (assetType == (sbyte)AssetType.Unknown)
  74. {
  75. System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);
  76. m_log.ErrorFormat("[ASSETBASE]: Creating asset '{0}' ({1}) with an unknown asset type\n{2}",
  77. name, assetID, trace.ToString());
  78. }
  79. m_metadata = new AssetMetadata();
  80. m_metadata.ID = assetID;
  81. m_metadata.Name = name;
  82. m_metadata.Type = assetType;
  83. }
  84. public bool ContainsReferences
  85. {
  86. get
  87. {
  88. return
  89. IsTextualAsset && (
  90. Type != (sbyte)AssetType.Notecard
  91. && Type != (sbyte)AssetType.CallingCard
  92. && Type != (sbyte)AssetType.LSLText
  93. && Type != (sbyte)AssetType.Landmark);
  94. }
  95. }
  96. public bool IsTextualAsset
  97. {
  98. get
  99. {
  100. return !IsBinaryAsset;
  101. }
  102. }
  103. /// <summary>
  104. /// Checks if this asset is a binary or text asset
  105. /// </summary>
  106. public bool IsBinaryAsset
  107. {
  108. get
  109. {
  110. return
  111. (Type == (sbyte) AssetType.Animation ||
  112. Type == (sbyte)AssetType.Gesture ||
  113. Type == (sbyte)AssetType.Simstate ||
  114. Type == (sbyte)AssetType.Unknown ||
  115. Type == (sbyte)AssetType.Object ||
  116. Type == (sbyte)AssetType.Sound ||
  117. Type == (sbyte)AssetType.SoundWAV ||
  118. Type == (sbyte)AssetType.Texture ||
  119. Type == (sbyte)AssetType.TextureTGA ||
  120. Type == (sbyte)AssetType.Folder ||
  121. Type == (sbyte)AssetType.RootFolder ||
  122. Type == (sbyte)AssetType.LostAndFoundFolder ||
  123. Type == (sbyte)AssetType.SnapshotFolder ||
  124. Type == (sbyte)AssetType.TrashFolder ||
  125. Type == (sbyte)AssetType.ImageJPEG ||
  126. Type == (sbyte) AssetType.ImageTGA ||
  127. Type == (sbyte) AssetType.LSLBytecode);
  128. }
  129. }
  130. public virtual byte[] Data
  131. {
  132. get { return m_data; }
  133. set { m_data = value; }
  134. }
  135. /// <summary>
  136. /// Asset UUID
  137. /// </summary>
  138. public UUID FullID
  139. {
  140. get { return m_metadata.FullID; }
  141. set { m_metadata.FullID = value; }
  142. }
  143. /// <summary>
  144. /// Asset MetaData ID (transferring from UUID to string ID)
  145. /// </summary>
  146. public string ID
  147. {
  148. get { return m_metadata.ID; }
  149. set { m_metadata.ID = value; }
  150. }
  151. public string Name
  152. {
  153. get { return m_metadata.Name; }
  154. set { m_metadata.Name = value; }
  155. }
  156. public string Description
  157. {
  158. get { return m_metadata.Description; }
  159. set { m_metadata.Description = value; }
  160. }
  161. /// <summary>
  162. /// (sbyte) AssetType enum
  163. /// </summary>
  164. public sbyte Type
  165. {
  166. get { return m_metadata.Type; }
  167. set { m_metadata.Type = value; }
  168. }
  169. /// <summary>
  170. /// Is this a region only asset, or does this exist on the asset server also
  171. /// </summary>
  172. public bool Local
  173. {
  174. get { return m_metadata.Local; }
  175. set { m_metadata.Local = value; }
  176. }
  177. /// <summary>
  178. /// Is this asset going to be saved to the asset database?
  179. /// </summary>
  180. public bool Temporary
  181. {
  182. get { return m_metadata.Temporary; }
  183. set { m_metadata.Temporary = value; }
  184. }
  185. [XmlIgnore]
  186. public AssetMetadata Metadata
  187. {
  188. get { return m_metadata; }
  189. set { m_metadata = value; }
  190. }
  191. public override string ToString()
  192. {
  193. return FullID.ToString();
  194. }
  195. }
  196. [Serializable]
  197. public class AssetMetadata
  198. {
  199. private UUID m_fullid;
  200. // m_id added as a dirty hack to transition from FullID to ID
  201. private string m_id;
  202. private string m_name = String.Empty;
  203. private string m_description = String.Empty;
  204. private DateTime m_creation_date;
  205. private sbyte m_type = (sbyte)AssetType.Unknown;
  206. private string m_content_type;
  207. private byte[] m_sha1;
  208. private bool m_local;
  209. private bool m_temporary;
  210. //private Dictionary<string, Uri> m_methods = new Dictionary<string, Uri>();
  211. //private OSDMap m_extra_data;
  212. public UUID FullID
  213. {
  214. get { return m_fullid; }
  215. set { m_fullid = value; m_id = m_fullid.ToString(); }
  216. }
  217. public string ID
  218. {
  219. //get { return m_fullid.ToString(); }
  220. //set { m_fullid = new UUID(value); }
  221. get
  222. {
  223. if (String.IsNullOrEmpty(m_id))
  224. m_id = m_fullid.ToString();
  225. return m_id;
  226. }
  227. set
  228. {
  229. UUID uuid = UUID.Zero;
  230. if (UUID.TryParse(value, out uuid))
  231. {
  232. m_fullid = uuid;
  233. m_id = m_fullid.ToString();
  234. }
  235. else
  236. m_id = value;
  237. }
  238. }
  239. public string Name
  240. {
  241. get { return m_name; }
  242. set { m_name = value; }
  243. }
  244. public string Description
  245. {
  246. get { return m_description; }
  247. set { m_description = value; }
  248. }
  249. public DateTime CreationDate
  250. {
  251. get { return m_creation_date; }
  252. set { m_creation_date = value; }
  253. }
  254. public sbyte Type
  255. {
  256. get { return m_type; }
  257. set { m_type = value; }
  258. }
  259. public string ContentType
  260. {
  261. get { return m_content_type; }
  262. set { m_content_type = value; }
  263. }
  264. public byte[] SHA1
  265. {
  266. get { return m_sha1; }
  267. set { m_sha1 = value; }
  268. }
  269. public bool Local
  270. {
  271. get { return m_local; }
  272. set { m_local = value; }
  273. }
  274. public bool Temporary
  275. {
  276. get { return m_temporary; }
  277. set { m_temporary = value; }
  278. }
  279. //public Dictionary<string, Uri> Methods
  280. //{
  281. // get { return m_methods; }
  282. // set { m_methods = value; }
  283. //}
  284. //public OSDMap ExtraData
  285. //{
  286. // get { return m_extra_data; }
  287. // set { m_extra_data = value; }
  288. //}
  289. }
  290. }