NullRegionData.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. foreach (RegionData r in m_regionData.Values)
  102. {
  103. // m_log.DebugFormat("[NULL REGION DATA]: comparing {0} to {1}", cleanName, r.RegionName.ToLower());
  104. if (queryMatch(r.RegionName.ToLower()))
  105. ret.Add(r);
  106. }
  107. if (ret.Count > 0)
  108. return ret;
  109. return null;
  110. }
  111. public RegionData Get(int posX, int posY, UUID scopeID)
  112. {
  113. if (m_useStaticInstance && Instance != this)
  114. return Instance.Get(posX, posY, scopeID);
  115. List<RegionData> ret = new List<RegionData>();
  116. foreach (RegionData r in m_regionData.Values)
  117. {
  118. if (r.posX == posX && r.posY == posY)
  119. ret.Add(r);
  120. }
  121. if (ret.Count > 0)
  122. return ret[0];
  123. return null;
  124. }
  125. public RegionData Get(UUID regionID, UUID scopeID)
  126. {
  127. if (m_useStaticInstance && Instance != this)
  128. return Instance.Get(regionID, scopeID);
  129. if (m_regionData.ContainsKey(regionID))
  130. return m_regionData[regionID];
  131. return null;
  132. }
  133. public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
  134. {
  135. if (m_useStaticInstance && Instance != this)
  136. return Instance.Get(startX, startY, endX, endY, scopeID);
  137. List<RegionData> ret = new List<RegionData>();
  138. foreach (RegionData r in m_regionData.Values)
  139. {
  140. if (r.posX >= startX && r.posX <= endX && r.posY >= startY && r.posY <= endY)
  141. ret.Add(r);
  142. }
  143. return ret;
  144. }
  145. public bool Store(RegionData data)
  146. {
  147. if (m_useStaticInstance && Instance != this)
  148. return Instance.Store(data);
  149. // m_log.DebugFormat(
  150. // "[NULL REGION DATA]: Storing region {0} {1}, scope {2}", data.RegionName, data.RegionID, data.ScopeID);
  151. m_regionData[data.RegionID] = data;
  152. return true;
  153. }
  154. public bool SetDataItem(UUID regionID, string item, string value)
  155. {
  156. if (m_useStaticInstance && Instance != this)
  157. return Instance.SetDataItem(regionID, item, value);
  158. if (!m_regionData.ContainsKey(regionID))
  159. return false;
  160. m_regionData[regionID].Data[item] = value;
  161. return true;
  162. }
  163. public bool Delete(UUID regionID)
  164. {
  165. if (m_useStaticInstance && Instance != this)
  166. return Instance.Delete(regionID);
  167. // m_log.DebugFormat("[NULL REGION DATA]: Deleting region {0}", regionID);
  168. if (!m_regionData.ContainsKey(regionID))
  169. return false;
  170. m_regionData.Remove(regionID);
  171. return true;
  172. }
  173. public List<RegionData> GetDefaultRegions(UUID scopeID)
  174. {
  175. return Get((int)RegionFlags.DefaultRegion, scopeID);
  176. }
  177. public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y)
  178. {
  179. List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID);
  180. RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y);
  181. regions.Sort(distanceComparer);
  182. return regions;
  183. }
  184. public List<RegionData> GetHyperlinks(UUID scopeID)
  185. {
  186. return Get((int)RegionFlags.Hyperlink, scopeID);
  187. }
  188. private List<RegionData> Get(int regionFlags, UUID scopeID)
  189. {
  190. if (Instance != this)
  191. return Instance.Get(regionFlags, scopeID);
  192. List<RegionData> ret = new List<RegionData>();
  193. foreach (RegionData r in m_regionData.Values)
  194. {
  195. if ((Convert.ToInt32(r.Data["flags"]) & regionFlags) != 0)
  196. ret.Add(r);
  197. }
  198. return ret;
  199. }
  200. }
  201. }