Db4LocalStorage.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using Db4objects.Db4o;
  30. using Db4objects.Db4o.Query;
  31. using libsecondlife;
  32. using OpenSim.Framework.Interfaces;
  33. using OpenSim.Framework.Assets;
  34. using OpenSim.Framework.Terrain;
  35. namespace OpenSim.Storage.LocalStorageDb4o
  36. {
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. public class Db4LocalStorage : ILocalStorage
  41. {
  42. private IObjectContainer db;
  43. public Db4LocalStorage()
  44. {
  45. try
  46. {
  47. db = Db4oFactory.OpenFile("localworld.yap");
  48. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4LocalStorage creation");
  49. }
  50. catch(Exception e)
  51. {
  52. db.Close();
  53. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4LocalStorage :Constructor - Exception occured");
  54. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(e.ToString());
  55. }
  56. }
  57. public void StorePrim(PrimData prim)
  58. {
  59. IObjectSet result = db.Query(new UUIDQuery(prim.FullID));
  60. if(result.Count>0)
  61. {
  62. //prim already in storage
  63. //so update it
  64. PrimData found = (PrimData) result.Next();
  65. found.PathBegin = prim.PathBegin;
  66. found.PathCurve= prim.PathCurve;
  67. found.PathEnd = prim.PathEnd;
  68. found.PathRadiusOffset = prim.PathRadiusOffset;
  69. found.PathRevolutions = prim.PathRevolutions;
  70. found.PathScaleX= prim.PathScaleX;
  71. found.PathScaleY = prim.PathScaleY;
  72. found.PathShearX = prim.PathShearX;
  73. found.PathShearY = prim.PathShearY;
  74. found.PathSkew = prim.PathSkew;
  75. found.PathTaperX = prim.PathTaperX;
  76. found.PathTaperY = prim.PathTaperY;
  77. found.PathTwist = prim.PathTwist;
  78. found.PathTwistBegin = prim.PathTwistBegin;
  79. found.PCode = prim.PCode;
  80. found.ProfileBegin = prim.ProfileBegin;
  81. found.ProfileCurve = prim.ProfileCurve;
  82. found.ProfileEnd = prim.ProfileEnd;
  83. found.ProfileHollow = prim.ProfileHollow;
  84. found.Position = prim.Position;
  85. found.Rotation = prim.Rotation;
  86. found.Texture = prim.Texture;
  87. db.Set(found);
  88. db.Commit();
  89. }
  90. else
  91. {
  92. //not in storage
  93. db.Set(prim);
  94. db.Commit();
  95. }
  96. }
  97. public void RemovePrim(LLUUID primID)
  98. {
  99. IObjectSet result = db.Query(new UUIDQuery(primID));
  100. if(result.Count>0)
  101. {
  102. PrimData found = (PrimData) result.Next();
  103. db.Delete(found);
  104. }
  105. }
  106. public void LoadPrimitives(ILocalStorageReceiver receiver)
  107. {
  108. IObjectSet result = db.Get(typeof(PrimData));
  109. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Db4LocalStorage.cs: LoadPrimitives() - number of prims in storages is "+result.Count);
  110. foreach (PrimData prim in result) {
  111. receiver.PrimFromStorage(prim);
  112. }
  113. }
  114. public float[,] LoadWorld()
  115. {
  116. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Loading world....");
  117. //World blank = new World();
  118. float[,] heightmap = null;
  119. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Looking for a heightmap in local DB");
  120. IObjectSet world_result = db.Get(typeof(MapStorage));
  121. if (world_result.Count > 0)
  122. {
  123. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Found a heightmap in local database, loading");
  124. MapStorage map = (MapStorage)world_result.Next();
  125. //blank.LandMap = map.Map;
  126. heightmap = map.Map;
  127. }
  128. else
  129. {
  130. /*
  131. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - No heightmap found, generating new one");
  132. HeightmapGenHills hills = new HeightmapGenHills();
  133. // blank.LandMap = hills.GenerateHeightmap(200, 4.0f, 80.0f, false);
  134. // heightmap = hills.GenerateHeightmap(200, 4.0f, 80.0f, false);
  135. heightmap = new float[256, 256];
  136. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("LoadWorld() - Saving heightmap to local database");
  137. MapStorage map = new MapStorage();
  138. map.Map = heightmap; //blank.LandMap;
  139. db.Set(map);
  140. db.Commit();
  141. */
  142. }
  143. return heightmap;
  144. }
  145. public void SaveMap(float[,] heightmap)
  146. {
  147. IObjectSet world_result = db.Get(typeof(MapStorage));
  148. if (world_result.Count > 0)
  149. {
  150. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("SaveWorld() - updating saved copy of heightmap in local database");
  151. MapStorage map = (MapStorage)world_result.Next();
  152. db.Delete(map);
  153. }
  154. MapStorage map1 = new MapStorage();
  155. map1.Map = heightmap; //OpenSim_Main.local_world.LandMap;
  156. db.Set(map1);
  157. db.Commit();
  158. }
  159. public void ShutDown()
  160. {
  161. db.Commit();
  162. db.Close();
  163. }
  164. }
  165. }