NullRegionData.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /// <summary>
  41. /// Should we use the static instance for all invocations?
  42. /// </summary>
  43. private bool m_useStaticInstance = true;
  44. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. Dictionary<UUID, RegionData> m_regionData = new Dictionary<UUID, RegionData>();
  46. public NullRegionData(string connectionString, string realm)
  47. {
  48. // m_log.DebugFormat(
  49. // "[NULL REGION DATA]: Constructor got connectionString {0}, realm {1}", connectionString, realm);
  50. // The !static connection string is a hack so that regression tests can use this module without a high degree of fragility
  51. // in having to deal with the static reference in the once-loaded NullRegionData class.
  52. //
  53. // In standalone operation, we have to use only one instance of this class since the login service and
  54. // simulator have no other way of using a common data store.
  55. if (connectionString == "!static")
  56. m_useStaticInstance = false;
  57. else if (Instance == null)
  58. Instance = this;
  59. }
  60. private delegate bool Matcher(string value);
  61. public List<RegionData> Get(string regionName, UUID scopeID)
  62. {
  63. if (m_useStaticInstance && Instance != this)
  64. return Instance.Get(regionName, scopeID);
  65. // m_log.DebugFormat("[NULL REGION DATA]: Getting region {0}, scope {1}", regionName, scopeID);
  66. string cleanName = regionName.ToLower();
  67. // Handle SQL wildcards
  68. const string wildcard = "%";
  69. bool wildcardPrefix = false;
  70. bool wildcardSuffix = false;
  71. if (cleanName.Equals(wildcard))
  72. {
  73. wildcardPrefix = wildcardSuffix = true;
  74. cleanName = string.Empty;
  75. }
  76. else
  77. {
  78. if (cleanName.StartsWith(wildcard))
  79. {
  80. wildcardPrefix = true;
  81. cleanName = cleanName.Substring(1);
  82. }
  83. if (regionName.EndsWith(wildcard))
  84. {
  85. wildcardSuffix = true;
  86. cleanName = cleanName.Remove(cleanName.Length - 1);
  87. }
  88. }
  89. Matcher queryMatch;
  90. if (wildcardPrefix && wildcardSuffix)
  91. queryMatch = delegate(string s) { return s.Contains(cleanName); };
  92. else if (wildcardSuffix)
  93. queryMatch = delegate(string s) { return s.StartsWith(cleanName); };
  94. else if (wildcardPrefix)
  95. queryMatch = delegate(string s) { return s.EndsWith(cleanName); };
  96. else
  97. queryMatch = delegate(string s) { return s.Equals(cleanName); };
  98. // Find region data
  99. List<RegionData> ret = new List<RegionData>();
  100. foreach (RegionData r in m_regionData.Values)
  101. {
  102. // m_log.DebugFormat("[NULL REGION DATA]: comparing {0} to {1}", cleanName, r.RegionName.ToLower());
  103. if (queryMatch(r.RegionName.ToLower()))
  104. ret.Add(r);
  105. }
  106. if (ret.Count > 0)
  107. return ret;
  108. return null;
  109. }
  110. public RegionData Get(int posX, int posY, UUID scopeID)
  111. {
  112. if (m_useStaticInstance && Instance != this)
  113. return Instance.Get(posX, posY, scopeID);
  114. List<RegionData> ret = new List<RegionData>();
  115. foreach (RegionData r in m_regionData.Values)
  116. {
  117. if (r.posX == posX && r.posY == posY)
  118. ret.Add(r);
  119. }
  120. if (ret.Count > 0)
  121. return ret[0];
  122. return null;
  123. }
  124. public RegionData Get(UUID regionID, UUID scopeID)
  125. {
  126. if (m_useStaticInstance && Instance != this)
  127. return Instance.Get(regionID, scopeID);
  128. if (m_regionData.ContainsKey(regionID))
  129. return m_regionData[regionID];
  130. return null;
  131. }
  132. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  133. {
  134. if (m_useStaticInstance && Instance != this)
  135. return Instance.Get(startX, startY, endX, endY, scopeID);
  136. List<RegionData> ret = new List<RegionData>();
  137. foreach (RegionData r in m_regionData.Values)
  138. {
  139. if (r.posX >= startX && r.posX <= endX && r.posY >= startY && r.posY <= endY)
  140. ret.Add(r);
  141. }
  142. return ret;
  143. }
  144. public bool Store(RegionData data)
  145. {
  146. if (m_useStaticInstance && Instance != this)
  147. return Instance.Store(data);
  148. // m_log.DebugFormat(
  149. // "[NULL REGION DATA]: Storing region {0} {1}, scope {2}", data.RegionName, data.RegionID, data.ScopeID);
  150. m_regionData[data.RegionID] = data;
  151. return true;
  152. }
  153. public bool SetDataItem(UUID regionID, string item, string value)
  154. {
  155. if (m_useStaticInstance && Instance != this)
  156. return Instance.SetDataItem(regionID, item, value);
  157. if (!m_regionData.ContainsKey(regionID))
  158. return false;
  159. m_regionData[regionID].Data[item] = value;
  160. return true;
  161. }
  162. public bool Delete(UUID regionID)
  163. {
  164. if (m_useStaticInstance && Instance != this)
  165. return Instance.Delete(regionID);
  166. // m_log.DebugFormat("[NULL REGION DATA]: Deleting region {0}", regionID);
  167. if (!m_regionData.ContainsKey(regionID))
  168. return false;
  169. m_regionData.Remove(regionID);
  170. return true;
  171. }
  172. public List<RegionData> GetDefaultRegions(UUID scopeID)
  173. {
  174. return Get((int)RegionFlags.DefaultRegion, scopeID);
  175. }
  176. public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y)
  177. {
  178. List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID);
  179. RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y);
  180. regions.Sort(distanceComparer);
  181. return regions;
  182. }
  183. public List<RegionData> GetHyperlinks(UUID scopeID)
  184. {
  185. return Get((int)RegionFlags.Hyperlink, scopeID);
  186. }
  187. private List<RegionData> Get(int regionFlags, UUID scopeID)
  188. {
  189. if (Instance != this)
  190. return Instance.Get(regionFlags, scopeID);
  191. List<RegionData> ret = new List<RegionData>();
  192. foreach (RegionData r in m_regionData.Values)
  193. {
  194. if ((Convert.ToInt32(r.Data["flags"]) & regionFlags) != 0)
  195. ret.Add(r);
  196. }
  197. return ret;
  198. }
  199. }
  200. }