NullRegionData.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. RegionData ret = null;
  119. lock (m_regionData)
  120. {
  121. foreach (RegionData r in m_regionData.Values)
  122. {
  123. if (posX >= r.posX && posX < r.posX + r.sizeX
  124. && posY >= r.posY && posY < r.posY + r.sizeY)
  125. {
  126. ret = r;
  127. break;
  128. }
  129. }
  130. }
  131. return ret;
  132. }
  133. public RegionData Get(UUID regionID, UUID scopeID)
  134. {
  135. if (m_useStaticInstance && Instance != this)
  136. return Instance.Get(regionID, scopeID);
  137. lock (m_regionData)
  138. {
  139. if (m_regionData.ContainsKey(regionID))
  140. return m_regionData[regionID];
  141. }
  142. return null;
  143. }
  144. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  145. {
  146. if (m_useStaticInstance && Instance != this)
  147. return Instance.Get(startX, startY, endX, endY, scopeID);
  148. List<RegionData> ret = new List<RegionData>();
  149. lock (m_regionData)
  150. {
  151. foreach (RegionData r in m_regionData.Values)
  152. {
  153. if (r.posX + r.sizeX > startX && r.posX <= endX
  154. && r.posY + r.sizeX > startY && r.posY <= endY)
  155. ret.Add(r);
  156. }
  157. }
  158. return ret;
  159. }
  160. public bool Store(RegionData data)
  161. {
  162. if (m_useStaticInstance && Instance != this)
  163. return Instance.Store(data);
  164. // m_log.DebugFormat(
  165. // "[NULL REGION DATA]: Storing region {0} {1}, scope {2}", data.RegionName, data.RegionID, data.ScopeID);
  166. lock (m_regionData)
  167. {
  168. m_regionData[data.RegionID] = data;
  169. }
  170. return true;
  171. }
  172. public bool SetDataItem(UUID regionID, string item, string value)
  173. {
  174. if (m_useStaticInstance && Instance != this)
  175. return Instance.SetDataItem(regionID, item, value);
  176. lock (m_regionData)
  177. {
  178. if (!m_regionData.ContainsKey(regionID))
  179. return false;
  180. m_regionData[regionID].Data[item] = value;
  181. }
  182. return true;
  183. }
  184. public bool Delete(UUID regionID)
  185. {
  186. if (m_useStaticInstance && Instance != this)
  187. return Instance.Delete(regionID);
  188. // m_log.DebugFormat("[NULL REGION DATA]: Deleting region {0}", regionID);
  189. lock (m_regionData)
  190. {
  191. if (!m_regionData.ContainsKey(regionID))
  192. return false;
  193. m_regionData.Remove(regionID);
  194. }
  195. return true;
  196. }
  197. public List<RegionData> GetDefaultRegions(UUID scopeID)
  198. {
  199. return Get((int)RegionFlags.DefaultRegion, scopeID);
  200. }
  201. public List<RegionData> GetDefaultHypergridRegions(UUID scopeID)
  202. {
  203. return Get((int)RegionFlags.DefaultHGRegion, scopeID);
  204. }
  205. public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y)
  206. {
  207. List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID);
  208. RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y);
  209. regions.Sort(distanceComparer);
  210. return regions;
  211. }
  212. public List<RegionData> GetHyperlinks(UUID scopeID)
  213. {
  214. return Get((int)RegionFlags.Hyperlink, scopeID);
  215. }
  216. private List<RegionData> Get(int regionFlags, UUID scopeID)
  217. {
  218. if (Instance != this)
  219. return Instance.Get(regionFlags, scopeID);
  220. List<RegionData> ret = new List<RegionData>();
  221. lock (m_regionData)
  222. {
  223. foreach (RegionData r in m_regionData.Values)
  224. {
  225. if ((Convert.ToInt32(r.Data["flags"]) & regionFlags) != 0)
  226. ret.Add(r);
  227. }
  228. }
  229. return ret;
  230. }
  231. }
  232. }