Files
OpenFrontIO/src/core/game/BinaryLoaderGameMapLoader.ts
T
Antoine ad2598361b Fix remaining errors and enable strict mode (#1628)
## Description:

#1075 

Fixing all remaining type errors caused by strict mode and enable it.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I have read and accepted the CLA agreement (only required once).

## Please put your Discord username so you can be contacted if a bug or
regression is found:

azlod

---------

Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
2025-08-03 23:06:31 +00:00

88 lines
2.3 KiB
TypeScript

import { GameMapType } from "./Game";
import { GameMapLoader, MapData } from "./GameMapLoader";
import { MapManifest } from "./TerrainMapLoader";
export interface BinModule {
default: string;
}
interface NationMapModule {
default: MapManifest;
}
export class BinaryLoaderGameMapLoader implements GameMapLoader {
private maps: Map<GameMapType, MapData>;
constructor() {
this.maps = new Map<GameMapType, MapData>();
}
private createLazyLoader<T>(importFn: () => Promise<T>): () => Promise<T> {
let cache: Promise<T> | null = null;
return () => {
cache ??= importFn();
return cache;
};
}
getMapData(map: GameMapType): MapData {
const cachedMap = this.maps.get(map);
if (cachedMap) {
return cachedMap;
}
const key = Object.keys(GameMapType).find(
(k) => GameMapType[k as keyof typeof GameMapType] === map,
);
const fileName = key?.toLowerCase();
const mapData = {
mapBin: this.createLazyLoader(() =>
(
import(
`!!binary-loader!../../../resources/maps/${fileName}/map.bin`
) as Promise<BinModule>
).then((m) => this.toUInt8Array(m.default)),
),
miniMapBin: this.createLazyLoader(() =>
(
import(
`!!binary-loader!../../../resources/maps/${fileName}/mini_map.bin`
) as Promise<BinModule>
).then((m) => this.toUInt8Array(m.default)),
),
manifest: this.createLazyLoader(() =>
(
import(
`../../../resources/maps/${fileName}/manifest.json`
) as Promise<NationMapModule>
).then((m) => m.default),
),
webpPath: this.createLazyLoader(() =>
(
import(
`../../../resources/maps/${fileName}/thumbnail.webp`
) as Promise<{ default: string }>
).then((m) => m.default),
),
} satisfies MapData;
this.maps.set(map, mapData);
return mapData;
}
/**
* Converts a given string into a UInt8Array where each character in the string
* is represented as an 8-bit unsigned integer.
*/
private toUInt8Array(data: string) {
const rawData = new Uint8Array(data.length);
for (let i = 0; i < data.length; i++) {
rawData[i] = data.charCodeAt(i);
}
return rawData;
}
}