48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import { app } from 'electron'
|
|
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
|
|
import { join } from 'path'
|
|
|
|
export interface CustomGame {
|
|
id: string
|
|
name: string
|
|
exe: string
|
|
args: string[]
|
|
image: string
|
|
category: string
|
|
source: 'custom'
|
|
}
|
|
|
|
interface GamesConfig {
|
|
games: CustomGame[]
|
|
}
|
|
|
|
function getConfigPath(): string {
|
|
const dir = app.getPath('userData')
|
|
return join(dir, 'games.json')
|
|
}
|
|
|
|
export function readCustomGames(): CustomGame[] {
|
|
const path = getConfigPath()
|
|
if (!existsSync(path)) return []
|
|
|
|
try {
|
|
const raw = readFileSync(path, 'utf-8')
|
|
const data = JSON.parse(raw) as GamesConfig
|
|
return Array.isArray(data.games) ? data.games : []
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
export function writeCustomGames(games: CustomGame[]): void {
|
|
const path = getConfigPath()
|
|
const dir = path.substring(0, path.lastIndexOf(require('path').sep))
|
|
|
|
if (!existsSync(dir)) {
|
|
mkdirSync(dir, { recursive: true })
|
|
}
|
|
|
|
const data: GamesConfig = { games }
|
|
writeFileSync(path, JSON.stringify(data, null, 2), 'utf-8')
|
|
}
|