diff --git a/src/modules/hytale-patches/formatter.ts b/src/modules/hytale-patches/formatter.ts index c930a38..b1bc65f 100644 --- a/src/modules/hytale-patches/formatter.ts +++ b/src/modules/hytale-patches/formatter.ts @@ -1,7 +1,20 @@ import type { PatchesUpdate } from './tracker.js' +function formatBytes(bytes: number): string { + const units = ['B', 'KiB', 'MiB', 'GiB'] + let size = bytes + let unitIndex = 0 + + while (size >= 1024 && unitIndex < units.length - 1) { + size /= 1024 + unitIndex++ + } + + return `${size.toFixed(1)} ${units[unitIndex]}` +} + export function formatPatchesUpdate(update: PatchesUpdate): string { - const { patchline, version, previousVersion, patchId } = update + const { patchline, version, previousVersion, patchId, patchSize } = update const emoji = patchline === 'release' ? '🎮' : '🧪' return `${emoji} Hytale Game Patch Update @@ -9,5 +22,6 @@ export function formatPatchesUpdate(update: PatchesUpdate): string { Patchline: ${patchline} New version: ${version} ${previousVersion !== 'unknown' ? `Previous: ${previousVersion}` : ''} -${patchId ? `Patch ID: ${patchId}` : ''}` +${patchId ? `Patch ID: ${patchId}` : ''} +${patchSize ? `Size: ~${formatBytes(patchSize)}` : ''}` } diff --git a/src/modules/hytale-patches/tracker.ts b/src/modules/hytale-patches/tracker.ts index 111677d..7044e0e 100644 --- a/src/modules/hytale-patches/tracker.ts +++ b/src/modules/hytale-patches/tracker.ts @@ -14,6 +14,7 @@ export interface PatchesUpdate { version: string previousVersion: string patchId: number + patchSize?: number } const ENDPOINT = 'https://account-data.hytale.com/my-account/get-launcher-data?arch=amd64&os=windows' @@ -22,10 +23,26 @@ const STATE_KEY = 'hytale-patches' export interface PatchesState { [patchline: string]: { lastVersion: string + lastPatchId: number lastCheck: string } } +async function getPatchSize(patchline: string, fromPatch: number, toPatch: number): Promise { + const url = `https://game-patches.hytale.com/patches/windows/amd64/${patchline}/${fromPatch}/${toPatch}.pwr` + try { + const response = await fetch(url, { method: 'HEAD' }) + if (response.ok) { + const contentLength = response.headers.get('content-length') + return contentLength ? Number.parseInt(contentLength, 10) : undefined + } + } + catch { + // Silently fail if HEAD request fails + } + return undefined +} + export async function checkPatchesUpdate( stateStore: StateStore, patchlines: string[], @@ -57,18 +74,24 @@ export async function checkPatchesUpdate( if (lastState?.lastVersion === currentVersion) { // No update - state[patchline] = { lastVersion: currentVersion, lastCheck: new Date().toISOString() } + state[patchline] = { lastVersion: currentVersion, lastPatchId: patchId, lastCheck: new Date().toISOString() } continue } // Update detected - state[patchline] = { lastVersion: currentVersion, lastCheck: new Date().toISOString() } + state[patchline] = { lastVersion: currentVersion, lastPatchId: patchId, lastCheck: new Date().toISOString() } + + // Fetch patch size + const patchSize = lastState?.lastPatchId !== undefined + ? await getPatchSize(patchline, lastState.lastPatchId, patchId) + : undefined updates.push({ patchline, version: currentVersion, previousVersion: lastState?.lastVersion ?? 'unknown', patchId, + patchSize, }) }