AssetBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. public static readonly int MAX_ASSET_NAME = 64;
  50. public static readonly int MAX_ASSET_DESC = 64;
  51. /// <summary>
  52. /// Data of the Asset
  53. /// </summary>
  54. private byte[] m_data;
  55. /// <summary>
  56. /// Meta Data of the Asset
  57. /// </summary>
  58. private AssetMetadata m_metadata;
  59. private int m_uploadAttempts;
  60. // This is needed for .NET serialization!!!
  61. // Do NOT "Optimize" away!
  62. public AssetBase()
  63. {
  64. m_metadata = new AssetMetadata();
  65. m_metadata.FullID = UUID.Zero;
  66. m_metadata.ID = UUID.Zero.ToString();
  67. m_metadata.Type = (sbyte)AssetType.Unknown;
  68. m_metadata.CreatorID = String.Empty;
  69. }
  70. public AssetBase(UUID assetID, string name, sbyte assetType, string creatorID)
  71. {
  72. if (assetType == (sbyte)AssetType.Unknown)
  73. {
  74. System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);
  75. m_log.ErrorFormat("[ASSETBASE]: Creating asset '{0}' ({1}) with an unknown asset type\n{2}",
  76. name, assetID, trace.ToString());
  77. }
  78. m_metadata = new AssetMetadata();
  79. m_metadata.FullID = assetID;
  80. m_metadata.Name = name;
  81. m_metadata.Type = assetType;
  82. m_metadata.CreatorID = creatorID;
  83. }
  84. public AssetBase(string assetID, string name, sbyte assetType, string creatorID)
  85. {
  86. if (assetType == (sbyte)AssetType.Unknown)
  87. {
  88. System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);
  89. m_log.ErrorFormat("[ASSETBASE]: Creating asset '{0}' ({1}) with an unknown asset type\n{2}",
  90. name, assetID, trace.ToString());
  91. }
  92. m_metadata = new AssetMetadata();
  93. m_metadata.ID = assetID;
  94. m_metadata.Name = name;
  95. m_metadata.Type = assetType;
  96. m_metadata.CreatorID = creatorID;
  97. }
  98. public bool ContainsReferences
  99. {
  100. get
  101. {
  102. return
  103. IsTextualAsset && (
  104. Type != (sbyte)AssetType.Notecard
  105. && Type != (sbyte)AssetType.CallingCard
  106. && Type != (sbyte)AssetType.LSLText
  107. && Type != (sbyte)AssetType.Landmark);
  108. }
  109. }
  110. public bool IsTextualAsset
  111. {
  112. get
  113. {
  114. return !IsBinaryAsset;
  115. }
  116. }
  117. /// <summary>
  118. /// Checks if this asset is a binary or text asset
  119. /// </summary>
  120. public bool IsBinaryAsset
  121. {
  122. get
  123. {
  124. return
  125. (Type == (sbyte)AssetType.Animation ||
  126. Type == (sbyte)AssetType.Gesture ||
  127. Type == (sbyte)AssetType.Simstate ||
  128. Type == (sbyte)AssetType.Unknown ||
  129. Type == (sbyte)AssetType.Object ||
  130. Type == (sbyte)AssetType.Sound ||
  131. Type == (sbyte)AssetType.SoundWAV ||
  132. Type == (sbyte)AssetType.Texture ||
  133. Type == (sbyte)AssetType.TextureTGA ||
  134. Type == (sbyte)AssetType.Folder ||
  135. Type == (sbyte)AssetType.ImageJPEG ||
  136. Type == (sbyte)AssetType.ImageTGA ||
  137. Type == (sbyte)AssetType.Mesh ||
  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. public int UploadAttempts
  181. {
  182. get { return m_uploadAttempts; }
  183. set { m_uploadAttempts = value; }
  184. }
  185. /// <summary>
  186. /// Is this a region only asset, or does this exist on the asset server also
  187. /// </summary>
  188. public bool Local
  189. {
  190. get { return m_metadata.Local; }
  191. set { m_metadata.Local = value; }
  192. }
  193. /// <summary>
  194. /// Is this asset going to be saved to the asset database?
  195. /// </summary>
  196. public bool Temporary
  197. {
  198. get { return m_metadata.Temporary; }
  199. set { m_metadata.Temporary = value; }
  200. }
  201. public string CreatorID
  202. {
  203. get { return m_metadata.CreatorID; }
  204. set { m_metadata.CreatorID = value; }
  205. }
  206. public AssetFlags Flags
  207. {
  208. get { return m_metadata.Flags; }
  209. set { m_metadata.Flags = value; }
  210. }
  211. [XmlIgnore]
  212. public AssetMetadata Metadata
  213. {
  214. get { return m_metadata; }
  215. set { m_metadata = value; }
  216. }
  217. public override string ToString()
  218. {
  219. return FullID.ToString();
  220. }
  221. }
  222. [Serializable]
  223. public class AssetMetadata
  224. {
  225. private UUID m_fullid;
  226. private string m_id;
  227. private string m_name = String.Empty;
  228. private string m_description = String.Empty;
  229. private DateTime m_creation_date;
  230. private sbyte m_type = (sbyte)AssetType.Unknown;
  231. private string m_content_type;
  232. private byte[] m_sha1;
  233. private bool m_local;
  234. private bool m_temporary;
  235. private string m_creatorid;
  236. private AssetFlags m_flags;
  237. public UUID FullID
  238. {
  239. get { return m_fullid; }
  240. set { m_fullid = value; m_id = m_fullid.ToString(); }
  241. }
  242. public string ID
  243. {
  244. //get { return m_fullid.ToString(); }
  245. //set { m_fullid = new UUID(value); }
  246. get
  247. {
  248. if (String.IsNullOrEmpty(m_id))
  249. m_id = m_fullid.ToString();
  250. return m_id;
  251. }
  252. set
  253. {
  254. UUID uuid = UUID.Zero;
  255. if (UUID.TryParse(value, out uuid))
  256. {
  257. m_fullid = uuid;
  258. m_id = m_fullid.ToString();
  259. }
  260. else
  261. m_id = value;
  262. }
  263. }
  264. public string Name
  265. {
  266. get { return m_name; }
  267. set { m_name = value; }
  268. }
  269. public string Description
  270. {
  271. get { return m_description; }
  272. set { m_description = value; }
  273. }
  274. public DateTime CreationDate
  275. {
  276. get { return m_creation_date; }
  277. set { m_creation_date = value; }
  278. }
  279. public sbyte Type
  280. {
  281. get { return m_type; }
  282. set { m_type = value; }
  283. }
  284. public string ContentType
  285. {
  286. get
  287. {
  288. if (!String.IsNullOrEmpty(m_content_type))
  289. return m_content_type;
  290. else
  291. return SLUtil.SLAssetTypeToContentType(m_type);
  292. }
  293. set
  294. {
  295. m_content_type = value;
  296. sbyte type = (sbyte)SLUtil.ContentTypeToSLAssetType(value);
  297. if (type != -1)
  298. m_type = type;
  299. }
  300. }
  301. public byte[] SHA1
  302. {
  303. get { return m_sha1; }
  304. set { m_sha1 = value; }
  305. }
  306. public bool Local
  307. {
  308. get { return m_local; }
  309. set { m_local = value; }
  310. }
  311. public bool Temporary
  312. {
  313. get { return m_temporary; }
  314. set { m_temporary = value; }
  315. }
  316. public string CreatorID
  317. {
  318. get { return m_creatorid; }
  319. set { m_creatorid = value; }
  320. }
  321. public AssetFlags Flags
  322. {
  323. get { return m_flags; }
  324. set { m_flags = value; }
  325. }
  326. }
  327. }