-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.js
149 lines (133 loc) · 4.28 KB
/
images.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
var fs = require('fs'),
PNG = require('pngjs').PNG;
var Anvil=require("prismarine-provider-anvil").Anvil("1.8");
var Vec3 = require("vec3");
var path = require('path');
var mapColors = require("./map-color-index.json")
var REGION_IMG_DIR="./web/assets/region"
var MINECRAFT_DIR='C:\\Users\\Simon\\AppData\\Roaming\\.minecraft\\saves\\SimonSim'
var anvil = new Anvil(path.join(MINECRAFT_DIR, 'region'));
function getChunkBlocks(chunk_x, chunk_z){
var blocks = [];
return new Promise(function(resolve, reject){
var d=anvil.load(chunk_x, chunk_z);
d.then(function(data){
if (data == null){
reject(null);
return
}
for(var x=0; x<16;x++){
blocks.push([]);
for(var z=0; z<16;z++){
for(var y=255;y>=0;y--){
var blockType = data.getBlockType(new Vec3(x, y, z));
// console.log(" BT" + blockType)
if(blockType !== 0){
blocks[x][z] = blockType;
break;
}
}
}
}
console.log("Got chunk " + chunk_x, chunk_z + " " + blocks.length + "x" + blocks[0].length)
resolve(blocks);
}).catch(function(err){
console.log("anvil load error")
console.log(err)
console.log(err.stack);
reject(err)
});
})
}
var addChunk = function(chunk, file, start_x, start_y){
return new Promise(function(resolve, reject){
for (var y = 0; y < 16; y++) {
for (var x = 0; x < 16; x++) {
var idx = (file.width * (start_y+y) + (start_x+x)) << 2;
var col = (start_x+x) < (file.width >> 1) ^ (start_y+y) < (file.height >> 1) ? 0xe5 : 0xff;
file.data[idx] = mapColors[chunk[x][y]][0];
file.data[idx + 1] = mapColors[chunk[x][y]][1];
file.data[idx + 2] = mapColors[chunk[x][y]][2];
file.data[idx + 3] = 0xff;
}
}
//console.log("Wrote chunk " + start_x/16, start_y/16)
resolve();
});
};
var drawRegion = function(rx, rz, filename){
var regionImage = new PNG({width:16*32,height:16*32});
var promises = [];
for(var x=0;x<32;x++){
for(var z=0;z<32;z++){
(function(x, z){
promises.push(
getChunkBlocks(rx*32 + x, rz*32 + z)
.then(chunk => {
return addChunk(chunk, regionImage, x*16, z*16)
})
.catch(err => {
if(err !== null)console.log(err)
})
)
})(x, z)
}
}
return Promise.all(promises).then(() => {
console.log("Got all chunks " + rx + " " + rz)
return new Promise(function(resolve, reject){
regionImage.pack()
.pipe(fs.createWriteStream(REGION_IMG_DIR + '/' + filename))
.on('finish', function() {
console.log('Written! ' + rx + " " + rz);
resolve()
})
.on('error', function(err){
reject(err);
});
})
.catch(err => console.log(err))
});
};
var getOutdatedRegions = function(directory){
return new Promise(function(resolve, reject){
fs.readdir(directory, function(err, regions) {
outdatedRegions = []
for(r in regions){
var regionModified = new Date(fs.statSync(path.join(directory,regions[r])).mtime);
var imageFilename = path.basename(regions[r], ".mca") + ".png"
try{
var imageStat = fs.statSync(path.join(REGION_IMG_DIR, imageFilename))
var imageModified = new Date(imageStat.mtime);
if(imageModified < regionModified){
outdatedRegions.push(regions[r])
}
}catch(err){
outdatedRegions.push(regions[r])
}
}
console.log(outdatedRegions)
resolve(outdatedRegions);
});
});
};
// getOutdatedRegions(path.join(MINECRAFT_DIR, 'region')).catch(err => console.log(err));
var updateRegionImages = function(){
return getOutdatedRegions(path.join(MINECRAFT_DIR, 'region'))
.catch(err => console.log(err))
.then(regions => regions.map(r => {
var region = path.basename(r)
var regionRegex = /r\.(-?\d+)\.(-?\d+)\.mca/g
var results = regionRegex.exec(region)
if(results){
var x = parseInt(results[1]);
var z = parseInt(results[2]);
console.log("Drawing region " + x + " " + z)
drawRegion(x, z, "r."+ x + "." + z + ".png")
}
}))
.catch(err => console.log(err))
};
module.exports = {
updateRegionImages: updateRegionImages
}