Sort src/ into subsystem folders (engine, game, level, level/grid, entity with player, npc, boss, familiar, effect, pickup and weapon, ui, ui/menu, lua, online), drop each folder's prefix from file names, include by path from src/, glob src/ recursively

This commit is contained in:
shweet committed 2026-11-23 16:41:49 -05:00
1 parent 9254158eef
commit 7d3cf33c20
706 files changed
+5337 -5336

No files matched your search

+452
View File
@@ -0,0 +1,452 @@
#pragma once
#include "lua/engine.h"
#include <bitset>
#include <cstring>
#include <memory>
#include <new>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
// classes.h declares the original's Lua imports, and its luaL_newstate differs from Lua's only in its return type.
#define luaL_newstate lua_newstate_auxiliary
extern "C"
{
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
}
#undef luaL_newstate
struct ActiveItemDesc;
struct ColorMod;
struct EntityPtr;
struct EntityList_EL;
struct EntityRef;
struct Entity_Bomb;
struct Entity_Effect;
struct Entity_Familiar;
struct Entity_Knife;
struct Entity_Laser;
struct Entity_NPC;
struct Entity_Pickup;
struct Entity_Player;
struct Entity_Projectile;
struct Entity_Tear;
struct Font;
struct FontSettings;
struct GridEntity;
struct GridEntityDesc;
struct GridEntity_Door;
struct GridEntity_Pit;
struct GridEntity_Poop;
struct GridEntity_PressurePlate;
struct GridEntity_Rock;
struct GridEntity_Spikes;
struct GridEntity_TNT;
struct ItemConfig;
struct ItemConfig_Card;
struct ItemConfig_Costume;
struct ItemConfig_Item;
struct ItemConfig_Pill;
struct KColor;
struct ANM2;
struct PathFinder;
struct PosVel;
struct ProjectileParams;
struct QueueItemData;
struct RoomConfig_Room;
struct RoomEntry;
struct RoomSpawn;
struct RoomDescriptor;
struct Seeds;
struct TearParams;
struct TemporaryEffect;
struct TemporaryEffects;
class Game;
class HUD;
class ItemPool;
class Level;
class MusicManager;
class Room;
class SFXManager;
// Where lua_class_end puts a class: a global, or a field of the namespace begun below it.
enum LuaScope
{
LUA_SCOPE_GLOBAL,
LUA_SCOPE_NAMESPACE
};
// An argument a binding is handed but does not read, such as the table a constructor's __call gets first.
struct LuaIgnored
{
};
// A value the original pushes as a class it never registers: a userdata without a metatable, which scripts can only
// hold or pass on.
struct LuaOpaque
{};
// An argument a script may leave out, which reads as Default when it is nil or missing.
template <typename T, auto Default> struct LuaDefault
{
T value = Default;
};
template <typename T>
constexpr bool IS_LUA_DEFAULT = false;
template <typename T, auto Default>
constexpr bool IS_LUA_DEFAULT<LuaDefault<T, Default>> = true;
// The rooms of a floor as Level:GetRooms hands them out: the room list and how many rooms it has.
struct LuaRoomList
{
RoomDescriptor* rooms = nullptr;
int count = 0;
};
// The Lua class each C++ type is pushed and checked as. TearFlags are Lua's BitSet128 and ColorMod is Lua's Color.
template <typename T>
constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_COUNT;
template <> inline constexpr LuaClass LUA_CLASS_OF<ActiveItemDesc> = LUA_CLASS_ACTIVE_ITEM_DESC;
template <> inline constexpr LuaClass LUA_CLASS_OF<std::bitset<128>> = LUA_CLASS_BIT_SET128;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ColorMod> = LUA_CLASS_COLOR;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity> = LUA_CLASS_ENTITY;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Bomb> = LUA_CLASS_BOMB;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Effect> = LUA_CLASS_EFFECT;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Familiar> = LUA_CLASS_FAMILIAR;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Knife> = LUA_CLASS_KNIFE;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Laser> = LUA_CLASS_LASER;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_NPC> = LUA_CLASS_NPC;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Pickup> = LUA_CLASS_PICKUP;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Player> = LUA_CLASS_PLAYER;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Projectile> = LUA_CLASS_PROJECTILE;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Entity_Tear> = LUA_CLASS_TEAR;
template <> inline constexpr LuaClass LUA_CLASS_OF<EntityList_EL> = LUA_CLASS_ENTITY_LIST;
template <> inline constexpr LuaClass LUA_CLASS_OF<EntityPtr> = LUA_CLASS_ENTITY_PTR;
template <>
inline constexpr LuaClass LUA_CLASS_OF<EntityRef> = LUA_CLASS_ENTITY_REF;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Font> = LUA_CLASS_FONT;
template <>
inline constexpr LuaClass LUA_CLASS_OF<FontSettings> = LUA_CLASS_FONT_SETTINGS;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Game> = LUA_CLASS_GAME;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity> = LUA_CLASS_GRID_ENTITY;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntityDesc> = LUA_CLASS_GRID_ENTITY_DESC;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity_Door> = LUA_CLASS_GRID_ENTITY_DOOR;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity_Pit> = LUA_CLASS_GRID_ENTITY_PIT;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity_Poop> = LUA_CLASS_GRID_ENTITY_POOP;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity_PressurePlate> = LUA_CLASS_GRID_ENTITY_PRESSURE_PLATE;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity_Rock> = LUA_CLASS_GRID_ENTITY_ROCK;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity_Spikes> = LUA_CLASS_GRID_ENTITY_SPIKES;
template <>
inline constexpr LuaClass LUA_CLASS_OF<GridEntity_TNT> = LUA_CLASS_GRID_ENTITY_TNT;
template <>
inline constexpr LuaClass LUA_CLASS_OF<HUD> = LUA_CLASS_HUD;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ItemConfig> = LUA_CLASS_ITEM_CONFIG;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ItemConfig_Card> = LUA_CLASS_ITEM_CONFIG_CARD;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ItemConfig_Costume> = LUA_CLASS_ITEM_CONFIG_COSTUME;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ItemConfig_Item> = LUA_CLASS_ITEM_CONFIG_ITEM;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ItemConfig_Pill> = LUA_CLASS_ITEM_CONFIG_PILL;
template <>
inline constexpr LuaClass LUA_CLASS_OF<std::vector<std::unique_ptr<ItemConfig_Card>>> = LUA_CLASS_ITEM_CONFIG_CARD_LIST;
template <>
inline constexpr LuaClass LUA_CLASS_OF<std::vector<std::unique_ptr<ItemConfig_Item>>> = LUA_CLASS_ITEM_CONFIG_ITEM_LIST;
template <>
inline constexpr LuaClass LUA_CLASS_OF<std::vector<std::unique_ptr<ItemConfig_Pill>>> = LUA_CLASS_ITEM_CONFIG_PILL_LIST;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ItemPool> = LUA_CLASS_ITEM_POOL;
template <>
inline constexpr LuaClass LUA_CLASS_OF<KColor> = LUA_CLASS_KCOLOR;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Level> = LUA_CLASS_LEVEL;
template <>
inline constexpr LuaClass LUA_CLASS_OF<MusicManager> = LUA_CLASS_MUSIC_MANAGER;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ProjectileParams> = LUA_CLASS_PROJECTILE_PARAMS;
template <>
inline constexpr LuaClass LUA_CLASS_OF<QueueItemData> = LUA_CLASS_QUEUE_ITEM_DATA;
template <>
inline constexpr LuaClass LUA_CLASS_OF<RNG> = LUA_CLASS_RNG;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Room> = LUA_CLASS_ROOM;
template <>
inline constexpr LuaClass LUA_CLASS_OF<RoomEntry> = LUA_CLASS_ROOM_CONFIG_ENTRY;
template <>
inline constexpr LuaClass LUA_CLASS_OF<std::vector<RoomEntry>> = LUA_CLASS_ROOM_CONFIG_ENTRY_LIST;
template <>
inline constexpr LuaClass LUA_CLASS_OF<RoomConfig_Room> = LUA_CLASS_ROOM_CONFIG_ROOM;
template <>
inline constexpr LuaClass LUA_CLASS_OF<RoomSpawn> = LUA_CLASS_ROOM_CONFIG_SPAWN;
template <>
inline constexpr LuaClass LUA_CLASS_OF<std::vector<RoomSpawn>> = LUA_CLASS_ROOM_CONFIG_SPAWN_LIST;
template <>
inline constexpr LuaClass LUA_CLASS_OF<RoomDescriptor> = LUA_CLASS_ROOM_DESCRIPTOR;
template <>
inline constexpr LuaClass LUA_CLASS_OF<LuaRoomList> = LUA_CLASS_ROOM_LIST;
template <>
inline constexpr LuaClass LUA_CLASS_OF<PathFinder> = LUA_CLASS_PATH_FINDER;
template <> inline constexpr LuaClass LUA_CLASS_OF<PosVel> = LUA_CLASS_POS_VEL;
template <> inline constexpr LuaClass LUA_CLASS_OF<Seeds> = LUA_CLASS_SEEDS;
template <>
inline constexpr LuaClass LUA_CLASS_OF<SFXManager> = LUA_CLASS_SFX_MANAGER;
template <>
inline constexpr LuaClass LUA_CLASS_OF<ANM2> = LUA_CLASS_SPRITE;
template <>
inline constexpr LuaClass LUA_CLASS_OF<TearParams> = LUA_CLASS_TEAR_PARAMS;
template <>
inline constexpr LuaClass LUA_CLASS_OF<TemporaryEffect> = LUA_CLASS_TEMPORARY_EFFECT;
template <> inline constexpr LuaClass LUA_CLASS_OF<std::vector<TemporaryEffect>> = LUA_CLASS_EFFECT_LIST;
template <> inline constexpr LuaClass LUA_CLASS_OF<TemporaryEffects> = LUA_CLASS_TEMPORARY_EFFECTS;
template <>
inline constexpr LuaClass LUA_CLASS_OF<Vector> = LUA_CLASS_VECTOR;
template <> inline constexpr LuaClass LUA_CLASS_OF<std::vector<Vector>> = LUA_CLASS_VECTOR_LIST;
// The class and member type of a pointer to a data member, for decltype only.
template <typename C, typename T>
C lua_member_class(T C::*);
template <typename C, typename T>
T lua_member_type(T C::*);
// Whether a type has a Lua class to push and check it as.
template <typename T>
constexpr bool IS_LUA_CLASS = LUA_CLASS_OF<T> != LUA_CLASS_COUNT;
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_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);
void lua_class_end(lua_State* state, LuaScope scope = LUA_SCOPE_GLOBAL);
void lua_table_set(lua_State* state, int table, const char* name);
void lua_property_set(lua_State* state, const char* table, const char* name);
void lua_api_bit_set128_register(lua_State* state);
void lua_api_color_register(lua_State* state);
void lua_api_containers_register(lua_State* state);
void lua_api_entity_register(lua_State* state);
void lua_api_entities_register(lua_State* state);
void lua_api_entity_player_register(lua_State* state);
void lua_api_game_register(lua_State* state);
void lua_api_grid_entity_register(lua_State* state);
void lua_api_isaac_register(lua_State* state);
void lua_api_item_config_register(lua_State* state);
void lua_api_rng_register(lua_State* state);
void lua_api_room_register(lua_State* state);
void lua_api_sprite_register(lua_State* state);
void lua_api_vector_register(lua_State* state);
// Pushes a copy Lua owns: a userdata holding the copy's pointer first, as an object's, then the copy.
// Classes whose copies need destroying get a __gc from lua_gc_set.
template <typename T>
void lua_copy_push(lua_State* state, const T& value)
{
static_assert(alignof(T) <= alignof(void*));
void** block = (void**)lua_newuserdata(state, sizeof(void*) + sizeof(T));
*block = new (block + 1) T(value);
lua_rawgetp(state, LUA_REGISTRYINDEX, lua_class_key(LUA_CLASS_OF<T>));
lua_setmetatable(state, -2);
}
// (0085c010, 0085c030, 0085c050) Pushes a value as the API does: numbers, booleans and strings as themselves, objects by pointer (nil for none) and
// class values as copies.
template <typename T>
void lua_push(lua_State* state, const T& value)
{
if constexpr (std::is_same_v<T, bool>) lua_pushboolean(state, value);
else if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) lua_pushinteger(state, (lua_Integer)value);
else if constexpr (std::is_floating_point_v<T>) lua_pushnumber(state, value);
else if constexpr (IS_LUA_STRING<T>) lua_pushlstring(state, value.data(), value.size());
else if constexpr (std::is_same_v<T, const char*>) lua_pushstring(state, value);
else if constexpr (std::is_same_v<T, LuaOpaque>)
lua_newuserdata(state, sizeof(void*));
else if constexpr (std::is_pointer_v<T>)
{
static_assert(IS_LUA_CLASS<std::remove_cv_t<std::remove_pointer_t<T>>>);
lua_object_push(state, (void*)value, LUA_CLASS_OF<std::remove_cv_t<std::remove_pointer_t<T>>>);
}
else
{
static_assert(IS_LUA_CLASS<T>);
lua_copy_push(state, value);
}
}
// Reads argument index as a T: numbers are checked, booleans are truthiness, objects are checked against their class
// (nil only for a pointer), a class value is copied unless T is a reference, and a LuaDefault may be left out.
template <typename T>
T lua_get(lua_State* state, int index)
{
using V = std::remove_cvref_t<T>;
if constexpr (std::is_same_v<V, LuaIgnored>) return {};
else if constexpr (IS_LUA_DEFAULT<V>)
return lua_isnoneornil(state, index) ? V{} : V{lua_get<decltype(V::value)>(state, index)};
else if constexpr (std::is_same_v<V, bool>) return lua_toboolean(state, index);
else if constexpr (std::is_integral_v<V> || std::is_enum_v<V>) return (V)luaL_checkinteger(state, index);
else if constexpr (std::is_floating_point_v<V>) return (V)luaL_checknumber(state, index);
else if constexpr (IS_LUA_STRING<V>)
{
size_t length = 0;
const char* text = luaL_checklstring(state, index, &length);
return V(text, length);
}
else if constexpr (std::is_pointer_v<V>)
{
using O = std::remove_cv_t<std::remove_pointer_t<V>>;
static_assert(IS_LUA_CLASS<O>);
return lua_isnoneornil(state, index) ? nullptr : (V)lua_object_check(state, index, LUA_CLASS_OF<O>);
}
else
{
static_assert(IS_LUA_CLASS<V>);
return *(V*)lua_object_check(state, index, LUA_CLASS_OF<V>);
}
}
// The C function a binding runs as: reads its arguments from the stack, calls it, and pushes what it returns.
// References to objects are pushed as the objects, not copies.
template <typename R, typename... Args>
int lua_thunk(lua_State* state)
{
R (*function)(Args...);
std::memcpy(&function, lua_touserdata(state, lua_upvalueindex(1)), sizeof(function));
return [&]<size_t... I>(std::index_sequence<I...>) {
if constexpr (std::is_void_v<R>)
{
function(lua_get<Args>(state, I + 1)...);
return 0;
}
else
{
using V = std::remove_cvref_t<R>;
if constexpr (std::is_lvalue_reference_v<R> && std::is_class_v<V> && !IS_LUA_STRING<V>)
lua_push(state, &function(lua_get<Args>(state, I + 1)...));
else lua_push<V>(state, function(lua_get<Args>(state, I + 1)...));
return 1;
}
}(std::index_sequence_for<Args...>{});
}
// Pushes a binding as a closure over its function pointer.
template <typename R, typename... Args>
void lua_function_push(lua_State* state, R (*function)(Args...))
{
std::memcpy(lua_newuserdata(state, sizeof(function)), &function, sizeof(function));
lua_pushcclosure(state, lua_thunk<R, Args...>, 1);
}
// Adds a method to the class begun last, or a function to the namespace begun last.
template <typename F>
void lua_method(lua_State* state, const char* name, F function)
{
lua_function_push(state, +function);
lua_table_set(state, -2, name);
}
// Adds a function to the static table of the class begun last, such as its __call constructor.
template <typename F>
void lua_static(lua_State* state, const char* name, F function)
{
lua_function_push(state, +function);
lua_table_set(state, -3, name);
}
// Adds a property to the class or namespace begun last, read-only without a setter.
template <typename G>
void lua_property(lua_State* state, const char* name, G getter)
{
lua_function_push(state, +getter);
lua_property_set(state, "__propget", name);
}
template <typename G, typename S>
void lua_property(lua_State* state, const char* name, G getter, S setter)
{
lua_property(state, name, getter);
lua_function_push(state, +setter);
lua_property_set(state, "__propset", name);
}
// Adds a property reading and writing a data member, of class C when the member is its base's.
template <auto Member, typename C = decltype(lua_member_class(Member))>
void lua_field(lua_State* state, const char* name)
{
using T = decltype(lua_member_type(Member));
lua_property(
state, name, [](C& object) -> T { return object.*Member; }, [](C& object, T value) { object.*Member = value; });
}
// Adds a read-only property for a data member.
template <auto Member, typename C = decltype(lua_member_class(Member))>
void lua_field_get(lua_State* state, const char* name)
{
using T = decltype(lua_member_type(Member));
lua_property(state, name, [](C& object) -> T { return object.*Member; });
}
// Gives the class begun last a __gc that destroys the copies Lua owns, such as sprites scripts construct.
template <typename T>
void lua_gc_set(lua_State* state)
{
lua_pushcfunction(state, [](lua_State* gcState) {
void** block = (void**)lua_touserdata(gcState, 1);
if (block && *block == block + 1) static_cast<T*>(*block)->~T();
return 0;
});
lua_table_set(state, -2, "__gc");
}
// The object a list element is pushed as: what a unique_ptr holds, else the element itself.
template <typename T> auto* lua_list_element(T& element)
{
if constexpr (requires { element.get(); })
return element.get();
else
return &element;
}
// Registers a list a binding hands out (the original's CppContainer classes): its Size and length, and Get, nil past
// the end where the original throws.
template <typename List> void lua_list_register(lua_State* state, const char* name)
{
lua_class_begin(state, name, LUA_CLASS_OF<List>);
lua_property(state, "Size", [](List& list) { return (int)list.size(); });
lua_method(state, "__len", [](List& list) { return (int)list.size(); });
lua_method(state, "Get", [](List& list, uint32_t index) {
return index < list.size() ? lua_list_element(list[index]) : nullptr;
});
lua_class_end(state, LUA_SCOPE_NAMESPACE);
}