241 lines
8.5 KiB
C++
241 lines
8.5 KiB
C++
#pragma once
|
|
|
|
#include "lua_callbacks.h"
|
|
#include <array>
|
|
#include <bitset>
|
|
#include <cstdint>
|
|
#include <list>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
struct Entity;
|
|
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 EntityRef;
|
|
struct EntitySpawn;
|
|
struct RNG;
|
|
struct RoomEntry;
|
|
struct Vector;
|
|
struct lua_State;
|
|
|
|
constexpr int LUA_GC_HISTORY_SIZE = 10;
|
|
constexpr int LUA_GC_STEP_MIN = 50;
|
|
constexpr int LUA_MEMORY_HISTORY_SIZE = 128;
|
|
// Lua's LUA_NOREF: a registry reference to nothing.
|
|
constexpr int LUA_REF_NONE = -2;
|
|
|
|
// The game classes the API binds, each with the registry key of the metatable its objects get.
|
|
enum LuaClass
|
|
{
|
|
LUA_CLASS_ACTIVE_ITEM_DESC,
|
|
LUA_CLASS_BIT_SET128,
|
|
LUA_CLASS_COLOR,
|
|
LUA_CLASS_ENTITY,
|
|
LUA_CLASS_BOMB,
|
|
LUA_CLASS_EFFECT,
|
|
LUA_CLASS_FAMILIAR,
|
|
LUA_CLASS_KNIFE,
|
|
LUA_CLASS_LASER,
|
|
LUA_CLASS_NPC,
|
|
LUA_CLASS_PICKUP,
|
|
LUA_CLASS_PLAYER,
|
|
LUA_CLASS_PROJECTILE,
|
|
LUA_CLASS_TEAR,
|
|
LUA_CLASS_EFFECT_LIST,
|
|
LUA_CLASS_ENTITY_LIST,
|
|
LUA_CLASS_ENTITY_PTR,
|
|
LUA_CLASS_ENTITY_REF,
|
|
LUA_CLASS_FONT,
|
|
LUA_CLASS_FONT_SETTINGS,
|
|
LUA_CLASS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ASS_SPRITE,
|
|
LUA_CLASS_TEAR_PARAMS,
|
|
LUA_CLASS_TEMPORARY_EFFECT,
|
|
LUA_CLASS_TEMPORARY_EFFECTS,
|
|
LUA_CLASS_VECTOR,
|
|
LUA_CLASS_VECTOR_LIST,
|
|
LUA_CLASS_COUNT
|
|
};
|
|
|
|
// Whether Lua runs sandboxed, or with every library, require searching resources/scripts/ and _LUADEBUG set.
|
|
enum LuaMode
|
|
{
|
|
LUA_MODE_NORMAL,
|
|
LUA_MODE_DEBUG
|
|
};
|
|
|
|
// A mod a script registered with RegisterMod, kept until that script runs again or Lua resets.
|
|
struct LuaMod
|
|
{
|
|
std::string directory;
|
|
std::string name;
|
|
std::string scriptPath;
|
|
int apiVersion = 0;
|
|
int tableRef = LUA_REF_NONE;
|
|
};
|
|
|
|
// 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.
|
|
// 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::list<LuaMod> mods;
|
|
std::string scriptPath;
|
|
lua_State* state = nullptr;
|
|
LuaMode mode = LUA_MODE_NORMAL;
|
|
int gcCooldown = 0;
|
|
int gcFrame = 0;
|
|
int gcLastMemory = -1;
|
|
int gcStepSize = LUA_GC_STEP_MIN;
|
|
int memoryHistoryCount = 0;
|
|
int memoryHistoryIndex = 0;
|
|
int memoryHistoryTimer = 0;
|
|
int runCallbackRef = LUA_REF_NONE;
|
|
int scriptApiVersion = -1;
|
|
int unloadModRef = LUA_REF_NONE;
|
|
uint32_t pointerMask = 0;
|
|
|
|
void Init(LuaMode luaMode);
|
|
void RegisterAPI();
|
|
void destroy();
|
|
bool RunScript(std::string_view path);
|
|
bool LuamodCMD(std::string_view modName);
|
|
void gc_update();
|
|
static int Isaac_GetItemIdByName(std::string_view name);
|
|
|
|
LuaResult PreBombCollision(Entity_Bomb* bomb, Entity* collider, bool isLow);
|
|
LuaResult PreFamiliarCollision(Entity_Familiar* familiar, Entity* collider, bool isLow);
|
|
LuaResult PreKnifeCollision(Entity_Knife* knife, Entity* collider, bool isLow);
|
|
LuaResult PreNPCCollision(Entity_NPC* npc, Entity* collider, bool isLow);
|
|
LuaResult PrePickupCollision(Entity_Pickup* pickup, Entity* collider, bool isLow);
|
|
LuaResult PrePlayerCollide(Entity_Player* player, Entity* collider, bool isLow);
|
|
LuaResult PreProjectileCollision(Entity_Projectile* projectile, Entity* collider, bool isLow);
|
|
LuaResult PreTearCollision(Entity_Tear* tear, Entity* collider, bool isLow);
|
|
|
|
int GetCard(const LuaCardDraw& draw);
|
|
int GetPillColor(uint32_t seed);
|
|
int GetPillEffect(int pillEffect, int pillColor);
|
|
int GetTrinket(int trinket, const RNG& rng);
|
|
std::optional<bool> InputAction(Entity* entity, int hook, int action);
|
|
std::optional<float> InputActionValue(Entity* entity, int action);
|
|
int PostCurseEval(int curses);
|
|
int PostGetCollectible(const LuaCollectibleDraw& draw);
|
|
int PreGetCollectible(const LuaCollectibleDraw& draw);
|
|
bool EntityTakeDamage(Entity* entity, float damage, uint64_t damageFlags, const EntityRef& source, int countdown);
|
|
bool PreEntityDevolve(Entity* entity);
|
|
bool PreNPCUpdate(Entity_NPC* npc);
|
|
bool PreSpawnCleanAward(const RNG& rng, const Vector& position);
|
|
bool PreUseItem(const LuaItemUse& use);
|
|
void Callback_PreEntitySpawn(EntitySpawn& spawn);
|
|
bool PreRoomEntitySpawn(RoomEntry& entry, int gridIndex, uint32_t seed);
|
|
void PostPickupSelection(Entity_Pickup* pickup, int& variant, int& subType);
|
|
bool UseItem(LuaItemUse& use);
|
|
|
|
void EvaluateItems(Entity_Player* player, int cacheFlag);
|
|
void FamiliarInit(Entity_Familiar* familiar);
|
|
void FamiliarUpdate(Entity_Familiar* familiar);
|
|
void PostBombInit(Entity_Bomb* bomb);
|
|
void PostBombRender(Entity_Bomb* bomb, const Vector& offset);
|
|
void PostBombUpdate(Entity_Bomb* bomb);
|
|
void PostEffectInit(Entity_Effect* effect);
|
|
void PostEffectRender(Entity_Effect* effect, const Vector& offset);
|
|
void PostEffectUpdate(Entity_Effect* effect);
|
|
void PostEntityKill(Entity* entity);
|
|
void PostEntityRemove(Entity* entity);
|
|
void PostFamiliarRender(Entity_Familiar* familiar, const Vector& offset);
|
|
void PostFireTear(Entity_Tear* tear);
|
|
void PostGameEnd(bool isGameOver);
|
|
void PostGameStart(bool isContinued);
|
|
void PostKnifeInit(Entity_Knife* knife);
|
|
void PostKnifeRender(Entity_Knife* knife, const Vector& offset);
|
|
void PostKnifeUpdate(Entity_Knife* knife);
|
|
void PostLaserInit(Entity_Laser* laser);
|
|
void PostLaserRender(Entity_Laser* laser, const Vector& offset);
|
|
void PostLaserUpdate(Entity_Laser* laser);
|
|
void PostNewLevel();
|
|
void PostNewRoom();
|
|
void PostNPCDeath(Entity_NPC* npc);
|
|
void PostNPCInit(Entity_NPC* npc);
|
|
void PostNPCRender(Entity_NPC* npc, const Vector& offset);
|
|
void PostNPCUpdate(Entity_NPC* npc);
|
|
void PostPickupInit(Entity_Pickup* pickup);
|
|
void PostPickupRender(Entity_Pickup* pickup, const Vector& offset);
|
|
void PostPickupUpdate(Entity_Pickup* pickup);
|
|
void PostPEffectUpdate(Entity_Player* player);
|
|
void PostPlayerInit(Entity_Player* player);
|
|
void PostPlayerRender(Entity_Player* player, const Vector& offset);
|
|
void PostPlayerUpdate(Entity_Player* player);
|
|
void PostProjectileInit(Entity_Projectile* projectile);
|
|
void PostProjectileRender(Entity_Projectile* projectile, const Vector& offset);
|
|
void PostProjectileUpdate(Entity_Projectile* projectile);
|
|
void PostRender();
|
|
void PostTearInit(Entity_Tear* tear);
|
|
void PostTearRender(Entity_Tear* tear, const Vector& offset);
|
|
void PostTearUpdate(Entity_Tear* tear);
|
|
void PostUpdate();
|
|
void PreGameExit(bool isSaving);
|
|
void UseCard(int card, Entity_Player* player, unsigned int useFlags);
|
|
void UsePill(int pillEffect, Entity_Player* player, unsigned int useFlags);
|
|
};
|
|
|
|
extern void* g_luaEngine;
|
|
|
|
const void* lua_class_key(LuaClass luaClass);
|
|
void lua_error_log(lua_State* state, const char* name);
|
|
|
|
// The game's Lua engine, which Manager creates at startup.
|
|
inline LuaEngine& lua_engine_get()
|
|
{
|
|
return *static_cast<LuaEngine*>(g_luaEngine);
|
|
}
|