AssetBase.cs 11 KB

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