1
0

AssetHelpers.cs 6.1 KB

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