REPENTOGON phase 1: 361 of 396 callbacks fired at their original sites with REPENTOGON's arguments, filters and return handling, inert without mods (72 goldens identical), vanilla changes behind the parity switch; source_tree.py skips src/'s own template structs
This commit is contained in:
1 parent
2cc082f6ee
commit
74322af121
167 files changed
+6408
-546
No files matched your search
+17
-6
@@ -91,14 +91,14 @@ void lua_object_push(lua_State* state, void* object, LuaClass luaClass)
|
||||
lua_setmetatable(state, -2);
|
||||
}
|
||||
|
||||
// (0085c590, 0085c3e0) The object argument index holds when it is of the class or one derived from it. Raises an
|
||||
// argument error if not.
|
||||
void* lua_object_check(lua_State* state, int index, LuaClass luaClass)
|
||||
// The object index holds when it is of the class or one derived from it, else nullptr.
|
||||
void* lua_object_test(lua_State* state, int index, LuaClass luaClass)
|
||||
{
|
||||
void** block = lua_type(state, index) == LUA_TUSERDATA ? (void**)lua_touserdata(state, index) : nullptr;
|
||||
const char* actual = luaL_typename(state, index);
|
||||
if (!block || !lua_getmetatable(state, index)) return nullptr;
|
||||
|
||||
lua_rawgetp(state, LUA_REGISTRYINDEX, lua_class_key(luaClass));
|
||||
if (!block || !lua_getmetatable(state, index)) lua_pushnil(state);
|
||||
lua_insert(state, -2);
|
||||
while (!lua_isnil(state, -1))
|
||||
{
|
||||
if (lua_rawequal(state, -1, -2))
|
||||
@@ -109,8 +109,19 @@ void* lua_object_check(lua_State* state, int index, LuaClass luaClass)
|
||||
field_raw_get(state, -1, "__parent");
|
||||
lua_remove(state, -2);
|
||||
}
|
||||
lua_pop(state, 2);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
field_raw_get(state, -2, "__name");
|
||||
// (0085c590, 0085c3e0) The object argument index holds when it is of the class or one derived from it. Raises an
|
||||
// argument error if not.
|
||||
void* lua_object_check(lua_State* state, int index, LuaClass luaClass)
|
||||
{
|
||||
if (void* object = lua_object_test(state, index, luaClass)) return object;
|
||||
|
||||
const char* actual = luaL_typename(state, index);
|
||||
lua_rawgetp(state, LUA_REGISTRYINDEX, lua_class_key(luaClass));
|
||||
field_raw_get(state, -1, "__name");
|
||||
const char* expected = lua_pushfstring(state, "%s expected, got %s", lua_tostring(state, -1), actual);
|
||||
luaL_argerror(state, index, expected);
|
||||
return nullptr;
|
||||
|
||||
@@ -246,6 +246,7 @@ template <typename T>
|
||||
constexpr bool IS_LUA_STRING = std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>;
|
||||
|
||||
void lua_object_push(lua_State* state, void* object, LuaClass luaClass);
|
||||
void* lua_object_test(lua_State* state, int index, LuaClass luaClass);
|
||||
void* lua_object_check(lua_State* state, int index, LuaClass luaClass);
|
||||
void lua_class_begin(lua_State* state, const char* name, LuaClass luaClass, LuaClass parent = LUA_CLASS_COUNT);
|
||||
void lua_namespace_begin(lua_State* state, const char* name);
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
#include "level/grid/spikes.h"
|
||||
#include "level/grid/tnt.h"
|
||||
#include "lua/api.h"
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
#include "repentogon/render.h"
|
||||
#endif
|
||||
|
||||
// Adds a To<Kind> method casting a grid entity to a subclass, nil when it is not one.
|
||||
template <typename T>
|
||||
@@ -46,7 +49,11 @@ static void grid_entity_base_register(lua_State* state)
|
||||
lua_method(state, "GetSaveState", [](GridEntity& grid) { return grid.desc_save(); });
|
||||
lua_method(state, "Init", [](GridEntity& grid, uint32_t seed) { grid.Init(seed); });
|
||||
lua_method(state, "Update", [](GridEntity& grid) { grid.Update(); });
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
lua_method(state, "Render", [](GridEntity& grid, Vector offset) { grid_entity_render(grid, offset); });
|
||||
#else
|
||||
lua_method(state, "Render", [](GridEntity& grid, Vector offset) { grid.Render(offset); });
|
||||
#endif
|
||||
lua_method(state, "PostInit", [](GridEntity& grid) { grid.PostInit(); });
|
||||
lua_method(state, "Hurt", [](GridEntity& grid, int damage) { return grid.Hurt(damage, EntityRef()); });
|
||||
lua_method(state, "HurtWithSource",
|
||||
|
||||
+31
-9
@@ -4,17 +4,19 @@
|
||||
#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 parameter is as wide as a Lua integer, for REPENTOGON's object hashes, or a string, which replaces the first.
|
||||
// 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;
|
||||
std::optional<lua_Integer> param = CALLBACK_PARAM_NONE;
|
||||
std::optional<lua_Integer> secondParam;
|
||||
const char* stringParam = nullptr;
|
||||
};
|
||||
|
||||
// Pushes a filter parameter, nil when there is none.
|
||||
inline void param_push(lua_State* state, std::optional<int> param)
|
||||
inline void param_push(lua_State* state, std::optional<lua_Integer> param)
|
||||
{
|
||||
if (param) lua_pushinteger(state, *param);
|
||||
else lua_pushnil(state);
|
||||
@@ -53,7 +55,8 @@ bool callback_run(LuaEngine& engine, CallbackRun run, Args... args)
|
||||
|
||||
lua_rawgeti(state, LUA_REGISTRYINDEX, isTwoParams ? engine.runCallbackTwoParamsRef : engine.runCallbackRef);
|
||||
lua_pushinteger(state, run.id);
|
||||
param_push(state, run.param);
|
||||
if (run.stringParam) lua_pushstring(state, run.stringParam);
|
||||
else 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;
|
||||
@@ -93,7 +96,8 @@ bool callback_boolean_run(LuaEngine& engine, CallbackRun run, bool fallback, Arg
|
||||
return result;
|
||||
}
|
||||
|
||||
// Runs a Pre*Collision callback for an entity and what it hits, and reads its result as true, false or nothing.
|
||||
// Runs a Pre*Collision callback for an entity and what it hits, and reads its result as true, false or nothing,
|
||||
// which the engine keeps for REPENTOGON's collision callbacks.
|
||||
template <typename T>
|
||||
LuaResult collision_run(LuaEngine& engine, CallbackRun run, T* entity, Entity* collider, bool isLow)
|
||||
{
|
||||
@@ -103,6 +107,7 @@ LuaResult collision_run(LuaEngine& engine, CallbackRun run, T* entity, Entity* c
|
||||
: lua_toboolean(engine.state, -1) ? LUA_RESULT_TRUE
|
||||
: LUA_RESULT_FALSE;
|
||||
lua_pop(engine.state, 1);
|
||||
engine.collisionResult = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -115,14 +120,31 @@ inline void field_read(lua_State* state, const char* key, bool& value)
|
||||
}
|
||||
|
||||
// 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.
|
||||
// The original aborts on an element that is not of the value's type (an integer, number, boolean or string). This keeps
|
||||
// the value instead.
|
||||
template <typename T>
|
||||
bool element_read(lua_State* state, int key, T& value)
|
||||
{
|
||||
int isInteger = 0;
|
||||
int isOfType = 0;
|
||||
bool isSet = lua_geti(state, -1, key) != LUA_TNIL;
|
||||
lua_Integer element = lua_tointegerx(state, -1, &isInteger);
|
||||
if (isInteger) value = (T)element;
|
||||
if constexpr (std::is_same_v<T, bool>)
|
||||
{
|
||||
if (lua_isboolean(state, -1)) value = lua_toboolean(state, -1);
|
||||
}
|
||||
else if constexpr (std::is_floating_point_v<T>)
|
||||
{
|
||||
lua_Number element = lua_tonumberx(state, -1, &isOfType);
|
||||
if (isOfType) value = (T)element;
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, std::string>)
|
||||
{
|
||||
if (lua_type(state, -1) == LUA_TSTRING) value = lua_tostring(state, -1);
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_Integer element = lua_tointegerx(state, -1, &isOfType);
|
||||
if (isOfType) value = (T)element;
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
return isSet;
|
||||
}
|
||||
|
||||
+71
-11
@@ -12,61 +12,97 @@
|
||||
#include "entity/weapon/tear.h"
|
||||
#include "game/game.h"
|
||||
#include "level/room_config.h"
|
||||
#include "game/manager.h"
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
#include "repentogon/callbacks.h"
|
||||
#endif
|
||||
|
||||
constexpr int PILL_EFFECT_UNCHANGED = -1;
|
||||
|
||||
// 00862250 Runs MC_PRE_BOMB_COLLISION, filtered by the bomb's variant.
|
||||
// 00862250 Runs MC_PRE_BOMB_COLLISION, filtered by the bomb's variant. Without vanilla parity REPENTOGON runs
|
||||
// it around the bomb's collision instead.
|
||||
LuaResult LuaEngine::PreBombCollision(Entity_Bomb* bomb, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PreBombCollision", MC_PRE_BOMB_COLLISION, (int)bomb->variant};
|
||||
return collision_run(*this, run, bomb, collider, isLow);
|
||||
}
|
||||
|
||||
// 008610d0 Runs MC_PRE_FAMILIAR_COLLISION, filtered by the familiar's variant.
|
||||
// 008610d0 Runs MC_PRE_FAMILIAR_COLLISION, filtered by the familiar's variant. Without vanilla parity REPENTOGON runs
|
||||
// it around the familiar's collision instead.
|
||||
LuaResult LuaEngine::PreFamiliarCollision(Entity_Familiar* familiar, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PreFamiliarCollision", MC_PRE_FAMILIAR_COLLISION, (int)familiar->variant};
|
||||
return collision_run(*this, run, familiar, collider, isLow);
|
||||
}
|
||||
|
||||
// 00861c40 Runs MC_PRE_KNIFE_COLLISION, filtered by the knife's subtype.
|
||||
// 00861c40 Runs MC_PRE_KNIFE_COLLISION, filtered by the knife's subtype. Without vanilla parity REPENTOGON runs
|
||||
// it around the knife's collision instead.
|
||||
LuaResult LuaEngine::PreKnifeCollision(Entity_Knife* knife, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PreKnifeCollision", MC_PRE_KNIFE_COLLISION, (int)knife->subtype};
|
||||
return collision_run(*this, run, knife, collider, isLow);
|
||||
}
|
||||
|
||||
// 00860d70 Runs MC_PRE_NPC_COLLISION, filtered by the NPC's type.
|
||||
// 00860d70 Runs MC_PRE_NPC_COLLISION, filtered by the NPC's type. Without vanilla parity REPENTOGON runs
|
||||
// it around the NPC's collision instead.
|
||||
LuaResult LuaEngine::PreNPCCollision(Entity_NPC* npc, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PreNPCCollision", MC_PRE_NPC_COLLISION, (int)npc->type};
|
||||
return collision_run(*this, run, npc, collider, isLow);
|
||||
}
|
||||
|
||||
// 008631b0 Runs MC_PRE_PICKUP_COLLISION, filtered by the pickup's variant.
|
||||
// 008631b0 Runs MC_PRE_PICKUP_COLLISION, filtered by the pickup's variant. Without vanilla parity REPENTOGON runs it
|
||||
// around the pickup's collision instead.
|
||||
LuaResult LuaEngine::PrePickupCollision(Entity_Pickup* pickup, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PrePickupCollision", MC_PRE_PICKUP_COLLISION, (int)pickup->variant};
|
||||
return collision_run(*this, run, pickup, collider, isLow);
|
||||
}
|
||||
|
||||
// 00864300 Runs MC_PRE_PLAYER_COLLISION, filtered by the player's variant.
|
||||
// 00864300 Runs MC_PRE_PLAYER_COLLISION, filtered by the player's variant. Without vanilla parity REPENTOGON runs it
|
||||
// around the player's collision instead.
|
||||
LuaResult LuaEngine::PrePlayerCollide(Entity_Player* player, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PrePlayerCollision", MC_PRE_PLAYER_COLLISION, (int)player->variant};
|
||||
return collision_run(*this, run, player, collider, isLow);
|
||||
}
|
||||
|
||||
// 00862760 Runs MC_PRE_PROJECTILE_COLLISION, filtered by the projectile's variant.
|
||||
// 00862760 Runs MC_PRE_PROJECTILE_COLLISION, filtered by the projectile's variant. Without vanilla parity REPENTOGON
|
||||
// runs it around the projectile's collision instead.
|
||||
LuaResult LuaEngine::PreProjectileCollision(Entity_Projectile* projectile, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PreProjectileCollision", MC_PRE_PROJECTILE_COLLISION, (int)projectile->variant};
|
||||
return collision_run(*this, run, projectile, collider, isLow);
|
||||
}
|
||||
|
||||
// 00862d70 Runs MC_PRE_TEAR_COLLISION, filtered by the tear's variant.
|
||||
// 00862d70 Runs MC_PRE_TEAR_COLLISION, filtered by the tear's variant. Without vanilla parity REPENTOGON runs
|
||||
// it around the tear's collision instead.
|
||||
LuaResult LuaEngine::PreTearCollision(Entity_Tear* tear, Entity* collider, bool isLow)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return LUA_RESULT_NIL;
|
||||
#endif
|
||||
CallbackRun run = {"PreTearCollision", MC_PRE_TEAR_COLLISION, (int)tear->variant};
|
||||
return collision_run(*this, run, tear, collider, isLow);
|
||||
}
|
||||
@@ -85,9 +121,13 @@ int LuaEngine::GetPillColor(uint32_t seed)
|
||||
return callback_integer_run(*this, {"GetPill", MC_GET_PILL_COLOR}, 0, seed);
|
||||
}
|
||||
|
||||
// 00865ad0 Runs MC_GET_PILL_EFFECT for the effect a pill color got. The effect a mod chose, -1 to keep it.
|
||||
// 00865ad0 Runs MC_GET_PILL_EFFECT for the effect a pill color got. The effect a mod chose, -1 to keep it. Without
|
||||
// vanilla parity REPENTOGON runs it after ItemPool::GetPillEffect instead.
|
||||
int LuaEngine::GetPillEffect(int pillEffect, int pillColor)
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (!repentogonOptions.isVanillaParity) return PILL_EFFECT_UNCHANGED;
|
||||
#endif
|
||||
CallbackRun run = {"GetPillEffect", MC_GET_PILL_EFFECT};
|
||||
return callback_integer_run(*this, run, PILL_EFFECT_UNCHANGED, pillEffect, pillColor);
|
||||
}
|
||||
@@ -99,11 +139,31 @@ int LuaEngine::GetTrinket(int trinket, const RNG& rng)
|
||||
return callback_integer_run(*this, {"GetTrinket", MC_GET_TRINKET}, 0, trinket, rng);
|
||||
}
|
||||
|
||||
// Whether the game asks mods about input: in a run, or with REPENTOGON (ASMPatchInputAction) anywhere, as
|
||||
// MC_MENU_INPUT_ACTION outside a run.
|
||||
bool is_input_hook_asked()
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
return g_luaEngine;
|
||||
#else
|
||||
return g_manager->state == MANAGER_STATE_GAME && g_luaEngine;
|
||||
#endif
|
||||
}
|
||||
|
||||
// The callback input hooks run: MC_INPUT_ACTION, or with REPENTOGON outside a run its MC_MENU_INPUT_ACTION.
|
||||
static int input_action_callback()
|
||||
{
|
||||
#ifdef ISAAC_REPENTOGON
|
||||
if (g_manager->state != MANAGER_STATE_GAME || !g_game) return MC_MENU_INPUT_ACTION;
|
||||
#endif
|
||||
return MC_INPUT_ACTION;
|
||||
}
|
||||
|
||||
// 00864c30 Runs MC_INPUT_ACTION for a boolean input hook, filtered by the hook. What a mod answered, nothing when no
|
||||
// mod did.
|
||||
std::optional<bool> LuaEngine::InputAction(Entity* entity, int hook, int action)
|
||||
{
|
||||
CallbackRun run = {"PreActionHookB", MC_INPUT_ACTION, hook};
|
||||
CallbackRun run = {"PreActionHookB", input_action_callback(), hook};
|
||||
if (!callback_run(*this, run, entity, hook, action)) return std::nullopt;
|
||||
|
||||
std::optional<bool> value;
|
||||
@@ -115,7 +175,7 @@ std::optional<bool> LuaEngine::InputAction(Entity* entity, int hook, int action)
|
||||
// 00864d80 Runs MC_INPUT_ACTION for the action value hook. The value a mod answered, nothing when no mod did.
|
||||
std::optional<float> LuaEngine::InputActionValue(Entity* entity, int action)
|
||||
{
|
||||
CallbackRun run = {"PreActionHookF", MC_INPUT_ACTION, INPUT_HOOK_VALUE};
|
||||
CallbackRun run = {"PreActionHookF", input_action_callback(), INPUT_HOOK_VALUE};
|
||||
if (!callback_run(*this, run, entity, INPUT_HOOK_VALUE, action)) return std::nullopt;
|
||||
|
||||
std::optional<float> value;
|
||||
|
||||
@@ -148,6 +148,7 @@ struct LuaEngine
|
||||
std::string scriptPath;
|
||||
lua_State* state = nullptr;
|
||||
LuaMode mode = LUA_MODE_NORMAL;
|
||||
LuaResult collisionResult = LUA_RESULT_NIL;
|
||||
int gcCooldown = 0;
|
||||
int gcFrame = 0;
|
||||
int gcLastMemory = -1;
|
||||
@@ -250,6 +251,7 @@ extern void* g_luaEngine;
|
||||
|
||||
const void* lua_class_key(LuaClass luaClass);
|
||||
void lua_error_log(lua_State* state, const char* name);
|
||||
bool is_input_hook_asked();
|
||||
|
||||
// The game's Lua engine, which Manager creates at startup.
|
||||
inline LuaEngine& lua_engine_get()
|
||||
|
||||
Reference in new issue
Block a user