Mixins
Preparation
Make sure you are importing MixinType from PolyTypes.js:
js
import { PolyMod, MixinType } from "https://cdn.polymodloader.com/cb/PolyTrackMods/PolyModLoader/<version>/PolyTypes.js";The deprecated MixinTypes from pre-0.6 have been entirely removed, left with only these:
INSERT: Inserts a piece of code (string) after a given token.REPLACEBETWEEN: Replaces code with some other code between two given tokens.REMOVEBETWEEN: Removes code between twp given tokens.
Mixin Types
Every type of mixin in some way uses the MixinArgs type:
For INSERT:
ts
type MixinArgs: {
type: MixinType.INSERT,
token: string | MixinToken,
func: string | Function
}For REPLACEBETWEEN:
ts
type MixinArgs: {
type: MixinType.REPLACEBETWEEN,
tokenStart: string | MixinToken,
tokenEnd: string | MixinToken,
func: string | Function
}For REMOVEBETWEEN, it is the same as REPLACEBETWEEN but without a func.
You might have noticed the MixinToken type. This is another QOL addition to PolyModLoader for 0.6 and above, and is declared like so:
ts
type MixinToken: {
token: string,
occ: number
}token is simply the string to look for, and occ is the nth occurrence to look for. For example, { token: "));", occ: 4} represent the 4th appearance of the token ));.
Mixin Functions
There exists multiple functions that can be called to register mixins:
ts
// Class / funcs in main.bundle.js
registerClassMixin(scope: string, path: string, mixinArg: MixinArgs): void;
registerFuncMixin(path: string, mixinArg: MixinArgs): void;
registerClassWideMixin(path: string, mixinArg: MixinArgs): void;
// Sim worker (currenetly unimplemented)
registerSimWorkerClassMixin(scope: string, path: string, mixinArg: MixinArgs): void;
registerSimWorkerFuncMixin(path: string, mixinArg: MixinArgs): void;
// Global ish (overshadows literally all other mixins except sim)
registerGlobalMixin(mixinArg: MixinArgs): void;
registerChunkMixin(bundleName: string, mixinArg: MixinArgs): void;A few things to note:
- Because of bundling changes in PolyTrack 0.6.0, class/classwide and func mixins don't work on a considerable about of classes (notably Three.js related), which makes global mixins the only option for those classes.
- Another change brought to 0.6.0 was the separation of the editor and garage code into their own separate webpack chunks, thus the need for chunk mixins.