Skip to content

Commit

Permalink
Rename the global namespace to window.instanceinator
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Nov 26, 2024
1 parent b18e599 commit 65cc706
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 91 deletions.
34 changes: 17 additions & 17 deletions src/instance.ts
Original file line number Diff line number Diff line change
@@ -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<Instance> {
static async copy(s: InstanceinatorInstance, name?: string, display?: boolean): Promise<InstanceinatorInstance> {
const ig: any = {}
const igToInit: string[] = []
for (const key in s.ig) {
Expand Down Expand Up @@ -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}`
Expand All @@ -113,7 +113,7 @@ export class Instance {

igset(
'system',
new inst.classes.System(
new instanceinator.gameClasses.System(
'#' + canvasId,
'#' + gameId,
s.ig.system.fps,
Expand Down Expand Up @@ -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<void>(res => {
Expand All @@ -222,16 +222,16 @@ export class Instance {
public name: string = 'default',
public display: boolean = true
) {
this.id = Instance.instanceIdCounter
Instance.instanceIdCounter++
this.id = InstanceinatorInstance.instanceIdCounter
InstanceinatorInstance.instanceIdCounter++
}

apply() {
// @ts-expect-error
global.ig = window.ig = this.ig
// @ts-expect-error
global.sc = window.sc = this.sc
inst.instanceId = this.id
instanceinator.instanceId = this.id
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
87 changes: 44 additions & 43 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
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<number, Instance> = {}

classes!: {
System: ig.SystemConstructor
CrossCode: sc.CrossCodeConstructor
}
Instance: typeof Instance

constructor(mod: Mod1) {
CCInstanceinator.dir = mod.baseDirectory
CCInstanceinator.mod = mod
CCInstanceinator.mod.isCCL3 = mod.findAllAssets ? true : false
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() {},
}),
Expand All @@ -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<number, InstanceinatorInstance> = {}

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
}
}
}
34 changes: 17 additions & 17 deletions src/tests.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -23,22 +23,22 @@ 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()
}
},
update(_frame) {
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)
}
},
})
Expand All @@ -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) {
Expand Down
28 changes: 14 additions & 14 deletions src/tiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
})
}
Expand All @@ -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) {
Expand All @@ -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++
}
Expand All @@ -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)
}
},
Expand Down

0 comments on commit 65cc706

Please sign in to comment.