Skip to content

Commit 3ece6ca

Browse files
authored
Add compatability with CCLoader2 & Simplify globals (#22)
* Add compatability with CCLoader2 & Simplify globals * Fix legacy asset paths in manifest * Run the formatter * Apply suggestions
1 parent ff9cf33 commit 3ece6ca

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

packages/core/src/modloader.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ export async function boot(): Promise<void> {
5656

5757
let virtualPackages = new Map<ModID, semver.SemVer>()
5858
.set('crosscode', gameVersion)
59-
.set('ccloader', modloaderMetadata.version);
59+
.set('ccloader', modloaderMetadata.version)
60+
.set('Simplify', new semver.SemVer('2.14.2'));
6061
if (typeof process !== 'undefined') {
6162
virtualPackages.set('nw', new semver.SemVer(process.versions.nw!));
6263
}
@@ -178,6 +179,9 @@ export async function boot(): Promise<void> {
178179
console.log("stage 'poststart' reached!");
179180
await executeStage(loadedMods, 'poststart');
180181

182+
// NOTE: LEGACY CCLOADER2 EVENT
183+
document.body.dispatchEvent(new Event('modsLoaded', { bubbles: true }));
184+
181185
activeDelegateFn();
182186
console.log('crosscode with mods is now fully loaded!');
183187
}
@@ -304,6 +308,20 @@ async function loadModMetadata(baseDirectory: string): Promise<Mod | null> {
304308
} else {
305309
manifestData = manifestData as Manifest;
306310
manifestValidator.validate(manifestData);
311+
312+
// NOTE: During the migration period of legacy mods to the new format,
313+
// their `package.json`s were converted to `ccmod.json`. The problem is
314+
// that apparently nobody involved in this process (even the ones who
315+
// implemented this detail) didn't remember that in the new format, paths
316+
// are assumed to be within `assets/`. So, what happened? The `assets`
317+
// array from `package.json` was copied 1:1 to `ccmod.json`, meaning that
318+
// there are now `ccmod.json`s with paths in `assets` that start with
319+
// `assets/`. This is resolved exclusively for legacy manifests
320+
// (obviously) by the `convertFromLegacy` call in the `if` block above,
321+
// but not for `ccmod.json`s that were incorrectly migrated. Instead of
322+
// going through the hassle of getting those mods updated *again*, I'll
323+
// just fix it once and for all.
324+
manifestData.assets = manifestData.assets?.map((e) => e.replace(/^assets\//, ''));
307325
}
308326
} catch (err) {
309327
if (utils.errorHasMessage(err)) {

packages/runtime/src/_main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default class RuntimeModMainClass implements modloader.Mod.PluginClass {
3333
moduleCache,
3434
resources,
3535
});
36+
37+
// There's probably an issue with this being a non-awaited import.
38+
import('./fixes/ccloader2-polyfill.js');
3639
}
3740

3841
public onImpactInit(): void {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* eslint-disable consistent-return */
2+
// This may be one of the worst pieces of this codebase. Please don't judge too heavily!
3+
4+
import semver from '@ccloader3/common/vendor-libs/semver';
5+
import type { LocalizedString } from 'ultimate-crosscode-typedefs/file-types/mod-manifest';
6+
7+
interface LegacyMod {
8+
baseDirectory: string;
9+
name: LocalizedString;
10+
displayName: LocalizedString;
11+
}
12+
13+
const Plugin = class Plugin {
14+
public preload(): void {}
15+
public postload(): void {}
16+
public prestart(): void {}
17+
public main(): void {}
18+
};
19+
20+
// The de-facto Simplify Compat Layer
21+
// <https://github.com/CCDirectLink/CCLoader/blob/0b23512543bf85ef21757b36492f991c03a16ef7/assets/mods/simplify/mod.js>
22+
const _simplify = {
23+
version: '2.14.2',
24+
updateHandlers: [] as Array<() => void>,
25+
26+
registerUpdate(handler?: () => void) {
27+
if (handler && typeof handler === 'function') {
28+
this.updateHandlers.push(handler);
29+
}
30+
},
31+
// technically never called by us, but let's keep it in just to be sure.
32+
fireUpdate() {
33+
for (const handler of this.updateHandlers) {
34+
handler();
35+
}
36+
},
37+
38+
getMod(name: string): LegacyMod | undefined {
39+
const mod = modloader.loadedMods.get(name);
40+
if (!mod) return;
41+
42+
return {
43+
baseDirectory: mod.baseDirectory,
44+
name: mod.id,
45+
displayName: mod.manifest.title || mod.id,
46+
};
47+
},
48+
49+
// seriously? :harold:
50+
jumpHigh() {
51+
ig.game.playerEntity.doJump(185, 16, 100);
52+
},
53+
};
54+
55+
// For some reason this was just an alias to `window`????
56+
const cc = window;
57+
const simplify = new Proxy(_simplify, {
58+
get(target, p) {
59+
// This is honestly just vile
60+
const prop = target[p as keyof typeof _simplify];
61+
p = String(p);
62+
63+
// Should it ever occur that I've forgotten something in the compat layer, let the user know.
64+
if (!prop) {
65+
console.warn(new Error(`Couldn't find '${p}' in Simplify compat layer. Please report!`));
66+
return;
67+
}
68+
69+
console.debug(new Error(`Simplify property '${p}' requested. This is deprecated.`));
70+
return prop;
71+
},
72+
});
73+
74+
Object.assign(window, {
75+
cc,
76+
simplify,
77+
Plugin,
78+
semver,
79+
80+
versions: Array.from(modloader.installedMods.values()).reduce<Record<string, string>>(
81+
(acc, mod) => {
82+
if (!mod.version) {
83+
console.debug('Mod without a version detected in construction of window.versions');
84+
return acc;
85+
}
86+
acc[mod.id] = mod.version.toString();
87+
return acc;
88+
},
89+
{},
90+
),
91+
activeMods: [...modloader.loadedMods.values()],
92+
inactiveMods: [...modloader.installedMods.values()].filter(
93+
(mod) => !modloader.modDataStorage.isModEnabled(mod.id),
94+
),
95+
});
96+
97+
// For some reason these were distinct events despite the fact that they fire
98+
// right after eachother. After the `main` stage ran, `modsLoaded` was
99+
// dispatched, while Simplify was already listening for it, causing it to
100+
// trigger immediately. Strange decision to even include `simplifyInitialized`.
101+
document.body.addEventListener('modsLoaded', () => {
102+
document.body.dispatchEvent(new Event('simplifyInitialized', { bubbles: true }));
103+
});

0 commit comments

Comments
 (0)