ScriptUtils.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 OpenMetaverse;
  28. using OpenSim.Framework;
  29. namespace OpenSim.Region.Framework.Scenes.Scripting
  30. {
  31. /// <summary>
  32. /// Utility functions for use by scripts manipulating the scene.
  33. /// </summary>
  34. public static class ScriptUtils
  35. {
  36. /// <summary>
  37. /// Get an asset id given an item name and an item type.
  38. /// </summary>
  39. /// <returns>UUID.Zero if the name and type did not match any item.</returns>
  40. /// <param name='part'></param>
  41. /// <param name='name'></param>
  42. /// <param name='type'></param>
  43. public static UUID GetAssetIdFromItemName(SceneObjectPart part, string name, int type)
  44. {
  45. TaskInventoryItem item = part.Inventory.GetInventoryItem(name, type);
  46. if (item is not null)
  47. return item.AssetID;
  48. return UUID.Zero;
  49. }
  50. /// <summary>
  51. /// accepts a valid UUID, -or- a name of an inventory item.
  52. /// Returns a valid UUID or UUID.Zero if key invalid and item not found
  53. /// in prim inventory.
  54. /// </summary>
  55. /// <param name="part">Scene object part to search for inventory item</param>
  56. /// <param name="key"></param>
  57. /// <returns></returns>
  58. public static UUID GetAssetIdFromKeyOrItemName(SceneObjectPart part, string identifier)
  59. {
  60. if(string.IsNullOrEmpty(identifier) || part.Inventory is null)
  61. return UUID.Zero;
  62. // if we can parse the string as a key, use it.
  63. // else try to locate the name in inventory of object. found returns key,
  64. // not found returns UUID.Zero
  65. if (UUID.TryParse(identifier, out UUID key))
  66. return key;
  67. TaskInventoryItem item = part.Inventory.GetInventoryItem(identifier);
  68. return item is not null ? item.AssetID : UUID.Zero;
  69. }
  70. /// <summary>
  71. /// Return the UUID of the asset matching the specified key or name
  72. /// and asset type.
  73. /// </summary>
  74. /// <param name="part">Scene object part to search for inventory item</param>
  75. /// <param name="identifier"></param>
  76. /// <param name="type"></param>
  77. /// <returns></returns>
  78. public static UUID GetAssetIdFromKeyOrItemName(SceneObjectPart part, string identifier, AssetType type)
  79. {
  80. if (UUID.TryParse(identifier, out UUID key) || part.Inventory is null)
  81. return key;
  82. TaskInventoryItem item = part.Inventory.GetInventoryItem(identifier, (int)type);
  83. return item is not null ? item.AssetID : UUID.Zero;
  84. }
  85. public static UUID GetAssetIdFromKeyOrItemName(SceneObjectPart part, SceneObjectPart host, string identifier, AssetType type)
  86. {
  87. if (UUID.TryParse(identifier, out UUID key))
  88. return key;
  89. TaskInventoryItem item;
  90. if (part.Inventory is not null)
  91. {
  92. item = part.Inventory.GetInventoryItem(identifier, (int)type);
  93. if (item is not null)
  94. return item.AssetID;
  95. }
  96. if (part.LocalId != host.LocalId && host.Inventory is not null)
  97. {
  98. item = host.Inventory.GetInventoryItem(identifier, (int)type);
  99. if (item is not null)
  100. return item.AssetID;
  101. }
  102. return UUID.Zero;
  103. }
  104. }
  105. }