NullRegionData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 RegionData GetSpecific(string regionName, UUID scopeID)
  63. {
  64. if (m_useStaticInstance && Instance != this)
  65. return Instance.GetSpecific(regionName, scopeID);
  66. string cleanName = regionName.ToLower();
  67. Matcher queryMatch;
  68. queryMatch = delegate (string s) { return s.Equals(cleanName); };
  69. lock (m_regionData)
  70. {
  71. foreach (RegionData r in m_regionData.Values)
  72. {
  73. // m_log.DebugFormat("[NULL REGION DATA]: comparing {0} to {1}", cleanName, r.RegionName.ToLower());
  74. if (queryMatch(r.RegionName.ToLower()))
  75. return(r);
  76. }
  77. }
  78. return null;
  79. }
  80. public List<RegionData> Get(string regionName, UUID scopeID)
  81. {
  82. if (m_useStaticInstance && Instance != this)
  83. return Instance.Get(regionName, scopeID);
  84. // m_log.DebugFormat("[NULL REGION DATA]: Getting region {0}, scope {1}", regionName, scopeID);
  85. string cleanName = regionName.ToLower();
  86. // Handle SQL wildcards
  87. const string wildcard = "%";
  88. bool wildcardPrefix = false;
  89. bool wildcardSuffix = false;
  90. if (cleanName.Equals(wildcard))
  91. {
  92. wildcardPrefix = wildcardSuffix = true;
  93. cleanName = string.Empty;
  94. }
  95. else
  96. {
  97. if (cleanName.StartsWith(wildcard))
  98. {
  99. wildcardPrefix = true;
  100. cleanName = cleanName.Substring(1);
  101. }
  102. if (regionName.EndsWith(wildcard))
  103. {
  104. wildcardSuffix = true;
  105. cleanName = cleanName.Remove(cleanName.Length - 1);
  106. }
  107. }
  108. Matcher queryMatch;
  109. if (wildcardPrefix && wildcardSuffix)
  110. queryMatch = delegate(string s) { return s.Contains(cleanName); };
  111. else if (wildcardSuffix)
  112. queryMatch = delegate(string s) { return s.StartsWith(cleanName); };
  113. else if (wildcardPrefix)
  114. queryMatch = delegate(string s) { return s.EndsWith(cleanName); };
  115. else
  116. queryMatch = delegate(string s) { return s.Equals(cleanName); };
  117. // Find region data
  118. List<RegionData> ret = new List<RegionData>();
  119. lock (m_regionData)
  120. {
  121. foreach (RegionData r in m_regionData.Values)
  122. {
  123. // m_log.DebugFormat("[NULL REGION DATA]: comparing {0} to {1}", cleanName, r.RegionName.ToLower());
  124. if (queryMatch(r.RegionName.ToLower()))
  125. ret.Add(r);
  126. }
  127. }
  128. if (ret.Count > 0)
  129. return ret;
  130. return null;
  131. }
  132. public RegionData Get(int posX, int posY, UUID scopeID)
  133. {
  134. if (m_useStaticInstance && Instance != this)
  135. return Instance.Get(posX, posY, scopeID);
  136. RegionData ret = null;
  137. lock (m_regionData)
  138. {
  139. foreach (RegionData r in m_regionData.Values)
  140. {
  141. if (posX >= r.posX && posX < r.posX + r.sizeX
  142. && posY >= r.posY && posY < r.posY + r.sizeY)
  143. {
  144. ret = r;
  145. break;
  146. }
  147. }
  148. }
  149. return ret;
  150. }
  151. public RegionData Get(UUID regionID, UUID scopeID)
  152. {
  153. if (m_useStaticInstance && Instance != this)
  154. return Instance.Get(regionID, scopeID);
  155. lock (m_regionData)
  156. {
  157. if (m_regionData.ContainsKey(regionID))
  158. return m_regionData[regionID];
  159. }
  160. return null;
  161. }
  162. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  163. {
  164. if (m_useStaticInstance && Instance != this)
  165. return Instance.Get(startX, startY, endX, endY, scopeID);
  166. List<RegionData> ret = new List<RegionData>();
  167. lock (m_regionData)
  168. {
  169. foreach (RegionData r in m_regionData.Values)
  170. {
  171. if (r.posX + r.sizeX > startX && r.posX <= endX
  172. && r.posY + r.sizeX > startY && r.posY <= endY)
  173. ret.Add(r);
  174. }
  175. }
  176. return ret;
  177. }
  178. public bool Store(RegionData data)
  179. {
  180. if (m_useStaticInstance && Instance != this)
  181. return Instance.Store(data);
  182. // m_log.DebugFormat(
  183. // "[NULL REGION DATA]: Storing region {0} {1}, scope {2}", data.RegionName, data.RegionID, data.ScopeID);
  184. lock (m_regionData)
  185. {
  186. m_regionData[data.RegionID] = data;
  187. }
  188. return true;
  189. }
  190. public bool SetDataItem(UUID regionID, string item, string value)
  191. {
  192. if (m_useStaticInstance && Instance != this)
  193. return Instance.SetDataItem(regionID, item, value);
  194. lock (m_regionData)
  195. {
  196. if (!m_regionData.ContainsKey(regionID))
  197. return false;
  198. m_regionData[regionID].Data[item] = value;
  199. }
  200. return true;
  201. }
  202. public bool Delete(UUID regionID)
  203. {
  204. if (m_useStaticInstance && Instance != this)
  205. return Instance.Delete(regionID);
  206. // m_log.DebugFormat("[NULL REGION DATA]: Deleting region {0}", regionID);
  207. lock (m_regionData)
  208. {
  209. if (!m_regionData.ContainsKey(regionID))
  210. return false;
  211. m_regionData.Remove(regionID);
  212. }
  213. return true;
  214. }
  215. public List<RegionData> GetDefaultRegions(UUID scopeID)
  216. {
  217. return Get((int)RegionFlags.DefaultRegion, scopeID);
  218. }
  219. public List<RegionData> GetDefaultHypergridRegions(UUID scopeID)
  220. {
  221. return Get((int)RegionFlags.DefaultHGRegion, scopeID);
  222. }
  223. public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y)
  224. {
  225. List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID);
  226. RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y);
  227. regions.Sort(distanceComparer);
  228. return regions;
  229. }
  230. public List<RegionData> GetHyperlinks(UUID scopeID)
  231. {
  232. return Get((int)RegionFlags.Hyperlink, scopeID);
  233. }
  234. private List<RegionData> Get(int regionFlags, UUID scopeID)
  235. {
  236. if (Instance != this)
  237. return Instance.Get(regionFlags, scopeID);
  238. List<RegionData> ret = new List<RegionData>();
  239. lock (m_regionData)
  240. {
  241. foreach (RegionData r in m_regionData.Values)
  242. {
  243. if ((Convert.ToInt32(r.Data["flags"]) & regionFlags) != 0)
  244. ret.Add(r);
  245. }
  246. }
  247. return ret;
  248. }
  249. }
  250. }