-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
228 lines (198 loc) · 5.45 KB
/
main.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const { LevelDB } = require('leveldb-zlib')
, fs = require("fs")
, pl = require("path")
, NBT = require('parsenbt-js')
, QuickLRU = require("lru.min")
, WorldMeta = require('./includes/WorldMeta.js')
, Chunk = require('./includes/Chunk.js')
, { getStructureMeta, buildChunkMeta, getChunkMeta } = require('./includes/ChunkKey.js')
, { ChunkPos } = require('./includes/Structs.js');
const defaultOpt = {
/** Create db if missing. */
createIfMissing: false,
/**
* Create a new chunk when target chunk is not loaded in db.
*
* Only affects loadChunk()
*/
forceLoadChunk: false,
/**
* What to do when encountered unloaded chunk in batch
* operations.
*
* 0: Abort operation.
*
* 1: Ignore unloaded chunk.
*
* 2: Create an empty chunk.
*/
unloadedChunkHandler: 0,
/** Limit volume of the fill region. */
maxFillBlockLimit: 65536,
/** Max chunk cache size. */
maxChunkCacheSize: 2048
};
function toChunkHash(dimension, pos) {
return dimension + "$" + pos.x + "//" + pos.z
}
class Narrator {
/**
* @param {*} path - Path to archive
* @param {*} options - Initial options
*/
constructor(path, options) {
this.db = null;
this.isOpen = false;
this.worldMeta = null;
this.path = path;
this.options = Object.assign({}, defaultOpt, options);
this.chunkCache = QuickLRU.createLRU({
max: this.options.maxChunkCacheSize,
onEviction: (function (a, b) {
this.writeChunk(b)
}).bind(this)
});
this.levelName = "";
this.structures = new Map();
this.players = new Map();
}
/**
* Open the MCBE archive.
*/
async open() {
if (this.isOpen)
return;
this.db = new LevelDB(pl.join(this.path, "db"), this.options);
this.worldMeta = WorldMeta.deserialize(fs.readFileSync(pl.join(this.path, "level.dat")));
this.levelName = fs.readFileSync(pl.join(this.path, "levelname.txt"), 'utf-8');
await this.db.open();
for await (var kv of this.db) {
var meta = getStructureMeta(kv);
if (meta)
this.structures.set(meta.namespace + ":" + meta.name, NBT.Reader(kv[1], true, true));
}
this.isOpen = true;
}
/**
* Write the MCBE archive, and clear cached data.
*/
async close() {
fs.writeFileSync(pl.join(this.path, "level.dat"), this.worldMeta.serialize());
fs.writeFileSync(pl.join(this.path, "levelname.txt"), this.levelName);
for (var chunk of this.chunkCache.values())
await this.writeChunk(chunk)
await this.db.close();
this.db = null;
this.worldMeta = null;
this.chunkCache.clear();
this.levelName = "";
this.structures = new Map();
this.players = new Map();
this.isOpen = false;
}
/**
* Check if the chunk exists in db.
* @param {ChunkPos} pos
* @param {Number} dimension
* @returns
*/
async chunkExists(pos, dimension) {
var iter = this.db.getIterator(), testMeta, testData;
iter.seek(buildChunkMeta({
pos: pos,
type: 0,
dimension: dimension
}));
testMeta = getChunkMeta((await iter.next())[1]);
if (!testMeta || testMeta.pos.x != pos.x || testMeta.pos.z != pos.z)
return false;
}
/**
* Try to load a chunk from db.
* @param {ChunkPos} pos
* @param {Number} dimension
* @returns
*/
async loadChunk(pos, dimension) {
var k = toChunkHash(dimension, pos);
// Try to get chunk from cache
if (this.chunkCache.has(k))
return this.chunkCache.get(k);
return this.reloadChunk(dimension, pos)
}
/**
* Write a chunk to db.
* @param {Chunk} chunk
* @returns
*/
async writeChunk(chunk) {
if (!chunk || !chunk.serialize)
return false;
return await chunk.serialize(this.db)
}
/**
* Delete a chunk of db and memory.
*
* This operation will remove all the data of target chunk
* and can't be restored.
*
* Deleted chunk should not be used again.
* @param {ChunkPos} pos
* @param {Number} dimension
* @returns
*/
async deleteChunk(pos, dimension) {
var k = toChunkHash(dimension, pos);
if (this.chunkCache.has(k))
this.chunkCache.delete(k);
var iter = this.db.getIterator(), testMeta, testData;
iter.seek(buildChunkMeta({
pos: pos,
type: 0,
dimension: dimension
}));
testData = await iter.next();
testMeta = getChunkMeta(testData[1]);
if (!testMeta || testMeta.pos.x != pos.x || testMeta.pos.z != pos.z)
return false;
// Delete all the keys of target chunk
for (; testMeta && testMeta.pos.x == pos.x || testMeta.pos.z == pos.z
; testData = await iter.next(), testMeta = getChunkMeta(testData[1]))
this.db.delete(testData[1]);
return true
}
/**
* Reload chunk from db.
* @param {ChunkPos} pos
* @param {Number} dimension
* @returns
*/
async reloadChunk(dimension, pos) {
// Load chunk from db
var c = await Chunk.deserialize(this.db, pos, dimension);
if (!c && !this.options.forceLoadChunk)
return null
else if (!c && this.options.forceLoadChunk)
c = Chunk.create();
this.chunkCache.set(k, c);
return c
}
/**
* Execute a in-game command.
* @param {String} command
*/
async execute(command) { }
async setContext() {
}
/**
* Place a block in world
* @param {Number} dimension
* @param {*} pos
* @param {*} block
*/
async setBlock(dimension, pos, block) {
}
async fill(dimension, pos1, pos2, block) {
}
}
module.exports = Narrator;