Skip to content

Commit 5335114

Browse files
committed
Clean up to-minimap
1 parent a7da481 commit 5335114

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/to-minimap.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ const writeBuffer = (fileName, buffer) => {
2424
console.log('Undefined buffer; skipping creating `' + fileName + '`');
2525
return;
2626
}
27-
const writeStream = fs.createWriteStream(fileName);
28-
writeStream.write(buffer);
29-
writeStream.end();
30-
//console.log(`${fileName} created successfully.`);
27+
return fsp.writeFile(fileName, buffer);
3128
};
3229

33-
const forEachTile = (context, map, callback, name, floorID) => {
30+
const forEachTile = (context, map, createBufferCallback, writeBufferCallback, floorID) => {
3431
const isGroundFloor = floorID == '07';
3532
const z = Number(floorID);
3633
const bounds = GLOBALS.bounds;
@@ -45,20 +42,10 @@ const forEachTile = (context, map, callback, name, floorID) => {
4542
while (xOffset < bounds.width) {
4643
const x = bounds.xMin + xOffset;
4744
const pixels = context.getImageData(xOffset, yOffset, 256, 256);
48-
const buffer = callback(pixels, isGroundFloor);
45+
const buffer = createBufferCallback(pixels, isGroundFloor);
4946
const id = `${x}_${y}_${z}`;
5047
if (buffer) {
51-
if (name === 'mapBuffer') {
52-
writeBuffer(
53-
`${GLOBALS.outputPath}/Minimap_Color_${id}.png`,
54-
wrapColorData(buffer, { overlayGrid: GLOBALS.overlayGrid })
55-
);
56-
} else if (name === 'pathBuffer') {
57-
writeBuffer(
58-
`${GLOBALS.outputPath}/Minimap_WaypointCost_${id}.png`,
59-
wrapWaypointData(buffer)
60-
);
61-
}
48+
writeBufferCallback(buffer, id);
6249
}
6350
xOffset += 256;
6451
}
@@ -71,15 +58,29 @@ const createBinaryMap = async (floorID) => {
7158
const canvas = Canvas.createCanvas(bounds.width, bounds.height);
7259
const context = canvas.getContext('2d');
7360
const map = await fsp.readFile(`${GLOBALS.dataDirectory}/floor-${floorID}-map.png`);
74-
forEachTile(context, map, pixelDataToMapBuffer, 'mapBuffer', floorID);
61+
forEachTile(context, map, pixelDataToMapBuffer, writeBinaryMapBuffer, floorID);
62+
};
63+
64+
const writeBinaryMapBuffer = (buffer, id) => {
65+
GLOBALS.ioPromises.push(writeBuffer(
66+
`${GLOBALS.outputPath}/Minimap_Color_${id}.png`,
67+
wrapColorData(buffer, { overlayGrid: GLOBALS.overlayGrid })
68+
));
7569
};
7670

7771
const createBinaryPath = async (floorID) => {
7872
const bounds = GLOBALS.bounds;
7973
const canvas = Canvas.createCanvas(bounds.width, bounds.height);
8074
const context = canvas.getContext('2d');
8175
const map = await fsp.readFile(`${GLOBALS.dataDirectory}/floor-${floorID}-path.png`);
82-
forEachTile(context, map, pixelDataToPathBuffer, 'pathBuffer', floorID);
76+
forEachTile(context, map, pixelDataToPathBuffer, writeBinaryPathBuffer, floorID);
77+
};
78+
79+
const writeBinaryPathBuffer = (buffer, id) => {
80+
GLOBALS.ioPromises.push(writeBuffer(
81+
`${GLOBALS.outputPath}/Minimap_WaypointCost_${id}.png`,
82+
wrapWaypointData(buffer)
83+
));
8384
};
8485

8586
let MINIMAP_MARKERS = Buffer.alloc(0);
@@ -106,28 +107,31 @@ const convertToMinimap = async (dataDirectory, outputPath, includeMarkers, overl
106107
GLOBALS.dataDirectory = dataDirectory;
107108
GLOBALS.outputPath = outputPath;
108109
GLOBALS.overlayGrid = overlayGrid;
110+
GLOBALS.ioPromises = [];
109111
const bounds = JSON.parse(fs.readFileSync(`${dataDirectory}/bounds.json`));
110112
GLOBALS.bounds = bounds;
111113
GLOBALS.canvas = Canvas.createCanvas(bounds.width, bounds.height);
112114
GLOBALS.context = GLOBALS.canvas.getContext('2d');
113115
const floorIDs = bounds.floorIDs;
114116
try {
115-
const promises = [
117+
const bufferPromises = [
116118
handleParallel(floorIDs, createBinaryMap),
117119
handleParallel(floorIDs, createBinaryPath),
118120
];
119121
if (includeMarkers) {
120-
promises.push(handleParallel(floorIDs, createBinaryMarkers));
122+
bufferPromises.push(handleParallel(floorIDs, createBinaryMarkers));
121123
}
122-
await Promise.all(promises);
124+
await Promise.all(bufferPromises);
123125
// TODO: We *could* keep track of all the files that have been written, and
124126
// if any `Color` files don’t have a corresponding `WaypointCost` file or
125127
// vice versa, we could then create it using `EMPTY_PATH_BUFFER` or
126128
// `EMPTY_MAP_BUFFER`. Not sure if this is worth the hassle, though.
127129
if (includeMarkers && MINIMAP_MARKERS.length) {
128130
// The Tibia 11 installer doesn’t create the file if no markers are set.
129-
writeBuffer(`${outputPath}/minimapmarkers.bin`, MINIMAP_MARKERS);
131+
GLOBALS.ioPromises.push(writeBuffer(`${outputPath}/minimapmarkers.bin`, MINIMAP_MARKERS));
130132
}
133+
// Wait for all file operations to complete.
134+
await Promise.all(GLOBALS.ioPromises);
131135
} catch (exception) {
132136
console.error(exception.stack);
133137
reject(exception);

0 commit comments

Comments
 (0)