Skip to content

Commit

Permalink
attempt to add sudo remove to reset dolphin
Browse files Browse the repository at this point in the history
this actually failed but it's fine to merge for now, can fix it later
  • Loading branch information
JLaferri committed Jul 16, 2020
1 parent 9c55da7 commit 722ceb8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
18 changes: 9 additions & 9 deletions app/actions/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const SELECT_FILE = 'SELECT_FILE';
export const ISO_VALIDATION_START = 'ISO_VALIDATION_START';
export const ISO_VALIDATION_COMPLETE = 'ISO_VALIDATION_COMPLETE';
export const SET_RESET_CONFIRM = 'SET_RESET_CONFIRM';
export const RESETTING_DOLPHIN = 'RESETTING_DOLPHIN'
export const RESETTING_DOLPHIN = 'RESETTING_DOLPHIN';

async function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -67,7 +67,7 @@ export function browseFile(field) {
// Maybe this should be done as some kind of callback or something... but this works
if (field === "isoPath") {
validateISO()(dispatch, getState);
getState().dolphinManager.setGamePath(filePath)
getState().dolphinManager.setGamePath(filePath);
}
};
}
Expand Down Expand Up @@ -102,7 +102,7 @@ export function validateISO() {
} catch (err) {
// Do nothing
}

if (!fileStats) {
dispatch({
type: ISO_VALIDATION_COMPLETE,
Expand All @@ -120,7 +120,7 @@ export function validateISO() {
});
return;
}

const hash = crypto.createHash('sha1');
const input = fs.createReadStream(isoPath);

Expand Down Expand Up @@ -151,12 +151,12 @@ export function validateISO() {
if (data) {
hash.update(data);
return;
}
}

// Reading complete, check hash
const resultHash = hash.digest('hex');
const isValidISO = _.get(ISOHashes, resultHash) || "unknown";

isoStateLocalCache[cacheKey] = isValidISO;

dispatch({
Expand Down Expand Up @@ -190,17 +190,17 @@ export function resetDolphin() {
await wait(10);
try {
const dolphinManager = getState().settings.dolphinManager;
dolphinManager.resetDolphin();
await dolphinManager.resetDolphin();
const meleeFile = electronSettings.get('settings.isoPath');
dolphinManager.setGamePath(meleeFile);
} catch(err) {
} catch (err) {
log.info("Dolphin could not be reset");
log.warn(err.message);
const errorAction = displayError(
'settings-global',
`Dolphin could not be reset. ${err.message}`,
);

dispatch(errorAction);
}
dispatch({
Expand Down
33 changes: 28 additions & 5 deletions app/domain/DolphinManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ini from 'ini';
import electronSettings from 'electron-settings';

import { getDolphinPath } from '../utils/settings';
import { sudoRemovePath } from '../utils/sudoExec';

const { app } = require('electron').remote;

Expand Down Expand Up @@ -53,16 +54,38 @@ export default class DolphinManager {
await this.runDolphin(false);
}

resetDolphin() {
async resetDolphin() {
const appPath = app.getAppPath();
const originalDolphinPath = path.join(appPath, "../app.asar.unpacked/app/dolphin");
log.info("Resetting dolphin");
const userDataPath = app.getPath("userData");
const targetPath = path.join(userDataPath, 'dolphin');
log.info("Overwriting dolphin");
fs.removeSync(targetPath);
fs.copySync(originalDolphinPath, targetPath);
log.info("Dolphin was reset");

let isCopySuccess = false;
try {
fs.removeSync(targetPath);
fs.copySync(originalDolphinPath, targetPath);
log.info("Dolphin was reset");
isCopySuccess = true;
} catch (err) {
log.error("Failed to reset Dolphin, will try again with elevated permissions");
}

if (!isCopySuccess) {
try {
// TODO: This doesn't actually work, the UAC prompt never shows up. Might need to use
// TODO: ipc to trigger it in the main process? But I'm too lazy right now
await sudoRemovePath(targetPath);
fs.copySync(originalDolphinPath, targetPath);
log.info("Dolphin was reset");
isCopySuccess = true;
} catch (err) {
log.error("Failed to reset Dolphin, will try again with elevated permissions");
log.error(err);
throw new Error("Failed to reset Dolphin. You may need to reinstall the desktop app.");
}
}
}

setGamePath(filePath) {
Expand Down Expand Up @@ -93,7 +116,7 @@ export default class DolphinManager {
const newINI = ini.encode(dolphinINI);
fs.writeFileSync(iniPath, newINI);
} catch (err) {
log.warn(`Failed to update the dolphin paths\n${err}`)
log.warn(`Failed to update the dolphin paths\n${err}`);
throw err;
}
}
Expand Down
15 changes: 2 additions & 13 deletions app/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import fs from 'fs-extra';
import ini from 'ini';
import semver from 'semver';
import MenuBuilder from './menu';
import {sudoExecAsyncNonWindows, sudoExecAsyncWindows} from './utils/sudoExec';
import { sudoRemovePath } from './utils/sudoExec';

// Set up AppUpdater
log.transports.file.level = 'info';
Expand All @@ -36,10 +36,6 @@ const appDataPath = app.getPath("appData");
const isProd = process.env.NODE_ENV === 'production';
const isDev = process.env.NODE_ENV === "development";

const sudoOptions = {
name: "Slippi Desktop App",
};

let mainWindow = null;
let didFinishLoad = false;

Expand Down Expand Up @@ -128,14 +124,7 @@ const handlePreloadLogic = async () => {

try {
log.info("Copying dolphin instance...");
switch (platform) {
case "win32": // windows
await sudoExecAsyncWindows(`rmdir /Q /S "${targetPath}"`);
break;
default:
await sudoExecAsyncNonWindows("rm -rf \"$DOLPHIN_PATH\"", { ...sudoOptions, env: { DOLPHIN_PATH: targetPath } });
}

await sudoRemovePath(targetPath);
fs.copySync(originalDolphinPath, targetPath);
isCopySuccess = true;
} catch (ex) {
Expand Down
25 changes: 20 additions & 5 deletions app/utils/sudoExec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ import sudo from 'sudo-prompt';
import path from 'path';
import { exec } from 'child_process';

const sudoOptions = {
name: "Slippi Desktop App",
};

export async function sudoRemovePath(pathToRemove) {
const platform = process.platform;
switch (platform) {
case "win32": // windows
await sudoExecAsyncWindows(`rmdir /Q /S "${pathToRemove}"`);
break;
default:
await sudoExecAsyncNonWindows("rm -rf \"$DOLPHIN_PATH\"", { ...sudoOptions, env: { DOLPHIN_PATH: pathToRemove } });
}
}

export function sudoExecAsyncNonWindows(command, options) {
return new Promise((resolve, reject) => {
sudo.exec(
command,
command,
options,
(error) => {
if (
Expand All @@ -18,8 +33,8 @@ export function sudoExecAsyncNonWindows(command, options) {
} else {
reject(new Error(`Could not run elevated command: ${error}`));
}
})
})
});
});
}

export function sudoExecAsyncWindows(command) {
Expand All @@ -38,6 +53,6 @@ export function sudoExecAsyncWindows(command) {
} else {
reject(new Error(`Could not run elevated command: ${error}`));
}
})
})
});
});
}

0 comments on commit 722ceb8

Please sign in to comment.