Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save/Restore entities callback #382

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libzhl/functions/Room.zhl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ __thiscall void Room::SaveState();
"568b35????????578bf9833e0d":
__thiscall uint32_t Room::GetBossVictoryJingle();

"558bec83f905":
static cleanup bool Room::ShouldSaveEntity(unsigned int Type<ecx>, unsigned int Variant<edx>, unsigned int SubType, unsigned int SpawnerType, bool Unk);

"558bec6aff68????????64a1????????5083ec3056":
__thiscall bool Room::SaveEntity(Entity* ent, EntitySaveState* saveState, bool unk);

"558bec83e4f883ec1c5356578b7d??8bf1":
__thiscall void Room::RestoreEntity(Entity* ent, EntitySaveState* saveState);

struct Room depends (EntityList, RoomDescriptor, TemporaryEffects, RailManager) {
{{
inline Camera* GetCamera() { return *(Camera**)((char*)this + 0x11F8); }
Expand Down
79 changes: 79 additions & 0 deletions repentogon/LuaInterfaces/CustomCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3935,4 +3935,83 @@ HOOK_METHOD(Minimap, Render, () -> void) {
lua_rawgeti(L, LUA_REGISTRYINDEX, g_LuaEngine->runCallbackRegistry->key);
lua::LuaCaller(L).push(postcallbackid).call(1);
}
}

HOOK_STATIC(Room, ShouldSaveEntity, (unsigned int Type, unsigned int Variant, unsigned int SubType, unsigned int SpawnerType, bool unk) -> bool) {
const int callbackid = 1481;
if (CallbackState.test(callbackid - 1000)) {
lua_State* L = g_LuaEngine->_state;
lua::LuaStackProtector protector(L);

lua_rawgeti(L, LUA_REGISTRYINDEX, g_LuaEngine->runCallbackRegistry->key);

lua::LuaResults result = lua::LuaCaller(L).push(callbackid)
.pushnil()
.push(Type)
.push(Variant)
.push(SubType)
.push(SpawnerType)
.push(unk)
.call(1);

if (!result) {
if (lua_isboolean(L, -1)) {
return lua_toboolean(L, -1);
}
}
}
super(Type, Variant, SubType, SpawnerType, unk);
}

/*void ProcessSaveRestoreCallbacks(Entity* entity, EntitySaveState* state, const char* mode, bool isCleared) {

}
*/

HOOK_METHOD(Room, SaveEntity, (Entity* entity, EntitySaveState* state, bool isCleared) -> bool) {
const int callbackid = 1482;
if (CallbackState.test(callbackid - 1000)) {
lua_State* L = g_LuaEngine->_state;
lua::LuaStackProtector protector(L);

lua_rawgeti(L, LUA_REGISTRYINDEX, g_LuaEngine->runCallbackRegistry->key);

lua::LuaCaller caller(L);



lua::LuaResults result = lua::LuaCaller(L).push(callbackid)
.push(entity->_type)
.push(entity, lua::Metatables::ENTITY)
.push(&state, lua::metatables::EntitySaveStateMT)
.push(isCleared)
.call(1);

if (!result) {
if (lua_isboolean(L, -1)) {
return lua_toboolean(L, -1);
}
}
}

return super(entity, state, isCleared);

}

HOOK_METHOD(Room, RestoreEntity, (Entity* entity, EntitySaveState* state) -> void) {
const int callbackid = 1483;
if (CallbackState.test(callbackid - 1000)) {
lua_State* L = g_LuaEngine->_state;
lua::LuaStackProtector protector(L);

lua_rawgeti(L, LUA_REGISTRYINDEX, g_LuaEngine->runCallbackRegistry->key);

lua::LuaCaller(L).push(callbackid)
.push(entity->_type)
.push(entity, lua::Metatables::ENTITY)
.push(state, lua::metatables::EntitySaveStateMT)
.call(1);
}

super(entity, state);
}
Loading