OSDN Git Service

Revisit(er) & ReadOnly chunk use
[automap/automap.git] / ShardProcessor / WalkableMapDB.cs
1 using System;
2 using System.Linq;
3 using System.Collections.Generic;
4 using System.Data;
5 using System.Data.Common;
6 using System.Data.SQLite;
7
8 using ProtoBuf;
9
10
11
12 using Vintagestory.API.Common;
13 using Vintagestory.GameContent;
14 using Vintagestory.API.Util;
15 using Vintagestory.API.MathTools;
16
17 namespace ShardProcessor
18 {
19         public class WalkableMapDB : MapDB
20         {
21                 private SQLiteCommand walkMapPieceCmd;
22
23                 public WalkableMapDB(ILogger logger) : base(logger)
24         {
25                 }
26
27                 public override void OnOpened( )
28                 {
29                 base.OnOpened( );
30
31                 walkMapPieceCmd = sqliteConn.CreateCommand( );
32                 walkMapPieceCmd.CommandText = @"SELECT position, data FROM mappiece";
33                 walkMapPieceCmd.Prepare( );
34                 }
35
36
37                 public IEnumerable<LocalizedMapPiece> WalkMapTiles( )
38                 {                       
39                 using (SQLiteDataReader sqlite_datareader = walkMapPieceCmd.ExecuteReader( )) 
40                 {
41                 int numForPos = sqlite_datareader.GetOrdinal(@"position");
42                 int numForData = sqlite_datareader.GetOrdinal(@"data");
43
44                         while (sqlite_datareader.Read( )) 
45                                 {                                       
46                                 var posInteger = sqlite_datareader.GetInt64(numForPos);//[];//Integer KEY               
47                                 object data = sqlite_datareader[numForData];
48                                 if (data == null) yield return null;
49
50                                 var rawMapP = SerializerUtil.Deserialize<MapPieceDB>(data as byte[ ]);
51
52                                         var nextPiece = new LocalizedMapPiece( ) {
53                                         ChunkPos = posInteger.Convert(),
54                                         Pixels =  rawMapP.Pixels,
55                                         };
56
57                                 yield return nextPiece;
58                                 }
59                         }
60                 }
61         }
62
63         public class LocalizedMapPiece : MapPieceDB
64         {
65                 public Vec2i ChunkPos;
66
67         }
68
69         public static class MapDB_Assist
70         {
71                 public static Vec2i Convert(this long input)
72                 {
73                 var vector = new Vec2i( );
74                 vector.X = ( int )(0x7FFFFFF & input); //Passthru only last 27 bits
75                 vector.Y = ( int )(input >> 27 );//Shift 27 right
76                 return vector;
77                 }
78         }
79
80 }
81