fix (i hope) all bugs
This commit is contained in:
parent
e8cd664965
commit
201f9d7a7e
11 changed files with 195 additions and 37 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const { DIRS, readJson, writeJson } = require('./paths')
|
||||
const { DIRS, readJson, writeJson, toPortablePath, resolvePortablePath } = require('./paths')
|
||||
const { prefixPath } = require('./prefixes')
|
||||
|
||||
const REGISTRY = path.join(DIRS.DATA, 'apps.json')
|
||||
|
|
@ -32,14 +32,30 @@ function writePlaySeconds(id, seconds) {
|
|||
function loadGames() {
|
||||
const games = readJson(REGISTRY, [])
|
||||
for (const g of games) {
|
||||
// stored relative to FILES (portable) -> resolve to absolute for use
|
||||
g.path = resolvePortablePath(g.path)
|
||||
g.exePath = resolvePortablePath(g.exePath)
|
||||
if (g.workdir) g.workdir = resolvePortablePath(g.workdir)
|
||||
if (g.iconSource) g.iconSource = resolvePortablePath(g.iconSource)
|
||||
const ps = readPlaySeconds(g.id)
|
||||
if (ps != null) g.playSeconds = ps
|
||||
}
|
||||
return games
|
||||
}
|
||||
|
||||
// Paths of anything living inside FILES/ are stored RELATIVE to FILES so the
|
||||
// whole launcher folder can be moved (portable mode). Absolute paths outside
|
||||
// FILES (apps left in place) are kept as-is.
|
||||
function portableGame(g) {
|
||||
const out = { ...g }
|
||||
for (const k of ['path', 'exePath', 'workdir', 'iconSource']) {
|
||||
if (out[k]) out[k] = toPortablePath(out[k])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function saveGames(games) {
|
||||
writeJson(REGISTRY, games)
|
||||
writeJson(REGISTRY, games.map(portableGame))
|
||||
}
|
||||
|
||||
function getGame(id) {
|
||||
|
|
@ -94,9 +110,19 @@ function saveSettings(patch) {
|
|||
return s
|
||||
}
|
||||
|
||||
function newGame({ name, exePath, workdir, mode }) {
|
||||
// ID is the current timestamp (ms); while a game with that id or a folder
|
||||
// with that name exists, keep incrementing - so ids and import folder names
|
||||
// can never collide.
|
||||
function newGameId() {
|
||||
let id = Date.now().toString()
|
||||
const taken = () => getGame(id) || fs.existsSync(path.join(DIRS.APPS, id))
|
||||
while (taken()) id = String(Number(id) + 1)
|
||||
return id
|
||||
}
|
||||
|
||||
function newGame({ id, name, exePath, workdir, mode }) {
|
||||
return {
|
||||
id: 'g' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
|
||||
id: id || newGameId(),
|
||||
name,
|
||||
mode,
|
||||
path: workdir,
|
||||
|
|
@ -120,4 +146,4 @@ function resolvePrefixDir(game) {
|
|||
return prefixPath(defs.default || 'default')
|
||||
}
|
||||
|
||||
module.exports = { loadGames, saveGames, getGame, addGame, updateGame, removeGame, loadSettings, saveSettings, newGame, resolvePrefixDir, clearPlaytime, readPlaySeconds, writePlaySeconds, playtimeFile }
|
||||
module.exports = { loadGames, saveGames, getGame, addGame, updateGame, removeGame, loadSettings, saveSettings, newGame, newGameId, resolvePrefixDir, clearPlaytime, readPlaySeconds, writePlaySeconds, playtimeFile }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue