AssetBase.cs 10 KB

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