/
BlockDBFile.cs
205 lines (171 loc) · 8.57 KB
/
BlockDBFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
https://opensource.org/license/ecl-2-0/
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.IO;
using MCGalaxy.Maths;
using MCGalaxy.Util;
namespace MCGalaxy.DB
{
public unsafe abstract class BlockDBFile
{
public const byte Version = 1;
public const int EntrySize = 16;
public const int HeaderEntries = 1;
public const int BulkEntries = 4096;
public static BlockDBFile V1 = new BlockDBFile_V1();
public static string FilePath(string map) { return "blockdb/" + map + ".cbdb"; }
public static string DumpPath(string map) { return "blockdb/" + map + ".dump"; }
public static string TempPath(string map) { return "blockdb/" + map + ".temp"; }
public static void WriteHeader(Stream s, Vec3U16 dims) {
byte[] header = new byte[EntrySize * HeaderEntries * 4];
NetUtils.Write("CBDB_MCG", header, 0, false);
WriteU16(Version, header, 8);
WriteU16(dims.X, header, 10);
WriteU16(dims.Y, header, 12);
WriteU16(dims.Z, header, 14);
s.Write(header, 0, EntrySize);
}
public static BlockDBFile ReadHeader(Stream s, out Vec3U16 dims) {
dims = default(Vec3U16);
byte[] header = new byte[EntrySize * HeaderEntries];
ReadFully(s, header, 0, header.Length);
// Check constants are expected
// TODO: check 8 byte string identifier
ushort fileVersion = ReadU16(header, 8);
if (fileVersion != Version)
throw new NotSupportedException("only version 1 is supported");
dims.X = ReadU16(header, 10);
dims.Y = ReadU16(header, 12);
dims.Z = ReadU16(header, 14);
return V1;
}
public abstract void WriteEntries(Stream s, FastList<BlockDBEntry> entries);
public abstract void WriteEntries(Stream s, BlockDBCache cache);
/// <summary> Reads a block of BlockDB entries, in a forward streaming manner. </summary>
/// <returns> The number of entries read. </returns>
public abstract int ReadForward(Stream s, byte[] bulk, BlockDBEntry* entryPtr);
/// <summary> Reads a block of BlockDB entries, in a backward streaming manner. </summary>
/// <returns> The number of entries read. </returns>
public abstract int ReadBackward(Stream s, byte[] bulk, BlockDBEntry* entryPtr);
/// <summary> Returns number of entries in the backing file. </summary>
public abstract long CountEntries(Stream s);
/// <summary> Deletes the backing file on disc if it exists. </summary>
public static void DeleteBackingFile(string map) {
string path = FilePath(map);
if (!File.Exists(path)) return;
File.Delete(path);
}
/// <summary> Moves the backing file on disc if it exists. </summary>
public static void MoveBackingFile(string srcMap, string dstMap) {
string srcPath = FilePath(srcMap), dstPath = FilePath(dstMap);
if (!File.Exists(srcPath)) return;
if (File.Exists(dstPath)) File.Delete(dstPath);
File.Move(srcPath, dstPath);
}
public static void ResizeBackingFile(BlockDB db, string path) {
Logger.Log(LogType.BackgroundActivity, "Resizing BlockDB for " + db.MapName);
string tempPath = TempPath(db.MapName);
using (Stream src = File.OpenRead(path), dst = File.Create(tempPath)) {
Vec3U16 dims;
ReadHeader(src, out dims);
WriteHeader(dst, db.Dims);
int width = db.Dims.X, length = db.Dims.Z;
byte[] bulk = new byte[BulkEntries * EntrySize];
fixed (byte* ptr = bulk) {
BlockDBEntry* entryPtr = (BlockDBEntry*)ptr;
while (true) {
int count = V1.ReadForward(src, bulk, entryPtr);
if (count == 0) break;
for (int i = 0; i < count; i++) {
int index = entryPtr[i].Index;
int x = index % dims.X;
int y = (index / dims.X) / dims.Z;
int z = (index / dims.X) % dims.Z;
entryPtr[i].Index = (y * length + z) * width + x;
}
dst.Write(bulk, 0, count * EntrySize);
}
}
}
File.Delete(path);
File.Move(tempPath, path);
}
/// <summary> Returns number of entries in the backing file on disc if it exists. </summary>
public static long CountEntries(string map) {
string path = FilePath(map);
if (!File.Exists(path)) return 0;
using (Stream src = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)) {
Vec3U16 dims;
BlockDBFile file = ReadHeader(src, out dims);
return file.CountEntries(src);
}
}
/// <summary> Iterates from the very oldest to newest entry in the BlockDB. </summary>
public void FindChangesAt(Stream s, int index, Action<BlockDBEntry> output) {
byte[] bulk = new byte[BulkEntries * EntrySize];
fixed (byte* ptr = bulk) {
while (true) {
BlockDBEntry* entryPtr = (BlockDBEntry*)ptr;
int count = ReadForward(s, bulk, entryPtr);
if (count == 0) return;
for (int i = 0; i < count; i++) {
if (entryPtr->Index == index) { output(*entryPtr); }
entryPtr++;
}
}
}
}
/// <summary> Iterates from the very newest to oldest entry in the BlockDB. </summary>
/// <returns> whether an entry before start time was reached. </returns>
public bool FindChangesBy(Stream s, int[] ids, int start, int end, Action<BlockDBEntry> output) {
byte[] bulk = new byte[BulkEntries * EntrySize];
s.Position = s.Length;
fixed (byte* ptr = bulk) {
while (true) {
BlockDBEntry* entryPtr = (BlockDBEntry*)ptr;
int count = ReadBackward(s, bulk, entryPtr);
if (count == 0) break;
entryPtr += (count - 1);
for (int i = count - 1; i >= 0; i--) {
if (entryPtr->TimeDelta < start) return true;
if (entryPtr->TimeDelta <= end) {
for (int j = 0; j < ids.Length; j++) {
if (entryPtr->PlayerID != ids[j]) continue;
output(*entryPtr); break;
}
}
entryPtr--;
}
}
}
return false;
}
static ushort ReadU16(byte[] array, int offset) {
return (ushort)(array[offset] | array[offset + 1] << 8);
}
static void WriteU16(ushort value, byte[] array, int index) {
array[index++] = (byte)(value);
array[index++] = (byte)(value >> 8);
}
internal static void ReadFully(Stream stream, byte[] dst, int offset, int count) {
int total = 0;
do {
int read = stream.Read(dst, offset + total, count - total);
if (read == 0) throw new EndOfStreamException();
total += read;
} while (total < count);
}
}
}