| 1 | import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, unlinkSync, writeFileSync } from "node:fs"; |
| 2 | import { dirname, join } from "node:path"; |
| 3 | import { normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 4 | import |
| 5 | { |
| 6 | MINIMAP_CHUNK_PIXELS, |
| 7 | MINIMAP_SPAN_CHUNKS, |
| 8 | minimapRadius, |
| 9 | renderChunkMinimapRgba |
| 10 | } from "../../../shared/render/chunkMinimap.js"; |
| 11 | import { decodeChunkBlob } from "../world/chunkData.js"; |
| 12 | |
| 13 | const RGBA_BYTES = MINIMAP_CHUNK_PIXELS * MINIMAP_CHUNK_PIXELS * 4; |
| 14 | |
| 15 | /** |
| 16 | * Per-dimension minimap images, one uncompressed RGBA file per saved chunk. |
| 17 | * |
| 18 | * Lives under the server data directory next to `world.sqlite`: |
| 19 | * `minimap/<dimensionId>/<chunkX>_<chunkY>.rgba` |
| 20 | * (16×16×4 bytes while chunks are 16 tiles square). |
| 21 | * |
| 22 | * A file is written when that chunk is saved, and created on read when it is |
| 23 | * missing but the chunk exists. Chunks with no row are drawn as empty by the |
| 24 | * client and any stale file for them is removed. Changing sprite colours |
| 25 | * (see `color-stamp.txt`) deletes every cached image so the next read rebuilds |
| 26 | * them. The editor asks for a window of chunks; only cache misses load the |
| 27 | * chunk blob. |
| 28 | */ |
| 29 | export class MinimapCache |
| 30 | { |
| 31 | constructor (rootDir, worldDb) |
| 32 | { |
| 33 | this.rootDir = rootDir; |
| 34 | this.worldDb = worldDb; |
| 35 | } |
| 36 | |
| 37 | pathFor (dimensionId, chunkX, chunkY) |
| 38 | { |
| 39 | const dim = normalizeDimensionId(dimensionId); |
| 40 | return join(this.rootDir, dim, `${chunkX | 0}_${chunkY | 0}.rgba`); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Drop cached images when the sprite-colour stamp changes (new or edited art). |
| 45 | * @param {string} stamp |
| 46 | */ |
| 47 | syncColorStamp (stamp) |
| 48 | { |
| 49 | mkdirSync(this.rootDir, { recursive: true }); |
| 50 | const file = join(this.rootDir, "color-stamp.txt"); |
| 51 | const next = String(stamp ?? ""); |
| 52 | let previous = null; |
| 53 | try |
| 54 | { previous = readFileSync(file, "utf8"); } |
| 55 | catch |
| 56 | { previous = null; } |
| 57 | if (previous === next) |
| 58 | { return; } |
| 59 | let entries = []; |
| 60 | try |
| 61 | { entries = readdirSync(this.rootDir, { withFileTypes: true }); } |
| 62 | catch |
| 63 | { entries = []; } |
| 64 | for (const entry of entries) |
| 65 | { |
| 66 | if (!entry.isDirectory()) |
| 67 | { continue; } |
| 68 | rmSync(join(this.rootDir, entry.name), { recursive: true, force: true }); |
| 69 | } |
| 70 | writeFileSync(file, next); |
| 71 | } |
| 72 | |
| 73 | removeDimensions (dimensionIds) |
| 74 | { |
| 75 | for (const dimensionId of dimensionIds ?? []) |
| 76 | { |
| 77 | rmSync(join(this.rootDir, normalizeDimensionId(dimensionId)), { recursive: true, force: true }); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param {string} dimensionId |
| 83 | * @param {number} chunkX |
| 84 | * @param {number} chunkY |
| 85 | * @param {object} chunkData |
| 86 | * @param {number} defaultGroundTypeId |
| 87 | * @returns {Buffer} |
| 88 | */ |
| 89 | write (dimensionId, chunkX, chunkY, chunkData, defaultGroundTypeId) |
| 90 | { |
| 91 | const rgba = renderChunkMinimapRgba(chunkData, defaultGroundTypeId); |
| 92 | const file = this.pathFor(dimensionId, chunkX, chunkY); |
| 93 | mkdirSync(dirname(file), { recursive: true }); |
| 94 | const buffer = Buffer.from(rgba.buffer, rgba.byteOffset, rgba.byteLength); |
| 95 | writeFileSync(file, buffer); |
| 96 | return buffer; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @returns {Buffer | null} cached or freshly built image, or null when the chunk is not saved |
| 101 | */ |
| 102 | readOrCreate (dimensionId, chunkX, chunkY, revisionId, defaultGroundTypeId) |
| 103 | { |
| 104 | const file = this.pathFor(dimensionId, chunkX, chunkY); |
| 105 | const cached = this.#readValid(file); |
| 106 | const saved = this.worldDb.hasChunkVersion(chunkX, chunkY, revisionId, dimensionId); |
| 107 | if (!saved) |
| 108 | { |
| 109 | if (cached) |
| 110 | { unlinkSync(file); } |
| 111 | return null; |
| 112 | } |
| 113 | if (cached) |
| 114 | { return cached; } |
| 115 | const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId, dimensionId); |
| 116 | if (!row) |
| 117 | { return null; } |
| 118 | const data = decodeChunkBlob(row.data_blob, defaultGroundTypeId, chunkX, chunkY); |
| 119 | return this.write(dimensionId, chunkX, chunkY, data, defaultGroundTypeId); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * 25×25 chunk window centred on `centerChunkX` / `centerChunkY`. |
| 124 | * Present chunks are raw RGBA, base64. Missing chunks are omitted. |
| 125 | */ |
| 126 | window (dimensionId, centerChunkX, centerChunkY, revisionId, defaultGroundTypeId) |
| 127 | { |
| 128 | const dim = normalizeDimensionId(dimensionId); |
| 129 | const centerX = centerChunkX | 0; |
| 130 | const centerY = centerChunkY | 0; |
| 131 | const radius = minimapRadius(MINIMAP_SPAN_CHUNKS); |
| 132 | const chunks = {}; |
| 133 | for (let dy = -radius; dy <= radius; dy++) |
| 134 | { |
| 135 | for (let dx = -radius; dx <= radius; dx++) |
| 136 | { |
| 137 | const chunkX = centerX + dx; |
| 138 | const chunkY = centerY + dy; |
| 139 | try |
| 140 | { |
| 141 | const image = this.readOrCreate(dim, chunkX, chunkY, revisionId, defaultGroundTypeId); |
| 142 | if (image) |
| 143 | { chunks[`${chunkX},${chunkY}`] = image.toString("base64"); } |
| 144 | } |
| 145 | catch (error) |
| 146 | { |
| 147 | console.error( |
| 148 | `[minimap] skipped ${dim} ${chunkX},${chunkY}:`, |
| 149 | error?.message ?? error |
| 150 | ); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | return { |
| 155 | dimensionId: dim, |
| 156 | chunkX: centerX, |
| 157 | chunkY: centerY, |
| 158 | chunkPixels: MINIMAP_CHUNK_PIXELS, |
| 159 | span: MINIMAP_SPAN_CHUNKS, |
| 160 | chunks |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | #readValid (file) |
| 165 | { |
| 166 | if (!existsSync(file)) |
| 167 | { return null; } |
| 168 | const buffer = readFileSync(file); |
| 169 | if (buffer.length !== RGBA_BYTES) |
| 170 | { |
| 171 | unlinkSync(file); |
| 172 | return null; |
| 173 | } |
| 174 | return buffer; |
| 175 | } |
| 176 | } |