AssetBase.cs 11 KB

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