mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 10:47:04 +02:00
caching for strings
This commit is contained in:
1 parent
116c30be88
commit
d79e0ddb5d
1 file changed
+25
-16
@@ -25,8 +25,8 @@ import {
|
|||||||
VehicleDefinitionList,
|
VehicleDefinitionList,
|
||||||
} from "@blitzkit/core";
|
} from "@blitzkit/core";
|
||||||
import { AbstractVFS } from "@blitzkit/core/src/blitzkit/vfs/abstract";
|
import { AbstractVFS } from "@blitzkit/core/src/blitzkit/vfs/abstract";
|
||||||
import { SUPPORTED_LOCALE_BLITZ_MAP } from "@blitzkit/i18n";
|
|
||||||
import locales from "@blitzkit/i18n/locales.json";
|
import locales from "@blitzkit/i18n/locales.json";
|
||||||
|
import { SUPPORTED_LOCALE_BLITZ_MAP } from "@blitzkit/i18n/src/strings";
|
||||||
import { parse as parseYaml } from "yaml";
|
import { parse as parseYaml } from "yaml";
|
||||||
import { BlitzKitAPI } from "../base";
|
import { BlitzKitAPI } from "../base";
|
||||||
|
|
||||||
@@ -113,6 +113,9 @@ export abstract class ServerBlitzKitAPI0 extends BlitzKitAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
|
const { existsSync } = await import("fs");
|
||||||
|
const { readFile, writeFile, mkdir } = await import("fs/promises");
|
||||||
|
|
||||||
console.log("Initializing virtual file system...");
|
console.log("Initializing virtual file system...");
|
||||||
await this.vfs.init();
|
await this.vfs.init();
|
||||||
|
|
||||||
@@ -155,31 +158,37 @@ export abstract class ServerBlitzKitAPI0 extends BlitzKitAPI {
|
|||||||
|
|
||||||
console.log("Fetching game localizations...");
|
console.log("Fetching game localizations...");
|
||||||
|
|
||||||
if (import.meta.env.DEV) {
|
|
||||||
locales.supported = [locales.supported[0]];
|
|
||||||
}
|
|
||||||
|
|
||||||
let fetchedLocalizations = 0;
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
locales.supported.map(async ({ locale }) => {
|
locales.supported.map(async ({ locale }) => {
|
||||||
|
const root = "../../temp/strings";
|
||||||
|
const cachePath = `${root}/${locale}.json`;
|
||||||
|
|
||||||
|
if (existsSync(cachePath)) {
|
||||||
|
console.log(` Cache hit for ${locale}`);
|
||||||
|
|
||||||
|
const content = await readFile(cachePath, "utf-8");
|
||||||
|
this.stringsI18n[locale] = JSON.parse(content) as BlitzStrings;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(` Cache miss for ${locale} (this will take a while...)`);
|
||||||
|
|
||||||
const blitzLocale = SUPPORTED_LOCALE_BLITZ_MAP[locale];
|
const blitzLocale = SUPPORTED_LOCALE_BLITZ_MAP[locale];
|
||||||
const cache = await fetch(
|
const networkStrings = (await fetch(
|
||||||
`https://stufficons.wgcdn.co/localizations/${blitzLocale}.yaml`,
|
`https://stufficons.wgcdn.co/localizations/${blitzLocale}.yaml`,
|
||||||
)
|
)
|
||||||
.then((response) => response.text())
|
.then((response) => response.text())
|
||||||
.then((string) => parseYaml(string) as BlitzStrings);
|
.then(parseYaml)) as BlitzStrings;
|
||||||
const preInstalled = await this.vfs.yaml<BlitzStrings>(
|
const localStrings = await this.vfs.yaml<BlitzStrings>(
|
||||||
`Data/Strings/${blitzLocale}.yaml`,
|
`Data/Strings/${blitzLocale}.yaml`,
|
||||||
);
|
);
|
||||||
|
const combined = { ...networkStrings, ...localStrings };
|
||||||
|
|
||||||
this.stringsI18n[locale] = {
|
await mkdir(root, { recursive: true });
|
||||||
...cache,
|
await writeFile(cachePath, JSON.stringify(combined));
|
||||||
...preInstalled,
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log(
|
this.stringsI18n[locale] = combined;
|
||||||
`Fetched localizations for ${locale} (${++fetchedLocalizations}/${locales.supported.length})`,
|
|
||||||
);
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user