Skip to content

Commit ef33ec5

Browse files
Version 1.0
1 parent a6d4729 commit ef33ec5

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
# CCResetButton
1+
# CCResetButton
2+
3+
Adds a button to restart the game without memory leaks
4+
5+
This currently only works on Windows, but can easily be updated for Mac and Linux.
6+
I just need someone to help with testing on those platforms.
7+
8+
## Install
9+
10+
Install [CCLoader](https://github.com/CCDirectLink/CCLoader)
11+
12+
[Download a release](https://github.com/bluecheetah001/CCResetButton/releases) and unpack into the CCLoader mods folder
13+
14+
To keep the window in the same location after restarting add the option `"id": "CrossCode"` to the `"window"` object in CrossCode's package.json.
15+
16+
## Usage
17+
18+
Press <kbd>L</kbd> to restart the game. This can be configured at the bottom of the Controls tab

mod.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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);

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Restart Button",
3+
"version": "1.0.0",
4+
"description": "Adds a button to restart the game without memory leaks",
5+
"main": "mod.js",
6+
"module": true,
7+
"dependencies": {
8+
"Simplify": "^2.2.0"
9+
}
10+
}

scripts/windows.bat

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@echo off
2+
REM up to 5 times at ~1 second intervals (though it almost always works first try)
3+
FOR /L %%a IN (1,1,5) DO (
4+
REM why does windows not have a sleep command?
5+
REM there is timeout, but it does not work in background processes
6+
ping 127.0.0.1 -n 2 > NUL
7+
8+
REM if CrossCode.exe is not running start it
9+
tasklist | find "CrossCode.exe" || (
10+
start CrossCode.exe
11+
goto :eof
12+
)
13+
)

0 commit comments

Comments
 (0)