@@ -24,13 +24,10 @@ const writeBuffer = (fileName, buffer) => {
24
24
console . log ( 'Undefined buffer; skipping creating `' + fileName + '`' ) ;
25
25
return ;
26
26
}
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 ) ;
31
28
} ;
32
29
33
- const forEachTile = ( context , map , callback , name , floorID ) => {
30
+ const forEachTile = ( context , map , createBufferCallback , writeBufferCallback , floorID ) => {
34
31
const isGroundFloor = floorID == '07' ;
35
32
const z = Number ( floorID ) ;
36
33
const bounds = GLOBALS . bounds ;
@@ -45,20 +42,10 @@ const forEachTile = (context, map, callback, name, floorID) => {
45
42
while ( xOffset < bounds . width ) {
46
43
const x = bounds . xMin + xOffset ;
47
44
const pixels = context . getImageData ( xOffset , yOffset , 256 , 256 ) ;
48
- const buffer = callback ( pixels , isGroundFloor ) ;
45
+ const buffer = createBufferCallback ( pixels , isGroundFloor ) ;
49
46
const id = `${ x } _${ y } _${ z } ` ;
50
47
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 ) ;
62
49
}
63
50
xOffset += 256 ;
64
51
}
@@ -71,15 +58,29 @@ const createBinaryMap = async (floorID) => {
71
58
const canvas = Canvas . createCanvas ( bounds . width , bounds . height ) ;
72
59
const context = canvas . getContext ( '2d' ) ;
73
60
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
+ ) ) ;
75
69
} ;
76
70
77
71
const createBinaryPath = async ( floorID ) => {
78
72
const bounds = GLOBALS . bounds ;
79
73
const canvas = Canvas . createCanvas ( bounds . width , bounds . height ) ;
80
74
const context = canvas . getContext ( '2d' ) ;
81
75
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
+ ) ) ;
83
84
} ;
84
85
85
86
let MINIMAP_MARKERS = Buffer . alloc ( 0 ) ;
@@ -106,28 +107,31 @@ const convertToMinimap = async (dataDirectory, outputPath, includeMarkers, overl
106
107
GLOBALS . dataDirectory = dataDirectory ;
107
108
GLOBALS . outputPath = outputPath ;
108
109
GLOBALS . overlayGrid = overlayGrid ;
110
+ GLOBALS . ioPromises = [ ] ;
109
111
const bounds = JSON . parse ( fs . readFileSync ( `${ dataDirectory } /bounds.json` ) ) ;
110
112
GLOBALS . bounds = bounds ;
111
113
GLOBALS . canvas = Canvas . createCanvas ( bounds . width , bounds . height ) ;
112
114
GLOBALS . context = GLOBALS . canvas . getContext ( '2d' ) ;
113
115
const floorIDs = bounds . floorIDs ;
114
116
try {
115
- const promises = [
117
+ const bufferPromises = [
116
118
handleParallel ( floorIDs , createBinaryMap ) ,
117
119
handleParallel ( floorIDs , createBinaryPath ) ,
118
120
] ;
119
121
if ( includeMarkers ) {
120
- promises . push ( handleParallel ( floorIDs , createBinaryMarkers ) ) ;
122
+ bufferPromises . push ( handleParallel ( floorIDs , createBinaryMarkers ) ) ;
121
123
}
122
- await Promise . all ( promises ) ;
124
+ await Promise . all ( bufferPromises ) ;
123
125
// TODO: We *could* keep track of all the files that have been written, and
124
126
// if any `Color` files don’t have a corresponding `WaypointCost` file or
125
127
// vice versa, we could then create it using `EMPTY_PATH_BUFFER` or
126
128
// `EMPTY_MAP_BUFFER`. Not sure if this is worth the hassle, though.
127
129
if ( includeMarkers && MINIMAP_MARKERS . length ) {
128
130
// 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 ) ) ;
130
132
}
133
+ // Wait for all file operations to complete.
134
+ await Promise . all ( GLOBALS . ioPromises ) ;
131
135
} catch ( exception ) {
132
136
console . error ( exception . stack ) ;
133
137
reject ( exception ) ;
0 commit comments