AssetHelpers.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 uuid and dummy text.
  39. /// </summary>
  40. /// <param name="creatorId">/param>
  41. /// <returns></returns>
  42. public static AssetBase CreateAsset(UUID creatorId)
  43. {
  44. return CreateAsset(UUID.Random(), AssetType.Notecard, "hello", creatorId);
  45. }
  46. /// <summary>
  47. /// Create and store a notecard asset with a random uuid and dummy text.
  48. /// </summary>
  49. /// <param name="creatorId">/param>
  50. /// <returns></returns>
  51. public static AssetBase CreateAsset(Scene scene, UUID creatorId)
  52. {
  53. AssetBase asset = CreateAsset(UUID.Random(), AssetType.Notecard, "hello", creatorId);
  54. scene.AssetService.Store(asset);
  55. return asset;
  56. }
  57. /// <summary>
  58. /// Create an asset from the given scene object.
  59. /// </summary>
  60. /// <param name="assetUuid"></param>
  61. /// <param name="sog"></param>
  62. /// <returns></returns>
  63. public static AssetBase CreateAsset(UUID assetUuid, SceneObjectGroup sog)
  64. {
  65. return CreateAsset(
  66. assetUuid,
  67. AssetType.Object,
  68. Encoding.ASCII.GetBytes(SceneObjectSerializer.ToOriginalXmlFormat(sog)),
  69. sog.OwnerID);
  70. }
  71. /// <summary>
  72. /// Create an asset from the given scene object.
  73. /// </summary>
  74. /// <param name="assetUuidTailZ">
  75. /// The hexadecimal last part of the UUID for the asset created. A UUID of the form "00000000-0000-0000-0000-{0:XD12}"
  76. /// will be used.
  77. /// </param>
  78. /// <param name="coa"></param>
  79. /// <returns></returns>
  80. public static AssetBase CreateAsset(int assetUuidTail, CoalescedSceneObjects coa)
  81. {
  82. return CreateAsset(new UUID(string.Format("00000000-0000-0000-0000-{0:X12}", assetUuidTail)), coa);
  83. }
  84. /// <summary>
  85. /// Create an asset from the given scene object.
  86. /// </summary>
  87. /// <param name="assetUuid"></param>
  88. /// <param name="coa"></param>
  89. /// <returns></returns>
  90. public static AssetBase CreateAsset(UUID assetUuid, CoalescedSceneObjects coa)
  91. {
  92. return CreateAsset(
  93. assetUuid,
  94. AssetType.Object,
  95. Encoding.ASCII.GetBytes(CoalescedSceneObjectsSerializer.ToXml(coa)),
  96. coa.CreatorId);
  97. }
  98. /// <summary>
  99. /// Create an asset from the given data.
  100. /// </summary>
  101. public static AssetBase CreateAsset(UUID assetUuid, AssetType assetType, string data, UUID creatorID)
  102. {
  103. return CreateAsset(assetUuid, assetType, Encoding.ASCII.GetBytes(data), creatorID);
  104. }
  105. /// <summary>
  106. /// Create an asset from the given data.
  107. /// </summary>
  108. public static AssetBase CreateAsset(UUID assetUuid, AssetType assetType, byte[] data, UUID creatorID)
  109. {
  110. AssetBase asset = new AssetBase(assetUuid, assetUuid.ToString(), (sbyte)assetType, creatorID.ToString());
  111. asset.Data = data;
  112. return asset;
  113. }
  114. public static string ReadAssetAsString(IAssetService assetService, UUID uuid)
  115. {
  116. byte[] assetData = assetService.GetData(uuid.ToString());
  117. return Encoding.ASCII.GetString(assetData);
  118. }
  119. }
  120. }