1
0

ScriptUtils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. UUID key;
  64. // if we can parse the string as a key, use it.
  65. // else try to locate the name in inventory of object. found returns key,
  66. // not found returns UUID.Zero
  67. if (!UUID.TryParse(identifier, out key))
  68. {
  69. TaskInventoryItem item = part.Inventory.GetInventoryItem(identifier);
  70. if (item != null)
  71. key = item.AssetID;
  72. else
  73. key = UUID.Zero;
  74. }
  75. return key;
  76. }
  77. /// <summary>
  78. /// Return the UUID of the asset matching the specified key or name
  79. /// and asset type.
  80. /// </summary>
  81. /// <param name="part">Scene object part to search for inventory item</param>
  82. /// <param name="identifier"></param>
  83. /// <param name="type"></param>
  84. /// <returns></returns>
  85. public static UUID GetAssetIdFromKeyOrItemName(SceneObjectPart part, string identifier, AssetType type)
  86. {
  87. UUID key;
  88. if (!UUID.TryParse(identifier, out key))
  89. {
  90. TaskInventoryItem item = part.Inventory.GetInventoryItem(identifier);
  91. if (item != null && item.Type == (int)type)
  92. key = item.AssetID;
  93. }
  94. return key;
  95. }
  96. }
  97. }