Files

2382 lines
82 KiB
Lua

REPENTOGON = {
["Real"] = true,
["Name"] = "REPENTOGON", --I avoid having to do RegisterMod this way, sorry ;P
["Version"] = "1.1.3",
["Extras"] = { ["Misc"]={}, }, -- Tables containing additional REPENTOGON data structures, example: ChangeLog or StatsMenu
}
pcall(collectgarbage, "generational") -- rebuild: Lua 5.3 has no generational mode
--- Each module is a { module, loaded } pair
local s_modules = {}
local debug_getinfo = debug.getinfo
local function cleanTraceback(level) -- similar to debug.traceback but breaks at xpcall, uses spaces instead of tabs, and doesn't call local functions upvalues
level = level + 1
local msg = "Stack Traceback:\n"
while true do
local info = debug_getinfo(level, "Sln")
if not info then break end
if info.what == "C" then
if info.name == "xpcall" then break end
msg = msg .. " in method " .. tostring(info.name) .. "\n"
else
if info.name then
msg = msg ..
" " ..
tostring(info.short_src) ..
":" .. tostring(info.currentline) .. ": in function '" .. tostring(info.name) .. "'\n"
else
msg = msg ..
" " ..
tostring(info.short_src) ..
":" .. tostring(info.currentline) .. ": in function at line " .. tostring(info.linedefined) .. "\n"
end
end
level = level + 1
end
return msg
end
local errorHandler = function(msg)
return msg .. "\n" .. cleanTraceback(2)
end
---@param modName string
function _GetModule(modName)
local cachedMod = s_modules[modName]
if cachedMod then
if not cachedMod[2] then
error(string.format("[ERROR] [REPENTOGON] module \"%s\" caused a recursive dependency", modName), 0) -- use level 0 to not print a source line
end
return cachedMod[1]
end
cachedMod = {nil, false}
s_modules[modName] = cachedMod
local ok, mod = xpcall(include, errorHandler, modName)
if not ok then
s_modules[modName] = nil
error(string.format("[ERROR] [REPENTOGON] Failed to load module \"%s\": %s", modName, mod), 0) -- use level 0 to not print a source line
end
cachedMod[1] = mod
cachedMod[2] = true
return mod
end
local RaiseModError = _CBindings.RaiseModError
-- rebuild: the vanilla parity option
local IsVanillaParity = _CBindings.IsVanillaParity
local ModManager = _GetModule("repentogon_extras.mod_manager")
local function MeetsVersion(targetVersion)
if (REPENTOGON.Version == "1.1.3") then return true end
local version = {}
local target = {}
for num in REPENTOGON.Version:gmatch("%d+") do
table.insert(version, tonumber(num))
end
for num in targetVersion:gmatch("%d+") do
table.insert(target, tonumber(num))
end
-- Compare each component of the version strings
for i = 1, math.max(#version, #target) do
local v = version[i] or 0
local t = target[i] or 0
if v < t then
return false
elseif v > t then
return true
end
end
local targetsuffix = targetVersion:match("%a+$")
local currsuffix = REPENTOGON.Version:match("%a+$")
return (not targetsuffix) or (targetsuffix <= currsuffix)
end
REPENTOGON.MeetsVersion = MeetsVersion
local callbackIDToName = {}
for k, v in pairs(ModCallbacks) do
callbackIDToName[v] = k
end
local oldrequire = require
function require(modname)
local ret = oldrequire(modname)
return ret
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- COLOR STUFF
----------------------------------------------------------------------------------------------------
rawset(getmetatable(Color), "EmberFade", Color(1, 1, 1, 1, 1, 0.514, 0.004))
Color.EmberFade:SetTint(0, 0, 0, 1.1)
local function quickRegisterColorPreset(name, R, G, B, A, RO, GO, BO, RC, GC, BC, CA)
rawset(getmetatable(Color), name, Color(R, G, B, A or 1, RO or 0, GO or 0, BO or 0))
Color[name]:SetColorize(RC or 0, GC or 0, BC or 0, CA or 0)
end
rawset(getmetatable(Vector), "__type", "Vector")
local colorPresets = {
TearIpecac = {0.5, 0.9, 0.4},
TearHoming = {0.4, 0.15, 0.38, 1, 71/255, 0, 116/255},
TearTar = {0.95, 0.8, 0.6, 1, -150/255, -150/255, -150/255},
TearSoy = {1.5, 2, 2},
TearChocolate = {0.33, 0.18, 0.18, 1, 66/255, 40/255, 40/255},
TearAlmond = {1.8, 1.7, 1},
TearScorpio = {50/255, 1, 50/255},
TearSerpentsKiss = {0.5, 0.97, 0.5},
TearCommonCold = {0.4, 0.97, 0.5},
TearCoal = {0.2, 0.09, 0.065},
TearNumberOne = {1, 1, 0, 1, 45/255, 15/255, 0},
ProjectileIpecac = {1, 1, 1, 1, 0, 0, 0, 0.4, 2, 0.5, 1},
ProjectileHoming = {1, 1, 1, 1, 0.26, 0.05, 0.4, 0.8, 0.15, 1, 1},
ProjectileTar = {1, 1, 1, 1, 0, 0, 0, 0.5, 0.5, 0.5, 1},
ProjectileSoy = {1, 1, 1, 1, 0.8, 0.7, 0.5, 1, 1, 1, 1},
ProjectileFireWave = {1, 1, 1, 1, 1, 0.3, 0},
ProjectileCageBlue = {1, 1, 1, 1, 0, 0, 0, 0.8, 1, 0.85, 1},
ProjectileMegaSatanBlack = {1, 1, 1, 1, 0, 0, 0, 0.6, 0.6, 0.6, 1},
ProjectileMegaSatanWhite = {1, 1, 1, 1, 0, 0, 0, 2, 2, 2, 1},
ProjectileHushBlue = {1, 1, 1, 1, 0, 0.2, 0.4},
ProjectileHushYellow = {1, 1, 1, 1, 0.4, 0.2, 0},
ProjectileHushGreen = {1, 1, 1, 1, 0.2, 0.2, 0},
LaserIpecac = {1, 1, 1, 1, 0, 0, 0, 1.8, 3, 1, 1},
LaserHoming = {1, 1, 1, 1, 0, 0, 0, 3, 1, 3.5, 1},
LaserSoy = {1, 1, 1, 1, 0, 0, 0, 5.6, 5, 4.2, 1},
LaserChocolate = {1, 1, 1, 1, 0, 0, 0, 3, 1.7, 1.7, 1},
LaserAlmond = {1, 1, 1, 1, 0, 0, 0, 5.6, 5.2, 3.8, 1},
LaserPoison = {1, 1, 1, 1, 0, 0, 0, 1.8, 4, 1, 1},
LaserCoal = {3, 3, 3, 1, -0.5, -0.5, -0.5, 1.3, 1.2, 1.2, 1},
LaserFireMind = {1, 1, 1, 1, 0, 0, 0, 5, 3, 1, 1},
LaserNumberOne = {1, 1, 1, 1, 0, 0, 0, 5, 4.9, 1, 1},
LaserMother = {1, 1, 1, 1, 0, 0, 0, 2, 2.2, 1, 1},
ProjectileCorpsePink = {1, 1, 1, 1, 0, 0, 0, 4, 3.5, 3.2, 1},
ProjectileCorpseWhite = {1, 1, 1, 1, 0, 0, 0, 2.7, 3, 2, 1},
ProjectileCorpseGreen = {1, 1, 1, 1, 0, 0, 0, 1.5, 2, 1, 1},
ProjectileCorpseYellow = {1, 1, 1, 1, 0, 0, 0, 3.5, 2.5, 1, 1},
ProjectileCorpseClusterLight = {4, 4, 4, 1, 0, 0, 0, 0.63, 0.85, 0.32, 1},
ProjectileCorpseClusterDark = {1, 1, 1, 1, 0, 0, 0, 0.63, 0.85, 0.32, 1},
}
for colorName, colorData in pairs(colorPresets) do
quickRegisterColorPreset(colorName, table.unpack(colorData))
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- TYPE CHECKING UTILITIES
----------------------------------------------------------------------------------------------------
-- I hate Luabridge, we can't have nice things.
local function GetMetatableType(ret)
-- TODO directly pcall in here?
-- Vector will CRASH THE GAME through a pcall if we try getting __name or __type from it, and getmetatable doesn't work on it.
-- We have no choice but to check manually.
if ret.X and ret.Y then
return "Vector"
else
-- Directly trying __name or __type, too, will crash the game on occasion, even through a pcall.
-- Absolute masterclass of an API here. Best in show, really.
return getmetatable(ret).__name or getmetatable(ret).__type
end
end
local function checkMetatable(mtType)
return function(val)
if GetMetatableType(val) ~= mtType then
return "bad return type (" .. mtType .. " expected, got " .. GetMetatableType(val).__type .. ")"
end
end
end
local function checkInteger(val)
if math.type(val) ~= "integer" then
return "bad return type (number has no integer representation)"
end
end
local function checkNumberGreaterThanFunction(minimum)
return function(val)
if val <= minimum then
return "bad return value (number must be > " .. minimum .. ")"
end
end
end
local function checkNumberGreaterOrEqualFunction(minimum)
return function(val)
if val < minimum then
return "bad return value (number must be >= " .. minimum .. ")"
end
end
end
local function checkTableSizeFunction(size)
return function(val)
local tablesize = 0
for i, tabletype in pairs(val) do
tablesize = tablesize + 1
end
if tablesize > size then
return "bad return table length (" .. tostring(size) .. " expected, got " .. tostring(tablesize) .. ")"
end
end
end
local function checkTableIndexes(typestrings)
return function(tbl)
for i, param in pairs(tbl) do
if not typestrings[i] then
return "return table has unexpected key `" .. tostring(i) .. "` with value type " .. type(param)
end
end
end
end
local function checkTableSizeFunctionUpTo(size)
return function(val)
local tablesize = 0
for i, tabletype in pairs(val) do
tablesize = tablesize + 1
end
if tablesize > size then
return "bad return table length (up to " .. tostring(size) .. " expected, got " .. tostring(tablesize) .. ")"
end
end
end
local function checkTableSizeFunctionMinimum(size)
return function(val)
local tablesize = 0
for i, tabletype in pairs(val) do
tablesize = tablesize + 1
end
if tablesize < size then
return "bad return table length (minimum " .. tostring(size) .. " expected, got " .. tostring(tablesize) .. ")"
end
end
end
local function checkTableTypeFunction(typestrings)
return function(tbl)
local tablesize = 0
for i, tabletype in pairs(tbl) do
tablesize = tablesize + 1
end
for i, param in pairs(tbl) do
if typestrings[i] then --no point in doing any of this if the thing doesnt even fucking exist
local paramType = type(param)
if paramType == "number" and math.type(param) == "integer" then
paramType = "integer"
end
if paramType == "userdata" then
paramType = GetMetatableType(param)
end
if paramType ~= typestrings[i] and not (paramType == "integer" and typestrings[i] == "number") then
return "bad return type for table value with key `" ..
tostring(i) .. "` (" .. typestrings[i] .. " expected, got " .. paramType .. ")"
end
end
end
end
end
local function backEnum(enum, id)
for k, v in pairs(enum) do
if v == id then
return k
end
end
end
local typecheckFunctions = {
[ModCallbacks.MC_PRE_ADD_COLLECTIBLE] = {
["table"] = checkTableTypeFunction({ "integer", "integer", "boolean", "integer", "integer" }),
["number"] = checkInteger,
["boolean"] = true,
},
[ModCallbacks.MC_PRE_SFX_PLAY] = {
["table"] = checkTableTypeFunction({ "integer", "number", "integer", "boolean", "number", "number" }),
["boolean"] = true
},
[ModCallbacks.MC_PRE_ROOM_ENTITY_SPAWN] = {
["table"] = checkTableTypeFunction({ "integer", "integer", "integer" })
},
[ModCallbacks.MC_USE_ITEM] = {
["table"] = true,
["boolean"] = true,
},
[ModCallbacks.MC_GET_SHADER_PARAMS] = {
["table"] = true
},
[ModCallbacks.MC_PRE_MUSIC_PLAY] = {
["table"] = checkTableTypeFunction({ "integer", "number", "boolean" }),
["boolean"] = true,
["number"] = checkInteger
},
[ModCallbacks.MC_PRE_RENDER_PLAYER_HEAD] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_RENDER_PLAYER_BODY] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_ENTITY_THROW] = {
["Vector"] = true
},
[ModCallbacks.MC_PRE_CHANGE_ROOM] = {
["table"] = checkTableTypeFunction({ "integer", "integer" }),
},
[ModCallbacks.MC_PRE_PICKUP_MORPH] = {
["table"] = checkTableTypeFunction({ "integer", "integer", "integer", "boolean", "boolean", "boolean" }),
["boolean"] = true
},
[ModCallbacks.MC_PRE_NPC_MORPH] = {
["table"] = checkTableTypeFunction({ "integer", "integer", "integer", "integer" }),
["boolean"] = true
},
[ModCallbacks.MC_PRE_FAMILIAR_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_NPC_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_PLAYER_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_PICKUP_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_TEAR_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_PROJECTILE_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_KNIFE_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_EFFECT_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_BOMB_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
[ModCallbacks.MC_PRE_SLOT_RENDER] = {
["Vector"] = true,
["boolean"] = true
},
--[[
// This seems to be eating the callback entirely
[ModCallbacks.MC_PRE_GRID_ENTITY_SPAWN] = {
["boolean"] = true,
["GridEntityDesc"] = true,
["table"] = checkTableTypeFunction({ "integer", "integer", "integer", "integer" })
},
]]--
[ModCallbacks.MC_PRE_REPLACE_SPRITESHEET] = {
["table"] = checkTableTypeFunction({ "integer", "string" })
},
[ModCallbacks.MC_PRE_PLANETARIUM_APPLY_TREASURE_PENALTY] = {
["boolean"] = true,
["number"] = checkInteger
},
[ModCallbacks.MC_PRE_PLANETARIUM_APPLY_ITEMS] = {
["number"] = true,
},
[ModCallbacks.MC_PRE_PLANETARIUM_APPLY_TELESCOPE_LENS] = {
["number"] = true,
},
[ModCallbacks.MC_POST_PLANETARIUM_CALCULATE] = {
["number"] = true,
},
[ModCallbacks.MC_PRE_DEVIL_APPLY_ITEMS] = {
["number"] = true,
},
[ModCallbacks.MC_PRE_DEVIL_APPLY_SPECIAL_ITEMS] = {
["number"] = true,
},
[ModCallbacks.MC_POST_DEVIL_CALCULATE] = {
["number"] = true,
},
[ModCallbacks.MC_PRE_ITEM_OVERLAY_SHOW] = {
["boolean"] = true,
["number"] = checkInteger
},
[ModCallbacks.MC_PRE_GET_LIGHTING_ALPHA] = {
["number"] = true,
},
[ModCallbacks.MC_PRE_PLAYER_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_TEAR_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_FAMILIAR_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_BOMB_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_PICKUP_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_SLOT_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_KNIFE_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_PROJECTILE_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_NPC_COLLISION] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_ENTITY_TAKE_DMG] = {
["boolean"] = true,
["table"] = checkTableTypeFunction({ Damage="number", DamageFlags="integer", DamageCountdown="number" }),
},
[ModCallbacks.MC_PRE_PLAYER_ADD_CARD] = {
["boolean"] = true,
["number"] = checkInteger,
},
[ModCallbacks.MC_PRE_PLAYER_ADD_PILL] = {
["boolean"] = true,
["number"] = checkInteger,
},
[ModCallbacks.MC_CONSOLE_AUTOCOMPLETE] = {
["table"] = true,
},
[ModCallbacks.MC_PRE_STATUS_EFFECT_APPLY] = {
["number"] = checkInteger,
["boolean"] = true,
["table"] = checkTableSizeFunctionUpTo(4), -- per-status type checking done manually
},
[ModCallbacks.MC_PRE_GRID_HURT] = {
["number"] = checkInteger,
["boolean"] = true,
},
[ModCallbacks.MC_PRE_LEVEL_PLACE_ROOM] = {
["Room"] = true,
},
[ModCallbacks.MC_PRE_SELECT_AMBUSH_WAVE] = {
["Room"] = true,
},
[ModCallbacks.MC_PRE_SELECT_GREED_WAVE] = {
["Room"] = true,
},
[ModCallbacks.MC_SHOULD_SAVE_ENTITY] = {
["boolean"] = true,
}
}
local typecheckWarnFunctions = {
[ModCallbacks.MC_INPUT_ACTION] = {
["number"] = function(ret, retType, entity, hook)
if hook ~= InputHook.GET_ACTION_VALUE then
return "bad return type for hook " .. backEnum(InputHook, hook) .. " (boolean expected, got number)"
elseif ret < -1 or ret > 1 then
return "bad return value for hook GET_ACTION_VALUE (number must be within range -1 to 1)"
end
end,
["boolean"] = function(ret, retType, entity, hook)
if hook == InputHook.GET_ACTION_VALUE then
return "bad return type for hook GET_ACTION_VALUE (number expected, got boolean)"
end
end
},
[ModCallbacks.MC_EXECUTE_CMD] = { -- returning any value causes the game to crash... but strings seem to work? docs aren't clear
["string"] = true
},
[ModCallbacks.MC_PRE_ENTITY_SPAWN] = {
["table"] = checkTableSizeFunction(4)
},
[ModCallbacks.MC_POST_PICKUP_SELECTION] = {
["table"] = checkTableTypeFunction({ "integer", "integer", "boolean"})
},
[ModCallbacks.MC_PRE_ADD_COLLECTIBLE] = {
["table"] = checkTableSizeFunctionUpTo(5), --{ "integer", "integer", "boolean", "integer", "integer" }),
["number"] = checkInteger,
["boolean"] = true,
},
[ModCallbacks.MC_PRE_SFX_PLAY] = {
["table"] = checkTableSizeFunctionUpTo(6), --{ "integer", "number", "integer", "boolean", "number", "number" }),
["boolean"] = true
},
[ModCallbacks.MC_PRE_ROOM_ENTITY_SPAWN] = {
["table"] = checkTableSizeFunctionUpTo(3), --{ "integer", "integer", "integer" })
},
[ModCallbacks.MC_PRE_MUSIC_PLAY] = {
["table"] = checkTableSizeFunctionUpTo(3), --{ "integer", "number", "boolean" }),
["boolean"] = true,
["number"] = checkInteger,
},
[ModCallbacks.MC_PRE_CHANGE_ROOM] = {
["table"] = checkTableSizeFunctionUpTo(2), --{ "integer", "integer" }),
},
[ModCallbacks.MC_PRE_PICKUP_MORPH] = {
["table"] = checkTableSizeFunctionUpTo(6), --{ "integer", "integer", "integer", "integer", "integer", "integer" }),
["boolean"] = true
},
[ModCallbacks.MC_PRE_NPC_MORPH] = {
["table"] = checkTableSizeFunctionUpTo(6), --{ "integer", "integer", "integer", "integer", "integer", "integer" }),
["boolean"] = true
},
[ModCallbacks.MC_PRE_REPLACE_SPRITESHEET] = {
["table"] = checkTableSizeFunctionUpTo(2) --{ "integer", "string" })
},
[ModCallbacks.MC_PRE_PLAYER_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_TEAR_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_FAMILIAR_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_BOMB_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_PICKUP_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_SLOT_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_KNIFE_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_PROJECTILE_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_PRE_NPC_COLLISION] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Collide="boolean", SkipCollisionEffects="boolean" }),
},
[ModCallbacks.MC_ENTITY_TAKE_DMG] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Damage="number", DamageFlags="integer", DamageCountdown="number" }),
},
[ModCallbacks.MC_USE_ITEM] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Discharge = "boolean", Remove = "boolean", ShowAnim = "boolean", }),
},
[ModCallbacks.MC_PRE_PLAYER_ADD_CARD] = {
["boolean"] = true,
["number"] = checkNumberGreaterThanFunction(0),
},
[ModCallbacks.MC_PRE_PLAYER_ADD_PILL] = {
["boolean"] = true,
["number"] = checkNumberGreaterThanFunction(0),
},
[ModCallbacks.MC_PLAYER_GET_ACTIVE_MAX_CHARGE] = {
["number"] = checkNumberGreaterOrEqualFunction(0),
},
[ModCallbacks.MC_PLAYER_GET_ACTIVE_MIN_USABLE_CHARGE] = {
["number"] = checkNumberGreaterOrEqualFunction(0),
},
[ModCallbacks.MC_PRE_USE_ITEM] = {
["boolean"] = true,
["table"] = checkTableIndexes({ Discharge="boolean" }),
},
}
local boolCallbacks = {
ModCallbacks.MC_PRE_LASER_COLLISION,
ModCallbacks.MC_PRE_NPC_UPDATE,
ModCallbacks.MC_PRE_ENTITY_DEVOLVE,
ModCallbacks.MC_PRE_SPAWN_CLEAN_AWARD,
ModCallbacks.MC_PRE_TRIGGER_PLAYER_DEATH,
ModCallbacks.MC_TRIGGER_PLAYER_DEATH_POST_CHECK_REVIVES,
ModCallbacks.MC_PRE_USE_CARD,
ModCallbacks.MC_PRE_USE_PILL,
ModCallbacks.MC_PRE_PLAYER_TRIGGER_ROOM_CLEAR,
ModCallbacks.MC_PRE_PLANETARIUM_APPLY_STAGE_PENALTY,
ModCallbacks.MC_PRE_PLANETARIUM_APPLY_PLANETARIUM_PENALTY,
ModCallbacks.MC_PRE_SLOT_CREATE_EXPLOSION_DROPS,
ModCallbacks.MC_PRE_DEVIL_APPLY_STAGE_PENALTY,
ModCallbacks.MC_PRE_MEGA_SATAN_ENDING,
ModCallbacks.MC_PRE_PLAYER_USE_BOMB,
ModCallbacks.MC_PRE_PLAYER_TAKE_DMG,
ModCallbacks.MC_PRE_PLAYER_COLLECT_CARD,
ModCallbacks.MC_PRE_PLAYER_COLLECT_PILL,
ModCallbacks.MC_PRE_PLAYER_GIVE_BIRTH_CAMBION,
ModCallbacks.MC_PRE_PLAYER_GIVE_BIRTH_IMMACULATE,
ModCallbacks.MC_PRE_UPDATE,
}
for _, callback in ipairs(boolCallbacks) do
typecheckFunctions[callback] = { ["boolean"] = true }
end
local intCallbacks = {
ModCallbacks.MC_POST_CURSE_EVAL,
ModCallbacks.MC_GET_CARD,
ModCallbacks.MC_PRE_GET_COLLECTIBLE,
ModCallbacks.MC_GET_PILL_COLOR,
ModCallbacks.MC_GET_PILL_EFFECT,
ModCallbacks.MC_GET_TRINKET,
ModCallbacks.MC_GET_FOLLOWER_PRIORITY,
ModCallbacks.MC_GET_SHOP_ITEM_PRICE,
ModCallbacks.MC_PLAYER_GET_HEALTH_TYPE,
ModCallbacks.MC_PLAYER_GET_ACTIVE_MAX_CHARGE,
ModCallbacks.MC_PLAYER_GET_ACTIVE_MIN_USABLE_CHARGE,
ModCallbacks.MC_PLAYER_GET_HEART_LIMIT,
ModCallbacks.MC_PRE_SLOT_SET_PRIZE_COLLECTIBLE,
}
for _, callback in ipairs(intCallbacks) do
typecheckFunctions[callback] = { ["number"] = checkInteger }
end
local function setExpectedTypes(typeTable)
local validTypes = {}
for typ, func in pairs(typeTable) do
validTypes[#validTypes + 1] = typ
end
table.sort(validTypes)
local expected = ""
for i, typ in ipairs(validTypes) do
if i ~= #validTypes then
expected = expected .. typ .. ", "
elseif #validTypes == 1 then
expected = expected .. typ
else
expected = expected .. "or " .. typ
end
end
if expected == "" then
expected = "no return"
end
typeTable.expectedtypes = expected
end
for callback, typeTable in pairs(typecheckFunctions) do
setExpectedTypes(typeTable)
end
for callback, typeTable in pairs(typecheckWarnFunctions) do
setExpectedTypes(typeTable)
end
rawset(Isaac, "SetCallbackTypeCheck", function(callbackID, tbl, noSetExpected)
typecheckFunctions[callbackID] = tbl
if not noSetExpected then
setExpectedTypes(tbl)
end
end)
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- ERROR LOGGING / DISPLAY
----------------------------------------------------------------------------------------------------
-- Whether or not a lua error has been displayed in the ErrorDisplayWindow.
local err_shown = false
-- Tracks how many times the same error has appeared consecutively.
local err_dupecount = 1
-- Tracks printed warnings to avoid them reappearing in the same session.
---@type string[]
local printedWarnings = {}
local function logWarning(callbackID, modName, warn)
local cbName = callbackIDToName[callbackID] or callbackID
for _, printedWarning in pairs(printedWarnings) do
if warn == printedWarning then return end
end
table.insert(printedWarnings, warn)
Console.PrintWarning('"' .. cbName .. '" from "' .. modName .. '" warning: ' .. warn .. "\n(This warning will not reappear until the next Lua reload.)")
Isaac.DebugString('Warning in "' .. cbName .. '" from "' .. modName .. '": ' .. warn .. "(This warning will not reappear until the next Lua reload.)")
end
local function imGuiError(errortext)
local windowId = "ErrorDisplayWindow"
if not ImGui.ElementExists("ErrorDisplayMenu") then
ImGui.CreateMenu("ErrorDisplayMenu", "REPENTOGON Error Display")
ImGui.AddElement("ErrorDisplayMenu", "ErrorDisplayMenuEntry", ImGuiElement.MenuItem, "Error Display")
ImGui.CreateWindow(windowId, "Error Display")
ImGui.LinkWindowToElement(windowId, "ErrorDisplayMenuEntry")
ImGui.AddText(windowId, "Below is the first error in the chain, which is likely (but not always) the one causing further problems.\nThis is **not** a replacement for a proper log file: it is simply an overview.\nAlways be sure to provide your full log file to mod developers.\n\n", true)
ImGui.AddText(windowId, errortext, true, "ErrorDisplayText")
ImGui.AddElement(windowId, "ErrorDisplayCopyToClipboard", ImGuiElement.Button, "Copy to Clipboard")
ImGui.AddCallback("ErrorDisplayCopyToClipboard", ImGuiCallback.Clicked, function() Isaac.SetClipboard(errortext) end)
else
ImGui.UpdateText("ErrorDisplayText", errortext)
end
ImGui.SetVisible(windowId, true)
end
local function logError(callbackID, modName, err)
--[[ In order to squash multiple instances of an error into one line,
we want to get the *last* instance of "(x*)" for comparison here,
so we can remove our (x2) (x3) etc. from a comparison.
Unfortunately, Lua has no support for removing the last instance of a pattern,
but it very much has support for removing the *first* instance of a pattern.
For better or worse, we use a reversed version of both the target log and console history for comparison.
This is likely not the best for performance, but this only runs when there's an error anyways-
all bets are already off the table at that point.
]]
local ohistory = Console.GetHistory()
local history = {}
--[[ First, inverse the table, as console history is returned last-to-first.
Next, concatenate the table with \n so we can compare with our tracebacks.
]]
for i = #ohistory, 1, -1 do
history[#history + 1] = ohistory[i]
end
history = table.concat(history, "\n"):reverse()
local cbName = callbackIDToName[callbackID] or tostring(callbackID)
local consoleLog = '"' .. cbName .. '" from "' .. modName .. '" failed: ' .. err
-- We add a \n to our comparison to account for the parsed history having one.
local historyComparison = (consoleLog .. "\n"):reverse()
local parsedHistory = history:gsub("%)(%d+)x%(%s", "", 1):sub(1, #historyComparison)
if parsedHistory == historyComparison then
--[[ Determine how many entries to remove.
Newlines automatically increment the amount of strings we need to remove by 1.
Since we previously concatenated history into a string with newlines, we can count
the amount of newlines, and this will return the amount to pop.
]]
err_dupecount = err_dupecount + 1
Console.PopHistory(select(2, string.gsub(parsedHistory, "\n", "")))
consoleLog = consoleLog .. " (x" .. err_dupecount .. ")"
else
err_dupecount = 1
end
Console.PrintError(consoleLog)
Isaac.DebugString('Error in "' .. cbName .. '" call from "' .. modName .. '": ' .. err) -- this should be replaced with a proper log function so it can have the [INFO] header
if not err_shown then
imGuiError(consoleLog)
RaiseModError(modName)
err_shown = true
end
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- TYPE CHECKING FOR CALLBACK RETURN VALUES
----------------------------------------------------------------------------------------------------
local function typeCheckCallback(callback, callbackID, ret, ...)
local typeCheck = typecheckFunctions[callbackID]
if typeCheck then
local err
local typ = type(ret)
if typ == "userdata" then typ = GetMetatableType(ret) end
if typeCheck[typ] == true then
elseif typeCheck[typ] then
err = typeCheck[typ](ret, typ, ...)
else
err = "bad return type (" ..
typeCheck.expectedtypes .. " expected, got " .. tostring(typ) .. ")"
end
if err then
local info = debug_getinfo(callback.Function, "S")
logError(callbackID, callback.Mod.Name,
info.short_src .. ": " .. info.linedefined .. ": " .. err)
return err
end
end
local typeCheckWarn = typecheckWarnFunctions[callbackID]
if typeCheckWarn then
local warn
local typ = type(ret)
if typeCheckWarn[typ] == true then
elseif typeCheckWarn[typ] then
warn = typeCheckWarn[typ](ret, typ, ...)
else
warn = "bad return type (" ..
typeCheckWarn.expectedtypes .. " expected, got " .. tostring(typ) .. ")"
end
if warn then
local info = debug_getinfo(callback.Function, "S")
logWarning(callbackID, callback.Mod.Name,
info.short_src .. ": " .. info.linedefined .. ": " .. warn)
end
end
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- TYPE CHECKING FOR CALLBACK SYSTEM APIS (AddCallback, RemoveCallback, etc)
----------------------------------------------------------------------------------------------------
-- Function argument type checking for AddCallback, RemoveCallback, etc.
local CALLBACK_ID_TYPE = "Callback ID"
local function checkArgType(index, val, expectedType, level)
level = (level or 2) + 1
local actualType = type(val)
local valid
if expectedType == CALLBACK_ID_TYPE then
-- Literally anything except nil is a valid callback ID.
valid = (val ~= nil)
else
valid = (actualType == expectedType)
end
if not valid then
if debug_getinfo(level+1).short_src == "resources/scripts/main.lua" then
local name = debug_getinfo(level+1).name
if name == "AddCallback" or name == "RemoveCallback" or name == "AddPriorityCallback" then
-- Throw the error up an extra level for those mod table wrappers, to show the actual call site.
-- IE, mod:AddCallback() / mod:AddPriorityCallback() / mod:RemoveCallback().
level = level + 1
end
end
error(string.format("Bad argument #%d to '%s' (%s expected, got %s)", index, debug_getinfo(level).name, expectedType, actualType), level+1)
end
end
local function checkNumberArg(index, val, level) checkArgType(index, val, "number", level) end
local function checkFunctionArg(index, val, level) checkArgType(index, val, "function", level) end
local function checkCallbackIdArg(index, val, level) checkArgType(index, val, CALLBACK_ID_TYPE, level) end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- CALLBACK SYSTEM CORE
----------------------------------------------------------------------------------------------------
local Callbacks = {}
-- The new callback structure does not use this at all.
-- However, it's kept for the legacy list of "ALL" callbacks to accomodate some niche custom callback usage.
local legacyCallbackMeta = {
__matchParams = function(a, b)
return not a or not b or a == -1 or b == -1 or a == b
end
}
local function InitCallbackIfNeeded(callbackID)
if not Callbacks[callbackID] then
Callbacks[callbackID] = {
NUM_ADDED = 0,
ALL = setmetatable({}, legacyCallbackMeta),
COMMON = {},
PARAM = {},
}
end
end
-- Returns true if callback A should run before callback B.
local function CallbackComparator(a, b)
if not a or not b then
return a ~= nil
elseif a.Priority == b.Priority then
return a.AddOrder < b.AddOrder
else
return a.Priority < b.Priority
end
end
-- Support to apply a conversion function to the params for certain callbacks (ie, for stuff like tables/userdata).
local function ConvertItemConfigItemParam(param)
if param and type(param) == "userdata" and GetMetatableType(param) == "Item" then
return GetPtrHash(param)
end
return param
end
local CALLBACK_PARAM_CONVERSION_FUNCS = {
[ModCallbacks.MC_POST_PLAYER_TRIGGER_EFFECT_REMOVED] = ConvertItemConfigItemParam,
[ModCallbacks.MC_POST_ROOM_TRIGGER_EFFECT_REMOVED] = ConvertItemConfigItemParam,
[ModCallbacks.MC_POST_PLAYER_ADD_EFFECT] = ConvertItemConfigItemParam,
[ModCallbacks.MC_POST_ROOM_ADD_EFFECT] = ConvertItemConfigItemParam,
[ModCallbacks.MC_PRE_PLAYER_ADD_COSTUME] = ConvertItemConfigItemParam,
[ModCallbacks.MC_POST_PLAYER_ADD_COSTUME] = ConvertItemConfigItemParam,
[ModCallbacks.MC_PRE_PLAYER_REMOVE_COSTUME] = ConvertItemConfigItemParam,
[ModCallbacks.MC_POST_PLAYER_REMOVE_COSTUME] = ConvertItemConfigItemParam,
}
local function ConvertCallbackParam(callbackID, param)
if callbackID and param then
local convertFunc = CALLBACK_PARAM_CONVERSION_FUNCS[callbackID]
if convertFunc then
return convertFunc(param)
end
end
return param
end
-- Normally if you execute a callback with -1 as the param, it will run ALL added callbacks regardless of their specified param.
-- This behaviour exists for some vanilla callbacks that aren't intended to support optional params in the first place.
-- However, it can cause problems if a callback that DOES use params can get executed with -1 as the param, such as the first glitch item ID.
local RUN_CALLBACK_MINUS_ONE_PARAM_BLACKLIST = {
[ModCallbacks.MC_PRE_USE_ITEM] = true,
[ModCallbacks.MC_USE_ITEM] = true,
[ModCallbacks.MC_POST_USE_ITEM] = true,
[ModCallbacks.MC_PRE_ADD_COLLECTIBLE] = true,
[ModCallbacks.MC_POST_ADD_COLLECTIBLE] = true,
[ModCallbacks.MC_POST_TRIGGER_COLLECTIBLE_ADDED] = true,
[ModCallbacks.MC_POST_TRIGGER_COLLECTIBLE_REMOVED] = true,
[ModCallbacks.MC_POST_ADD_INNATE_COLLECTIBLE] = true,
[ModCallbacks.MC_POST_REMOVE_INNATE_COLLECTIBLE] = true,
[ModCallbacks.MC_PLAYER_GET_ACTIVE_MAX_CHARGE] = true,
[ModCallbacks.MC_PLAYER_GET_ACTIVE_MIN_USABLE_CHARGE] = true,
[ModCallbacks.MC_POST_DISCHARGE_ACTIVE_ITEM] = true,
[ModCallbacks.MC_PRE_PLAYERHUD_RENDER_ACTIVE_ITEM] = true,
[ModCallbacks.MC_POST_PLAYERHUD_RENDER_ACTIVE_ITEM] = true,
-- For the character select callbacks, -1 is the "random" character's "PlayerType".
[ModCallbacks.MC_PRE_RENDER_CHARACTER_SELECT_PORTRAIT] = true,
[ModCallbacks.MC_POST_RENDER_CHARACTER_SELECT_PORTRAIT] = true,
[ModCallbacks.MC_PRE_RENDER_CHARACTER_SELECT_PAGE] = true,
[ModCallbacks.MC_POST_RENDER_CHARACTER_SELECT_PAGE] = true,
}
-- Returns an iterator function that takes advantage of how callbacks are now mapped by their optional params.
-- Allows other code to easily iterate over callbacks using a param, in the correct execution order.
local function GetCallbackIterator(callbackID, param, extraParam)
local callbackData = Callbacks[callbackID]
if not callbackData then
-- No callbacks to run. Return an empty iterator.
return function() end
end
if param == -1 and not RUN_CALLBACK_MINUS_ONE_PARAM_BLACKLIST[callbackID] then
-- If the callback is executed with -1 as the param, run ALL callbacks.
-- This is sometimes used by the game for callbacks not intended to support optional params.
local allCallback = callbackData.ALL[1]
return function()
local nextCallback
-- Skip over removed callbacks if needed.
repeat
nextCallback = allCallback
if allCallback then
allCallback = allCallback.NextAll
end
until not nextCallback or not nextCallback.Removed
return nextCallback
end
end
param = ConvertCallbackParam(callbackID, param)
local commonCallback = callbackData.COMMON[1]
local paramCallback = param and callbackData.PARAM[param] and callbackData.PARAM[param][1]
local extraParamCallback = extraParam and callbackData.PARAM[extraParam] and callbackData.PARAM[extraParam][1]
if not paramCallback and extraParamCallback then
paramCallback = extraParamCallback
extraParamCallback = nil
end
if not paramCallback then
-- No parameterized callbacks to run, so just iterate over the common callbacks.
return function()
local nextCallback
-- Skip over removed callbacks if needed.
repeat
nextCallback = commonCallback
if commonCallback then
commonCallback = commonCallback.NextParam
end
until not nextCallback or not nextCallback.Removed
return nextCallback
end
end
if not extraParamCallback then
-- Simultaneously iterate over both the common callbacks and the relevant parameterized ones.
-- By comparing the Priority of the callbacks and the order they were added, we can iterate in the correct order.
return function()
local nextCallback
-- Skip over removed callbacks if needed.
repeat
if CallbackComparator(commonCallback, paramCallback) then
nextCallback = commonCallback
commonCallback = commonCallback.NextParam
elseif paramCallback then
nextCallback = paramCallback
paramCallback = paramCallback.NextParam
else
nextCallback = nil
end
until not nextCallback or not nextCallback.Removed
return nextCallback
end
end
-- New: Iterate over callbacks using... two parameters at once???
return function()
local nextCallback
repeat
nextCallback = commonCallback
if CallbackComparator(paramCallback, nextCallback) then
nextCallback = paramCallback
end
if CallbackComparator(extraParamCallback, nextCallback) then
nextCallback = extraParamCallback
end
if nextCallback == commonCallback and commonCallback then
commonCallback = commonCallback.NextParam
elseif nextCallback == paramCallback and paramCallback then
paramCallback = paramCallback.NextParam
elseif nextCallback == extraParamCallback and extraParamCallback then
extraParamCallback = extraParamCallback.NextParam
else
nextCallback = nil
end
until not nextCallback or not nextCallback.Removed
return nextCallback
end
end
rawset(Isaac, "GetCallbackIterator", function(callbackID, param)
checkCallbackIdArg(1, callbackID)
return GetCallbackIterator(callbackID, param)
end)
-- Maintain legacy Isaac.GetCallbacks behaviour as it is accessible to mods and expects a table of ALL the callbacks, in execution order.
rawset(Isaac, "GetCallbacks", function(callbackID, createIfMissing)
checkCallbackIdArg(1, callbackID)
if createIfMissing then
InitCallbackIfNeeded(callbackID)
end
return Callbacks[callbackID] and Callbacks[callbackID].ALL or {}
end)
-- Finds the correct spot to insert a new callback, taking into account CallbackPriority and the order they were added.
-- Utilizes Binary Search to improve performance when a lot of callbacks are added.
local function FindCallbackInsertPos(callbackList, newCallback)
-- Check the front or back of the list first.
if #callbackList == 0 or CallbackComparator(newCallback, callbackList[1]) then
return 1
elseif CallbackComparator(callbackList[#callbackList], newCallback) then
return #callbackList + 1
end
-- Ok, binary search time.
local minIdx = 2
local maxIdx = #callbackList - 1
local mid
while true do
mid = math.floor((maxIdx+minIdx)/2)
local callback = callbackList[mid]
if CallbackComparator(callback, newCallback) then
minIdx = mid + 1
if minIdx > maxIdx then
return mid+1
end
elseif CallbackComparator(newCallback, callback) then
maxIdx = mid - 1
if minIdx > maxIdx then
return mid
end
else
return mid
end
end
end
local function AddToCallbackList(callbackList, newCallback, isAllList)
local idx = FindCallbackInsertPos(callbackList, newCallback)
table.insert(callbackList, idx, newCallback)
local prevCallback = callbackList[idx-1]
local nextCallback = callbackList[idx+1]
-- Establish links to the previous/next callbacks in the list.
-- This is done to make removing callbacks while the callback is running a little safer.
if isAllList then
if prevCallback then
prevCallback.NextAll = newCallback
end
newCallback.NextAll = nextCallback
else
if prevCallback then
prevCallback.NextParam = newCallback
end
newCallback.NextParam = nextCallback
end
end
local DEPRECATED_CALLBACKS = {
[ModCallbacks.MC_POST_PLAYER_GET_MULTI_SHOT_PARAMS] = "MC_POST_PLAYER_GET_MULTI_SHOT_PARAMS is deprecated - please use MC_EVALUATE_MULTI_SHOT_PARAMS instead.",
[ModCallbacks.MC_PRE_PLAYER_APPLY_INNATE_COLLECTIBLE_NUM] = "MC_PRE_PLAYER_APPLY_INNATE_COLLECTIBLE_NUM is deprecated - please use functions such as player:AddInnateCollectible instead.",
[ModCallbacks.MC_PRE_OPENGL_RENDER] = "MC_PRE_OPENGL_RENDER is unused, and does not run.",
}
rawset(Isaac, "AddPriorityCallback", function(mod, callbackID, priority, fn, param)
checkCallbackIdArg(2, callbackID)
checkNumberArg(3, priority)
checkFunctionArg(4, fn)
if DEPRECATED_CALLBACKS[callbackID] then
local info = debug_getinfo(fn, "S")
logWarning(callbackID, mod and mod.Name or "???",
info.short_src .. ": " .. info.linedefined .. ": " .. DEPRECATED_CALLBACKS[callbackID])
end
param = ConvertCallbackParam(callbackID, param)
InitCallbackIfNeeded(callbackID)
local callbackData = Callbacks[callbackID]
local wasEmpty = (#callbackData.ALL == 0)
local numAdded = callbackData.NUM_ADDED + 1
callbackData.NUM_ADDED = numAdded
local newCallback = {Mod = mod, Function = fn, Priority = priority, AddOrder = numAdded, Param = param}
AddToCallbackList(callbackData.ALL, newCallback, true)
-- Callbacks with -1 as their param behave the same as those with no param.
-- This aligns with the behaviour of the old implementation.
if param and param ~= -1 then
if not callbackData.PARAM[param] then
callbackData.PARAM[param] = {}
end
AddToCallbackList(callbackData.PARAM[param], newCallback, false)
else
AddToCallbackList(callbackData.COMMON, newCallback, false)
end
if wasEmpty then
if type(callbackID) == "number" then
-- Enable this callback
Isaac.SetBuiltInCallbackState(callbackID, true)
end
end
end)
rawset(Isaac, "AddCallback", function(mod, callbackID, fn, param)
checkCallbackIdArg(2, callbackID)
checkFunctionArg(3, fn)
Isaac.AddPriorityCallback(mod, callbackID, CallbackPriority.DEFAULT, fn, param)
end)
local function RemoveCallbacksIf(callbackList, removeConditionFunc, isAllList)
local removed = false
for i=#callbackList,1,-1 do
local callback = callbackList[i]
if removeConditionFunc(callback) then
-- Fix the iteration link of the previous callback.
if callbackList[i-1] then
if isAllList then
callbackList[i-1].NextAll = callback.NextAll
else
callbackList[i-1].NextParam = callback.NextParam
end
end
callback.Removed = true
table.remove(callbackList, i)
removed = true
end
end
return removed
end
local function RemoveAllCallbacksIf(callbackID, removeConditionFunc)
local callbackData = Callbacks[callbackID]
if callbackData and RemoveCallbacksIf(callbackData.ALL, removeConditionFunc, true) then
RemoveCallbacksIf(callbackData.COMMON, removeConditionFunc, false)
for param, paramCallbacks in pairs(callbackData.PARAM) do
if RemoveCallbacksIf(paramCallbacks, removeConditionFunc, false) and #paramCallbacks == 0 then
callbackData.PARAM[param] = nil
end
end
if #callbackData.ALL == 0 then
if type(callbackID) == "number" then
-- No more functions left, disable this callback
Isaac.SetBuiltInCallbackState(callbackID, false)
else
Callbacks[callbackID] = nil
end
end
return true
end
end
rawset(Isaac, "RemoveCallback", function(mod, callbackID, fn)
checkCallbackIdArg(2, callbackID)
checkFunctionArg(3, fn)
RemoveAllCallbacksIf(callbackID, function(callback) return callback.Function == fn end)
end)
local function RemoveAllCallbacksForMod(mod)
for callbackID, callbackData in pairs(Callbacks) do
RemoveAllCallbacksIf(callbackID, function(callback) return callback.Mod == mod end)
end
end
function _UnloadMod(mod)
-- _UnloadMod is called late into the shutdown process where it is not safe to run a callback.
-- We separately trigger MC_PRE_MOD_UNLOAD earlier in the shutdown process.
-- rebuild: vanilla parity runs it as main.lua does
if IsVanillaParity() then
Isaac.RunCallback(ModCallbacks.MC_PRE_MOD_UNLOAD, mod)
elseif not Isaac.IsShuttingDown() then
Isaac.RunCallback(ModCallbacks.MC_PRE_MOD_UNLOAD, mod, false)
end
RemoveAllCallbacksForMod(mod)
ModManager.detail.UnloadMod(mod)
end
-- Runs a single callback function and checks the results.
-- Only returns the value returned by the callback if no errors occured and the return value passed the type checks.
local function RunCallbackInternal(callbackID, callback, ...)
local status, ret = xpcall(callback.Function, function(msg)
return msg .. "\n" .. cleanTraceback(2)
end, callback.Mod, ...)
if not status then
logError(callbackID, callback.Mod.Name, ret)
elseif ret ~= nil and not typeCheckCallback(callback, callbackID, ret, ...) then
return ret
end
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
---- RUNCALLBACK LOGIC
----------------------------------------------------------------------------------------------------
-- Default callback behaviour (first returned non-nil value terminates the callback).
local function DefaultRunCallbackLogic(callbackID, callbackIterator, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, ...)
if ret ~= nil then
return ret
end
end
end
-- Slightly modified callback that only breaks on returning false specifically.
local function RunFalseBreakCallbackLogic(callbackID, callbackIterator, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, ...)
if ret == false then
return ret
end
end
end
-- For callbacks with no return values that don't want to allow mods to terminate them early.
local function RunNoReturnCallback(callbackID, callbackIterator, ...)
for callback in callbackIterator do
RunCallbackInternal(callbackID, callback, ...)
end
end
-- Basic "additive" callback behaviour. Values returned from a callback replace the value of the FIRST arg for subsequent callbacks.
-- Separate implementations are used depending on which arg is updated by the return value, because table.unpack tricks are slower.
local function RunAdditiveFirstArgCallback(callbackID, callbackIterator, value, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, value, ...)
if ret ~= nil then
value = ret
end
end
return value
end
local function RunAdditiveSecondArgCallback(callbackID, callbackIterator, arg1, value, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, arg1, value, ...)
if ret ~= nil then
value = ret
end
end
return value
end
local function RunAdditiveSecondArgCallbackWithBreak(callbackID, callbackIterator, arg1, value, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, arg1, value, ...)
if type(ret) == "boolean" then
if ret == false then
return ret
end
elseif ret ~= nil then
value = ret
end
end
return value
end
local function RunAdditiveThirdArgCallback(callbackID, callbackIterator, arg1, arg2, value, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, arg1, arg2, value, ...)
if ret ~= nil then
value = ret
end
end
return value
end
local function RunAdditiveThirdArgCallbackWithBreak(callbackID, callbackIterator, arg1, arg2, value, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, arg1, arg2, value, ...)
if type(ret) == "boolean" then
if ret == false then
return ret
end
elseif ret ~= nil then
value = ret
end
end
return value
end
local function RunAdditiveFourthArgCallback(callbackID, callbackIterator, arg1, arg2, arg3, value, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, arg1, arg2, arg3, value, ...)
if ret ~= nil then
value = ret
end
end
return value
end
local function RunPostModsLoadedCallback(callbackID, callbackIterator, ...)
DefaultRunCallbackLogic(callbackID, callbackIterator, ...)
ModManager.detail.OnModsLoaded()
end
local function RunPreAddCardPillCallback(callbackID, callbackIterator, player, pillCard, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, player, pillCard, ...)
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "number" and ret > 0 then
pillCard = ret
end
end
return pillCard
end
local function IsValidMultiShotParams(params)
return params ~= nil and type(params) == "userdata" and GetMetatableType(params) == "MultiShotParams"
end
local function RunGetMultiShotParamsCallback(callbackID, callbackIterator, player, multiShotParams, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, player, multiShotParams, ...)
if IsValidMultiShotParams(ret) then
multiShotParams = ret
end
end
return multiShotParams
end
function _RunGetMultiShotParamsCallbackWithLegacyCompat(param, player, multiShotParams, ...)
-- Run legacy callback first (didn't pass a modifiable MultiShotParams along, incentivised recursing GetMultiShotParams
-- inside the callback, returning a MultiShotParams terminated the callback and made it hard for multiple mods to modify).
-- Replacing it was cleaner for backwards compatability's sake, and we can just run the legacy callbacks here with no extra C jumps.
local legacyResult = Isaac.RunCallbackWithParam(ModCallbacks.MC_POST_PLAYER_GET_MULTI_SHOT_PARAMS, param, player)
if IsValidMultiShotParams(legacyResult) then
multiShotParams = legacyResult
end
return Isaac.RunCallbackWithParam(ModCallbacks.MC_EVALUATE_MULTI_SHOT_PARAMS, param, player, multiShotParams, ...)
end
-- Custom behaviour for pre-render callbacks (terminate on false, adds returned vectors to the render offset).
local function RunPreRenderCallback(callbackID, callbackIterator, mt, value, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, mt, value, ...)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
return false
elseif ret.X and ret.Y then -- We're a Vector, checking it directly will crash the game... While we should always be a Vector at this point it doesn't hurt to check
value = value + ret
end
end
end
return value
end
-- Custom handling for the MC_ENTITY_TAKE_DMG rewrite, so if a mod changes the damage amount etc that doesn't terminate the callback
-- and the updated values are shown to later callbacks. The callback also now ONLY terminates early if FALSE is returned.
local function RunEntityTakeDmgCallback(callbackID, callbackIterator, entity, damage, damageFlags, source, damageCountdown, extraSource)
local combinedRet
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, entity, damage, damageFlags, source, damageCountdown, extraSource)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
-- Only terminate the callback early if someone returns FALSE.
-- RIP to returning true stopping the callback.
return false
elseif type(ret) == "table" then
-- Set / update overrides to damage etc so that they are visible to later callbacks.
if ret.Damage and type(ret.Damage) == "number" then
damage = ret.Damage
end
if ret.DamageFlags and type(ret.DamageFlags) == "number" and math.tointeger(ret.DamageFlags) then
damageFlags = ret.DamageFlags
end
if ret.DamageCountdown and type(ret.DamageCountdown) == "number" then
damageCountdown = ret.DamageCountdown
end
if combinedRet then
for k, v in pairs(ret) do
combinedRet[k] = v
end
else
combinedRet = ret
end
end
end
end
return combinedRet
end
local function RunAccumulateReturnTableCallback(callbackID, callbackIterator, ...)
local retTable
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, ...)
if ret ~= nil then
if type(ret) == "boolean" then
return ret
elseif type(ret) == "table" then
if retTable then
for k, v in pairs(ret) do
retTable[k] = v
end
else
retTable = ret
end
end
end
end
return retTable
end
local function RunPreAddCollectibleCallback(callbackID, callbackIterator, collectibleType, charge, firstTime, slot, vardata, ...)
local retType
local retTable
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, collectibleType, charge, firstTime, slot, vardata, ...)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "number" then
retType = ret
collectibleType = ret
if retTable then
retTable.Type = ret
end
elseif type(ret) == "table" then
-- Set / update overrides so that they are visible to later callbacks.
collectibleType = ret[1] or collectibleType
charge = ret[2] or charge
if ret[3] ~= nil then
firstTime = ret[3]
end
slot = ret[4] or slot
vardata = ret[5] or vardata
if retTable then
for k, v in pairs(ret) do
retTable[k] = v
end
else
retTable = ret
retTable[1] = retTable[1] or collectibleType
end
end
end
end
return retTable or retType
end
local function RunPreAddTrinketCallback(callbackID, callbackIterator, player, trinketType, firstTime, ...)
local retType
local retTable
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, player, trinketType, firstTime, ...)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "number" then
retType = ret
trinketType = ret
if retTable then
retTable.Type = ret
end
elseif type(ret) == "table" then
-- Set / update overrides so that they are visible to later callbacks.
trinketType = ret[1] or trinketType
if ret[2] ~= nil then
firstTime = ret[2]
end
if retTable then
for k, v in pairs(ret) do
retTable[k] = v
end
else
retTable = ret
retTable[1] = retTable[1] or trinketType
end
end
end
end
return retTable or retType
end
-- Custom handling for MC_PRE_TRIGGER_PLAYER_DEATH and MC_TRIGGER_PLAYER_DEATH_POST_CHECK_REVIVES.
-- Terminate early if the player is revived by any means.
local function RunTriggerPlayerDeathCallback(callbackID, callbackIterator, player, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, player, ...)
if not player:IsDead() or not player:Exists() then
return
end
if ret == false then
return false
end
end
return true
end
-- Custom handling for MC_POST_PICKUP_SELECTION.
-- Terminate early if the table's 3rd argument is nil or false
local function RunPostPickupSelectionCallback(callbackID, callbackIterator, pickup, variant, subType, ...)
local recentRet = nil
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, pickup, variant, subType, ...)
if type(ret) == "table" then
if not ret[3] then
return ret
end
if (math.type(ret[1]) == "integer" and math.type(ret[2]) == "integer") then
recentRet = {ret[1], ret[2]}
variant = ret[1]
subType = ret[2]
end
end
end
return recentRet
end
local function RunTryAddToBagOfCraftingCallback(callbackID, callbackIterator, player, pickup, result, ...)
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, player, pickup, result)
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "table" then
result = {}
for i=1,8 do
if not ret[i] or ret[i] <= BagOfCraftingPickup.BOC_NONE or ret[i] > BagOfCraftingPickup.BOC_POOP then
break
else
table.insert(result, ret[i])
end
end
end
end
return result
end
local function RunPreApplyTearflagEffectsCallback(callbackID, callbackIterator, entity, pos, flags, source, damage)
local combinedRet
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, entity, pos, flags, source, damage)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "table" then
-- Set / update modified values so that they are visible to later callbacks.
if ret.Damage and type(ret.Damage) == "number" then
damage = ret.Damage
end
if ret.TearFlags and type(ret.TearFlags) == "userdata" and GetMetatableType(ret.TearFlags) == "BitSet128" then
flags = ret.TearFlags
end
if ret.Position and type(ret.Position) == "userdata" and GetMetatableType(ret.Position) == "Vector" then
pos = ret.Position
end
if combinedRet then
for k, v in pairs(ret) do
combinedRet[k] = v
end
else
combinedRet = ret
end
end
end
end
return combinedRet
end
local preStatusApplyReturnTableTypes = {
[StatusEffect.CONFUSION] = checkTableTypeFunction({ "integer", "boolean" }),
[StatusEffect.CHARMED] = checkTableTypeFunction({ "integer", "boolean" }),
[StatusEffect.FEAR] = checkTableTypeFunction({ "integer", "boolean" }),
[StatusEffect.FREEZE] = checkTableTypeFunction({ "integer", "boolean" }),
[StatusEffect.MIDAS_FREEZE] = checkTableTypeFunction({ "integer", "boolean" }),
[StatusEffect.SHRINK] = checkTableTypeFunction({ "integer", "boolean" }),
[StatusEffect.KNOCKBACK] = checkTableTypeFunction({ "integer", "Vector", "boolean" }),
[StatusEffect.SLOWING] = checkTableTypeFunction({ "integer", "number", "Color", "boolean" }),
[StatusEffect.POISON] = checkTableTypeFunction({ "integer", "number", "boolean" }),
[StatusEffect.BURN] = checkTableTypeFunction({ "integer", "number", "boolean" }),
}
-- Custom handling for MC_PRE_STATUS_EFFECT_APPLY
-- Handle type checking here since the callback is called for several status effects with different types,
-- terminate early if false is returned,
-- and use additive callback logic instead of the default one.
local function RunPreStatusEffectApplyCallback(callbackID, callbackIterator, status, entity, entityRef, duration, extraParam1, extraParam2, extraParam3)
local recentRet = nil
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, status, entity, entityRef, duration, extraParam1, extraParam2, extraParam3)
if type(ret) == "boolean" then
if ret == false then return false end
elseif type(ret) == "table" then
if preStatusApplyReturnTableTypes[status] then
local err = preStatusApplyReturnTableTypes[status](ret)
if err then
logError(callbackID, callback.Mod.Name, err)
else
if ret[1] ~= nil then duration = ret[1] end
if ret[2] ~= nil then extraParam1 = ret[2] end
if ret[3] ~= nil then extraParam2 = ret[3] end
if ret[4] ~= nil then extraParam3 = ret[4] end
recentRet = { duration, extraParam1, extraParam2, extraParam3 }
end
else
logError(callbackID, callback.Mod.Name, "bad return type (table not expected for status)")
end
elseif type(ret) == "number" then
if type(recentRet) == "table" then
recentRet[1] = ret
else
recentRet = ret
end
duration = ret
end
end
return recentRet
end
local function RunPreBombDamageCallback(callbackID, callbackIterator, pos, damage, radius, lineCheck, source, tearFlags, damageFlags, damageSource)
local combinedRet
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, pos, damage, radius, lineCheck, source, tearFlags, damageFlags, damageSource)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "table" then
if ret.Position and type(ret.Position) == "userdata" and GetMetatableType(ret.Position) == "Vector" then
pos = ret.Position
end
if ret.Damage and type(ret.Damage) == "number" then
damage = ret.Damage
end
if ret.Radius and type(ret.Radius) == "number" then
radius = ret.Radius
end
if ret.TearFlags and type(ret.TearFlags) == "userdata" and GetMetatableType(ret.TearFlags) == "BitSet128" then
tearFlags = ret.TearFlags
end
if ret.DamageFlags and type(ret.DamageFlags) == "number" and math.tointeger(ret.DamageFlags) then
damageFlags = ret.DamageFlags
end
if combinedRet then
for k, v in pairs(ret) do
combinedRet[k] = v
end
else
combinedRet = ret
end
end
end
end
return combinedRet
end
local function RunPreBombTearFlagEffectsCallback(callbackID, callbackIterator, pos, radius, tearFlags, source, radiusMult)
local combinedRet
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, pos, radius, tearFlags, source, radiusMult)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "table" then
if ret.Position and type(ret.Position) == "userdata" and GetMetatableType(ret.Position) == "Vector" then
pos = ret.Position
end
if ret.Radius and type(ret.Radius) == "number" then
radius = ret.Radius
end
if ret.RadiusMult and type(ret.RadiusMult) == "number" then
radiusMult = ret.RadiusMult
end
if ret.TearFlags and type(ret.TearFlags) == "userdata" and GetMetatableType(ret.TearFlags) == "BitSet128" then
tearFlags = ret.TearFlags
end
if combinedRet then
for k, v in pairs(ret) do
combinedRet[k] = v
end
else
combinedRet = ret
end
end
end
end
return combinedRet
end
local function RunPreHistoryHudRenderCallback(callbackID, callbackIterator, ...)
local combinedRet = {
HideCollectibles = {},
HideTrinkets = {},
}
for callback in callbackIterator do
local ret = RunCallbackInternal(callbackID, callback, ...)
if ret ~= nil then
if type(ret) == "boolean" and ret == false then
return false
elseif type(ret) == "table" then
if ret.HideCollectibles and type(ret.HideCollectibles) == "table" then
for _, id in pairs(ret.HideCollectibles) do
combinedRet.HideCollectibles[id] = true
end
end
if ret.HideTrinkets and type(ret.HideTrinkets) == "table" then
for _, id in pairs(ret.HideTrinkets) do
combinedRet.HideTrinkets[id] = true
end
end
end
end
end
return combinedRet
end
-- Legacy globals. Safer to just leave them alone since they were already exposed. Don't use these.
function _RunPreRenderCallback(callbackID, param, ...)
return RunPreRenderCallback(callbackID, GetCallbackIterator(callbackID, param), ...)
end
function _RunAdditiveCallback(callbackID, value, ...)
return RunAdditiveFirstArgCallback(callbackID, GetCallbackIterator(callbackID), value, ...)
end
function _RunEntityTakeDmgCallback(callbackID, param, ...)
return RunEntityTakeDmgCallback(callbackID, GetCallbackIterator(callbackID, param), ...)
end
function _RunPostPickupSelection(callbackID, param, ...)
return RunPostPickupSelectionCallback(callbackID, GetCallbackIterator(callbackID, param), ...)
end
function _RunTriggerPlayerDeathCallback(callbackID, param, ...)
return RunTriggerPlayerDeathCallback(callbackID, GetCallbackIterator(callbackID, param), ...)
end
rawset(Isaac, "RunPreRenderCallback", _RunPreRenderCallback)
rawset(Isaac, "RunAdditiveCallback", _RunAdditiveCallback)
rawset(Isaac, "RunEntityTakeDmgCallback", _RunEntityTakeDmgCallback)
rawset(Isaac, "RunTriggerPlayerDeathCallback", _RunTriggerPlayerDeathCallback)
-- Defines non-default callback handling logic to be used for specific callbacks.
-- If a callback is not specified here, "DefaultRunCallbackLogic" will be called.
local CustomRunCallbackLogic = {
[ModCallbacks.MC_ENTITY_TAKE_DMG] = RunEntityTakeDmgCallback,
[ModCallbacks.MC_EVALUATE_MULTI_SHOT_PARAMS] = RunGetMultiShotParamsCallback,
[ModCallbacks.MC_PRE_APPLY_TEARFLAG_EFFECTS] = RunPreApplyTearflagEffectsCallback,
[ModCallbacks.MC_POST_APPLY_TEARFLAG_EFFECTS] = RunNoReturnCallback,
[ModCallbacks.MC_POST_CURSE_EVAL] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_POST_ENTITY_REMOVE] = RunNoReturnCallback,
[ModCallbacks.MC_POST_PICKUP_SELECTION] = RunPostPickupSelectionCallback,
[ModCallbacks.MC_PRE_TRIGGER_PLAYER_DEATH] = RunTriggerPlayerDeathCallback,
[ModCallbacks.MC_TRIGGER_PLAYER_DEATH_POST_CHECK_REVIVES] = RunTriggerPlayerDeathCallback,
[ModCallbacks.MC_TRY_ADD_TO_BAG_OF_CRAFTING] = RunTryAddToBagOfCraftingCallback,
[ModCallbacks.MC_PRE_PLAYER_APPLY_INNATE_COLLECTIBLE_NUM] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_PRE_DEVIL_APPLY_ITEMS] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_PRE_DEVIL_APPLY_SPECIAL_ITEMS] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_POST_DEVIL_CALCULATE] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_PRE_PLANETARIUM_APPLY_ITEMS] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_PRE_PLANETARIUM_APPLY_TELESCOPE_LENS] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_POST_PLANETARIUM_CALCULATE] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_EVALUATE_CUSTOM_CACHE] = RunAdditiveThirdArgCallback,
[ModCallbacks.MC_EVALUATE_FAMILIAR_MULTIPLIER] = RunAdditiveSecondArgCallback,
[ModCallbacks.MC_PRE_PLAYER_ADD_CARD] = RunPreAddCardPillCallback,
[ModCallbacks.MC_PRE_PLAYER_ADD_PILL] = RunPreAddCardPillCallback,
[ModCallbacks.MC_PLAYER_GET_ACTIVE_MIN_USABLE_CHARGE] = RunAdditiveThirdArgCallback,
[ModCallbacks.MC_PLAYER_GET_ACTIVE_MAX_CHARGE] = RunAdditiveFourthArgCallback,
[ModCallbacks.MC_PRE_STATUS_EFFECT_APPLY] = RunPreStatusEffectApplyCallback,
[ModCallbacks.MC_PLAYER_HEALTH_TYPE_CHANGE] = RunNoReturnCallback,
[ModCallbacks.MC_EVALUATE_STAT] = RunAdditiveThirdArgCallback,
[ModCallbacks.MC_EVALUATE_TEAR_HIT_PARAMS] = RunAdditiveSecondArgCallback,
[ModCallbacks.MC_PRE_PLAYERHUD_RENDER_ACTIVE_ITEM] = RunAccumulateReturnTableCallback,
[ModCallbacks.MC_PRE_ADD_COLLECTIBLE] = RunPreAddCollectibleCallback,
[ModCallbacks.MC_PRE_ADD_TRINKET] = RunPreAddTrinketCallback,
[ModCallbacks.MC_POST_ADD_COLLECTIBLE] = RunNoReturnCallback,
[ModCallbacks.MC_PLAYER_GET_HEART_LIMIT] = RunAdditiveSecondArgCallback,
[ModCallbacks.MC_PRE_RENDER_CHARACTER_SELECT_PORTRAIT] = RunAdditiveThirdArgCallbackWithBreak,
[ModCallbacks.MC_POST_RENDER_CHARACTER_SELECT_PORTRAIT] = RunNoReturnCallback,
[ModCallbacks.MC_PRE_RENDER_CHARACTER_SELECT_PAGE] = RunFalseBreakCallbackLogic,
[ModCallbacks.MC_POST_RENDER_CHARACTER_SELECT_PAGE] = RunNoReturnCallback,
[ModCallbacks.MC_PRE_BOMB_DAMAGE] = RunPreBombDamageCallback,
[ModCallbacks.MC_POST_BOMB_DAMAGE] = RunNoReturnCallback,
[ModCallbacks.MC_PRE_BOMB_TEARFLAG_EFFECTS] = RunPreBombTearFlagEffectsCallback,
[ModCallbacks.MC_POST_BOMB_TEARFLAG_EFFECTS] = RunNoReturnCallback,
[ModCallbacks.MC_PRE_HISTORYHUD_RENDER] = RunPreHistoryHudRenderCallback,
[ModCallbacks.MC_POST_HISTORYHUD_RENDER] = RunNoReturnCallback,
[ModCallbacks.MC_POST_HISTORYHUD_RECOMPUTE] = RunNoReturnCallback,
[ModCallbacks.MC_CAN_SELECT_CHARACTER] = RunFalseBreakCallbackLogic,
[ModCallbacks.MC_PRE_GRID_HURT] = RunAdditiveSecondArgCallbackWithBreak,
[ModCallbacks.MC_POST_GRID_HURT] = RunNoReturnCallback,
[ModCallbacks.MC_POST_MODS_LOADED] = RunPostModsLoadedCallback,
[ModCallbacks.MC_PRE_RENDER_PLAYER_BODY] = RunAdditiveSecondArgCallbackWithBreak,
[ModCallbacks.MC_PRE_RENDER_PLAYER_HEAD] = RunAdditiveSecondArgCallbackWithBreak,
[ModCallbacks.MC_PRE_LEVEL_PLACE_ROOM] = RunAdditiveSecondArgCallback,
[ModCallbacks.MC_PRE_SELECT_AMBUSH_WAVE] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_PRE_SELECT_GREED_WAVE] = RunAdditiveFirstArgCallback,
[ModCallbacks.MC_PRE_PLAYERHUD_RENDER_INVENTORY] = RunAdditiveSecondArgCallbackWithBreak,
[ModCallbacks.MC_POST_PLAYERHUD_RENDER_INVENTORY] = RunNoReturnCallback,
[ModCallbacks.MC_PRE_PLAYERHUD_RENDER_POOP_SPELL_QUEUE] = RunAdditiveSecondArgCallbackWithBreak,
[ModCallbacks.MC_POST_PLAYERHUD_RENDER_POOP_SPELL_QUEUE] = RunNoReturnCallback,
[ModCallbacks.MC_PRE_PLAYERHUD_RENDER_CRAFTING_TABLE] = RunAdditiveSecondArgCallbackWithBreak,
[ModCallbacks.MC_POST_PLAYERHUD_RENDER_CRAFTING_TABLE] = RunNoReturnCallback,
[ModCallbacks.MC_POST_USE_ITEM] = RunNoReturnCallback,
}
for _, callback in ipairs({
ModCallbacks.MC_PRE_FAMILIAR_RENDER,
ModCallbacks.MC_PRE_NPC_RENDER,
ModCallbacks.MC_PRE_PLAYER_RENDER,
ModCallbacks.MC_PRE_PICKUP_RENDER,
ModCallbacks.MC_PRE_TEAR_RENDER,
ModCallbacks.MC_PRE_PROJECTILE_RENDER,
ModCallbacks.MC_PRE_KNIFE_RENDER,
ModCallbacks.MC_PRE_EFFECT_RENDER,
ModCallbacks.MC_PRE_BOMB_RENDER,
ModCallbacks.MC_PRE_SLOT_RENDER,
}) do
CustomRunCallbackLogic[callback] = RunPreRenderCallback
end
local function RunCallbackOverIterator(callbackID, callbackIterator, ...)
local runCallbackLogic = CustomRunCallbackLogic[callbackID] or DefaultRunCallbackLogic
return runCallbackLogic(callbackID, callbackIterator, ...)
end
rawset(Isaac, "RunCallbackOverIterator", RunCallbackOverIterator)
-- rebuild: vanilla main.lua's _RunCallback over the same callbacks, which runs the original's while vanilla parity is on
local function RunVanillaCallback(callbackID, param, ...)
local callbackData = Callbacks[callbackID]
if callbackData then
local callbacks = callbackData.ALL
local matchFunc = getmetatable(callbacks).__matchParams or legacyCallbackMeta.__matchParams
for _,v in ipairs(callbacks) do
if matchFunc(param, v.Param) then
local result = v.Function(v.Mod, ...)
if result ~= nil then
return result
end
end
end
end
end
function _RunCallback(callbackID, param, ...)
if IsVanillaParity() and not (type(callbackID) == "number" and callbackID >= 1000) then
return RunVanillaCallback(callbackID, param, ...)
end
local callbackIterator = GetCallbackIterator(callbackID, param)
return RunCallbackOverIterator(callbackID, callbackIterator, ...)
end
Isaac.RunCallbackWithParam = _RunCallback
function Isaac.RunCallback(callbackID, ...)
return Isaac.RunCallbackWithParam(callbackID, nil, ...)
end
function _RunCallbackWithTwoParams(callbackID, param1, param2, ...)
local callbackIterator = GetCallbackIterator(callbackID, param1, param2)
return RunCallbackOverIterator(callbackID, callbackIterator, ...)
end
rawset(Isaac, "RunCallbackWithTwoParams", _RunCallbackWithTwoParams)
-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
---- MISC STUFF
-----------------------------------------------------------------------------------------------------
local Original_HasModData = Isaac.HasModData
local Original_SaveModData = Isaac.SaveModData
local Original_LoadModData = Isaac.LoadModData
local Original_RemoveModData = Isaac.RemoveModData
local ModReference_HasModFile = _CBindings.ModReference_HasModFile
local ModReference_SaveModFile = _CBindings.ModReference_SaveModFile
local ModReference_LoadModFile = _CBindings.ModReference_LoadModFile
local ModReference_RemoveModFile = _CBindings.ModReference_RemoveModFile
rawset(Isaac, "HasModData", function(modRef, filename)
if type(filename) == "string" then
return ModReference_HasModFile(modRef, filename)
else
return Original_HasModData(modRef)
end
end)
rawset(Isaac, "SaveModData", function(modRef, data, filename)
if type(filename) == "string" then
return ModReference_SaveModFile(modRef, filename, data)
else
return Original_SaveModData(modRef, data)
end
end)
rawset(Isaac, "LoadModData", function(modRef, filename)
if type(filename) == "string" then
return ModReference_LoadModFile(modRef, filename)
else
return Original_LoadModData(modRef)
end
end)
rawset(Isaac, "RemoveModData", function(modRef, filename)
if type(filename) == "string" then
return ModReference_RemoveModFile(modRef, filename)
else
return Original_RemoveModData(modRef)
end
end)
--Menuman Hub
MenuManager.MainMenu = MainMenu
MenuManager.CharacterMenu = CharacterMenu
MenuManager.StatsMenu = StatsMenu
-- ImGui alias functions
--local ImGui = Isaac.GetImGui()
ImGui.WorldToImGui = function (world_pos)
local scr_xy = Isaac.WorldToScreen(world_pos)
-- scale the screen xy to the window rectangle
local _, imgui_wh = ImGui.GetGameWindowRect()
return Vector(scr_xy.X * imgui_wh.X / Isaac.GetScreenWidth(), scr_xy.Y * imgui_wh.Y / Isaac.GetScreenHeight())
end
ImGui.ImGuiToWorld = function (imgui_pos)
local _, imgui_wh = ImGui.GetGameWindowRect()
return Isaac.RenderToWorld(Vector(imgui_pos.X * Isaac.GetScreenWidth() / imgui_wh.X, imgui_pos.Y * Isaac.GetScreenHeight() / imgui_wh.Y))
end
if not _LUADEBUG then
debug = nil
if os then -- rebuild: vanilla parity leaves os closed
os = {
date = os.date,
time = os.time,
clock = os.clock,
difftime = os.difftime
}
end
end
--Object Cache stuff
local gaymeinstance
local oldgayme = Game
function Game()
if not gaymeinstance then
gaymeinstance = oldgayme()
end
return gaymeinstance
end
local oldregmod = RegisterMod
function RegisterMod(name, ver)
local out = oldregmod(name, ver)
out.Repentogon = REPENTOGON
ModManager.detail.RegisterMod(out)
out.SaveData = function(self, ...) return Isaac.SaveModData(self, ...) end
out.LoadData = function(self, ...) return Isaac.LoadModData(self, ...) end
out.HasData = function(self, ...) return Isaac.HasModData(self, ...) end
out.RemoveData = function(self, ...) Isaac.RemoveModData(self, ...) end
return out
end
rawset(Isaac, "GetEntityConfig", function() return EntityConfig end)
RoomConfigHolder = RoomConfig -- Backwards compatability
-- Reset Imgui Data after reload of all mods
ImGui.Reset()
--resource load error, used in changelog but it's not strictly a changelog thing so i'm putting it here
local res_error_font=Font()
local loaded_reserror_font=""
local curlocale="default"
local reserror_text={
["en"]={"Unable to load REPENTOGON",
"resources. Please make sure",
"that all files are copied",
"correctly and that mods are",
"enabled in the mods menu!",},
["ru"]={"Не удалось загрузить ресурсы",
"REPENTOGON-а. Просьба проверить",
"наличие всех файлов и убедиться,",
"что моды включены в меню модов!",},
["zh"]={"无法加载 REPENTOGON 资源。",
"请确保正确复制了所有文件,并在 mod",
"菜单中启用了 mod!",},
}
local reserror_fonts={
["en"]={"font/pftempestasevencondensed.fnt",0,0}, --font name, hori and vert offsets relative to en replacement
["ru"]={"font/pftempestasevencondensed.fnt",-30,0},
["zh"]={"font/cjk/lanapixel.fnt",-15,0},
}
local function NoRepentogonFolderErrRender()
-- if MenuManager:GetActiveMenu()==MainMenuType.TITLE then
curlocale=Options.Language
if not reserror_text[curlocale] then curlocale="en" end
if loaded_reserror_font~=reserror_fonts[curlocale][1] or (not res_error_font:IsLoaded()) then res_error_font:Load(reserror_fonts[curlocale][1]) loaded_reserror_font=reserror_fonts[curlocale][1] end
local rendercoords=Isaac.WorldToMenuPosition(MainMenuType.TITLE,Vector(260+reserror_fonts[curlocale][2],180+reserror_fonts[curlocale][3]))
for i,line in ipairs(reserror_text[curlocale]) do
res_error_font:DrawStringUTF8(line,rendercoords.X,rendercoords.Y + 12*(i-1) ,KColor(0.7,0.1,0.1,1.0),0,false)
end
-- end
end
REPENTOGON.Extras.Misc.NoRPTGNFldrErr=NoRepentogonFolderErrRender
--res load error stuff end
local RoomShapeToDoorMask = {
[RoomShape.ROOMSHAPE_1x1] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0,
[RoomShape.ROOMSHAPE_IH] = DoorMask.LEFT0 | DoorMask.RIGHT0,
[RoomShape.ROOMSHAPE_IV] = DoorMask.UP0 | DoorMask.DOWN0,
[RoomShape.ROOMSHAPE_1x2] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0 | DoorMask.LEFT1 | DoorMask.RIGHT1,
[RoomShape.ROOMSHAPE_IIV] = DoorMask.UP0 | DoorMask.DOWN0,
[RoomShape.ROOMSHAPE_2x1] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0 | DoorMask.UP1 | DoorMask.DOWN1,
[RoomShape.ROOMSHAPE_IIH] = DoorMask.LEFT0 | DoorMask.RIGHT0,
[RoomShape.ROOMSHAPE_2x2] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0 | DoorMask.LEFT1 | DoorMask.UP1 | DoorMask.RIGHT1 | DoorMask.DOWN1,
[RoomShape.ROOMSHAPE_LTL] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0 | DoorMask.LEFT1 | DoorMask.UP1 | DoorMask.RIGHT1 | DoorMask.DOWN1,
[RoomShape.ROOMSHAPE_LTR] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0 | DoorMask.LEFT1 | DoorMask.UP1 | DoorMask.RIGHT1 | DoorMask.DOWN1,
[RoomShape.ROOMSHAPE_LBL] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0 | DoorMask.LEFT1 | DoorMask.UP1 | DoorMask.RIGHT1 | DoorMask.DOWN1,
[RoomShape.ROOMSHAPE_LBR] = DoorMask.LEFT0 | DoorMask.UP0 | DoorMask.RIGHT0 | DoorMask.DOWN0 | DoorMask.LEFT1 | DoorMask.UP1 | DoorMask.RIGHT1 | DoorMask.DOWN1,
}
rawset(Isaac, "GetAllowedDoorsMaskForRoomShape", function(roomShape)
return roomShape and RoomShapeToDoorMask[roomShape] or 0
end)
local SpriteMT=getmetatable(Sprite)
local OldSprConstructor=SpriteMT.__call
function SpriteMT.__call(_,ANM2Path,LoadGraphics)
local out,isloaded=OldSprConstructor(),false
if LoadGraphics==nil then LoadGraphics=true end
if ANM2Path and type(ANM2Path)=="string" then
out:Load(ANM2Path,LoadGraphics)
isloaded=out:GetLayerCount()>0
end
return out,isloaded
end
local FontMT=getmetatable(Font)
local OldFontConstructor=FontMT.__call
function FontMT.__call(_,FontPath)
local out,isloaded=OldFontConstructor(),false
if FontPath and type(FontPath)=="string" then
out:Load(FontPath)
isloaded=out:IsLoaded()
end
return out,isloaded
end
-- Gets rid of the old `scripts/main.lua` wrappers for some re-implemented function hooks.
-- This can allow us to circumvent how the old wrappers handled certain inputs, as well as make
-- errors report the actual call site instead of the line in `scripts/main.lua`.
-- Make sure you understand what the old wrapper was doing, and that the new hook maintains any features like optional args.
local LuaWrappersToRemove = {
{ ItemPool, {"GetCollectible"} },
{
EntityPlayer, {
"AddCollectible",
"FullCharge",
"GetMultiShotParams",
"GetActiveItem",
"GetActiveCharge",
"GetActiveSubCharge",
"GetBatteryCharge",
"NeedsCharge",
"SetActiveCharge",
"DischargeActiveItem",
"SetPocketActiveItem",
"HasInvincibility",
"UseActiveItem",
}
},
}
for _, tab in ipairs(LuaWrappersToRemove) do
local class = tab[1]
local mt = getmetatable(class).__class
local oldIndex = mt.__index
local newIndex = {}
for _, funcName in ipairs(tab[2]) do
newIndex[funcName] = mt[funcName]
end
rawset(mt, "__index", function(self, k)
return newIndex[k] or oldIndex(self, k)
end)
end
LuaWrappersToRemove = nil
if not IsVanillaParity() then -- rebuild: the menu extras change the vanilla menus
pcall(require, "repentogon_extras/changelog")
pcall(require, "repentogon_extras/daily_stats")
pcall(require, "repentogon_extras/stats_menu")
pcall(require, "repentogon_extras/bestiary_menu")
-- pcall(require, "repentogon_extras/onlinestub") let's not load it
pcall(require, "repentogon_extras/mods_menu_tweaks")
end
pcall(require, "repentogon_api/room_descriptor")
local SSM = _GetModule("repentogon_extras.save_state_manager")
local ESSM = _GetModule("repentogon_extras.entity_save_state_manager")
local ESSM_OnNewEntity = ESSM._OnNewEntity
local ESSM_OnDeleteEntity = ESSM._OnDeleteEntity
---@class REPENTOGON._LuaBindings
---@field SSM REPENTOGON._LuaBindings.SSM
---@field ESSM REPENTOGON._LuaBindings.ESSM
---@field EntityManager REPENTOGON._LuaBindings.EntityManager
---@class REPENTOGON._LuaBindings.ESSM
---@field StoreEntity fun(entityId: integer, saveStateId: integer)
---@field RestoreEntity fun(entityId: integer, saveStateId: integer)
---@field ClearStates fun(saveStateIds: integer[])
---@field CopyStates fun(sourceIds: integer[], destIds: integer[]) -- sourceIds and destIds must have the same size
---@field Serialize fun(idMap: table<integer, integer>, fileName: string, checksum: integer) -- first integer is the serializationId, second integer is the actualId
---@field PreDeserialize fun(mods: ModReference[], modIds: string[]) -- list mods that have data that could be deserialized
---@field Deserialize fun(serializedIds: integer[], destIds: integer[], fileName: string, checksum: integer) -- serializedIds and destIds must have the same size
---@class REPENTOGON._LuaBindings.SSM
---@field StoreGameState fun(gameStateId: integer)
---@field RestoreGameState fun(gameStateId: integer)
---@field ClearGameState fun(gameStateId: integer)
---@field SerializeGameState fun(gameStateInfo: REPENTOGON.GameStateInfo)
---@field DeserializeGameState fun(gameStateInfo: REPENTOGON.GameStateInfo)
---@class REPENTOGON._LuaBindings.EntityManager
---@field NewEntity fun(entityId: integer)
---@field DeleteEntity fun(entityId: integer)
---@type REPENTOGON._LuaBindings.EntityManager
local _EntityManager = {
NewEntity = function (entityId)
ESSM_OnNewEntity(entityId)
end,
DeleteEntity = function (entityId)
ESSM_OnDeleteEntity(entityId)
end,
}
---@type REPENTOGON._LuaBindings.SSM
local _SSM = {
StoreGameState = SSM._StoreGameState,
RestoreGameState = SSM._RestoreGameState,
ClearGameState = SSM._ClearGameState,
SerializeGameState = SSM._SerializeGameState,
DeserializeGameState = SSM._DeserializeGameState,
DeleteGameState = SSM._DeleteGameState,
CleanupState = SSM._CleanupState,
}
---@param entity Entity
---@param entityId integer
---@param saveStateId integer
local function ESSM_StoreEntity(entity, entityId, saveStateId)
Isaac.RunCallback(ModCallbacks.MC_ENTITY_SAVE, entity)
ESSM._OnStoreEntity(entityId, saveStateId)
end
---@param entity Entity
---@param entityId integer
---@param saveStateId integer
local function ESSM_RestoreEntity(entity, entityId, saveStateId)
ESSM._OnRestoreEntity(entityId, saveStateId)
Isaac.RunCallback(ModCallbacks.MC_ENTITY_RESTORE, entity)
end
---@type REPENTOGON._LuaBindings.ESSM
local _ESSM = {
StoreEntity = ESSM_StoreEntity,
RestoreEntity = ESSM_RestoreEntity,
ClearStates = ESSM._OnClearSaveStates,
CopyStates = ESSM._OnCopySaveStates,
Serialize = ESSM._Serialize,
PreDeserialize = ESSM._PreDeserialize,
Deserialize = ESSM._Deserialize,
}
---@type REPENTOGON._LuaBindings
_LuaBindings = {
EntityManager = _EntityManager,
ESSM = _ESSM,
SSM = _SSM,
}
SaveStateManager = {
GetGameData = SSM.GetGameData,
ClearGameData = SSM.ClearGameData,
GetEntityData = ESSM.GetEntityData,
GetEntitySaveStateData = ESSM.GetEntitySaveStateData,
TryGetEntityData = ESSM.TryGetEntityData,
TryGetEntitySaveStateData = ESSM.TryGetEntitySaveStateData,
}
-- Backwards compatibility
EntitySaveStateManager = {
GetEntityData = ESSM.GetEntityData,
GetEntitySaveStateData = ESSM.GetEntitySaveStateData,
TryGetEntityData = ESSM.TryGetEntityData,
TryGetEntitySaveStateData = ESSM.TryGetEntitySaveStateData,
}
_GetModule = nil
s_modules = nil