|
| 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