ScriptUtils.cs 5.2 KB

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