aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MSSQL/MSSQLSimulationData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/MSSQL/MSSQLSimulationData.cs')
-rw-r--r--OpenSim/Data/MSSQL/MSSQLSimulationData.cs107
1 files changed, 49 insertions, 58 deletions
diff --git a/OpenSim/Data/MSSQL/MSSQLSimulationData.cs b/OpenSim/Data/MSSQL/MSSQLSimulationData.cs
index f41f60c..1a5ecd6 100644
--- a/OpenSim/Data/MSSQL/MSSQLSimulationData.cs
+++ b/OpenSim/Data/MSSQL/MSSQLSimulationData.cs
@@ -49,6 +49,7 @@ namespace OpenSim.Data.MSSQL
49 49
50 // private static FileSystemDataStore Instance = new FileSystemDataStore(); 50 // private static FileSystemDataStore Instance = new FileSystemDataStore();
51 private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 51 private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52 private static string LogHeader = "[REGION DB MSSQL]";
52 53
53 /// <summary> 54 /// <summary>
54 /// The database manager 55 /// The database manager
@@ -530,43 +531,53 @@ ELSE
530 /// <returns></returns> 531 /// <returns></returns>
531 public double[,] LoadTerrain(UUID regionID) 532 public double[,] LoadTerrain(UUID regionID)
532 { 533 {
533 double[,] terrain = new double[(int)Constants.RegionSize, (int)Constants.RegionSize]; 534 double[,] ret = null;
534 terrain.Initialize(); 535 TerrainData terrData = LoadTerrain(regionID, (int)Constants.RegionSize, (int)Constants.RegionSize, (int)Constants.RegionHeight);
536 if (terrData != null)
537 ret = terrData.GetDoubles();
538 return ret;
539 }
540
541 // Returns 'null' if region not found
542 public TerrainData LoadTerrain(UUID regionID, int pSizeX, int pSizeY, int pSizeZ)
543 {
544 TerrainData terrData = null;
535 545
536 string sql = "select top 1 RegionUUID, Revision, Heightfield from terrain where RegionUUID = @RegionUUID order by Revision desc"; 546 string sql = "select top 1 RegionUUID, Revision, Heightfield from terrain where RegionUUID = @RegionUUID order by Revision desc";
537 547
538 using (SqlConnection conn = new SqlConnection(m_connectionString)) 548 using (SqlConnection conn = new SqlConnection(m_connectionString))
539 using (SqlCommand cmd = new SqlCommand(sql, conn))
540 { 549 {
541 // MySqlParameter param = new MySqlParameter(); 550 using (SqlCommand cmd = new SqlCommand(sql, conn))
542 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
543 conn.Open();
544 using (SqlDataReader reader = cmd.ExecuteReader())
545 { 551 {
546 int rev; 552 // MySqlParameter param = new MySqlParameter();
547 if (reader.Read()) 553 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
554 conn.Open();
555 using (SqlDataReader reader = cmd.ExecuteReader())
548 { 556 {
549 MemoryStream str = new MemoryStream((byte[])reader["Heightfield"]); 557 int rev;
550 BinaryReader br = new BinaryReader(str); 558 if (reader.Read())
551 for (int x = 0; x < (int)Constants.RegionSize; x++)
552 { 559 {
553 for (int y = 0; y < (int)Constants.RegionSize; y++) 560 rev = (int)reader["Revision"];
554 { 561 byte[] blob = (byte[])reader["Heightfield"];
555 terrain[x, y] = br.ReadDouble(); 562 terrData = TerrainData.CreateFromDatabaseBlobFactory(pSizeX, pSizeY, pSizeZ, rev, blob);
556 }
557 } 563 }
558 rev = (int)reader["Revision"]; 564 else
559 } 565 {
560 else 566 _Log.Info("[REGION DB]: No terrain found for region");
561 { 567 return null;
562 _Log.Info("[REGION DB]: No terrain found for region"); 568 }
563 return null; 569 _Log.Info("[REGION DB]: Loaded terrain revision r" + rev);
564 } 570 }
565 _Log.Info("[REGION DB]: Loaded terrain revision r" + rev);
566 } 571 }
567 } 572 }
568 573
569 return terrain; 574 return terrData;
575 }
576
577 // Legacy entry point for when terrain was always a 256x256 hieghtmap
578 public void StoreTerrain(double[,] ter, UUID regionID)
579 {
580 StoreTerrain(new HeightmapTerrainData(ter), regionID);
570 } 581 }
571 582
572 /// <summary> 583 /// <summary>
@@ -574,10 +585,8 @@ ELSE
574 /// </summary> 585 /// </summary>
575 /// <param name="terrain">terrain map data.</param> 586 /// <param name="terrain">terrain map data.</param>
576 /// <param name="regionID">regionID.</param> 587 /// <param name="regionID">regionID.</param>
577 public void StoreTerrain(double[,] terrain, UUID regionID) 588 public void StoreTerrain(TerrainData terrData, UUID regionID)
578 { 589 {
579 int revision = Util.UnixTimeSinceEpoch();
580
581 //Delete old terrain map 590 //Delete old terrain map
582 string sql = "delete from terrain where RegionUUID=@RegionUUID"; 591 string sql = "delete from terrain where RegionUUID=@RegionUUID";
583 using (SqlConnection conn = new SqlConnection(m_connectionString)) 592 using (SqlConnection conn = new SqlConnection(m_connectionString))
@@ -590,17 +599,23 @@ ELSE
590 599
591 sql = "insert into terrain(RegionUUID, Revision, Heightfield) values(@RegionUUID, @Revision, @Heightfield)"; 600 sql = "insert into terrain(RegionUUID, Revision, Heightfield) values(@RegionUUID, @Revision, @Heightfield)";
592 601
602 int terrainDBRevision;
603 Array terrainDBblob;
604 terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);
605
593 using (SqlConnection conn = new SqlConnection(m_connectionString)) 606 using (SqlConnection conn = new SqlConnection(m_connectionString))
594 using (SqlCommand cmd = new SqlCommand(sql, conn))
595 { 607 {
596 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID)); 608 using (SqlCommand cmd = new SqlCommand(sql, conn))
597 cmd.Parameters.Add(_Database.CreateParameter("@Revision", revision)); 609 {
598 cmd.Parameters.Add(_Database.CreateParameter("@Heightfield", serializeTerrain(terrain))); 610 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
599 conn.Open(); 611 cmd.Parameters.Add(_Database.CreateParameter("@Revision", terrainDBRevision));
600 cmd.ExecuteNonQuery(); 612 cmd.Parameters.Add(_Database.CreateParameter("@Heightfield", terrainDBblob));
613 conn.Open();
614 cmd.ExecuteNonQuery();
615 }
601 } 616 }
602 617
603 _Log.Info("[REGION DB]: Stored terrain revision r " + revision); 618 _Log.InfoFormat("{0} Stored terrain revision r={1}", LogHeader, terrainDBRevision);
604 } 619 }
605 620
606 /// <summary> 621 /// <summary>
@@ -1345,30 +1360,6 @@ VALUES
1345 #region Private Methods 1360 #region Private Methods
1346 1361
1347 /// <summary> 1362 /// <summary>
1348 /// Serializes the terrain data for storage in DB.
1349 /// </summary>
1350 /// <param name="val">terrain data</param>
1351 /// <returns></returns>
1352 private static Array serializeTerrain(double[,] val)
1353 {
1354 MemoryStream str = new MemoryStream(((int)Constants.RegionSize * (int)Constants.RegionSize) * sizeof(double));
1355 BinaryWriter bw = new BinaryWriter(str);
1356
1357 // TODO: COMPATIBILITY - Add byte-order conversions
1358 for (int x = 0; x < (int)Constants.RegionSize; x++)
1359 for (int y = 0; y < (int)Constants.RegionSize; y++)
1360 {
1361 double height = val[x, y];
1362 if (height == 0.0)
1363 height = double.Epsilon;
1364
1365 bw.Write(height);
1366 }
1367
1368 return str.ToArray();
1369 }
1370
1371 /// <summary>
1372 /// Stores new regionsettings. 1363 /// Stores new regionsettings.
1373 /// </summary> 1364 /// </summary>
1374 /// <param name="regionSettings">The region settings.</param> 1365 /// <param name="regionSettings">The region settings.</param>