Files
blitzkit/packages/scripts/src/applyPatches.ts
T

106 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-10-12 14:12:47 -05:00
import { dvp } from "@blitzkit/closed";
import { assertSecret } from "@blitzkit/core";
import { mkdir, writeFile } from "fs/promises";
import { parse as parsePath } from "path";
import ProgressBar from "progress";
import { parse as parseYaml, stringify as stringifyYaml } from "yaml";
2026-06-22 19:28:02 -05:00
import { PATCHES_ROOT, vfs } from "./buildAssets/constants";
2025-10-12 14:12:47 -05:00
import { writeDVPL } from "./core/blitz/writeDVPL";
2024-03-13 00:17:30 -05:00
2025-11-26 13:18:44 -06:00
const versionTextFile = await vfs.text(`Data/version.txt`);
2024-08-12 13:31:33 -05:00
const currentVersion = versionTextFile
2025-10-12 14:12:47 -05:00
.split(" ")[0]
.split(".")
2024-08-12 13:31:33 -05:00
.slice(0, 3)
2025-10-12 14:12:47 -05:00
.join(".");
2024-03-13 00:17:30 -05:00
2024-03-13 00:22:54 -05:00
console.log(`Installing patches for ${currentVersion}...`);
2024-03-13 00:17:30 -05:00
let patchIndex = 1;
while (true) {
const response = await fetch(
2024-09-07 21:45:00 -05:00
`${assertSecret(
2026-06-22 19:28:02 -05:00
import.meta.env.WOTB_DLC_CDN,
)}/dlc/s${currentVersion}_${patchIndex}.yaml`,
2024-03-13 00:17:30 -05:00
);
2026-06-23 10:26:05 -05:00
console.log(patchIndex, response.status)
if (!response.ok) break;
2024-03-13 00:22:54 -05:00
console.log(`Applying patch ${patchIndex}...`);
2024-03-13 00:17:30 -05:00
const data = parseYaml(await response.text());
2024-08-27 14:40:43 -05:00
const dvpm = await fetch(
2026-06-22 19:28:02 -05:00
`${assertSecret(import.meta.env.WOTB_DLC_CDN)}/dlc/${data.dx11}`,
2024-08-27 14:40:43 -05:00
).then((response) => response.arrayBuffer());
2024-03-13 00:17:30 -05:00
const dvpd = await fetch(
2024-11-29 12:11:51 -06:00
`${assertSecret(import.meta.env.WOTB_DLC_CDN)}/dlc/${data.dx11.replace(
2025-10-12 14:12:47 -05:00
".dvpm",
2026-06-22 19:28:02 -05:00
".dvpd",
)}`,
2024-03-13 00:17:30 -05:00
).then((response) => response.arrayBuffer());
const files = await dvp(dvpm, dvpd);
2024-08-12 13:31:06 -05:00
const bar = new ProgressBar(
`Patching ${files.length} files :bar`,
2026-06-22 19:28:02 -05:00
files.length,
2024-08-12 13:31:06 -05:00
);
2024-03-13 00:17:30 -05:00
2025-01-18 09:19:38 -06:00
for (const { path, data } of files) {
2025-01-18 11:41:45 -06:00
console.log(`Patching "${path}"...`);
2025-01-18 09:19:38 -06:00
const { dir } = parsePath(path);
2025-01-17 20:15:09 -06:00
2025-01-18 11:41:45 -06:00
try {
2026-06-22 19:28:02 -05:00
await mkdir(`${PATCHES_ROOT}/Data/${dir}`, { recursive: true });
2025-01-18 11:41:45 -06:00
} catch (error) {
console.warn(`Failed to make directory "${dir}"`);
}
2025-03-03 16:15:58 -06:00
const buffer = Buffer.from(data);
2025-01-18 09:19:38 -06:00
await writeFile(
2026-06-23 10:07:54 -05:00
`${PATCHES_ROOT}/Data/${path}`,
new Uint8Array(buffer),
2025-01-18 09:19:38 -06:00
);
2024-03-13 00:17:30 -05:00
2025-01-18 09:19:38 -06:00
bar.tick();
}
2024-03-13 00:17:30 -05:00
2026-06-23 10:11:18 -05:00
console.log('patches done for patch')
2025-10-12 14:12:47 -05:00
if ("dynamicContentLocalizationsDir" in data) {
2026-06-23 10:14:37 -05:00
console.log(
`${assertSecret(import.meta.env.WOTB_DLC_CDN)}/dlc/${
data.dynamicContentLocalizationsDir
}/en.yaml`,
);
2024-08-12 13:31:06 -05:00
const localizationsResponse = await fetch(
2024-11-29 12:11:51 -06:00
`${assertSecret(import.meta.env.WOTB_DLC_CDN)}/dlc/${
2024-09-07 21:45:00 -05:00
data.dynamicContentLocalizationsDir
2026-06-22 19:28:02 -05:00
}/en.yaml`,
2024-08-12 13:31:06 -05:00
);
2026-06-23 10:20:22 -05:00
console.log('fetched')
2024-08-12 13:31:06 -05:00
const newStrings = parseYaml(await localizationsResponse.text());
2026-06-23 10:20:22 -05:00
console.log('got new strings')
2026-06-22 19:28:02 -05:00
const oldStrings =
await vfs.yaml<Record<string, string>>(`Data/Strings/en.yaml`);
2026-06-23 10:20:22 -05:00
console.log('got old strings')
2024-08-12 13:31:06 -05:00
const patchedStrings = { ...oldStrings, ...newStrings };
const patchedContent = stringifyYaml(patchedStrings);
2026-06-23 10:20:22 -05:00
console.log('stringified')
2024-08-12 13:31:06 -05:00
2026-06-22 19:57:41 -05:00
await mkdir(`${PATCHES_ROOT}/Data/Strings`, { recursive: true });
2026-06-23 10:20:22 -05:00
console.log('mkdir')
2024-08-12 13:31:06 -05:00
await writeFile(
2026-06-23 10:07:54 -05:00
`${PATCHES_ROOT}/Data/Strings/en.yaml`,
new Uint8Array((Buffer.from(patchedContent))),
2024-08-12 13:31:06 -05:00
);
2026-06-23 10:20:22 -05:00
console.log('file')
2024-08-12 13:31:06 -05:00
}
2024-03-13 00:17:30 -05:00
patchIndex++;
}
2026-06-23 10:30:56 -05:00
vfs.dispose()
2026-06-23 10:35:27 -05:00
process.exit(0);