REPENTOGON phase 0 (e2c5aec, GPL-2): ISAAC_REPENTOGON option (on), src/repentogon/ with the hidden REPENTOGON global switch, callback ids 1000-1499 with REPENTOGON's enable state and dispatch (priorities, optional and two-parameter filters, isolated errors), its scripts loaded from assets/repentogon/scripts, its options store and a vanilla-parity switch (on) in the overlay, scripts/repentogon_coverage.py and analysis/repentogon_coverage.tsv (2,834 rows)

This commit is contained in:
shweet committed 2026-11-24 03:23:39 -05:00
1 parent 78cb0775b0
commit 2cc082f6ee
31 files changed
+12955 -149

No files matched your search

+14 -9
View File
@@ -18,6 +18,9 @@
#include "engine/platform.h"
#include "level/room.h"
#include "level/room_grid.h"
#ifdef ISAAC_REPENTOGON
#include "repentogon/repentogon.h"
#endif
#include <SDL3/SDL_mouse.h>
#include <algorithm>
#include <cctype>
@@ -286,8 +289,8 @@ static int mod_data_remove(lua_State* state)
}
// 0085f0f0 The file a module name is in: dots become folders and .lua is added, searched in resources/scripts/, the
// running script's folder, and that folder under the mods folder. The name is lowercased, as the import tool lowercases
// every path a mod ships. Empty when none exists, with tried listing them.
// running script's folder, and that folder under the mods folder, with REPENTOGON's scripts folder first. The name is
// lowercased, as the import tool lowercases every path a mod ships. Empty when none exists, with tried listing them.
static std::string module_path_find(std::string_view name, std::string& tried)
{
std::string module(name);
@@ -299,7 +302,11 @@ static std::string module_path_find(std::string_view name, std::string& tried)
std::string scriptPath = lua_engine_get().scriptPath;
std::string local = scriptPath.substr(0, scriptPath.find_last_of(PATH_SEPARATORS)) + "/" + module;
for (const std::string& path : {resource_path(SCRIPTS_FOLDER + module), local, platform.modsDirectory + local})
for (const std::string& path : {
#ifdef ISAAC_REPENTOGON
repentogon_script_path(module),
#endif
resource_path(SCRIPTS_FOLDER + module), local, platform.modsDirectory + local})
{
std::error_code error;
if (std::filesystem::exists(path, error)) return path;
@@ -500,12 +507,10 @@ static void isaac_register(lua_State* state)
EntityList& list = g_game->room->entityList;
return std::max(list.bossCount + list.pendingBossCount, 0);
});
lua_method(state, "SetBuiltInCallbackState", [](int callback, bool isEnabled) {
if ((uint32_t)callback < LUA_CALLBACK_COUNT) lua_engine_get().enabledCallbacks[callback] = isEnabled;
});
lua_method(state, "GetBuiltInCallbackState", [](int callback) {
return (uint32_t)callback < LUA_CALLBACK_COUNT && lua_engine_get().enabledCallbacks[callback];
});
lua_method(state, "SetBuiltInCallbackState",
[](int callback, bool isEnabled) { lua_engine_get().callback_state_set(callback, isEnabled); });
lua_method(state, "GetBuiltInCallbackState",
[](int callback) { return lua_engine_get().is_callback_enabled(callback); });
lua_method(state, "WorldToScreen", [](Vector position) {
if (!g_game) return position;
+139
View File
@@ -0,0 +1,139 @@
#pragma once
#include "lua/api.h"
#include <optional>
// One run of a callback: the name its errors log, its id, and the parameter mods filter it by, nil when it has none.
// A REPENTOGON callback may filter by a second parameter, which runs it through _RunCallbackWithTwoParams.
struct CallbackRun
{
const char* name;
int id;
std::optional<int> param = CALLBACK_PARAM_NONE;
std::optional<int> secondParam;
};
// Pushes a filter parameter, nil when there is none.
inline void param_push(lua_State* state, std::optional<int> param)
{
if (param) lua_pushinteger(state, *param);
else lua_pushnil(state);
}
// Calls _RunCallback (_RunCallbackWithTwoParams for two filter parameters) with a callback's id, its filter parameters
// and its arguments when a mod added a function for it, leaving its one result on the stack. Whether it ran, false
// without a function or on an error, which is logged.
// The callers, result readers and error throws the original instantiates per callback are this and its readers
// (00872980, 00872c60, 00873470, 00873980, 00873d00, 00873fc0, 00874120, 00874210, 00874310, 00874a10, 00874f60)
// (0085c080, 008727d0, 0086130e, 00861225, 00861d96, 00861e7f, 008623a6, 0086248f, 008628b6, 0086299f, 00864453)
// (0086453c, 00865bbc, 00872a20, 00872b10, 00872ff0, 008730e0, 00873890, 00873ac0, 00873bb0, 00873e40, 008746b0)
// (00874860, 00874910, 00874ac0, 00874d20, 00874dd0, 00874e90, 00865a04, 0085bfd0, 0085bff0, 0085c270, 00873f40)
// (00872da0, 00872e90, 00873230, 00873320, 00873560, 00873650, 00873740, 00874400, 00874550, 00875050, 008663b6)
// (00872820, 00872860, 00872910, 00861c23, 00862233, 00862d53, 00865aad, 00865c61, 00866474, 00872a0c, 00872af1)
// (00872c43, 00872d80, 00872e71, 00872fc3, 008730c1, 00873213, 00873301, 00873453, 00873541, 00873631, 00873721)
// (00873873, 00873961, 00873aa0, 00873b91, 00873ce3, 00873e20, 00873f26, 00874103, 008741f1, 008742f7, 008743eb)
// (00874533, 0087469a, 00874846, 008748f6, 008749f7, 00874aa8, 00874bbb, 00874d0a, 00874db6, 00874e7e, 00874f4a)
// (00861bda, 008621ea, 00862d0a, 0085c250, 0085c2d0, 0085c300, 0085c340, 0085bf50, 0085c1d0, 0085c200, 0085c210)
// (0085da70)
// and their catches log through lua_error_log
// (00860ea3, 008611da, 0086191f, 00861d4b, 0086235b, 0086286b, 00862ea4, 008632e1)
// (00863706, 00863b6a, 00863e31, 00863f1e, 008640de, 00864408, 00864f6e, 00865b69, 008661cc, 00860826, 0086090e)
// (00860a70, 00860b4b, 00860c36, 00860d1e, 008613bf, 0086149f, 00861587, 00862a4c, 00862b2f, 00862cb8, 00862f8c)
// (0086306c, 00863154, 0086354b, 00863fff, 00864a6a, 00864bb8, 00864d16, 00865209, 008652e9, 00865424, 008659a7)
// (00865d53, 00865e99, 00865fe8, 008660ec)
// (00860f8b, 00861076, 0086166f, 0086174f, 00861837, 008619ff, 00861b88, 00861f2f, 0086200f, 00862198, 0086253f)
// (0086261f, 00862707, 0086362b, 00863879, 00863c6e, 00863d4e, 008641bb, 008642a4, 00864682, 00864e6d, 0086504e)
// (0086512e, 008655b7, 00866364, 0086675e, 008668e0).
template <typename... Args>
bool callback_run(LuaEngine& engine, CallbackRun run, Args... args)
{
if (!engine.is_callback_enabled(run.id)) return false;
lua_State* state = engine.state;
bool isTwoParams = run.secondParam.has_value();
lua_rawgeti(state, LUA_REGISTRYINDEX, isTwoParams ? engine.runCallbackTwoParamsRef : engine.runCallbackRef);
lua_pushinteger(state, run.id);
param_push(state, run.param);
if (isTwoParams) param_push(state, run.secondParam);
(lua_push(state, args), ...);
if (lua_pcall(state, sizeof...(Args) + 2 + isTwoParams, 1, 0) == LUA_OK) return true;
lua_error_log(state, run.name);
return false;
}
// Runs a callback whose result the game ignores.
template <typename... Args>
void callback_notify(LuaEngine& engine, CallbackRun run, Args... args)
{
if (callback_run(engine, run, args...)) lua_pop(engine.state, 1);
}
// Runs a callback whose result is an integer that replaces fallback.
// The original aborts on a result that is neither nil nor an integer. This keeps fallback instead.
template <typename... Args>
int callback_integer_run(LuaEngine& engine, CallbackRun run, int fallback, Args... args)
{
if (!callback_run(engine, run, args...)) return fallback;
int isInteger = 0;
lua_Integer result = lua_tointegerx(engine.state, -1, &isInteger);
lua_pop(engine.state, 1);
return isInteger ? (int)result : fallback;
}
// Runs a callback whose result, when not nil, is a boolean that replaces fallback.
template <typename... Args>
bool callback_boolean_run(LuaEngine& engine, CallbackRun run, bool fallback, Args... args)
{
if (!callback_run(engine, run, args...)) return fallback;
bool result = lua_isnil(engine.state, -1) ? fallback : lua_toboolean(engine.state, -1);
lua_pop(engine.state, 1);
return result;
}
// Runs a Pre*Collision callback for an entity and what it hits, and reads its result as true, false or nothing.
template <typename T>
LuaResult collision_run(LuaEngine& engine, CallbackRun run, T* entity, Entity* collider, bool isLow)
{
if (!callback_run(engine, run, entity, collider, isLow)) return LUA_RESULT_NIL;
LuaResult result = lua_isnil(engine.state, -1) ? LUA_RESULT_NIL
: lua_toboolean(engine.state, -1) ? LUA_RESULT_TRUE
: LUA_RESULT_FALSE;
lua_pop(engine.state, 1);
return result;
}
// Sets value to a field of the table on top of the stack, when the field is not nil.
inline void field_read(lua_State* state, const char* key, bool& value)
{
lua_getfield(state, -1, key);
if (!lua_isnil(state, -1)) value = lua_toboolean(state, -1);
lua_pop(state, 1);
}
// Sets value to an element of the table on top of the stack, when the element is not nil. Whether it was not.
// The original aborts on an element that is not an integer. This keeps the value instead.
template <typename T>
bool element_read(lua_State* state, int key, T& value)
{
int isInteger = 0;
bool isSet = lua_geti(state, -1, key) != LUA_TNIL;
lua_Integer element = lua_tointegerx(state, -1, &isInteger);
if (isInteger) value = (T)element;
lua_pop(state, 1);
return isSet;
}
// Pops a callback's result, and when it is a table sets values in order to its elements 1, 2 ... Whether any was set.
template <typename... T>
bool elements_pop(lua_State* state, T&... values)
{
int key = 1;
bool isChanged = false;
if (lua_istable(state, -1)) ((isChanged |= element_read(state, key++, values)), ...);
lua_pop(state, 1);
return isChanged;
}
+1 -125
View File
@@ -1,4 +1,4 @@
#include "lua/engine.h"
#include "lua/callback_run.h"
#include "classes.h"
#include "entity/weapon/bomb.h"
#include "entity/effect/effect.h"
@@ -11,134 +11,10 @@
#include "entity/weapon/projectile.h"
#include "entity/weapon/tear.h"
#include "game/game.h"
#include "lua/api.h"
#include "level/room_config.h"
constexpr int PILL_EFFECT_UNCHANGED = -1;
// One run of a callback: the name its errors log, its id, and the parameter mods filter it by.
struct CallbackRun
{
const char* name;
LuaCallback id;
int param = CALLBACK_PARAM_NONE;
};
// Calls _RunCallback with a callback's id, its filter parameter and its arguments when a mod added a function for it,
// leaving its one result on the stack. Whether it ran, false without a function or on an error, which is logged.
// The callers, result readers and error throws the original instantiates per callback are this and its readers
// (00872980, 00872c60, 00873470, 00873980, 00873d00, 00873fc0, 00874120, 00874210, 00874310, 00874a10, 00874f60)
// (0085c080, 008727d0, 0086130e, 00861225, 00861d96, 00861e7f, 008623a6, 0086248f, 008628b6, 0086299f, 00864453)
// (0086453c, 00865bbc, 00872a20, 00872b10, 00872ff0, 008730e0, 00873890, 00873ac0, 00873bb0, 00873e40, 008746b0)
// (00874860, 00874910, 00874ac0, 00874d20, 00874dd0, 00874e90, 00865a04, 0085bfd0, 0085bff0, 0085c270, 00873f40)
// (00872da0, 00872e90, 00873230, 00873320, 00873560, 00873650, 00873740, 00874400, 00874550, 00875050, 008663b6)
// (00872820, 00872860, 00872910, 00861c23, 00862233, 00862d53, 00865aad, 00865c61, 00866474, 00872a0c, 00872af1)
// (00872c43, 00872d80, 00872e71, 00872fc3, 008730c1, 00873213, 00873301, 00873453, 00873541, 00873631, 00873721)
// (00873873, 00873961, 00873aa0, 00873b91, 00873ce3, 00873e20, 00873f26, 00874103, 008741f1, 008742f7, 008743eb)
// (00874533, 0087469a, 00874846, 008748f6, 008749f7, 00874aa8, 00874bbb, 00874d0a, 00874db6, 00874e7e, 00874f4a)
// (00861bda, 008621ea, 00862d0a, 0085c250, 0085c2d0, 0085c300, 0085c340, 0085bf50, 0085c1d0, 0085c200, 0085c210)
// (0085da70)
// and their catches log through lua_error_log
// (00860ea3, 008611da, 0086191f, 00861d4b, 0086235b, 0086286b, 00862ea4, 008632e1)
// (00863706, 00863b6a, 00863e31, 00863f1e, 008640de, 00864408, 00864f6e, 00865b69, 008661cc, 00860826, 0086090e)
// (00860a70, 00860b4b, 00860c36, 00860d1e, 008613bf, 0086149f, 00861587, 00862a4c, 00862b2f, 00862cb8, 00862f8c)
// (0086306c, 00863154, 0086354b, 00863fff, 00864a6a, 00864bb8, 00864d16, 00865209, 008652e9, 00865424, 008659a7)
// (00865d53, 00865e99, 00865fe8, 008660ec)
// (00860f8b, 00861076, 0086166f, 0086174f, 00861837, 008619ff, 00861b88, 00861f2f, 0086200f, 00862198, 0086253f)
// (0086261f, 00862707, 0086362b, 00863879, 00863c6e, 00863d4e, 008641bb, 008642a4, 00864682, 00864e6d, 0086504e)
// (0086512e, 008655b7, 00866364, 0086675e, 008668e0).
template <typename... Args>
static bool callback_run(LuaEngine& engine, CallbackRun run, Args... args)
{
if (!engine.enabledCallbacks[run.id]) return false;
lua_State* state = engine.state;
lua_rawgeti(state, LUA_REGISTRYINDEX, engine.runCallbackRef);
lua_push(state, (int)run.id);
lua_push(state, run.param);
(lua_push(state, args), ...);
if (lua_pcall(state, sizeof...(Args) + 2, 1, 0) == LUA_OK) return true;
lua_error_log(state, run.name);
return false;
}
// Runs a callback whose result the game ignores.
template <typename... Args>
static void callback_notify(LuaEngine& engine, CallbackRun run, Args... args)
{
if (callback_run(engine, run, args...)) lua_pop(engine.state, 1);
}
// Runs a callback whose result is an integer that replaces fallback.
// The original aborts on a result that is neither nil nor an integer. This keeps fallback instead.
template <typename... Args>
static int callback_integer_run(LuaEngine& engine, CallbackRun run, int fallback, Args... args)
{
if (!callback_run(engine, run, args...)) return fallback;
int isInteger = 0;
lua_Integer result = lua_tointegerx(engine.state, -1, &isInteger);
lua_pop(engine.state, 1);
return isInteger ? (int)result : fallback;
}
// Runs a callback whose result, when not nil, is a boolean that replaces fallback.
template <typename... Args>
static bool callback_boolean_run(LuaEngine& engine, CallbackRun run, bool fallback, Args... args)
{
if (!callback_run(engine, run, args...)) return fallback;
bool result = lua_isnil(engine.state, -1) ? fallback : lua_toboolean(engine.state, -1);
lua_pop(engine.state, 1);
return result;
}
// Runs a Pre*Collision callback for an entity and what it hits, and reads its result as true, false or nothing.
template <typename T>
static LuaResult collision_run(LuaEngine& engine, CallbackRun run, T* entity, Entity* collider, bool isLow)
{
if (!callback_run(engine, run, entity, collider, isLow)) return LUA_RESULT_NIL;
LuaResult result = lua_isnil(engine.state, -1) ? LUA_RESULT_NIL
: lua_toboolean(engine.state, -1) ? LUA_RESULT_TRUE
: LUA_RESULT_FALSE;
lua_pop(engine.state, 1);
return result;
}
// Sets value to a field of the table on top of the stack, when the field is not nil.
static void field_read(lua_State* state, const char* key, bool& value)
{
lua_getfield(state, -1, key);
if (!lua_isnil(state, -1)) value = lua_toboolean(state, -1);
lua_pop(state, 1);
}
// Sets value to an element of the table on top of the stack, when the element is not nil. Whether it was not.
// The original aborts on an element that is not an integer. This keeps the value instead.
template <typename T>
static bool element_read(lua_State* state, int key, T& value)
{
int isInteger = 0;
bool isSet = lua_geti(state, -1, key) != LUA_TNIL;
lua_Integer element = lua_tointegerx(state, -1, &isInteger);
if (isInteger) value = (T)element;
lua_pop(state, 1);
return isSet;
}
// Pops a callback's result, and when it is a table sets values in order to its elements 1, 2 ... Whether any was set.
template <typename... T>
static bool elements_pop(lua_State* state, T&... values)
{
int key = 1;
bool isChanged = false;
if (lua_istable(state, -1)) ((isChanged |= element_read(state, key++, values)), ...);
lua_pop(state, 1);
return isChanged;
}
// 00862250 Runs MC_PRE_BOMB_COLLISION, filtered by the bomb's variant.
LuaResult LuaEngine::PreBombCollision(Entity_Bomb* bomb, Entity* collider, bool isLow)
{
+32 -4
View File
@@ -5,6 +5,9 @@
#include "game/game.h"
#include "game/manager.h"
#include "engine/platform.h"
#ifdef ISAAC_REPENTOGON
#include "repentogon/repentogon.h"
#endif
#include <algorithm>
#include <cstdio>
#include <cstdlib>
@@ -169,6 +172,9 @@ void LuaEngine::Init(LuaMode luaMode)
RegisterAPI();
RunScript(resource_path(ENUMS_SCRIPT));
RunScript(resource_path(MAIN_SCRIPT));
#ifdef ISAAC_REPENTOGON
repentogon_scripts_run(*this);
#endif
runCallbackRef = global_take(state, RUN_CALLBACK);
unloadModRef = global_take(state, UNLOAD_MOD);
}
@@ -193,10 +199,11 @@ void LuaEngine::destroy()
for (LuaMod& mod : mods) mod_unload(*this, mod);
mods.clear();
luaL_unref(state, LUA_REGISTRYINDEX, runCallbackRef);
luaL_unref(state, LUA_REGISTRYINDEX, unloadModRef);
runCallbackRef = LUA_REF_NONE;
unloadModRef = LUA_REF_NONE;
for (int* ref : {&runCallbackRef, &runCallbackTwoParamsRef, &unloadModRef})
{
luaL_unref(state, LUA_REGISTRYINDEX, *ref);
*ref = LUA_REF_NONE;
}
lua_close(state);
state = nullptr;
}
@@ -296,6 +303,27 @@ void LuaEngine::gc_update()
lua_gc(state, LUA_GCSTEP, gcStepSize);
}
// Whether a mod added a function for a callback, by its id: the original's, or with REPENTOGON one of its own.
bool LuaEngine::is_callback_enabled(int callback)
{
if ((uint32_t)callback < LUA_CALLBACK_COUNT) return enabledCallbacks[callback];
#ifdef ISAAC_REPENTOGON
uint32_t index = (uint32_t)(callback - REPENTOGON_CALLBACK_FIRST);
if (index < REPENTOGON_CALLBACK_COUNT) return enabledRepentogonCallbacks[index];
#endif
return false;
}
// Sets whether a callback runs, for the API's SetBuiltInCallbackState. Any other id is ignored.
void LuaEngine::callback_state_set(int callback, bool isEnabled)
{
if ((uint32_t)callback < LUA_CALLBACK_COUNT) enabledCallbacks[callback] = isEnabled;
#ifdef ISAAC_REPENTOGON
uint32_t index = (uint32_t)(callback - REPENTOGON_CALLBACK_FIRST);
if (index < REPENTOGON_CALLBACK_COUNT) enabledRepentogonCallbacks[index] = isEnabled;
#endif
}
// 0086f640 A collectible's id by its name, -1 for none. The original searches a copy of the name map.
int LuaEngine::Isaac_GetItemIdByName(std::string_view name)
{
+9 -1
View File
@@ -34,6 +34,9 @@ constexpr int LUA_MEMORY_HISTORY_SIZE = 128;
constexpr int LUA_REF_NONE = -2;
// A callback registered for every parameter.
constexpr int CALLBACK_PARAM_NONE = -1;
// REPENTOGON's callback ids, from 1000 up (enums_ex.lua's ModCallbacks), with room for as many as it tracks.
constexpr int REPENTOGON_CALLBACK_FIRST = 1000;
constexpr int REPENTOGON_CALLBACK_COUNT = 500;
// The input hooks of MC_INPUT_ACTION (Lua's InputHook).
enum InputHook : int
@@ -132,13 +135,15 @@ struct LuaMod
// The mod scripting host: the Lua 5.3 state, the game's scripts in resources/scripts/, the mods scripts register, and
// the entry points that run mods' callbacks. A callback runs only while enabledCallbacks has its id, which the API's
// SetBuiltInCallbackState sets once a mod adds a function for it. Lua errors are logged, never fatal.
// SetBuiltInCallbackState sets once a mod adds a function for it, REPENTOGON's ids from 1000 up in a set of their own.
// Lua errors are logged, never fatal.
// Construction is inlined in IsaacStartup, and the original never destroys the engine.
struct LuaEngine
{
std::array<int, LUA_MEMORY_HISTORY_SIZE> memoryHistory{};
std::array<int, LUA_GC_HISTORY_SIZE> gcGrowth{};
std::bitset<LUA_CALLBACK_COUNT> enabledCallbacks;
std::bitset<REPENTOGON_CALLBACK_COUNT> enabledRepentogonCallbacks;
std::list<LuaMod> mods;
std::string scriptPath;
lua_State* state = nullptr;
@@ -151,6 +156,7 @@ struct LuaEngine
int memoryHistoryIndex = 0;
int memoryHistoryTimer = 0;
int runCallbackRef = LUA_REF_NONE;
int runCallbackTwoParamsRef = LUA_REF_NONE;
int scriptApiVersion = -1;
int unloadModRef = LUA_REF_NONE;
uint32_t pointerMask = 0;
@@ -161,6 +167,8 @@ struct LuaEngine
bool RunScript(std::string_view path);
bool LuamodCMD(std::string_view modName);
void gc_update();
bool is_callback_enabled(int callback);
void callback_state_set(int callback, bool isEnabled);
static int Isaac_GetItemIdByName(std::string_view name);
LuaResult PreBombCollision(Entity_Bomb* bomb, Entity* collider, bool isLow);