NullRegionData.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Data;
  33. using System.Reflection;
  34. using log4net;
  35. namespace OpenSim.Data.Null
  36. {
  37. public class NullRegionData : IRegionData
  38. {
  39. private static NullRegionData Instance = null;
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. Dictionary<UUID, RegionData> m_regionData = new Dictionary<UUID, RegionData>();
  42. public NullRegionData(string connectionString, string realm)
  43. {
  44. if (Instance == null)
  45. Instance = this;
  46. //Console.WriteLine("[XXX] NullRegionData constructor");
  47. }
  48. private delegate bool Matcher(string value);
  49. public List<RegionData> Get(string regionName, UUID scopeID)
  50. {
  51. if (Instance != this)
  52. return Instance.Get(regionName, scopeID);
  53. string cleanName = regionName.ToLower();
  54. // Handle SQL wildcards
  55. const string wildcard = "%";
  56. bool wildcardPrefix = false;
  57. bool wildcardSuffix = false;
  58. if (cleanName.Equals(wildcard))
  59. {
  60. wildcardPrefix = wildcardSuffix = true;
  61. cleanName = string.Empty;
  62. }
  63. else
  64. {
  65. if (cleanName.StartsWith(wildcard))
  66. {
  67. wildcardPrefix = true;
  68. cleanName = cleanName.Substring(1);
  69. }
  70. if (regionName.EndsWith(wildcard))
  71. {
  72. wildcardSuffix = true;
  73. cleanName = cleanName.Remove(cleanName.Length - 1);
  74. }
  75. }
  76. Matcher queryMatch;
  77. if (wildcardPrefix && wildcardSuffix)
  78. queryMatch = delegate(string s) { return s.Contains(cleanName); };
  79. else if (wildcardSuffix)
  80. queryMatch = delegate(string s) { return s.StartsWith(cleanName); };
  81. else if (wildcardPrefix)
  82. queryMatch = delegate(string s) { return s.EndsWith(cleanName); };
  83. else
  84. queryMatch = delegate(string s) { return s.Equals(cleanName); };
  85. // Find region data
  86. List<RegionData> ret = new List<RegionData>();
  87. foreach (RegionData r in m_regionData.Values)
  88. {
  89. // m_log.DebugFormat("[NULL REGION DATA]: comparing {0} to {1}", cleanName, r.RegionName.ToLower());
  90. if (queryMatch(r.RegionName.ToLower()))
  91. ret.Add(r);
  92. }
  93. if (ret.Count > 0)
  94. return ret;
  95. return null;
  96. }
  97. public RegionData Get(int posX, int posY, UUID scopeID)
  98. {
  99. if (Instance != this)
  100. return Instance.Get(posX, posY, scopeID);
  101. List<RegionData> ret = new List<RegionData>();
  102. foreach (RegionData r in m_regionData.Values)
  103. {
  104. if (r.posX == posX && r.posY == posY)
  105. ret.Add(r);
  106. }
  107. if (ret.Count > 0)
  108. return ret[0];
  109. return null;
  110. }
  111. public RegionData Get(UUID regionID, UUID scopeID)
  112. {
  113. if (Instance != this)
  114. return Instance.Get(regionID, scopeID);
  115. if (m_regionData.ContainsKey(regionID))
  116. return m_regionData[regionID];
  117. return null;
  118. }
  119. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  120. {
  121. if (Instance != this)
  122. return Instance.Get(startX, startY, endX, endY, scopeID);
  123. List<RegionData> ret = new List<RegionData>();
  124. foreach (RegionData r in m_regionData.Values)
  125. {
  126. if (r.posX >= startX && r.posX <= endX && r.posY >= startY && r.posY <= endY)
  127. ret.Add(r);
  128. }
  129. return ret;
  130. }
  131. public bool Store(RegionData data)
  132. {
  133. if (Instance != this)
  134. return Instance.Store(data);
  135. m_regionData[data.RegionID] = data;
  136. return true;
  137. }
  138. public bool SetDataItem(UUID regionID, string item, string value)
  139. {
  140. if (Instance != this)
  141. return Instance.SetDataItem(regionID, item, value);
  142. if (!m_regionData.ContainsKey(regionID))
  143. return false;
  144. m_regionData[regionID].Data[item] = value;
  145. return true;
  146. }
  147. public bool Delete(UUID regionID)
  148. {
  149. if (Instance != this)
  150. return Instance.Delete(regionID);
  151. if (!m_regionData.ContainsKey(regionID))
  152. return false;
  153. m_regionData.Remove(regionID);
  154. return true;
  155. }
  156. public List<RegionData> GetDefaultRegions(UUID scopeID)
  157. {
  158. return Get((int)RegionFlags.DefaultRegion, scopeID);
  159. }
  160. public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y)
  161. {
  162. List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID);
  163. RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y);
  164. regions.Sort(distanceComparer);
  165. return regions;
  166. }
  167. public List<RegionData> GetHyperlinks(UUID scopeID)
  168. {
  169. return Get((int)RegionFlags.Hyperlink, scopeID);
  170. }
  171. private List<RegionData> Get(int regionFlags, UUID scopeID)
  172. {
  173. if (Instance != this)
  174. return Instance.Get(regionFlags, scopeID);
  175. List<RegionData> ret = new List<RegionData>();
  176. foreach (RegionData r in m_regionData.Values)
  177. {
  178. if ((Convert.ToInt32(r.Data["flags"]) & regionFlags) != 0)
  179. ret.Add(r);
  180. }
  181. return ret;
  182. }
  183. }
  184. }