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

93 lines
2.8 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";
import { readStringDVPL } from "../src/core/blitz/readStringDVPL";
import { DATA } from "./buildAssets/constants";
import { readYAMLDVPL } from "./core/blitz/readYAMLDVPL";
import { writeDVPL } from "./core/blitz/writeDVPL";
2024-03-13 00:17:30 -05:00
2025-01-14 19:22:41 -06:00
const versionTextFile = await readStringDVPL(`${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(
2025-10-12 14:12:47 -05:00
import.meta.env.WOTB_DLC_CDN
)}/dlc/s${currentVersion}_${patchIndex}.yaml`
2024-03-13 00:17:30 -05:00
);
if (response.status === 200) {
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(
2025-10-12 14:12:47 -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",
".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`,
2025-10-12 14:12:47 -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 {
await mkdir(`${DATA}/${dir}`, { recursive: true });
} catch (error) {
console.warn(`Failed to make directory "${dir}"`);
}
2025-10-12 14:12:47 -05:00
const isDvpl = path.endsWith(".dvpl");
2025-03-03 16:15:58 -06:00
const buffer = Buffer.from(data);
2025-01-18 09:19:38 -06:00
await writeFile(
2025-10-12 14:12:47 -05:00
`${DATA}/${path}${isDvpl ? "" : ".dvpl"}`,
new Uint8Array(isDvpl ? buffer : writeDVPL(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
2025-10-12 14:12:47 -05:00
if ("dynamicContentLocalizationsDir" in data) {
console.log("Found dynamic content localizations; patching...");
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
2025-10-12 14:12:47 -05:00
}/en.yaml`
2024-08-12 13:31:06 -05:00
);
const newStrings = parseYaml(await localizationsResponse.text());
const oldStrings = await readYAMLDVPL<Record<string, string>>(
2025-10-12 14:12:47 -05:00
`${DATA}/Strings/en.yaml`
2024-08-12 13:31:06 -05:00
);
const patchedStrings = { ...oldStrings, ...newStrings };
const patchedContent = stringifyYaml(patchedStrings);
await writeFile(
2025-01-14 19:22:41 -06:00
`${DATA}/Strings/en.yaml`,
2025-10-12 14:12:47 -05:00
new Uint8Array(writeDVPL(Buffer.from(patchedContent)))
2024-08-12 13:31:06 -05:00
);
}
2024-03-13 00:17:30 -05:00
patchIndex++;
} else break;
}