feat(saves): add local save selection and editing
This commit is contained in:
1 parent
2b1da24263
commit
073a907617
18 files changed
+634
-4
No files matched your search
@@ -0,0 +1,105 @@
|
||||
import { join } from 'node:path';
|
||||
import { utilityProcess } from 'electron';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import type { InstallRecord } from '@shared/schemas/install.ts';
|
||||
import type { CurrencyEdits, SaveDetails, SaveList, SaveRegistration } from '@shared/schemas/saves.ts';
|
||||
import type { SaveReply, SaveResult, SaveTask } from '../../workers/save-protocol.ts';
|
||||
import { maintenanceInProgress } from '../copies/copy-maintenance.ts';
|
||||
import { listCopies, updateCopyRecord } from '../install/install-record.ts';
|
||||
import { gameProcessRunning, launchInProgress } from '../launch/launch-controller.ts';
|
||||
import { labStatusUrl, readInstalledLab } from '../launch/lab-handoff.ts';
|
||||
import { forwardProcessOutput } from '../launch/process-output.ts';
|
||||
|
||||
let writing = false;
|
||||
|
||||
export function saveWriteInProgress(): boolean {
|
||||
return writing;
|
||||
}
|
||||
|
||||
async function requireCopy(id: string): Promise<InstallRecord> {
|
||||
const copy = (await listCopies()).copies.find((record) => record.id === id);
|
||||
if (!copy) throw new AppError('copy_not_found');
|
||||
return copy;
|
||||
}
|
||||
|
||||
function location(copy: InstallRecord) {
|
||||
return { gameFolder: copy.copyPath, dataFolder: copy.dataPath };
|
||||
}
|
||||
|
||||
export async function getSaveList(copyId: string): Promise<SaveList> {
|
||||
const copy = await requireCopy(copyId);
|
||||
const saves = await run({ operation: 'list', ...location(copy) }) as SaveList['saves'];
|
||||
const current = saves.find(({ userId }) => userId === copy.currentSaveId)?.userId ?? saves[0]?.userId ?? null;
|
||||
return { saves, current };
|
||||
}
|
||||
|
||||
export async function getSaveDetails(copyId: string, userId: string): Promise<SaveDetails> {
|
||||
const copy = await requireCopy(copyId);
|
||||
return await run({ operation: 'details', ...location(copy), userId }) as SaveDetails;
|
||||
}
|
||||
|
||||
export async function selectSave(copyId: string, userId: string): Promise<SaveList> {
|
||||
await getSaveDetails(copyId, userId);
|
||||
await updateCopyRecord(copyId, (record) => ({ ...record, currentSaveId: userId }));
|
||||
return getSaveList(copyId);
|
||||
}
|
||||
|
||||
export async function createSave({ copyId, login, password, nick }: SaveRegistration): Promise<SaveList> {
|
||||
return write(async () => {
|
||||
const copy = await requireCopy(copyId);
|
||||
const { userId } = await run({ operation: 'create', ...location(copy), login, password, nick }) as { userId: string };
|
||||
await updateCopyRecord(copyId, (record) => ({ ...record, currentSaveId: userId }));
|
||||
return getSaveList(copyId);
|
||||
});
|
||||
}
|
||||
|
||||
export async function setSaveCurrencies(copyId: string, userId: string, values: CurrencyEdits): Promise<SaveDetails> {
|
||||
return write(async () => {
|
||||
const copy = await requireCopy(copyId);
|
||||
await run({ operation: 'edit', ...location(copy), userId, values });
|
||||
return getSaveDetails(copyId, userId);
|
||||
});
|
||||
}
|
||||
|
||||
async function write<T>(operation: () => Promise<T>): Promise<T> {
|
||||
if (writing || launchInProgress() || maintenanceInProgress()) throw new AppError('save_read_only');
|
||||
writing = true;
|
||||
try {
|
||||
if (await gameProcessRunning(AbortSignal.timeout(4000)) || await labServerRunning()) {
|
||||
throw new AppError('save_read_only');
|
||||
}
|
||||
return await operation();
|
||||
} finally {
|
||||
writing = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function labServerRunning(): Promise<boolean> {
|
||||
const lab = await readInstalledLab().catch(() => null);
|
||||
if (!lab) return false;
|
||||
try {
|
||||
const response = await fetch(labStatusUrl(lab.port), { signal: AbortSignal.timeout(600) });
|
||||
return response.ok && (await response.json() as { app?: string }).app === 'wto-server';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function run(task: SaveTask): Promise<SaveResult> {
|
||||
const child = utilityProcess.fork(join(import.meta.dirname, 'save-worker.js'), [], { serviceName: 'WTOffline save management', stdio: 'pipe' });
|
||||
forwardProcessOutput(child.stdout, 'saves', 'info');
|
||||
forwardProcessOutput(child.stderr, 'saves', 'error');
|
||||
try {
|
||||
return await new Promise<SaveResult>((resolve, reject) => {
|
||||
child.once('exit', () => reject(new AppError('save_operation_failed')));
|
||||
child.once('message', (reply: SaveReply) => {
|
||||
if (reply.ok) resolve(reply.result);
|
||||
else reject(new AppError(reply.error));
|
||||
});
|
||||
child.postMessage(task);
|
||||
});
|
||||
} finally {
|
||||
child.removeAllListeners('exit');
|
||||
child.kill();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user