-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodloader.js
More file actions
279 lines (225 loc) · 8.46 KB
/
modloader.js
File metadata and controls
279 lines (225 loc) · 8.46 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
console.debug("[TerraML] Initializing TerraML...");
window.terra ??= {};
terra.internal ??= {};
/** Run this function right before `startGame()` is called, but after webpack is initialized */
async function preGameStart() {
window.__webpack_modules__ = __webpack_require__.m;
try {
setupWebpackExports();
await loadMods();
} catch (err) {
console.error(`[TerraML] Unexpected error encountered':`, err);
}
}
terra.internal._pre_start = preGameStart;
function injectAndRunGame(code) {
terra.internal.bundle_src_original = code;
let injectSuccessful = false;
code = code.replace(/\(\(\)\s*=>\s*\{\s*\/\/\s*webpackBootstrap/, () => {
injectSuccessful = true;
return "( async () => { // webpackBootstrap";
});
// Export __webpack_require__ to be used outside of the webpack file
code = code.replace(/;\/\/\s*(?:\.\.\/)*terra\/js\/game\/index\.js/, (match) => {
injectSuccessful = true;
return match + `\nwindow.__webpack_require__ = __webpack_require__;\nawait terra.internal._pre_start();`;
});
if (!injectSuccessful) {
console.error("[TerraML] Failed to inject webpack!")
}
// Turn unused webpack module exports into real exports
// Enjoy more open modding capabilities!
code = code.replace(
/\/\*\s*unused harmony exports?\s*(.*?) \*\//g,
(match, names) => {
const additions = names.split(",")
.map(n => n.trim())
.map(n => `${n}: () => ${n}`)
.join(", ");
return `${match}\n__webpack_require__.d(__webpack_exports__, { ${additions} });`;
}
);
code = code.replace(
/\/\/\s*UNUSED EXPORTS:\s*(.*?)\s*$/gm,
(match, names) => {
const additions = names.split(",")
.map(n => n.trim())
.map(n => `${n}: () => ${n}`)
.join(", ");
return `${match}\n__webpack_require__.d(__webpack_exports__, { ${additions} });`;
}
);
code += "\n//# sourceURL=dist/bundle.js";
terra.internal.bundle_src = code;
console.debug("[TerraML] Webpack successfully injected!");
// Run the original game as ES module
const blob = new Blob([code], { type: "text/javascript" });
const url = URL.createObjectURL(blob);
import(url).finally(() => URL.revokeObjectURL(url));
}
/** Appends addon to the game's AddonManager */
function addAddon(addon, orders = {}) {
terra.export.g_addons.add({ addon, orders });
}
terra.addAddon = addAddon;
// Hook into 'bundle.js' script initialization to inject our modloader goodness
new MutationObserver(function (mutations, observer) {
for (const { addedNodes } of mutations) {
for (const node of addedNodes) {
if (node.tagName !== "SCRIPT") continue;
const src = node.getAttribute("src");
if (!src || !src.includes("bundle.js")) continue;
observer.disconnect();
node.remove();
fetch(src)
.then(r => r.text())
.then(code => injectAndRunGame(code));
}
}
}).observe(document.documentElement, { childList: true, subtree: true });
/** Parses webpack modules to expose game exports */
function setupWebpackExports() {
const fileExports = {}; // "player.js" -> { id, exports: { realName -> minKey }, result }
terra.internal.fileExports = fileExports;
const globalExports = {};
terra.internal.globalExports = globalExports;
// Parse all webpack modules
for (const [id, factory] of Object.entries(__webpack_modules__)) {
const moduleCode = factory.toString();
const fileMarkers = [];
for (const match of moduleCode.matchAll(/\/\/# sourceMappingURL=(.+?)\.map/g)) {
fileMarkers.push({ pos: match.index, file: match[1].split("/").pop() });
}
if (!fileMarkers.length) continue;
// Get all exports of a module
const _moduleExports = {};
for (const block of moduleCode.matchAll(/__webpack_require__\.d\(__webpack_exports__,\s*\{([\s\S]*?)\}\s*\)/g)) {
for (const [, minKey, realName] of block[1].matchAll(
/([\w$]+)\s*:\s*\(\s*\)\s*=>\s*\(?(?:\/\*[^*]*\*\/\s*)?([\w$]+)\)?/g
)) {
_moduleExports[realName] = minKey;
}
}
// Map exports to their specific source files for concatenated modules
const _fileExports = {};
if (fileMarkers.length === 1) {
_fileExports[fileMarkers[0].file] = { ..._moduleExports };
} else {
for (const [realName, minKey] of Object.entries(_moduleExports)) {
const match = new RegExp(`(?:var|let|const|function\\*?|class)\\s+${realName}[\\s=({]`).exec(moduleCode);
if (!match) continue;
const marker = fileMarkers.find(m => m.pos > match.index);
if (!marker) continue;
(_fileExports[marker.file] ??= {})[realName] = minKey;
}
}
const raw = __webpack_require__(id);
for (const marker of fileMarkers) {
const exports = _fileExports[marker.file] ?? _moduleExports;
// Build a live result object for this file
const result = {};
for (const [realName, minKey] of Object.entries(exports)) {
Object.defineProperty(result, realName, {
get: () => raw[minKey],
enumerable: true,
configurable: true
});
}
fileExports[marker.file] = { id, exports, result };
// Merge into global exports (first definition wins to avoid conflicts)
for (const [realName, minKey] of Object.entries(exports)) {
if (!Object.prototype.hasOwnProperty.call(globalExports, realName)) {
Object.defineProperty(globalExports, realName, {
get: () => raw[minKey],
enumerable: true,
configurable: true
});
}
}
}
}
// terra.export — flat access to every export across all modules
window.terra.export = globalExports;
// terra.file — camelCase per-file access, plain object
const getFile = {};
for (const [file, { result }] of Object.entries(fileExports)) {
const camel = file.replace(/\.js$/, "").replace(/[-_]./g, m => m[1].toUpperCase());
Object.defineProperty(getFile, camel, {
value: result,
enumerable: true,
configurable: true
});
}
window.terra.file = getFile;
console.debug("[TerraML] Webpack exports set up!");
}
/** Reads the directories and loads the mods one by one */
async function loadMods() {
const fs = require("fs");
const path = require("path");
const modDirectory = "./mods";
if (!fs.existsSync(modDirectory)) {
fs.mkdirSync(modDirectory);
}
let modManifests = [];
for (const folder of fs.readdirSync(modDirectory)) {
const folderPath = path.join(modDirectory, folder);
if (!fs.statSync(folderPath).isDirectory()) continue;
const manifestPath = path.join(folderPath, "manifest.json");
if (!fs.existsSync(manifestPath)) continue;
try {
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.name = manifest.name || folder;
manifest.main = manifest.main || "main.js";
manifest.path = path.resolve(folderPath, manifest.main);
modManifests.push(manifest);
} catch (err) {
console.warn(`[TerraML] Failed to load mod in ${folderPath}:`, err);
}
}
modManifests.sort((a, b) => (b.priority || 0) - (a.priority || 0));
console.log(`[TerraML] Found mods: ${modManifests.length}`);
let loadedMods = 0;
for (const manifest of modManifests) {
if (!fs.existsSync(manifest.path)) {
console.warn(`[TerraML] Failed to load mod ${manifest.name}: '${manifest.main}' does not exist`);
continue;
}
try {
console.debug(`[TerraML] Loading mod: ${manifest.name} (ver. ${manifest.version})`);
const fileUrl = "file://" + manifest.path.replace(/\\/g, "/");
await import(fileUrl);
console.debug(`[TerraML] Loaded: ${manifest.name}`);
loadedMods++;
} catch (err) {
console.warn(`[TerraML] Error in '${manifest.name}':`, err);
}
}
terra.internal.modManifests = modManifests;
terra.internal.totalMods = modManifests.length;
terra.internal.loadedMods = loadedMods;
// Add mod counter to title screen
let originalStartGame = terra.export.SceneManager.prototype.startGame;
terra.export.SceneManager.prototype.startGame = function (...args) {
const titleScreenHook = terra.export.g_gui.hooks.find(hook =>
hook.gui && hook.gui.constructor.name === "TitleScreen"
);
titleScreenHook.gui.version.textBlock.setText(`${titleScreenHook.gui.version.textBlock.decoratedText} | Mods: ${loadedMods}`);
return originalStartGame.apply(this, args);
}
console.log(`[TerraML] Loaded mods: ${loadedMods}`);
}
const Module = require('module');
const { fileURLToPath } = require('url');
const path = require('path');
window.makeRequire = (metaUrl) => {
const dir = path.dirname(fileURLToPath(metaUrl));
const localRequire = Module.createRequire(metaUrl);
return (id) => {
// Relative path → resolve from caller's directory
if (id.startsWith('.')) return localRequire(path.resolve(dir, id));
// Bare specifier (npm package or built-in) → normal resolution
return localRequire(id);
};
}
//# sourceURL=modloader.js