From 65cc706d2bb7b9bb9a843e8093973a86a5866417 Mon Sep 17 00:00:00 2001 From: krypek Date: Tue, 26 Nov 2024 17:56:05 +0100 Subject: [PATCH] Rename the global namespace to window.instanceinator --- src/instance.ts | 34 +++++++++---------- src/plugin.ts | 87 +++++++++++++++++++++++++------------------------ src/tests.ts | 34 +++++++++---------- src/tiler.ts | 28 ++++++++-------- 4 files changed, 92 insertions(+), 91 deletions(-) diff --git a/src/instance.ts b/src/instance.ts index 580b2f0..04efe6e 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -1,22 +1,22 @@ import { getDisplayInstances } from './tiler' type SetFunc = (name: string, to?: any) => void -export class Instance { +export class InstanceinatorInstance { private static instanceIdCounter = 0 public static resetInstanceIdCounter() { - if (Object.keys(inst.instances).length != 0) - throw new Error('inst.instances need to be empty when calling resetInstanceIdCounter!') + if (Object.keys(instanceinator.instances).length != 0) + throw new Error('instanceinator.instances need to be empty when calling resetInstanceIdCounter!') this.instanceIdCounter = 0 } - static currentReference(name?: string, display?: boolean): Instance { - return new Instance(ig, sc, name, display) + static currentReference(name?: string, display?: boolean): InstanceinatorInstance { + return new InstanceinatorInstance(ig, sc, name, display) } public static displayInstanceId: boolean = true - static async copy(s: Instance, name?: string, display?: boolean): Promise { + static async copy(s: InstanceinatorInstance, name?: string, display?: boolean): Promise { const ig: any = {} const igToInit: string[] = [] for (const key in s.ig) { @@ -92,7 +92,7 @@ export class Instance { scset('skilltree') scset('version') - const ns = new Instance(ig, sc, name, display) + const ns = new InstanceinatorInstance(ig, sc, name, display) ns.apply() const canvasId = `canvas${ns.id}` @@ -113,7 +113,7 @@ export class Instance { igset( 'system', - new inst.classes.System( + new instanceinator.gameClasses.System( '#' + canvasId, '#' + gameId, s.ig.system.fps, @@ -198,7 +198,7 @@ export class Instance { // ig.ready = true - ig.mainLoader = new sc.StartLoader(inst.classes.CrossCode) + ig.mainLoader = new sc.StartLoader(instanceinator.gameClasses.CrossCode) ig.mainLoader.load() await new Promise(res => { @@ -222,8 +222,8 @@ export class Instance { public name: string = 'default', public display: boolean = true ) { - this.id = Instance.instanceIdCounter - Instance.instanceIdCounter++ + this.id = InstanceinatorInstance.instanceIdCounter + InstanceinatorInstance.instanceIdCounter++ } apply() { @@ -231,7 +231,7 @@ export class Instance { global.ig = window.ig = this.ig // @ts-expect-error global.sc = window.sc = this.sc - inst.instanceId = this.id + instanceinator.instanceId = this.id } } @@ -272,10 +272,10 @@ export function injectInstance() { }, draw() { this.parent() - if (!Instance.displayInstanceId || getDisplayInstances().length <= 1) return + if (!InstanceinatorInstance.displayInstanceId || getDisplayInstances().length <= 1) return const text = new ig.TextBlock( sc.fontsystem.font, - `#${inst.instanceId} ${inst.instances[inst.instanceId]?.name ?? 'initializing...'}`, + `#${instanceinator.instanceId} ${instanceinator.instances[instanceinator.instanceId]?.name ?? 'initializing...'}`, {} ) text.draw(2, 0) @@ -285,11 +285,11 @@ export function injectInstance() { ig.Loader.inject({ init(gameClass) { this.parent(gameClass) - this.instanceId = inst.instanceId + this.instanceId = instanceinator.instanceId }, finalize() { - if (this.instanceId != inst.instanceId) { - inst.instances[this.instanceId].ig.game.scheduledTasks.push(() => { + if (this.instanceId != instanceinator.instanceId) { + instanceinator.instances[this.instanceId].ig.game.scheduledTasks.push(() => { this.finalize() }) } else { diff --git a/src/plugin.ts b/src/plugin.ts index 0372263..bb86127 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,22 +1,13 @@ import { PluginClass } from 'ultimate-crosscode-typedefs/modloader/mod' import type {} from 'crossnode/crossnode.d.ts' import { Mod1 } from './types' -import { injectInstance, Instance } from './instance' +import { injectInstance, InstanceinatorInstance } from './instance' import { injectTiling } from './tiler' export default class CCInstanceinator implements PluginClass { static dir: string static mod: Mod1 - instanceId: number = 0 - instances: Record = {} - - classes!: { - System: ig.SystemConstructor - CrossCode: sc.CrossCodeConstructor - } - Instance: typeof Instance - constructor(mod: Mod1) { CCInstanceinator.dir = mod.baseDirectory CCInstanceinator.mod = mod @@ -24,13 +15,11 @@ export default class CCInstanceinator implements PluginClass { CCInstanceinator.mod.isCCModPacked = mod.baseDirectory.endsWith('.ccmod/') if (!CCInstanceinator.mod.isCCL3) Object.assign(mod, { id: CCInstanceinator.mod.name }) - this.Instance = Instance - - global.inst = window.inst = this + global.instanceinator = window.instanceinator = new Instanceinator() } async prestart() { - this.classes = { + instanceinator.gameClasses = { System: ig.System.extend({ startRunLoop() {}, }), @@ -44,52 +33,64 @@ export default class CCInstanceinator implements PluginClass { }), } - // ig.System.inject({ - // run() { - // const instances = Object.values(inst.instances).sort((a, b) => a.id - b.id) - // if (instances.length <= 1) return this.parent() - // - // if (instances.length == 6) { - // let nextInst = instances[instances.findIndex(a => a.id == inst.instanceId) + 1] - // if (!nextInst) nextInst = instances[0] - // - // nextInst.apply() - // } - // - // this.parent() - // }, - // }) injectInstance() injectTiling() if (window.crossnode?.options.test) { import('./tests') } + + ig.System.inject({ + run() { + const insts = Object.values(instanceinator.instances).sort((a, b) => a.id - b.id) + if (insts.length <= 1) return this.parent() + + if (insts.length == 6) { + let nextInst = insts[insts.findIndex(a => a.id == instanceinator.instanceId) + 1] + if (!nextInst) nextInst = insts[0] + + nextInst.apply() + } + + this.parent() + }, + }) + } + + async poststart() { + instanceinator.instances[0] = instanceinator.Instance.currentReference('master') + + for (let i = 1; i < 6; i++) { + const inst = await instanceinator.Instance.copy(instanceinator.instances[0], 'child') + instanceinator.append(inst) + inst.apply() + } + } +} + +class Instanceinator { + instanceId: number = 0 + instances: Record = {} + + Instance = InstanceinatorInstance + gameClasses!: { + System: ig.SystemConstructor + CrossCode: sc.CrossCodeConstructor } - // async poststart() { - // this.instances[0] = Instance.currentReference('master') - // - // for (let i = 1; i < 6; i++) { - // const instance = await Instance.copy(this.instances[0], 'child') - // this.append(instance) - // instance.apply() - // } - // } - - append(instance: Instance) { + append(instance: InstanceinatorInstance) { this.instances[instance.id] = instance } - delete(instance: Instance) { + delete(instance: InstanceinatorInstance) { delete this.instances[instance.id] } } declare global { - var inst: CCInstanceinator + var instanceinator: Instanceinator namespace NodeJS { interface Global { - inst: CCInstanceinator + instanceinator: Instanceinator } } } diff --git a/src/tests.ts b/src/tests.ts index a66f347..3c9cf80 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,14 +1,14 @@ -import CCInstanceinator from './plugin' +import CCInstanceInator from './plugin' -const modId = CCInstanceinator.mod.id +const modId = CCInstanceInator.mod.id ig.System.inject({ run() { if (crossnode.currentTestId != test1.id) return this.parent() - const instances = Object.values(inst.instances).sort((a, b) => a.id - b.id) + const instances = Object.values(instanceinator.instances).sort((a, b) => a.id - b.id) if (test1.startSwapping) { - let nextInst = instances[instances.findIndex(a => a.id == inst.instanceId) + 1] + let nextInst = instances[instances.findIndex(a => a.id == instanceinator.instanceId) + 1] if (!nextInst) nextInst = instances[0] nextInst.apply() @@ -23,12 +23,12 @@ crossnode.registerTest({ modId, skipFrameWait: true, async setup() { - inst.Instance.resetInstanceIdCounter() - inst.instances[0] = inst.Instance.currentReference('master') + instanceinator.Instance.resetInstanceIdCounter() + instanceinator.instances[0] = instanceinator.Instance.currentReference('master') for (let i = 1; i < 2; i++) { - const instance = await inst.Instance.copy(inst.instances[0], 'child') - inst.append(instance) + const instance = await instanceinator.Instance.copy(instanceinator.instances[0], 'child') + instanceinator.append(instance) instance.apply() } }, @@ -36,9 +36,9 @@ crossnode.registerTest({ this.finish(true) }, cleanup() { - inst.instances[0].apply() - for (const instance of Object.values(inst.instances)) { - inst.delete(instance) + instanceinator.instances[0].apply() + for (const instance of Object.values(instanceinator.instances)) { + instanceinator.delete(instance) } }, }) @@ -56,18 +56,18 @@ const test1 = crossnode.registerTest<{ instanceCount: 4, startSwapping: false, async setup() { - inst.Instance.resetInstanceIdCounter() - inst.instances[0] = inst.Instance.currentReference('master') + instanceinator.Instance.resetInstanceIdCounter() + instanceinator.instances[0] = instanceinator.Instance.currentReference('master') for (let i = 1; i < this.instanceCount; i++) { - const instance = await inst.Instance.copy(inst.instances[0], 'child') - inst.append(instance) + const instance = await instanceinator.Instance.copy(instanceinator.instances[0], 'child') + instanceinator.append(instance) } this.startSwapping = true }, update(frame) { - this.frameCountRecord[inst.instanceId] ??= 0 - this.frameCountRecord[inst.instanceId]++ + this.frameCountRecord[instanceinator.instanceId] ??= 0 + this.frameCountRecord[instanceinator.instanceId]++ const multi = 6 if (frame + 1 >= this.instanceCount * multi) { diff --git a/src/tiler.ts b/src/tiler.ts index eb56c77..db8bdc2 100644 --- a/src/tiler.ts +++ b/src/tiler.ts @@ -7,7 +7,7 @@ declare global { } export function getDisplayInstances() { - return Object.values(inst.instances).filter(i => i.display) + return Object.values(instanceinator.instances).filter(i => i.display) } export function injectTiling() { @@ -22,13 +22,13 @@ export function injectTiling() { sc.OptionModel.inject({ _setDisplaySize(schedule = true) { - const instances = getDisplayInstances().sort((a, b) => a.id - b.id) - if (instances.length <= 1) return this.parent() + const insts = getDisplayInstances().sort((a, b) => a.id - b.id) + if (insts.length <= 1) return this.parent() if (!schedule) return this.parent() - for (const instance of instances) { - instance.ig.game.scheduledTasks.push(() => { + for (const inst of insts) { + inst.ig.game.scheduledTasks.push(() => { sc.options?._setDisplaySize(false) }) } @@ -40,8 +40,8 @@ export function injectTiling() { let bestGrid = [0, 0] const aspectRatioRev = 320 / 568 - for (let nx = 1; nx <= Math.ceil(instances.length); nx++) { - const ny = Math.ceil(instances.length / nx) + for (let nx = 1; nx <= Math.ceil(insts.length); nx++) { + const ny = Math.ceil(insts.length / nx) const wi = Math.min(ws / nx, hs / ny / aspectRatioRev) if (wi > bestWi) { @@ -64,17 +64,17 @@ export function injectTiling() { let itemI = 0 for (let column = 0; column < grid[1]; column++) { for (let row = 0; row < grid[0]; row++) { - const instance = instances[itemI] - if (!instance) break - const item = instance.ig.system.inputDom + const inst = insts[itemI] + if (!inst) break + const item = inst.ig.system.inputDom item.style.position = 'absolute' item.style.left = `${row * width + offsetX}px` item.style.top = `${column * height + offsetY}px` item.style.width = `${width}px` item.style.height = `${height}px` - instance.ig.system.screenWidth = width - instance.ig.system.screenHeight = height + inst.ig.system.screenWidth = width + inst.ig.system.screenHeight = height itemI++ } @@ -83,10 +83,10 @@ export function injectTiling() { }) ig.Input.inject({ init() { - this.instanceId = inst.instanceId + this.instanceId = instanceinator.instanceId }, mousemove(event) { - if (this.instanceId == inst.instanceId) { + if (this.instanceId == instanceinator.instanceId) { this.parent(event) } },