AssetBase.cs 11 KB

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