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