NullRegionData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. return m_regionData.TryGetValue(regionID, out RegionData rd) ? rd : null;
  154. }
  155. }
  156. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  157. {
  158. if (m_useStaticInstance && Instance != this)
  159. return Instance.Get(startX, startY, endX, endY, scopeID);
  160. List<RegionData> ret = new List<RegionData>();
  161. lock (m_regionData)
  162. {
  163. foreach (RegionData r in m_regionData.Values)
  164. {
  165. if (r.posX + r.sizeX > startX && r.posX <= endX
  166. && r.posY + r.sizeX > startY && r.posY <= endY)
  167. ret.Add(r);
  168. }
  169. }
  170. return ret;
  171. }
  172. public bool Store(RegionData data)
  173. {
  174. if (m_useStaticInstance && Instance != this)
  175. return Instance.Store(data);
  176. // m_log.DebugFormat(
  177. // "[NULL REGION DATA]: Storing region {0} {1}, scope {2}", data.RegionName, data.RegionID, data.ScopeID);
  178. lock (m_regionData)
  179. {
  180. m_regionData[data.RegionID] = data;
  181. }
  182. return true;
  183. }
  184. public bool SetDataItem(UUID regionID, string item, string value)
  185. {
  186. if (m_useStaticInstance && Instance != this)
  187. return Instance.SetDataItem(regionID, item, value);
  188. lock (m_regionData)
  189. {
  190. if(m_regionData.TryGetValue(regionID, out RegionData rd))
  191. {
  192. rd.Data[item] = value;
  193. return true;
  194. }
  195. return false;
  196. }
  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. return m_regionData.Remove(regionID);
  206. }
  207. }
  208. public List<RegionData> GetDefaultRegions(UUID scopeID)
  209. {
  210. return Get((int)RegionFlags.DefaultRegion, scopeID);
  211. }
  212. public List<RegionData> GetDefaultHypergridRegions(UUID scopeID)
  213. {
  214. return Get((int)RegionFlags.DefaultHGRegion, scopeID);
  215. }
  216. public List<RegionData> GetFallbackRegions(UUID scopeID)
  217. {
  218. return Get((int)RegionFlags.FallbackRegion, scopeID);
  219. }
  220. public List<RegionData> GetHyperlinks(UUID scopeID)
  221. {
  222. return Get((int)RegionFlags.Hyperlink, scopeID);
  223. }
  224. public List<RegionData> GetOnlineRegions(UUID scopeID)
  225. {
  226. return Get((int)RegionFlags.RegionOnline, scopeID);
  227. }
  228. private List<RegionData> Get(int regionFlags, UUID scopeID)
  229. {
  230. if (Instance != this)
  231. return Instance.Get(regionFlags, scopeID);
  232. List<RegionData> ret = new List<RegionData>();
  233. lock (m_regionData)
  234. {
  235. foreach (RegionData r in m_regionData.Values)
  236. {
  237. if ((Convert.ToInt32(r.Data["flags"]) & regionFlags) != 0)
  238. ret.Add(r);
  239. }
  240. }
  241. return ret;
  242. }
  243. }
  244. }