NullRegionData.cs 9.4 KB

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