Skip to content

rq.ain ​

since 0.2.0

Reaches into the game's own code (the AIN): call, hook and patch its functions, and read and write its globals.

Names are the game's own, in UTF-8, such as T戦闘前初期化 or T_BattleSelect@select; find them with rq.ain.find. They can change with the game's data, so a mod that patches by number should check the count it gets back.

Functions ​

NameSummary
rq.ain.patch_floatReplaces float constants in one AIN function.
rq.ain.patch_intReplaces integer constants in one AIN function, such as a hard-coded skill number.
rq.ain.patch_int_afterReplaces the first integer constant from that follows a read of a global.
rq.ain.set_code_intWrites a new value into a constant of the game's code that rq.ain.patch_int_after changed.
rq.ain.code_intReads the value a place of rq.ain.patch_int_after holds in the game's code now.
rq.ain.stackReturns the names of the game's functions on its call stack now, innermost first.
rq.ain.callCalls any AIN function by its name.
rq.ain.hookRuns Lua before and after any AIN function.
rq.ain.methodCalls a method of the game on the object a global reaches.
rq.ain.globalReads a game global that holds an int, bool, float or string.
rq.ain.set_globalWrites a game global, as rq.ain.global reads it: set_global(name, [step, ...,] value).
rq.ain.global_lenReturns the size of an array global.
rq.ain.set_global_lenMakes an array global longer or shorter, as the game's own Realloc does.
rq.ain.skip_msgboxRemoves the game's message boxes whose text contains needle.
rq.ain.skip_sdl_msgboxSwallows the engine's own error boxes for the TADA and HTTP network checks (getserverversion).
rq.ain.findWrites the AIN functions whose name contains needle to the log.

rq.ain.patch_float ​

lua
rq.ain.patch_float(name, from, to, n) -> integer

since 0.2.0 changed 1.3.0 changed 2.34.0

Replaces float constants in one AIN function.

ParameterTypeDescription
namestringThe function's name.
fromnumberThe value to find.
tonumberThe new value.
n (optional)integerWhich occurrence, from 0. Default: all of them.
ReturnsTypeDescription
countintegerHow many constants were replaced.

Changed in 1.3.0: Reaches the game's code of a function the loader wraps (such as T戦闘前初期化); before, it patched only the wrapper and found nothing.

Changed in 2.34.0: Reaches the game's code of a function hooked with rq.ain.hook (a hook moves the function to a stub); before, a hook made first, as the loader's own on T戦闘前初期化 since this version, left nothing to find.

rq.ain.patch_int ​

lua
rq.ain.patch_int(name, from, to, n) -> integer

since 0.17.0 changed 1.3.0 changed 2.34.0

Replaces integer constants in one AIN function, such as a hard-coded skill number.

Occurrences count every constant of that value in the function, so they can differ between versions of the game's AIN; to find one constant by what it follows, use patch_int_after.

ParameterTypeDescription
namestringThe function's name.
fromintegerThe value to find.
tointegerThe new value.
n (optional)integerWhich occurrence, from 0. Default: all of them.
ReturnsTypeDescription
countintegerHow many constants were replaced.

Changed in 1.3.0: Reaches the game's code of a function the loader wraps.

Changed in 2.34.0: Reaches the game's code of a function hooked with rq.ain.hook.

See also: rq.ain.patch_int_after

rq.ain.patch_int_after ​

lua
rq.ain.patch_int_after(name, global, from, to, nth_ref, window) -> integer, integer[]

since 1.3.0 changed 2.22.0 changed 2.34.0

Replaces the first integer constant from that follows a read of a global.

It looks at the window instructions after the read, as for the 2 in if (◆2倍モード == 1) { x = x * 2; }. A variable's index is never taken for the constant.

It also returns the places it changed, for rq.ain.set_code_int: a mod finds a constant once, by what it follows, and writes it again later without searching. Searching again would be wrong once the constant holds a value that also comes before it, as the 1 of == 1 above.

Raises an error for an unknown function or global.

lua
local _, at = rq.ain.patch_int_after("T戦闘前初期化", "◆2倍モード", 2, 3, 0)
rq.ain.set_code_int(at[1], 5) -- later, with no search
ParameterTypeDescription
namestringThe function's name.
globalstringThe global's name.
fromintegerThe value to find.
tointegerThe new value.
nth_ref (optional)integerWhich read of the global in the function, from 0. Default: after every read.
window (optional)integerHow many instructions to look at after the read. Default 8.
ReturnsTypeDescription
countintegerHow many constants were replaced.
placesinteger[]One place for each constant replaced, in code order, for rq.ain.set_code_int and rq.ain.code_int.

Changed in 2.22.0: Returns the places too.

Changed in 2.34.0: Reaches the game's code of a function hooked with rq.ain.hook.

See also: rq.ain.set_code_int

