#pragma once #include "lua/engine.h" #include #include #include #include #include #include #include #include #include // 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 struct LuaDefault { T value = Default; }; template constexpr bool IS_LUA_DEFAULT = false; template constexpr bool IS_LUA_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 constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_COUNT; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ACTIVE_ITEM_DESC; template <> inline constexpr LuaClass LUA_CLASS_OF> = LUA_CLASS_BIT_SET128; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_COLOR; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ENTITY; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_BOMB; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_EFFECT; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_FAMILIAR; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_KNIFE; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_LASER; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_NPC; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_PICKUP; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_PLAYER; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_PROJECTILE; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_TEAR; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ENTITY_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ENTITY_PTR; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ENTITY_REF; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_FONT; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_FONT_SETTINGS; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GAME; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_DESC; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_DOOR; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_PIT; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_POOP; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_PRESSURE_PLATE; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_ROCK; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_SPIKES; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_GRID_ENTITY_TNT; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_HUD; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ITEM_CONFIG; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ITEM_CONFIG_CARD; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ITEM_CONFIG_COSTUME; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ITEM_CONFIG_ITEM; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ITEM_CONFIG_PILL; template <> inline constexpr LuaClass LUA_CLASS_OF>> = LUA_CLASS_ITEM_CONFIG_CARD_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF>> = LUA_CLASS_ITEM_CONFIG_ITEM_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF>> = LUA_CLASS_ITEM_CONFIG_PILL_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ITEM_POOL; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_KCOLOR; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_LEVEL; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_MUSIC_MANAGER; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_PROJECTILE_PARAMS; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_QUEUE_ITEM_DATA; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_RNG; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ROOM; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ROOM_CONFIG_ENTRY; template <> inline constexpr LuaClass LUA_CLASS_OF> = LUA_CLASS_ROOM_CONFIG_ENTRY_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ROOM_CONFIG_ROOM; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ROOM_CONFIG_SPAWN; template <> inline constexpr LuaClass LUA_CLASS_OF> = LUA_CLASS_ROOM_CONFIG_SPAWN_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ROOM_DESCRIPTOR; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_ROOM_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_PATH_FINDER; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_POS_VEL; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_SEEDS; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_SFX_MANAGER; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_SPRITE; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_TEAR_PARAMS; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_TEMPORARY_EFFECT; template <> inline constexpr LuaClass LUA_CLASS_OF> = LUA_CLASS_EFFECT_LIST; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_TEMPORARY_EFFECTS; template <> inline constexpr LuaClass LUA_CLASS_OF = LUA_CLASS_VECTOR; template <> inline constexpr LuaClass LUA_CLASS_OF> = LUA_CLASS_VECTOR_LIST; // The class and member type of a pointer to a data member, for decltype only. template C lua_member_class(T C::*); template T lua_member_type(T C::*); // Whether a type has a Lua class to push and check it as. template constexpr bool IS_LUA_CLASS = LUA_CLASS_OF != LUA_CLASS_COUNT; template constexpr bool IS_LUA_STRING = std::is_same_v || std::is_same_v; 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); 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 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)); 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 void lua_push(lua_State* state, const T& value) { if constexpr (std::is_same_v) lua_pushboolean(state, value); else if constexpr (std::is_integral_v || std::is_enum_v) lua_pushinteger(state, (lua_Integer)value); else if constexpr (std::is_floating_point_v) lua_pushnumber(state, value); else if constexpr (IS_LUA_STRING) lua_pushlstring(state, value.data(), value.size()); else if constexpr (std::is_same_v) lua_pushstring(state, value); else if constexpr (std::is_same_v) lua_newuserdata(state, sizeof(void*)); else if constexpr (std::is_pointer_v) { static_assert(IS_LUA_CLASS>>); lua_object_push(state, (void*)value, LUA_CLASS_OF>>); } else { static_assert(IS_LUA_CLASS); 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 T lua_get(lua_State* state, int index) { using V = std::remove_cvref_t; if constexpr (std::is_same_v) return {}; else if constexpr (IS_LUA_DEFAULT) return lua_isnoneornil(state, index) ? V{} : V{lua_get(state, index)}; else if constexpr (std::is_same_v) return lua_toboolean(state, index); else if constexpr (std::is_integral_v || std::is_enum_v) return (V)luaL_checkinteger(state, index); else if constexpr (std::is_floating_point_v) return (V)luaL_checknumber(state, index); else if constexpr (IS_LUA_STRING) { size_t length = 0; const char* text = luaL_checklstring(state, index, &length); return V(text, length); } else if constexpr (std::is_pointer_v) { using O = std::remove_cv_t>; static_assert(IS_LUA_CLASS); return lua_isnoneornil(state, index) ? nullptr : (V)lua_object_check(state, index, LUA_CLASS_OF); } else { static_assert(IS_LUA_CLASS); return *(V*)lua_object_check(state, index, LUA_CLASS_OF); } } // 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 int lua_thunk(lua_State* state) { R (*function)(Args...); std::memcpy(&function, lua_touserdata(state, lua_upvalueindex(1)), sizeof(function)); return [&](std::index_sequence) { if constexpr (std::is_void_v) { function(lua_get(state, I + 1)...); return 0; } else { using V = std::remove_cvref_t; if constexpr (std::is_lvalue_reference_v && std::is_class_v && !IS_LUA_STRING) lua_push(state, &function(lua_get(state, I + 1)...)); else lua_push(state, function(lua_get(state, I + 1)...)); return 1; } }(std::index_sequence_for{}); } // Pushes a binding as a closure over its function pointer. template 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, 1); } // Adds a method to the class begun last, or a function to the namespace begun last. template 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 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 void lua_property(lua_State* state, const char* name, G getter) { lua_function_push(state, +getter); lua_property_set(state, "__propget", name); } template 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 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 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 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(*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 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 void lua_list_register(lua_State* state, const char* name) { lua_class_begin(state, name, LUA_CLASS_OF); 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); }