Skip to content

Commit

Permalink
build bash start game script
Browse files Browse the repository at this point in the history
  • Loading branch information
hitman249 committed Nov 2, 2023
1 parent a8b12ac commit 5d15670
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 18 deletions.
82 changes: 78 additions & 4 deletions src/src/modules/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,15 @@ export default class Icon {
let startBy = null === autostart ? '' : ` --autostart ${autostart}`;
let wlHide = true === hide && Boolean(startBy) ? ' --hide' : '';

let exec = `"${binDir}/start" --game ${this.code}${startBy}${wlHide}`;

if (hide) {
exec = `"${binDir}/${this.code}.sh"`;
}

return `[Desktop Entry]
Version=1.0
Exec="${binDir}/start" --game ${this.code}${startBy}${wlHide}
Exec=${exec}
Path=${binDir}
Icon=${png}
Name=${this.title}
Expand Down Expand Up @@ -186,19 +192,20 @@ Categories=Game`;
* @param {boolean} desktop
* @param {string|null} autostart
* @param {boolean} hide
* @return {boolean}
* @return {Promise<boolean>}
*/
create(menu = true, desktop = true, autostart = null, hide = false) {
let png = this.getIcon();

if (!png) {
return false;
return Promise.resolve(false);
}

let result = false;
let template = this.getTemplate(png, autostart, hide);
let appsDir = this.findApplicationsDir();
let iconsDir = this.findIconsDir();
let binDir = this.appFolders.getBinDir();

if (appsDir && menu) {
result = true;
Expand All @@ -213,7 +220,22 @@ Categories=Game`;
this.fs.chmod(file);
}

return result;
if (hide) {
return this.buildBash(Boolean(autostart) ? autostart : undefined).then((bash) => {
const file = `${binDir}/${this.code}.sh`;

if (this.fs.exists(file)) {
this.fs.rm(file);
}

this.fs.filePutContents(file, bash);
this.fs.chmod(file);

return true;
}, () => false);
}

return Promise.resolve(result);
}

/**
Expand Down Expand Up @@ -304,4 +326,56 @@ Categories=Game`;

return this.extractIcon();
}

/**
* @return {Promise<string>}
*/
buildBash(mode = 'standard') {
return window.app.createTask(this.config).getCmd(mode).then((cmd) => {
return `#!/usr/bin/env bash
cd -P -- "$(dirname -- "$0")" || exit
# wl root dir
cd ..
WINE_DIR="${this.appFolders.getWineDir()}"
GAMES_DIR="${this.appFolders.getGamesDir()}"
SQUASHFUSE="${this.appFolders.getSquashfuseFile()}"
unmount() {
PATH_MOUNT_DIR="$1"
PATH_SQUASHFS="$1.squashfs"
if [[ -e "$PATH_MOUNT_DIR" ]] && [[ -e "$PATH_SQUASHFS" ]]; then
fusermount -u "$PATH_MOUNT_DIR" || true
rm -rf "$PATH_MOUNT_DIR" || true
fi
}
mount() {
PATH_MOUNT_DIR="$1"
PATH_SQUASHFS="$1.squashfs"
unmount "$PATH_MOUNT_DIR"
if [[ ! -e "$PATH_MOUNT_DIR" ]] && [[ -e "$PATH_SQUASHFS" ]]; then
mkdir "$PATH_MOUNT_DIR"
"$SQUASHFUSE" "$PATH_SQUASHFS" "$PATH_MOUNT_DIR"
fi
}
mount "$WINE_DIR"
mount "$GAMES_DIR"
# START GAME
${cmd}
# STOPPED GAME
unmount "$WINE_DIR"
unmount "$GAMES_DIR"`;
});
}
}
44 changes: 44 additions & 0 deletions src/src/modules/start-game-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

cd -P -- "$(dirname -- "$0")" || exit

# wl root dir
cd ..

WINE_DIR="${this.appFolders.getWineDir()}"
GAMES_DIR="${this.appFolders.getGamesDir()}"
SQUASHFUSE="${this.appFolders.getSquashfuseFile()}"

unmount() {
PATH_MOUNT_DIR="$1"
PATH_SQUASHFS="$1.squashfs"

if [[ -e "$PATH_MOUNT_DIR" ]] && [[ -e "$PATH_SQUASHFS" ]]; then
fusermount -u "$PATH_MOUNT_DIR" || true
rm -rf "$PATH_MOUNT_DIR" || true
fi
}

mount() {
PATH_MOUNT_DIR="$1"
PATH_SQUASHFS="$1.squashfs"

unmount "$PATH_MOUNT_DIR"

if [[ ! -e "$PATH_MOUNT_DIR" ]] && [[ -e "$PATH_SQUASHFS" ]]; then
mkdir "$PATH_MOUNT_DIR"
"$SQUASHFUSE" "$PATH_SQUASHFS" "$PATH_MOUNT_DIR"
fi
}

mount "$WINE_DIR"
mount "$GAMES_DIR"

# START GAME

${cmd}

# STOPPED GAME

unmount "$WINE_DIR"
unmount "$GAMES_DIR"
39 changes: 28 additions & 11 deletions src/src/modules/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ export default class Task {
/**
* @return {Promise}
*/
run(mode = 'standard', spawn = () => null) {
run(mode = 'standard', spawn = () => null, returnCmd = false) {
let wine = window.app.getKernel().clone();

let promise = Promise.resolve();
let logFile = `${this.appFolders.getLogsDir()}/${this.config.getGameName()}.log`;

if (this.fs.exists(logFile)) {
if (!returnCmd && this.fs.exists(logFile)) {
this.fs.rm(logFile);
}

Expand Down Expand Up @@ -156,14 +156,16 @@ export default class Task {
winePrefix.updatePulse();
winePrefix.updateCsmt();

let gamepads = window.app.getGamepads();
if (!returnCmd) {
let gamepads = window.app.getGamepads();

gamepads.changeConfig(this.config);
gamepads.stubPressEvents(this.config.isDisabledGamepads());
gamepads.changeConfig(this.config);
gamepads.stubPressEvents(this.config.isDisabledGamepads());

this.monitor.save();
this.monitor.save();

api.commit(action.get('logs').CLEAR);
api.commit(action.get('logs').CLEAR);
}

let runner;

Expand All @@ -175,10 +177,25 @@ export default class Task {

return runner.then((cmd) => window.app.createWineCommand(wine, this.config)
.watch(cmd, output => {
api.commit(action.get('logs').APPEND, output);
this.fs.filePutContents(logFile, output, this.fs.FILE_APPEND);
}, spawn, true))
.then(() => this.monitor.restore());
if (!returnCmd) {
api.commit(action.get('logs').APPEND, output);
this.fs.filePutContents(logFile, output, this.fs.FILE_APPEND);
}
}, spawn, true, false, returnCmd))
.then((cmd) => {
if (!returnCmd) {
this.monitor.restore();
}

return cmd;
});
});
}

/**
* @return {Promise<string>}
*/
getCmd(mode = 'standard') {
return this.run(mode, () => null, true);
}
}
2 changes: 1 addition & 1 deletion src/src/modules/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fs = require('fs');

export default class Update {

version = '1.5.23';
version = '1.5.24';

/**
* @type {string}
Expand Down
10 changes: 8 additions & 2 deletions src/src/modules/wine-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ export default class WineCommand extends Command {
* @param {Function} spawnObject
* @param {boolean} useExports
* @param {boolean} watchProcess
* @param {boolean} returnCmd
* @returns {Promise}
*/
watch(cmd, callable = () => null, spawnObject = () => null, useExports = false, watchProcess = false) {
watch(cmd, callable = () => null, spawnObject = () => null, useExports = false, watchProcess = false, returnCmd = false) {
return new Promise((resolve) => {
let runCmd = this.cast(cmd, useExports);

if (returnCmd) {
return resolve(runCmd);
}

callable(`[Wine Launcher] Run command:\n${runCmd}\n\n`, 'stdout');

let watch = child_process.spawn('sh', [ '-c', runCmd ], { detached: useExports });
Expand Down Expand Up @@ -138,7 +144,7 @@ export default class WineCommand extends Command {
}
}

return resolve();
return resolve(runCmd);
};

watch.stdout.on('data', (data) => callable(data.toString(), 'stdout'));
Expand Down

0 comments on commit 5d15670

Please sign in to comment.