rq.ain.set_code_int ​

lua
rq.ain.set_code_int(place, value) -> boolean

since 2.22.0 changed 2.34.0

Writes a new value into a constant of the game's code that rq.ain.patch_int_after changed.

The place is the constant patch_int_after found, written now as it wrote it. The game reads it the next time it runs that code, so a value changed between fights counts from the next fight. When another install wraps the function later, the place is still found: the wrapped code stays where it was, and a body moved and grown is searched for the same constant with the same instructions around it. So is it when the function is hooked later with rq.ain.hook.

Raises an error for a place that patch_int_after did not give.

ParameterTypeDescription
placeintegerA place from rq.ain.patch_int_after.
valueintegerThe new value.
ReturnsTypeDescription
okbooleanfalse when the place is no longer in the game's code as it was written (it is logged); nothing is written then.

Changed in 2.34.0: A place in a function hooked with rq.ain.hook after the patch is found; before, it returned false.

See also: rq.ain.patch_int_after

rq.ain.code_int ​

lua
rq.ain.code_int(place) -> integer|nil

since 2.22.0

Reads the value a place of rq.ain.patch_int_after holds in the game's code now.

Raises an error for a place that patch_int_after did not give.

ParameterTypeDescription
placeintegerA place from rq.ain.patch_int_after.
ReturnsTypeDescription
valueinteger|nilThe value, or nil when the place is no longer found.

rq.ain.stack ​

lua
rq.ain.stack(max) -> string[]

since 2.27.0

Returns the names of the game's functions on its call stack now, innermost first.

A method is named Class@method, as rq.ain.hook takes it. Inside a hook, the stack says who called the hooked function: rq.music reads it to tell a scene, the castle or the town apart. From rq.on_frame it shows the game's frame update (UpdateView, UPDATE) and the screen's loop under it.

lua
rq.ain.hook("音楽", { before = function()
  rq.log("music asked by " .. tostring(rq.ain.stack(3)[2]))
end })
ParameterTypeDescription
max (optional)integerAt most this many names. Default 32.
ReturnsTypeDescription
namesstring[]The function names, innermost first; empty outside the game's code.

rq.ain.call ​

lua
rq.ain.call(name, ...) -> any

since 0.18.0 changed 0.26.0 changed 0.27.0

Calls any AIN function by its name.

The arguments must be int, bool, float or string, and all of them are needed. A scene command blocks until it ends, so call it from a callback the game runs, not at the top level of a mod. A function that reaches a save under this call gets the game's own "save failed" message and nothing is written; one that reaches a load closes the game with an error. A bad index passed to a game function is a fatal error in the game, and this call cannot check it.

Raises an error for an unknown name, a wrong number of arguments, a reference or struct argument, a method (Class@method, which needs an object), or SYS_セーブ実行 and SYS_ロード実行.

lua
local name = rq.ain.call("クエスト_名前", 126)
ParameterTypeDescription
namestringThe function's name.
...integer|boolean|number|stringThe arguments.
ReturnsTypeDescription
resultanyWhat the function returns; nil for none or a struct.

Changed in 0.26.0: Refuses SYS_セーブ実行 and SYS_ロード実行.

Changed in 0.27.0: A save under the call fails safely.

rq.ain.hook ​

lua
rq.ain.hook(name, opts) -> fun()

since 0.18.0 changed 0.23.0 changed 2.55.0

Runs Lua before and after any AIN function.

A method is named Class@method. Many hooks may sit on one function. The function does not fire its hooks again while one of its callbacks runs. A table with neither side does nothing.

only keeps the callbacks to the calls a mod wants, checked in C: on a function the game calls thousands of times a frame (a skill check, a stat read), a hook for one skill number costs Lua nothing on the other calls. Every position named must hold one of its values. The after side reads the arguments as the function leaves them. Hooks with a different only on one function each check their own.

Raises an error for an unknown function, an only that is not a table of positions, a position the function has no argument at, an argument that is not an int, more than 4 positions, or a value that is not a whole number or a list of 1 to 16 of them.

lua
rq.ain.hook("message", { before = function(h)
  h.args[3] = h.args[3]:gsub("Rance", "Ranse")  -- rewrite every line of text
end })

-- Only the game's checks for skill 373: CS番号取得(character, skill, mode).
rq.ain.hook("CS番号取得", { only = { [2] = 373 }, after = function(h)
  rq.log("asked for Immature on", h.args[1], "->", h.result)
end })
ParameterTypeDescription
namestringThe function's name.
optsrq.AinHookOptsThe callbacks, and only.
ReturnsTypeDescription
removefun()Removes both sides of this hook.

Changed in 0.23.0: Returns a remover.

Changed in 2.55.0: only: the callbacks run only for calls with these int arguments.

rq.ain.method ​

lua
rq.ain.method(path, name, ...) -> any

