|
| 1 | +// can be modified to deconflict with other mods |
| 2 | +const headerId = 'restart'; |
| 3 | +const restartId = 'restart'; |
| 4 | + |
| 5 | +const cp = nw.require('child_process'); |
| 6 | + |
| 7 | +function getScriptsDir() { |
| 8 | + for(const mod of window.activeMods) { |
| 9 | + if(mod.name === 'Restart Button') { |
| 10 | + return mod.baseDirectory + 'scripts/'; |
| 11 | + } |
| 12 | + } |
| 13 | + console.error('Failed to find Restart Button mod, did the name change?'); |
| 14 | + return null; |
| 15 | +} |
| 16 | + |
| 17 | +function getRestartProcessCmd() { |
| 18 | + const dir = getScriptsDir(); |
| 19 | + if(dir === null) return null; |
| 20 | + |
| 21 | + switch(process.platform) { |
| 22 | + case 'win32': |
| 23 | + // Theoretically doing something like this should work, but I could not get it to |
| 24 | + // cp.spawn(file, [], {shell:true, detached:true, windowsHide:true}) |
| 25 | + // github issue that may be relavent: https://github.com/nodejs/node/issues/21825 |
| 26 | + // instead I am using `start /B` as a workaround to get a hidden detached process |
| 27 | + return 'start /B ' + dir.replace(/\//g, '\\') + 'windows.bat'; |
| 28 | + default: |
| 29 | + console.error('Restarting the process is not supported for \''+process.platform+'\' systems yet.'); |
| 30 | + return null; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function initialize() { |
| 35 | + const cmd = getRestartProcessCmd(); |
| 36 | + if(cmd === null) return; |
| 37 | + |
| 38 | + // localization |
| 39 | + ig.lang.labels.sc.gui.options.controls.keys[restartId] = 'Restart'; |
| 40 | + ig.lang.labels.sc.gui.options.headers[headerId] = 'restart'; |
| 41 | + |
| 42 | + // add option |
| 43 | + const tab = 5; |
| 44 | + const defaultKey = 'L'.charCodeAt(0); |
| 45 | + const defaultKeys = {key1: defaultKey, key2: undefined}; |
| 46 | + simplify.options.addEntry('keys-'+restartId, 'CONTROLS', defaultKeys, tab, undefined, undefined, headerId); |
| 47 | + ig.input.bind(defaultKey, restartId); // have to manually bind default keys |
| 48 | + |
| 49 | + // reload options |
| 50 | + simplify.options.reload(); |
| 51 | + |
| 52 | + // listen for key press |
| 53 | + simplify.registerUpdate(() => { |
| 54 | + if(ig.input.state(restartId)) { |
| 55 | + cp.exec(cmd).unref(); |
| 56 | + nw.App.quit(); |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +document.body.addEventListener('modsLoaded', initialize); |
0 commit comments