1
0

AssetHelpers.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Text;
  28. using OpenMetaverse;
  29. using OpenMetaverse.Assets;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Scenes;
  32. using OpenSim.Region.Framework.Scenes.Serialization;
  33. using OpenSim.Services.Interfaces;
  34. namespace OpenSim.Tests.Common
  35. {
  36. public class AssetHelpers
  37. {
  38. /// <summary>
  39. /// Create a notecard asset with a random uuids and dummy text.
  40. /// </summary>
  41. /// <returns></returns>
  42. public static AssetBase CreateNotecardAsset()
  43. {
  44. return CreateNotecardAsset(UUID.Random());
  45. }
  46. /// <summary>
  47. /// Create a notecard asset with dummy text and a random owner.
  48. /// </summary>
  49. /// <param name="assetId">/param>
  50. /// <returns></returns>
  51. public static AssetBase CreateNotecardAsset(UUID assetId)
  52. {
  53. return CreateNotecardAsset(assetId, "hello");
  54. }
  55. /// <summary>
  56. /// Create a notecard asset with a random owner.
  57. /// </summary>
  58. /// <param name="assetId">/param>
  59. /// <param name="text"></param>
  60. /// <returns></returns>
  61. public static AssetBase CreateNotecardAsset(UUID assetId, string text)
  62. {
  63. return CreateAsset(assetId, AssetType.Notecard, text, UUID.Random());
  64. }
  65. // /// <summary>
  66. // /// Create and store a notecard asset with a random uuid and dummy text.
  67. // /// </summary>
  68. // /// <param name="creatorId">/param>
  69. // /// <returns></returns>
  70. // public static AssetBase CreateNotecardAsset(Scene scene, UUID creatorId)
  71. // {
  72. // AssetBase asset = CreateAsset(UUID.Random(), AssetType.Notecard, "hello", creatorId);
  73. // scene.AssetService.Store(asset);
  74. // return asset;
  75. // }
  76. /// <summary>
  77. /// Create an asset from the given object.
  78. /// </summary>
  79. /// <param name="assetUuidTail">
  80. /// The hexadecimal last part of the UUID for the asset created. A UUID of the form "00000000-0000-0000-0000-{0:XD12}"
  81. /// will be used.
  82. /// </param>
  83. /// <param name="sog"></param>
  84. /// <returns></returns>
  85. public static AssetBase CreateAsset(int assetUuidTail, SceneObjectGroup sog)
  86. {
  87. return CreateAsset(new UUID(string.Format("00000000-0000-0000-0000-{0:X12}", assetUuidTail)), sog);
  88. }
  89. /// <summary>
  90. /// Create an asset from the given object.
  91. /// </summary>
  92. /// <param name="assetUuid"></param>
  93. /// <param name="sog"></param>
  94. /// <returns></returns>
  95. public static AssetBase CreateAsset(UUID assetUuid, SceneObjectGroup sog)
  96. {
  97. return CreateAsset(
  98. assetUuid,
  99. AssetType.Object,
  100. Encoding.ASCII.GetBytes(SceneObjectSerializer.ToOriginalXmlFormat(sog)),
  101. sog.OwnerID);
  102. }
  103. /// <summary>
  104. /// Create an asset from the given scene object.
  105. /// </summary>
  106. /// <param name="assetUuidTail">
  107. /// The hexadecimal last part of the UUID for the asset created. A UUID of the form "00000000-0000-0000-0000-{0:XD12}"
  108. /// will be used.
  109. /// </param>
  110. /// <param name="coa"></param>
  111. /// <returns></returns>
  112. public static AssetBase CreateAsset(int assetUuidTail, CoalescedSceneObjects coa)
  113. {
  114. return CreateAsset(new UUID(string.Format("00000000-0000-0000-0000-{0:X12}", assetUuidTail)), coa);
  115. }
  116. /// <summary>
  117. /// Create an asset from the given scene object.
  118. /// </summary>
  119. /// <param name="assetUuid"></param>
  120. /// <param name="coa"></param>
  121. /// <returns></returns>
  122. public static AssetBase CreateAsset(UUID assetUuid, CoalescedSceneObjects coa)
  123. {
  124. return CreateAsset(
  125. assetUuid,
  126. AssetType.Object,
  127. Encoding.ASCII.GetBytes(CoalescedSceneObjectsSerializer.ToXml(coa)),
  128. coa.CreatorId);
  129. }
  130. /// <summary>
  131. /// Create an asset from the given data.
  132. /// </summary>
  133. public static AssetBase CreateAsset(UUID assetUuid, AssetType assetType, string text, UUID creatorID)
  134. {
  135. AssetNotecard anc = new AssetNotecard();
  136. anc.BodyText = text;
  137. anc.Encode();
  138. return CreateAsset(assetUuid, assetType, anc.AssetData, creatorID);
  139. }
  140. /// <summary>
  141. /// Create an asset from the given data.
  142. /// </summary>
  143. public static AssetBase CreateAsset(UUID assetUuid, AssetType assetType, byte[] data, UUID creatorID)
  144. {
  145. AssetBase asset = new AssetBase(assetUuid, assetUuid.ToString(), (sbyte)assetType, creatorID.ToString());
  146. asset.Data = data;
  147. return asset;
  148. }
  149. public static string ReadAssetAsString(IAssetService assetService, UUID uuid)
  150. {
  151. byte[] assetData = assetService.GetData(uuid.ToString());
  152. return Encoding.ASCII.GetString(assetData);
  153. }
  154. }
  155. }