forked from emerladCoder/RPG-Maker-MV-Cheat-Menu-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cm-patcher.html
149 lines (131 loc) · 3.55 KB
/
cm-patcher.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<!-- DO NOT USE ADVANCED JS SYNTAX, KEEP IT AROUND ES5 -->
<!--suppress ES6ConvertVarToLetConst, EqualityComparisonWithCoercionJS, ExceptionCaughtLocallyJS -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PRG Maker Cheat Menu Patcher</title>
</head>
<body>
<div>
<h1>PRG Maker Cheat Menu Patcher</h1>
<button onclick="exit()">Exit</button>
<hr>
<div id="Logger">
Starting up... <br>
</div>
</div>
<script>
/**
* @typedef PluginDef
* @property {string} name
* @property {boolean} status
* @property {string} description
* @property {Object} parameters
*/
const EXTERNAL_PLUGIN_DEF_FILE_NAME = 'cm-patch.json';
/** @type PluginDef */
const DEFAULT_PLUGIN_DEF = { name: 'AsCheater', status: true, description: '', parameters: {} };
const MV_PLUGINS_JS = 'www/js/plugins.js';
const MZ_PLUGINS_JS = 'js/plugins.js';
/**
* @param {...string} msg
*/
function println(...msg) {
const logger = document.getElementById('Logger');
logger.innerHTML = logger.innerHTML + msg.join(' ') + '<br>';
}
function patch() {
var fs = require('fs');
/** @type PluginDef */
var pluginDef = DEFAULT_PLUGIN_DEF;
if (fs.existsSync(EXTERNAL_PLUGIN_DEF_FILE_NAME)) {
println('Found external plugin definition file...');
try {
const def = fs.readFileSync(EXTERNAL_PLUGIN_DEF_FILE_NAME, 'utf8');
pluginDef = JSON.parse(def);
println('Use patch content:', def);
} catch (e) {
const msg = 'Failed to parse external plugin definition file: ' + e.message;
println(msg);
alert(msg);
exit();
return;
}
}
var pluginJS = '';
if (fs.existsSync(MV_PLUGINS_JS)) {
println('Found MV plugins.js file...');
pluginJS = MV_PLUGINS_JS;
} else if (fs.existsSync(MZ_PLUGINS_JS)) {
println('Found MZ plugins.js file...');
pluginJS = MZ_PLUGINS_JS;
} else {
alert('Cannot find plugins.js file');
exit();
return;
}
/** @type PluginDef[] */
var plugins = [];
const pluginJsContent = fs.readFileSync(pluginJS, 'utf8');
try {
plugins = new Function(pluginJsContent + '\r\n\r\nreturn $plugins;')();
if (!(plugins instanceof Array)) {
throw new Error('plugins.js is not valid');
}
} catch (e) {
const msg = 'plugins.js is not valid, reinstall this game to restore: ' + e.message;
println(msg);
alert(msg);
exit();
return;
}
/** @type {'Patch' | 'Unpatch'} */
var operation;
const foundPlugins = plugins.filter(p => p.name == pluginDef.name);
if (foundPlugins.length > 0) {
println('This game has already been patched!');
if (confirm('This game has already been patched, unpatch this game?')) {
for (var i = 0; i < foundPlugins.length; i++) {
plugins.splice(plugins.indexOf(foundPlugins[i]), 1);
}
operation = 'Unpatch';
} else {
exit();
return;
}
} else {
operation = 'Patch';
plugins.push(pluginDef);
}
const backupFile = pluginJS + '~';
if (!fs.existsSync(backupFile)) {
fs.copyFileSync(pluginJS, backupFile);
}
var lines = [
'// Generated by PRG Maker Cheat Menu Patcher',
'',
'var $plugins = ' + JSON.stringify(plugins, null, '\t') + ';',
''
];
fs.writeFileSync(pluginJS, lines.join('\r\n'));
println(operation, 'successfully!');
setTimeout(function() {
alert(operation + ' successfully!');
exit();
}, 100);
}
function exit() {
if (confirm('Exit Patcher?')) {
window.close();
}
}
window.addEventListener('DOMContentLoaded', function() {
println('Ready to patch this game in 3 seconds...');
setTimeout(function() {
patch();
}, 3000);
});
</script>
</body>
</html>