Class Maginai

maginai main module class

All mods can access the maginai global variable, which is an instance of this class.
You can use various maginai features through its fields and methods.
Some properties, such as maginai.logging, are submodules that provide specific functions under them.

Submodules and their features:

Constructors

Properties

MOD_COMMAND_KEY_CODES: string[]

Key codes available for the mod command key

VERSION: string

Version string

VERSION_INFO: [number, number, number, string, number]

Version information

Represents [major, minor, patch, preid, prerelease] parts of the version string.
If the version is not prelease, (preid, prerelease) === (null, null)

completedMods: string[]

Successfully loaded mods

currentModPostprocess: Promise<any>

Current postprocess of the loading mod

Cycle of executing Postprocess:

  1. maginai starts loading a mod
  2. The mod sets their Postprocess in init.js using maginai.setModPostprocess
  3. maginai pops the PostProcess using popModPostprocess and executes it
  4. maginai proceeds to load the next mod
errorsOnLoadMods: [Error | ErrorEvent, string][]

List of (<error>, <mod's name>) during mod loading

maginai.events submodule

Provides the events of maginai.
See MaginaiEvents for details.

Instance of canvas control class for displaying information on title screens, etc.

inGameDebugLogQueue: string[]

Queue for logToInGameLogDebug

isFirstDummytWgmMainLoadFinished: boolean

To initialize tWgm only once, we should do it on the earlier of $(document).ready or main postprocess.
This field is for controlling it.

isGameLoadFinished: boolean

Whether gameLoadFinished is already triggered

For controlling the event

isModLoadFatalErrorOccured: boolean

Indicates whether an error occured during the main process of loading mods.
(Ignores errors during the loading of each mod)
Initially true; set to false on success.
Used for displaying the loading status on the title screen

loadedJs: Record<string, boolean>

JavaScript file paths loaded by loadJs

logging: RootLogger

maginai.logging submodule (=== loglevel module of the loglevel package)

It's recommended to use this for logging in each mod.

const logger = maginai.logging.getLogger("my-mod") // Pass your mod's name to `getLogger`
// Available log levels are trace/debug/info/warn/error
logger.info('info level log')
logger.debug('debug level log')
modCommandKey: ModCommandKey

Instance of ModCommandKey for setting mod command key

modSave: ModSave

maginai.modSave submodule

Provides methods for storing and retrieving the save object for each mod.
See ModSave class definition for details.

origtGameMain: any

Actual tGameMain

maginai replace tGameMain with a dummy class, so mods can't access actual tGameMain directly.
Use this origtGameMain property to access it.

patcher: Patcher

maginai.patcher submodule

Provides utility methods for method patching
See Patcher class definition for details.

patcher2: Patcher2

maginai.patcher2 submodule

Provides utility methods for method patching.
See Patcher2 class definition for details.

Methods

  • Internal

    Initialization which requires union.js loaded

    * Note: just instantiating Maginai doesn't require union.js.

    Returns void

  • Loads a JavaScript file.

    Loads the file by adding a script tag via DOM operation and returns an object containing the HTMLScriptElement of the loaded script.

    Parameters

    • path: string

    Returns Promise<{
        script: HTMLScriptElement;
    }>

  • Loads an object from var LOADDATA=... style JavaScript file

    Parameters

    • path: string

    Returns Promise<any>

    object loaded

  • Internal

    Loads all mods and the game

    Retrieves all mod's names from mods_load.js and loads all mods.
    After that, loads the game with loadtWgm

    Returns Promise<void>

  • Private

    Loads a mod

    1. Sets a custom error handler for catching errors during loading the mod
    2. Loads init.js
    3. Pops the Postprocess of the mod and execute it
    4. Unsets the error handler

    By the custom error handler, we can identify and aggregate errors from each mod.
    The errors are collected in errorsOnLoadMods.
    Exceptions during loading the mod are caught to prevent failure from affecting other mods or the main process.

    Parameters

    • modName: string

    Returns Promise<void>

  • Logs to the in-game log (for debugging)

    This method logs the passed message to the in-game log by "piggybacking" on the original logging process.
    You can call it from anywhere, and the message will be logged at the next logging reliably.
    However, due to the nature of this process, you cannot control exactly when the message will be logged, and there may be delays in logging.
    Use this method strictly for debugging purposes.
    For actual logging tasks, such as displaying used items, it's recommended to use the game's addAndViewLog method of the game.

    Parameters

    • message: string

    Returns void

  • Private

    Pops Postprocess Promise

    Note: this method returns Promise.resolve() even if no Postprocess has been set.

    Returns Promise<any>

    Postprocess

  • Sets Postprocess Promise

    This should be called in init.js.
    The set Postprocess will be executed after loading the mod.
    Note: this method accepts any non-Promise value and converts it to a Promise using Promise.resolve().

    Parameters

    • promise: Promise<any>

      Postprocess

    Returns void