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

Changes for better script online compatibility #369

Merged
merged 20 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0c65be3
WIP rollback function hook
estebanfer Feb 2, 2024
5a590a5
add local_data to StateMemory, to allow script data that gets affecte…
estebanfer Feb 9, 2024
2543422
hook rollback function with patch_and_redirect and fix it
estebanfer Feb 11, 2024
9a994e4
Merge branch 'main' into online-compat-stuff
estebanfer Feb 12, 2024
bbffae5
Merge branch 'main' into online-compat-stuff
estebanfer Mar 6, 2024
a91419c
Move local_state_datas to LocalStateData struct
estebanfer Mar 6, 2024
f2e4f8b
Merge branch 'main' into online-compat-stuff
estebanfer Mar 7, 2024
e70fa55
make more callbacks online compatible, fix deadlock
estebanfer Mar 8, 2024
79c13f3
format
estebanfer Mar 8, 2024
43386cb
docs, rename clone_heap -> copy_state, state local_data -> user_data
estebanfer Mar 29, 2024
8df6529
change state location to read-only via getter
estebanfer Mar 31, 2024
488d643
trigger PRE_COPY_STATE on Overlunky savestates
estebanfer Apr 10, 2024
c704236
move State::get_location to header file
estebanfer Apr 10, 2024
3881f06
minor autodoc fix
estebanfer Apr 11, 2024
7e9c411
remove PRE_COPY_STATE lua event for now
estebanfer May 1, 2024
f105e88
fix get_local_* return type in docs
estebanfer May 3, 2024
f93ee54
fix spawn_api and custom_allocator online
estebanfer Jun 3, 2024
4b1a8b2
Merge branch 'main' into online-compat-stuff
estebanfer Aug 3, 2024
98cb792
better deepcopy_object implementation
estebanfer Aug 3, 2024
9f70235
change get_location to get_offset, remove get_state_offset
estebanfer Aug 3, 2024
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
1 change: 1 addition & 0 deletions docs/game_data/spel2.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/src/includes/_enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ Name | Data | Description
[BLOCKED_UPDATE](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=ON.BLOCKED_UPDATE) | ON::BLOCKED_UPDATE | Runs instead of POST_UPDATE when anything blocks a PRE_UPDATE. Even runs in Playlunky when [Overlunky](#Overlunky) blocks a PRE_UPDATE.<br/>
[BLOCKED_GAME_LOOP](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=ON.BLOCKED_GAME_LOOP) | ON::BLOCKED_GAME_LOOP | Runs instead of POST_GAME_LOOP when anything blocks a PRE_GAME_LOOP. Even runs in Playlunky when [Overlunky](#Overlunky) blocks a PRE_GAME_LOOP.<br/>
[BLOCKED_PROCESS_INPUT](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=ON.BLOCKED_PROCESS_INPUT) | ON::BLOCKED_PROCESS_INPUT | Runs instead of POST_PROCESS_INPUT when anything blocks a PRE_PROCESS_INPUT. Even runs in Playlunky when [Overlunky](#Overlunky) blocks a PRE_PROCESS_INPUT.<br/>
[PRE_COPY_STATE](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=ON.PRE_COPY_STATE) | ON::PRE_COPY_STATE | Runs before the game copies a state to another in an online multiplayer game.<br/>Params: [StateMemory](#StateMemory) from, [StateMemory](#StateMemory) to<br/>

## PARTICLEEMITTER

Expand Down
7 changes: 7 additions & 0 deletions docs/src/includes/_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,10 @@ Runs instead of POST_GAME_LOOP when anything blocks a PRE_GAME_LOOP. Even runs i
> Search script examples for [ON.BLOCKED_PROCESS_INPUT](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=ON.BLOCKED_PROCESS_INPUT)

Runs instead of POST_PROCESS_INPUT when anything blocks a PRE_PROCESS_INPUT. Even runs in Playlunky when [Overlunky](#Overlunky) blocks a PRE_PROCESS_INPUT.<br/>

## ON.PRE_COPY_STATE


> Search script examples for [ON.PRE_COPY_STATE](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=ON.PRE_COPY_STATE)

Runs before the game copies a state to another in an online multiplayer game.<br/>Params: [StateMemory](#StateMemory) from, [StateMemory](#StateMemory) to<br/>
1 change: 1 addition & 0 deletions docs/src/includes/_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -3084,6 +3084,7 @@ array&lt;[THEME](#THEME), 9&gt; | [journal_progress_theme_slots](https://github.
[LiquidPhysics](#LiquidPhysics) | [liquid](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=liquid) |
int | [next_entity_uid](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=next_entity_uid) | Next entity spawned will have this uid
[RoomOwnersInfo](#RoomOwnersInfo) | [room_owners](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=room_owners) | Holds info about owned rooms and items (shops, challenge rooms, vault etc.)
| [user_data](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=user_data) | You can put any arbitrary lua object here and it will work correctly in online multiplayer, by having a copy on each state and being copied when the game does.<br/>

## Texture types

Expand Down
4 changes: 2 additions & 2 deletions src/game_api/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ size_t patch_and_redirect(size_t addr, size_t replace_size, const std::string_vi
}
else
{
std::memcpy(new_code + data_size_to_move, payload.data(), payload.size());
std::memcpy(new_code, payload.data(), payload.size());

if (!just_nop)
std::memcpy(new_code, (void*)addr, data_size_to_move);
std::memcpy(new_code + payload.size(), (void*)addr, data_size_to_move);
}

size_t return_addr = addr + replace_size;
Expand Down
13 changes: 10 additions & 3 deletions src/game_api/savestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ size_t get_state_offset()
return 0x4a0;
}

StateMemory* get_save_state_raw(int slot)
{
size_t arr = get_address("save_states");
size_t base = memory_read<size_t>(arr + (slot - 1) * 8);
auto state = reinterpret_cast<StateMemory*>(base + get_state_offset());
return state;
}

void copy_save_slot(int from, int to)
{
if ((from == 5 && pre_save_state(to, get_save_state(to))) ||
(to == 5 && pre_load_state(from, get_save_state(from))))
return;
pre_copy_state_event(get_save_state_raw(from), get_save_state_raw(to));
size_t arr = get_address("save_states");
size_t fromBaseState = memory_read<size_t>(arr + (from - 1) * 8);
size_t toBaseState = memory_read<size_t>(arr + (to - 1) * 8);
Expand Down Expand Up @@ -57,9 +66,7 @@ void copy_state(size_t fromBaseState, size_t toBaseState)

StateMemory* get_save_state(int slot)
{
size_t arr = get_address("save_states");
size_t base = memory_read<size_t>(arr + (slot - 1) * 8);
auto state = reinterpret_cast<StateMemory*>(base + get_state_offset());
auto state = get_save_state_raw(slot);
if (state->screen)
return state;
return nullptr;
Expand Down
10 changes: 10 additions & 0 deletions src/game_api/script/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,13 @@ void post_event(ON event)
return true;
});
}

void pre_copy_state_event(StateMemory* from, StateMemory* to)
{
LuaBackend::for_each_backend(
[&](LuaBackend::LockedBackend backend)
{
backend->pre_copy_state(from, to);
return true;
});
}
1 change: 1 addition & 0 deletions src/game_api/script/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bool pre_unload_level();
bool pre_unload_layer(LAYER layer);
bool pre_save_state(int slot, StateMemory* saved);
bool pre_load_state(int slot, StateMemory* loaded);
void pre_copy_state_event(StateMemory* from, StateMemory* to);

void post_load_screen();
void post_init_layer(LAYER layer);
Expand Down
Loading
Loading