AssetBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 string CreatorID
  197. {
  198. get { return m_metadata.CreatorID; }
  199. set { m_metadata.CreatorID = value; }
  200. }
  201. public AssetFlags Flags
  202. {
  203. get { return m_metadata.Flags; }
  204. set { m_metadata.Flags = value; }
  205. }
  206. [XmlIgnore]
  207. public AssetMetadata Metadata
  208. {
  209. get { return m_metadata; }
  210. set { m_metadata = value; }
  211. }
  212. public override string ToString()
  213. {
  214. return FullID.ToString();
  215. }
  216. }
  217. [Serializable]
  218. public class AssetMetadata
  219. {
  220. private UUID m_fullid;
  221. private string m_id;
  222. private string m_name = String.Empty;
  223. private string m_description = String.Empty;
  224. private DateTime m_creation_date;
  225. private sbyte m_type = (sbyte)AssetType.Unknown;
  226. private string m_content_type;
  227. private byte[] m_sha1;
  228. private bool m_local;
  229. private bool m_temporary;
  230. private string m_creatorid;
  231. private AssetFlags m_flags;
  232. public UUID FullID
  233. {
  234. get { return m_fullid; }
  235. set { m_fullid = value; m_id = m_fullid.ToString(); }
  236. }
  237. public string ID
  238. {
  239. //get { return m_fullid.ToString(); }
  240. //set { m_fullid = new UUID(value); }
  241. get
  242. {
  243. if (String.IsNullOrEmpty(m_id))
  244. m_id = m_fullid.ToString();
  245. return m_id;
  246. }
  247. set
  248. {
  249. UUID uuid = UUID.Zero;
  250. if (UUID.TryParse(value, out uuid))
  251. {
  252. m_fullid = uuid;
  253. m_id = m_fullid.ToString();
  254. }
  255. else
  256. m_id = value;
  257. }
  258. }
  259. public string Name
  260. {
  261. get { return m_name; }
  262. set { m_name = value; }
  263. }
  264. public string Description
  265. {
  266. get { return m_description; }
  267. set { m_description = value; }
  268. }
  269. public DateTime CreationDate
  270. {
  271. get { return m_creation_date; }
  272. set { m_creation_date = value; }
  273. }
  274. public sbyte Type
  275. {
  276. get { return m_type; }
  277. set { m_type = value; }
  278. }
  279. public string ContentType
  280. {
  281. get
  282. {
  283. if (!String.IsNullOrEmpty(m_content_type))
  284. return m_content_type;
  285. else
  286. return SLUtil.SLAssetTypeToContentType(m_type);
  287. }
  288. set
  289. {
  290. m_content_type = value;
  291. sbyte type = (sbyte)SLUtil.ContentTypeToSLAssetType(value);
  292. if (type != -1)
  293. m_type = type;
  294. }
  295. }
  296. public byte[] SHA1
  297. {
  298. get { return m_sha1; }
  299. set { m_sha1 = value; }
  300. }
  301. public bool Local
  302. {
  303. get { return m_local; }
  304. set { m_local = value; }
  305. }
  306. public bool Temporary
  307. {
  308. get { return m_temporary; }
  309. set { m_temporary = value; }
  310. }
  311. public string CreatorID
  312. {
  313. get { return m_creatorid; }
  314. set { m_creatorid = value; }
  315. }
  316. public AssetFlags Flags
  317. {
  318. get { return m_flags; }
  319. set { m_flags = value; }
  320. }
  321. }
  322. }