NullRegionData.cs 10 KB

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