ObjectAccessor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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;
  29. using System.Collections.Generic;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Framework.Scenes;
  33. using IEnumerable=System.Collections.IEnumerable;
  34. namespace OpenSim.Region.OptionalModules.Scripting.Minimodule
  35. {
  36. internal class IObjEnum : System.MarshalByRefObject, IEnumerator<IObject>
  37. {
  38. private readonly Scene m_scene;
  39. private readonly IEnumerator<EntityBase> m_sogEnum;
  40. private readonly ISecurityCredential m_security;
  41. private readonly List<EntityBase> m_entities;
  42. public IObjEnum(Scene scene, ISecurityCredential security)
  43. {
  44. m_scene = scene;
  45. m_security = security;
  46. m_entities = new List<EntityBase>(m_scene.Entities.GetEntities());
  47. m_sogEnum = m_entities.GetEnumerator();
  48. }
  49. public void Dispose()
  50. {
  51. m_sogEnum.Dispose();
  52. }
  53. public bool MoveNext()
  54. {
  55. return m_sogEnum.MoveNext();
  56. }
  57. public void Reset()
  58. {
  59. m_sogEnum.Reset();
  60. }
  61. public IObject Current
  62. {
  63. get
  64. {
  65. return new SOPObject(m_scene, m_sogEnum.Current.LocalId, m_security);
  66. }
  67. }
  68. object IEnumerator.Current
  69. {
  70. get { return Current; }
  71. }
  72. }
  73. public class ObjectAccessor : System.MarshalByRefObject, IObjectAccessor
  74. {
  75. private readonly Scene m_scene;
  76. private readonly ISecurityCredential m_security;
  77. public ObjectAccessor(Scene scene, ISecurityCredential security)
  78. {
  79. m_scene = scene;
  80. m_security = security;
  81. }
  82. public IObject this[int index]
  83. {
  84. get
  85. {
  86. return new SOPObject(m_scene, m_scene.Entities[(uint)index].LocalId, m_security);
  87. }
  88. }
  89. public IObject this[uint index]
  90. {
  91. get
  92. {
  93. return new SOPObject(m_scene, m_scene.Entities[index].LocalId, m_security);
  94. }
  95. }
  96. public IObject this[UUID index]
  97. {
  98. get
  99. {
  100. return new SOPObject(m_scene, m_scene.Entities[index].LocalId, m_security);
  101. }
  102. }
  103. public IObject Create(Vector3 position)
  104. {
  105. return Create(position, Quaternion.Identity);
  106. }
  107. public IObject Create(Vector3 position, Quaternion rotation)
  108. {
  109. SceneObjectGroup sog = m_scene.AddNewPrim(m_security.owner.GlobalID,
  110. UUID.Zero,
  111. position,
  112. rotation,
  113. PrimitiveBaseShape.CreateBox());
  114. IObject ret = new SOPObject(m_scene, sog.LocalId, m_security);
  115. return ret;
  116. }
  117. public IEnumerator<IObject> GetEnumerator()
  118. {
  119. return new IObjEnum(m_scene, m_security);
  120. }
  121. IEnumerator IEnumerable.GetEnumerator()
  122. {
  123. return GetEnumerator();
  124. }
  125. public void Add(IObject item)
  126. {
  127. throw new NotSupportedException("Collection is read-only. This is an API TODO FIX, creation of objects is presently impossible.");
  128. }
  129. public void Clear()
  130. {
  131. throw new NotSupportedException("Collection is read-only. TODO FIX.");
  132. }
  133. public bool Contains(IObject item)
  134. {
  135. return m_scene.Entities.ContainsKey(item.LocalID);
  136. }
  137. public void CopyTo(IObject[] array, int arrayIndex)
  138. {
  139. for (int i = arrayIndex; i < Count + arrayIndex; i++)
  140. {
  141. array[i] = this[i - arrayIndex];
  142. }
  143. }
  144. public bool Remove(IObject item)
  145. {
  146. throw new NotSupportedException("Collection is read-only. TODO FIX.");
  147. }
  148. public int Count
  149. {
  150. get { return m_scene.Entities.Count; }
  151. }
  152. public bool IsReadOnly
  153. {
  154. get { return true; }
  155. }
  156. }
  157. }