since 2.16.0

Calls a method of the game on the object a global reaches.

path is a global's name, then indexes and field names as rq.ain.global takes them, and it must end on a struct: {"g_sSystemMenu"}, {"g_sCastle", "m_sList"}. The method's class must be that struct's (T_SystemMenu@createButton on a T_SystemMenu). The arguments are as rq.ain.call's: int, bool, float or string, all of them. The same cautions hold: a method that draws or waits runs inside this call, and a bad value passed to the game can be a fatal error there.

Raises an error for a path that is not a table, does not start with a known global or does not reach a struct, an unknown function, a function that is not a method or not the struct's, a wrong number of arguments, or a reference or struct argument.

lua
local rows = rq.ain.method({ "g_sSystemMenu", "m_sButton" }, "T_ButtonGroup@getButtonCount")
ParameterTypeDescription
path(string|integer)[]The global's name, then indexes and field names to the object.
namestringThe method, Class@method.
...integer|boolean|number|stringThe arguments.
ReturnsTypeDescription
resultanyWhat the method returns; nil for none or a struct.

See also: rq.ain.call

rq.ain.global ​

lua
rq.ain.global(name, ...) -> integer|boolean|number|string

since 0.22.0 changed 0.24.0

Reads a game global that holds an int, bool, float or string.

Give the game's own index for an array element, and a field name to go into a struct, mixed as the game does.

Raises an error for an unknown name or field, an index out of range, a step that does not fit the value, a whole array or a struct.

lua
local fa = rq.ain.global("FA", 87)          -- FA[87]
local name = rq.ain.global("Q", 126, "名前")  -- Q[126].名前
ParameterTypeDescription
namestringThe global's name.
...integer|stringIndexes and field names.
ReturnsTypeDescription
valueinteger|boolean|number|stringThe value.

Changed in 0.24.0: Field names reach into structs.

rq.ain.set_global ​

lua
rq.ain.set_global(name, ...)

since 0.22.0

Writes a game global, as rq.ain.global reads it: set_global(name, [step, ...,] value).

It is the game's own state and goes into the next save, so restore what a test changes.

Raises an error as rq.ain.global does.

ParameterTypeDescription
namestringThe global's name.
...anyIndexes and field names, then the value.

rq.ain.global_len ​

lua
rq.ain.global_len(name, ...) -> integer

since 0.22.0

Returns the size of an array global.

Many game arrays start at 1 and leave 0 unused, so the size can be one more than the entries.

ParameterTypeDescription
namestringThe global's name.
...integer|stringIndexes and field names to reach an inner array.
ReturnsTypeDescription
sizeintegerThe number of elements.

rq.ain.set_global_len ​

lua
rq.ain.set_global_len(name, ...)

since 2.18.0

Makes an array global longer or shorter, as the game's own Realloc does.

Values that fit are kept; new elements are 0, false, an empty string or an empty struct. Only an array of one dimension. The array is the game's own state and goes into the next save at its new size, and a save made before brings back the old size, so grow it again where it is used (for example in a before hook). It is there only once the game runs, not while init.lua loads.

Raises an error for an unknown name, a value that is not an array, more than one dimension, or a size below 0 or above 65536.

lua
if rq.ain.global_len("取得可能スキル") < 145 then rq.ain.set_global_len("取得可能スキル", 145) end
ParameterTypeDescription
namestringThe global's name.
...integer|stringIndexes and field names to reach an inner array, then the new size.

rq.ain.skip_msgbox ​

lua
rq.ain.skip_msgbox(needle) -> integer

since 0.2.0

Removes the game's message boxes whose text contains needle.

Every system.MsgBox call that shows a string holding needle is taken out of the code.

Raises an error when the AIN is not loaded.

ParameterTypeDescription
needlestringPart of the message, in UTF-8.
ReturnsTypeDescription
sitesintegerHow many calls were taken out.

rq.ain.skip_sdl_msgbox ​

lua
rq.ain.skip_sdl_msgbox()

since 1.0.0

Swallows the engine's own error boxes for the TADA and HTTP network checks (getserverversion).

rq.ain.find ​

lua
rq.ain.find(needle) -> integer

since 0.2.0 changed 1.16.1

Writes the AIN functions whose name contains needle to the log.

Each line is find '<needle>' fno=<number> <name>, and the name is what rq.ain.call and rq.ain.hook take (a method as Class@method). The match is on the game's own spelling, so case and full-width letters count. At most 40 are listed; a longer needle narrows the list.

Raises an error when the AIN is not loaded.

lua
rq.ain.find("Encount")   -- [rqmod] find 'Encount' fno=... T_Player@checkEncount, ...
ParameterTypeDescription
needlestringPart of a name, in UTF-8.
ReturnsTypeDescription
countintegerHow many were listed.

Changed in 1.16.1: Each line also gives the function's name.