diff --git a/src/dev/mock.ts b/src/dev/mock.ts index addeee6..71ce41f 100644 --- a/src/dev/mock.ts +++ b/src/dev/mock.ts @@ -17,6 +17,7 @@ export {}; worldserver:null,authserver:null,banner_revision:"3567e2f8e9d5",bot_config_keys:71,client:null, notes:["worldserver.exe differs from the one listed in RELEASE.json (release 2026-09-11); this looks like a customised build."],modifies_files:false}; const svc=(n: string,p: number,st: string)=>({name:n,state:st,pid:st==="running"?100:null,port:p,port_ready:st==="running",conflict:null,uptime_secs:st==="running"?4206:null}); + { const q = new URLSearchParams(location.search); const w0 = window as any; if (q.get("client")) w0.__client = q.get("client"); if (q.get("modified")) w0.__modified = true; } (window as any).__TAURI_INTERNALS__ = { transformCallback: (cb: unknown)=>cb, unregisterCallback(){}, convertFileSrc:(x: unknown)=>x, invoke: async (cmd: string, args?: any)=>{ diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.ts index d851bdc..5463747 100644 --- a/src/i18n/locales/de.ts +++ b/src/i18n/locales/de.ts @@ -773,6 +773,8 @@ const de: Partial> = { "set.searchAll": "Alle Einstellungen nach Name oder Beschreibung durchsuchen…", "set.search": "Einstellungen durchsuchen…", "upd.current": "Du bist auf dem neuesten Stand (Version {v}).", + "client.job.checking": "Client wird geprüft…", + "client.job.updating": "Client wird aktualisiert", }; export default de; diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index 34e69a2..04fd349 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -773,6 +773,8 @@ const en = { "set.searchAll": "Search all settings by name or description…", "set.search": "Search settings…", "upd.current": "You are up to date (version {v}).", + "client.job.checking": "Checking the client…", + "client.job.updating": "Updating the client", } as const; export default en; diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.ts index 958af70..7cb5269 100644 --- a/src/i18n/locales/es.ts +++ b/src/i18n/locales/es.ts @@ -773,6 +773,8 @@ const es: Partial> = { "set.searchAll": "Buscar en todos los ajustes por nombre o descripción…", "set.search": "Buscar ajustes…", "upd.current": "Estás al día (versión {v}).", + "client.job.checking": "Comprobando el cliente…", + "client.job.updating": "Actualizando el cliente", }; export default es; diff --git a/src/i18n/locales/fr.ts b/src/i18n/locales/fr.ts index 0622bda..f79e5db 100644 --- a/src/i18n/locales/fr.ts +++ b/src/i18n/locales/fr.ts @@ -773,6 +773,8 @@ const fr: Partial> = { "set.searchAll": "Rechercher dans tous les réglages par nom ou description…", "set.search": "Rechercher un réglage…", "upd.current": "Vous êtes à jour (version {v}).", + "client.job.checking": "Vérification du client…", + "client.job.updating": "Mise à jour du client", }; export default fr; diff --git a/src/i18n/locales/ru.ts b/src/i18n/locales/ru.ts index e3e4041..d61937c 100644 --- a/src/i18n/locales/ru.ts +++ b/src/i18n/locales/ru.ts @@ -773,6 +773,8 @@ const ru: Partial> = { "set.searchAll": "Поиск по всем настройкам: название или описание…", "set.search": "Поиск по настройкам…", "upd.current": "У вас последняя версия ({v}).", + "client.job.checking": "Проверка клиента…", + "client.job.updating": "Обновление клиента", }; export default ru; diff --git a/src/lib/clientJob.ts b/src/lib/clientJob.ts new file mode 100644 index 0000000..9f961f2 --- /dev/null +++ b/src/lib/clientJob.ts @@ -0,0 +1,114 @@ +import { useSyncExternalStore } from "react"; +import { listen } from "@tauri-apps/api/event"; +import { api, asUiError, type ClientPlan, type ClientStep, type UiError } from "@/lib/api"; +import { checkClient } from "@/lib/clientUpdate"; + +/** + * Updating the game client from the main screen, without a window over everything: the "Update client" button turns into + * a progress bar (as in a game launcher). Only a choice that needs the owner's word (files they changed) is shown, in + * place, below the buttons. The first-time linking of a client keeps its dialog (ClientDialog). + */ +export type ClientJobPhase = "idle" | "scanning" | "choose" | "working" | "done" | "current" | "stopped" | "failed"; + +export interface ClientJob { + phase: ClientJobPhase; + step: ClientStep | null; + plan: ClientPlan | null; + error: UiError | null; +} + +const IDLE: ClientJob = { phase: "idle", step: null, plan: null, error: null }; +const jobs = new Map(); +const listeners = new Set<() => void>(); +const cancelled = new Set(); + +const get = (id: string) => jobs.get(id) ?? IDLE; +function set(id: string, next: Partial) { + jobs.set(id, { ...get(id), ...next }); + listeners.forEach((l) => l()); +} + +let unlisten: (() => void) | null = null; +async function followProgress(id: string) { + if (unlisten) return; + try { + unlisten = await listen("client-progress", (e) => { + const phase = get(id).phase; + if (phase === "scanning" || phase === "working") set(id, { step: e.payload }); + }); + } catch { + /* no event bridge (a plain browser): the bar just stays at zero */ + } +} + +function fail(id: string, e: unknown) { + if (cancelled.has(id)) set(id, { phase: "stopped", step: null }); + else set(id, { phase: "failed", step: null, error: asUiError(e) }); +} + +async function refresh(id: string) { + await checkClient(id); +} + +/** Look at what differs from the published client; update at once when nothing needs a decision. */ +export async function startClientUpdate(id: string): Promise { + const now = get(id).phase; + if (now === "scanning" || now === "working") return; + cancelled.delete(id); + set(id, { phase: "scanning", step: null, plan: null, error: null }); + void followProgress(id); + try { + const plan = await api.clientPlan(id); + if (plan.items.length === 0) { + set(id, { phase: "current", plan, step: null }); + void refresh(id); + setTimeout(() => { + if (get(id).phase === "current") set(id, { phase: "idle" }); + }, 5000); + return; + } + if (plan.items.some((i) => i.kind === "modified")) { + set(id, { phase: "choose", plan, step: null }); + return; + } + await syncClient(id, true); + } catch (e) { + fail(id, e); + } +} + +/** Download what is missing or changed; `keepModified` keeps the files the owner changed. */ +export async function syncClient(id: string, keepModified: boolean): Promise { + cancelled.delete(id); + set(id, { phase: "working", step: null, error: null }); + void followProgress(id); + try { + await api.clientSync(id, keepModified); + set(id, { phase: "done", step: null }); + void refresh(id); + setTimeout(() => { + if (get(id).phase === "done") set(id, { phase: "idle" }); + }, 6000); + } catch (e) { + fail(id, e); + } +} + +export function stopClientJob(id: string) { + cancelled.add(id); + void api.clientCancel().catch(() => undefined); +} + +export function dismissClientJob(id: string) { + set(id, { phase: "idle", error: null, plan: null, step: null }); +} + +export function useClientJob(id: string): ClientJob { + return useSyncExternalStore( + (l) => { + listeners.add(l); + return () => listeners.delete(l); + }, + () => get(id), + ); +} diff --git a/src/screens/Overview.tsx b/src/screens/Overview.tsx index 9ce33dc..cab9a83 100644 --- a/src/screens/Overview.tsx +++ b/src/screens/Overview.tsx @@ -1,7 +1,8 @@ +import { dismissClientJob, startClientUpdate, stopClientJob, syncClient, useClientJob } from "@/lib/clientJob"; import { useCallback, useEffect, useRef, useState } from "react"; -import { Loader2 } from "lucide-react"; +import { Loader2, X } from "lucide-react"; import { api, asUiError, type ClientInfo, type Human, type Performance, type Population, type ServerSummary, type ServiceStatus, type StatusView } from "@/lib/api"; -import { cn, formatUptime } from "@/lib/utils"; +import { cn, formatBytes, formatUptime } from "@/lib/utils"; import { useHuman, useT } from "@/i18n"; import { applyServerUpdate, useServerUpdate } from "@/lib/serverUpdate"; import { checkClient, useClientStatus } from "@/lib/clientUpdate"; @@ -74,6 +75,10 @@ export function Overview({ server, companions = true, onForget, onOpenUpdates }: const [dialog, setDialog] = useState(null); const clientStatus = useClientStatus(server.id).status; const [playing, setPlaying] = useState(false); + const job = useClientJob(server.id); + const jobBusy = job.phase === "scanning" || job.phase === "working"; + const jobPct = job.step && job.step.total > 0 ? Math.min(100, Math.floor((job.step.done / job.step.total) * 100)) : 0; + const [keepMine, setKeepMine] = useState(true); const [perf, setPerf] = useState(null); const upd = useServerUpdate(server.id); const needsDecision = (upd.preview?.conflicts.length ?? 0) > 0; @@ -91,6 +96,10 @@ export function Overview({ server, companions = true, onForget, onOpenUpdates }: }, [server.id]); const alive = useRef(true); + useEffect(() => { + if (job.phase === "done" || job.phase === "current") void api.clientInfo(server.id).then(setClient).catch(() => undefined); + }, [job.phase, server.id]); + const poll = useCallback(async () => { try { const s = await api.status(server.id); @@ -279,8 +288,35 @@ export function Overview({ server, companions = true, onForget, onOpenUpdates }: )}
{client && } - {client && clientStatus?.update_available ? ( - +
+ ) : client && (clientStatus?.update_available || job.phase === "choose") ? ( + ) : client ? ( @@ -298,6 +334,34 @@ export function Overview({ server, companions = true, onForget, onOpenUpdates }: {anyUp && !running && !transitioning && (

{t("overview.partial")}

)} + {job.phase === "choose" && job.plan && ( +
+

{t("client.upd.modifiedTitle", { n: job.plan.items.filter((i) => i.kind === "modified").length })}

+

{t("client.upd.modifiedText")}

+ +
+ + +
+
+ )} + {job.phase === "done" &&

{t("client.upd.done")}

} + {job.phase === "current" &&

{t("client.upd.upToDate", { v: job.plan?.version ?? "" })}

} + {job.phase === "stopped" && ( +

+ {t("client.dl.cancelled")} + +

+ )} + {job.phase === "failed" && job.error && ( +

+ {job.error.human.code === "unknown" ? job.error.technical : human(job.error.human).message} + +

+ )} {upd.available && upd.preview && (

{t("overview.updateAvail", { to: upd.preview.to_version, from: upd.preview.from_version ?? "?" })}