PLWL/src/openwith.js
2026-10-07 16:29:40 +02:00

81 lines
2.3 KiB
JavaScript

'use strict'
// "Open with PLWL": a file handed in via --open is parked here until the
// renderer picks it up, then launched ad hoc. Nothing is imported.
const fs = require('fs')
const path = require('path')
let pending = null // file path waiting for the picker (service not ready yet)
function setPending(p) {
pending = p
}
function getPending() {
return pending
}
function hasPending() {
return !!pending
}
function clearPending() {
pending = null
}
function textsFor(code) {
return require('./i18n').getLang(code).texts || {}
}
function resolveWine(id) {
return require('./wines').resolveWine(id)
}
function resolvePrefix(name) {
const defs = require('./prefixes').listPrefixes()
const known = name && defs.prefixes.some(p => p.name === name) ? name : ''
return known || defs.default || 'Main'
}
// Ad-hoc run target shaped like a library game; nothing persisted.
function makeTarget(openPath, wineId, prefixName) {
const dir = path.dirname(openPath)
return {
id: 'openwith',
name: path.basename(openPath),
mode: 'open',
path: dir,
exePath: openPath,
workdir: dir,
wine: wineId || 'default',
prefix: prefixName || '',
args: '',
dllOverrides: [],
disableUmu: false,
env: {},
iconSource: '',
playSeconds: 0,
created: 0,
lastPlayed: 0
}
}
// Launch without importing, exactly like a library app.
async function run({ openPath, wine: wineId, prefix: prefixName0, lang, onStarted }) {
if (!openPath || !fs.existsSync(openPath)) return { ok: false, error: 'file-missing' }
if (!/\.(exe|msi|bat|cmd|lnk)$/i.test(openPath)) return { ok: false, error: 'not-windows' }
const wine = resolveWine(wineId)
const prefixName = resolvePrefix(prefixName0)
const runner = require('./runner')
try {
// shared `openwith` id keeps logs/playtime away from library ids
const res = await runner.runAdHoc({ openPath, wine, systemWine: wine.type === 'system', prefix: prefixName })
if (onStarted) onStarted({ name: path.basename(openPath), pid: res.pid })
return { ok: true, pid: res.pid || null, logFile: res.logFile || null }
} catch (err) {
return { ok: false, error: String((err && err.message) || err) }
}
}
module.exports = { setPending, getPending, hasPending, clearPending, run, makeTarget, resolveWine, resolvePrefix, textsFor }