From 628572af84a422218036eeaca7e0f2817eea7d86 Mon Sep 17 00:00:00 2001 From: IQuant Date: Sun, 17 Nov 2024 17:10:57 +0300 Subject: [PATCH] Made cross calls work within the same lua context --- quant.ew/files/core/ctx.lua | 8 ------- quant.ew/files/core/util.lua | 21 ++++++++++++++++++- quant.ew/files/system/damage/damage.lua | 6 +++--- quant.ew/files/system/ending/ending.lua | 2 +- quant.ew/files/system/enemy_sync.lua | 2 +- .../files/system/ewext_init/ewext_init.lua | 2 +- quant.ew/files/system/flag_sync/flag_sync.lua | 2 +- .../system/fungal_shift/fungal_shift.lua | 4 ++-- quant.ew/files/system/gate_boss/gate_boss.lua | 2 +- quant.ew/files/system/gen_sync/gen_sync.lua | 2 +- quant.ew/files/system/hamis/hamis.lua | 2 +- quant.ew/files/system/kolmi/kolmi.lua | 8 +++---- .../system/local_health/local_health.lua | 4 ++-- .../system/perk_patches/perk_patches.lua | 8 +++---- .../system/potion_mimic/potion_mimic.lua | 4 ++-- quant.ew/files/system/shiny_orb/shiny_orb.lua | 2 +- .../files/system/spawn_hooks/spawn_hooks.lua | 8 +++---- .../system/spell_refresh/spell_refresh.lua | 2 +- quant.ew/files/system/stevari/stevari.lua | 2 +- .../system/streaming_sync/streaming_sync.lua | 2 +- .../files/system/wand_charm/wand_charm.lua | 2 +- .../world_sync_cuts/world_sync_cuts.lua | 2 +- quant.ew/init.lua | 4 ++-- 23 files changed, 56 insertions(+), 45 deletions(-) diff --git a/quant.ew/files/core/ctx.lua b/quant.ew/files/core/ctx.lua index b3e54049..31924155 100644 --- a/quant.ew/files/core/ctx.lua +++ b/quant.ew/files/core/ctx.lua @@ -27,14 +27,6 @@ ctx.init = function() ctx.host_frame_num = 0 end -np.CrossCallAdd("ew_host_frame_num", function() - if ctx.my_id == ctx.host_id then - return GameGetFrameNum() - else - return ctx.host_frame_num - end -end) - local function is_measure_perf_enabled() -- return ctx.proxy_opt.debug return false diff --git a/quant.ew/files/core/util.lua b/quant.ew/files/core/util.lua index 3e74432b..c1997692 100644 --- a/quant.ew/files/core/util.lua +++ b/quant.ew/files/core/util.lua @@ -324,10 +324,29 @@ function util.deserialize_entity(ent_data, x, y) np.DeserializeEntity(ent, ent_data, x, y) end if EntityGetFirstComponentIncludingDisabled(ent, "WorldStateComponent") ~= nil then - error("Tried to deserialize WorldStateEntity. The world is screwed.") + print("Tried to deserialize WorldStateEntity. The world is screwed.") EntityKill(ent) end return ent end +local cross_calls = {} + +function util.add_cross_call(name, fn) + np.CrossCallAdd(name, fn) + cross_calls[name] = fn +end + +function CrossCall(name, ...) + cross_calls[name](...) +end + +util.add_cross_call("ew_host_frame_num", function() + if ctx.my_id == ctx.host_id then + return GameGetFrameNum() + else + return ctx.host_frame_num + end +end) + return util \ No newline at end of file diff --git a/quant.ew/files/system/damage/damage.lua b/quant.ew/files/system/damage/damage.lua index 8cf90480..cbd3c97e 100644 --- a/quant.ew/files/system/damage/damage.lua +++ b/quant.ew/files/system/damage/damage.lua @@ -37,7 +37,7 @@ local function damage_received(damage, message, entity_id, add_healing_effect) end end -np.CrossCallAdd("ew_ds_damaged", damage_received) +util.add_cross_call("ew_ds_damaged", damage_received) local function do_game_over(message) net.proxy_notify_game_over() @@ -215,7 +215,7 @@ function rpc.effect_hearty(applied) GlobalsSetValue("ew_effect_hearty", tostring(hearty_applied_count)) end -np.CrossCallAdd("ew_ds_effect_hearty", rpc.effect_hearty) +util.add_cross_call("ew_ds_effect_hearty", rpc.effect_hearty) rpc.opts_reliable() function rpc.melee_damage_client(target_peer, damage, message) @@ -223,6 +223,6 @@ function rpc.melee_damage_client(target_peer, damage, message) EntityInflictDamage(ctx.my_player.entity, damage, "DAMAGE_MELEE", message, "NONE", 0, 0, 0) end end -np.CrossCallAdd("ew_ds_client_damaged", rpc.melee_damage_client) +util.add_cross_call("ew_ds_client_damaged", rpc.melee_damage_client) return module \ No newline at end of file diff --git a/quant.ew/files/system/ending/ending.lua b/quant.ew/files/system/ending/ending.lua index e1a6c2f2..67d65c4d 100644 --- a/quant.ew/files/system/ending/ending.lua +++ b/quant.ew/files/system/ending/ending.lua @@ -49,7 +49,7 @@ function rpc.gather_and_do_ending(x, y, sx, sy) end) end -np.CrossCallAdd("ew_ending_sequence", function(sx, sy, sampo_ent) +util.add_cross_call("ew_ending_sequence", function(sx, sy, sampo_ent) EntityKill(sampo_ent) local x, y = EntityGetTransform(ctx.my_player.entity) rpc.gather_and_do_ending(x, y, sx, sy) diff --git a/quant.ew/files/system/enemy_sync.lua b/quant.ew/files/system/enemy_sync.lua index 02d89de8..d18df1d3 100644 --- a/quant.ew/files/system/enemy_sync.lua +++ b/quant.ew/files/system/enemy_sync.lua @@ -87,7 +87,7 @@ for filename, _ in pairs(constants.phys_sync_allowed) do -- util.replace_text_in(filename, 'kill_entity_after_initialized="1"', 'kill_entity_after_initialized="0"') end -np.CrossCallAdd("ew_es_death_notify", function(enemy_id, responsible_id) +util.add_cross_call("ew_es_death_notify", function(enemy_id, responsible_id) local player_data = player_fns.get_player_data_by_local_entity_id(responsible_id) local responsible if player_data ~= nil then diff --git a/quant.ew/files/system/ewext_init/ewext_init.lua b/quant.ew/files/system/ewext_init/ewext_init.lua index 954cf232..e378cbc7 100644 --- a/quant.ew/files/system/ewext_init/ewext_init.lua +++ b/quant.ew/files/system/ewext_init/ewext_init.lua @@ -1,7 +1,7 @@ local ffi = require("ffi") local world_ffi = require("noitapatcher.nsew.world_ffi") --- np.CrossCallAdd("make_ephemerial", ewext.make_ephemerial) +-- util.add_cross_call("make_ephemerial", ewext.make_ephemerial) local initial_world_state_entity = nil diff --git a/quant.ew/files/system/flag_sync/flag_sync.lua b/quant.ew/files/system/flag_sync/flag_sync.lua index a7af05c0..bb32fff2 100644 --- a/quant.ew/files/system/flag_sync/flag_sync.lua +++ b/quant.ew/files/system/flag_sync/flag_sync.lua @@ -12,6 +12,6 @@ function EwHasPersistentFlag(flag) return has_flag(flag) end -np.CrossCallAdd("ew_has_flag", has_flag) +util.add_cross_call("ew_has_flag", has_flag) return {} diff --git a/quant.ew/files/system/fungal_shift/fungal_shift.lua b/quant.ew/files/system/fungal_shift/fungal_shift.lua index b400b7a7..c5c638cb 100644 --- a/quant.ew/files/system/fungal_shift/fungal_shift.lua +++ b/quant.ew/files/system/fungal_shift/fungal_shift.lua @@ -80,11 +80,11 @@ end local conversions = {} -np.CrossCallAdd("ew_fungal_shift_conversion", function(from_mat, to_mat) +util.add_cross_call("ew_fungal_shift_conversion", function(from_mat, to_mat) table.insert(conversions, {from_mat, to_mat}) end) -np.CrossCallAdd("ew_fungal_shift", function(iter, from_material_name) +util.add_cross_call("ew_fungal_shift", function(iter, from_material_name) rpc.fungal_shift(conversions, iter, from_material_name) conversions = {} end) diff --git a/quant.ew/files/system/gate_boss/gate_boss.lua b/quant.ew/files/system/gate_boss/gate_boss.lua index 7a010436..1bcd34a9 100644 --- a/quant.ew/files/system/gate_boss/gate_boss.lua +++ b/quant.ew/files/system/gate_boss/gate_boss.lua @@ -19,7 +19,7 @@ function rpc.spawn_gate() end end -np.CrossCallAdd("ew_spawn_gate", function() +util.add_cross_call("ew_spawn_gate", function() rpc.spawn_gate() end) diff --git a/quant.ew/files/system/gen_sync/gen_sync.lua b/quant.ew/files/system/gen_sync/gen_sync.lua index 0bcc568b..20822950 100644 --- a/quant.ew/files/system/gen_sync/gen_sync.lua +++ b/quant.ew/files/system/gen_sync/gen_sync.lua @@ -66,7 +66,7 @@ function rpc.remote_run_spawn_fn(fn_name, x, y, ...) run_spawn_fn(fn_name, x, y, ...) end -np.CrossCallAdd("ew_sync_gen", function(fn_name, x, y, ...) +util.add_cross_call("ew_sync_gen", function(fn_name, x, y, ...) if ctx.is_host then run_spawn_fn(fn_name, x, y, ...) else diff --git a/quant.ew/files/system/hamis/hamis.lua b/quant.ew/files/system/hamis/hamis.lua index d6a168b5..e2b2925e 100644 --- a/quant.ew/files/system/hamis/hamis.lua +++ b/quant.ew/files/system/hamis/hamis.lua @@ -54,7 +54,7 @@ function rpc.pet(entity_who_interacted, entity_interacted, num, hx, hy) GameEntityPlaySound( entity_who_interacted, "pet" ) end -np.CrossCallAdd("ew_pet_hamis", function(x, y) +util.add_cross_call("ew_pet_hamis", function(x, y) local hx, hy = EntityGetTransform(y) local ent = y for a, b in pairs(ctx.entity_by_remote_id) do diff --git a/quant.ew/files/system/kolmi/kolmi.lua b/quant.ew/files/system/kolmi/kolmi.lua index a3da9de3..bea1a800 100644 --- a/quant.ew/files/system/kolmi/kolmi.lua +++ b/quant.ew/files/system/kolmi/kolmi.lua @@ -82,7 +82,7 @@ function rpc.kolmi_shield(is_on, orbcount) end -np.CrossCallAdd("ew_sampo_spawned", function() +util.add_cross_call("ew_sampo_spawned", function() local sampo_ent = EntityGetClosestWithTag(0, 0, "this_is_sampo") if sampo_ent == nil or sampo_ent == 0 then -- In case sampo wasn't actually spawned. @@ -99,11 +99,11 @@ np.CrossCallAdd("ew_sampo_spawned", function() end end) -np.CrossCallAdd("ew_kolmi_spawn_portal", rpc.spawn_portal) +util.add_cross_call("ew_kolmi_spawn_portal", rpc.spawn_portal) -np.CrossCallAdd("ew_kolmi_anim", rpc.kolmi_anim) +util.add_cross_call("ew_kolmi_anim", rpc.kolmi_anim) -np.CrossCallAdd("ew_kolmi_shield", rpc.kolmi_shield) +util.add_cross_call("ew_kolmi_shield", rpc.kolmi_shield) local function is_in_box(x1, x2, y1, y2, x, y) return x1 < x and x < x2 and y1 < y and y < y2 diff --git a/quant.ew/files/system/local_health/local_health.lua b/quant.ew/files/system/local_health/local_health.lua index 4145aac7..06846960 100644 --- a/quant.ew/files/system/local_health/local_health.lua +++ b/quant.ew/files/system/local_health/local_health.lua @@ -20,7 +20,7 @@ local module = {} local last_damage_info = {0, "unknown", 1} -np.CrossCallAdd("ew_damage_message", function(message, entity_thats_responsible) +util.add_cross_call("ew_damage_message", function(message, entity_thats_responsible) last_damage_info = {GameGetFrameNum(), message, entity_thats_responsible} end) @@ -524,7 +524,7 @@ function rpc.melee_damage_client(target_peer, damage, message) EntityInflictDamage(ctx.my_player.entity, damage, "DAMAGE_MELEE", message, "NONE", 0, 0, 0) end end -np.CrossCallAdd("ew_ds_client_damaged", rpc.melee_damage_client) +util.add_cross_call("ew_ds_client_damaged", rpc.melee_damage_client) rpc.opts_everywhere() function rpc.send_status(status) diff --git a/quant.ew/files/system/perk_patches/perk_patches.lua b/quant.ew/files/system/perk_patches/perk_patches.lua index 645370ac..abc1514e 100644 --- a/quant.ew/files/system/perk_patches/perk_patches.lua +++ b/quant.ew/files/system/perk_patches/perk_patches.lua @@ -43,17 +43,17 @@ function rpc.modify_max_hp(percent_amount, do_heal) end end -np.CrossCallAdd("ew_perks_modify_max_hp", rpc.modify_max_hp) +util.add_cross_call("ew_perks_modify_max_hp", rpc.modify_max_hp) -np.CrossCallAdd("ew_ff", function() +util.add_cross_call("ew_ff", function() return ctx.proxy_opt.friendly_fire end) -np.CrossCallAdd("ew_perk_ban_list", function() +util.add_cross_call("ew_perk_ban_list", function() return ctx.proxy_opt.perk_ban_list end) -np.CrossCallAdd("ew_randomize_perks", function() +util.add_cross_call("ew_randomize_perks", function() return ctx.proxy_opt.randomize_perks end) diff --git a/quant.ew/files/system/potion_mimic/potion_mimic.lua b/quant.ew/files/system/potion_mimic/potion_mimic.lua index df5f68d8..16d4ac8a 100644 --- a/quant.ew/files/system/potion_mimic/potion_mimic.lua +++ b/quant.ew/files/system/potion_mimic/potion_mimic.lua @@ -22,11 +22,11 @@ function rpc.got_thrown(peer_id, vx, vy) end end -np.CrossCallAdd("ew_potion_mimic_throw", function(item, vx, vy) +util.add_cross_call("ew_potion_mimic_throw", function(item, vx, vy) rpc.got_thrown(player_fns.get_player_data_by_local_entity_id(item).peer_id, vx, vy) end) -np.CrossCallAdd("ew_potion_mimic_pickup", function() +util.add_cross_call("ew_potion_mimic_pickup", function() local inventory_state = player_fns.serialize_items(ctx.my_player) if inventory_state ~= nil then net.send_player_inventory(inventory_state) diff --git a/quant.ew/files/system/shiny_orb/shiny_orb.lua b/quant.ew/files/system/shiny_orb/shiny_orb.lua index 3b448172..79c4d113 100644 --- a/quant.ew/files/system/shiny_orb/shiny_orb.lua +++ b/quant.ew/files/system/shiny_orb/shiny_orb.lua @@ -29,7 +29,7 @@ function rpc.kicked_orb(gid, rx, ry, greed) drop() end -np.CrossCallAdd("ew_kicked_orb", function(entity, entity_who_kicked, greed) +util.add_cross_call("ew_kicked_orb", function(entity, entity_who_kicked, greed) if entity_who_kicked ~= ctx.my_player.entity then return end diff --git a/quant.ew/files/system/spawn_hooks/spawn_hooks.lua b/quant.ew/files/system/spawn_hooks/spawn_hooks.lua index 9b42461e..63efa827 100644 --- a/quant.ew/files/system/spawn_hooks/spawn_hooks.lua +++ b/quant.ew/files/system/spawn_hooks/spawn_hooks.lua @@ -39,7 +39,7 @@ local function is_sync_item(ent_path) return false end -np.CrossCallAdd("ew_spawn_hook_pre", function(ent_path, x, y) +util.add_cross_call("ew_spawn_hook_pre", function(ent_path, x, y) if ctx.is_host then if is_sync_item(ent_path) then local ent_id = EntityLoad(ent_path, x, y) @@ -57,17 +57,17 @@ np.CrossCallAdd("ew_spawn_hook_pre", function(ent_path, x, y) end end) -np.CrossCallAdd("ew_action_spawn_hook_pre", function() +util.add_cross_call("ew_action_spawn_hook_pre", function() return (not ctx.proxy_opt.item_dedup) or ctx.is_host end) -np.CrossCallAdd("ew_action_spawn_hook", function(eid) +util.add_cross_call("ew_action_spawn_hook", function(eid) ctx.cap.item_sync.globalize(eid, false) end) -- Called after entity was loaded. -- Might be useless in some cases, as entity was already despawned/serialized due to CameraBoundComponent. -np.CrossCallAdd("ew_spawn_hook_post", function(ent_path, ent) +util.add_cross_call("ew_spawn_hook_post", function(ent_path, ent) end) diff --git a/quant.ew/files/system/spell_refresh/spell_refresh.lua b/quant.ew/files/system/spell_refresh/spell_refresh.lua index 52815ee2..4b58df5a 100644 --- a/quant.ew/files/system/spell_refresh/spell_refresh.lua +++ b/quant.ew/files/system/spell_refresh/spell_refresh.lua @@ -1,4 +1,4 @@ -np.CrossCallAdd("ew_refresh_inventory", function() +util.add_cross_call("ew_refresh_inventory", function() local inventory_state = player_fns.serialize_items(ctx.my_player) if inventory_state ~= nil then net.send_player_inventory(inventory_state) diff --git a/quant.ew/files/system/stevari/stevari.lua b/quant.ew/files/system/stevari/stevari.lua index c976183c..aa270e9d 100644 --- a/quant.ew/files/system/stevari/stevari.lua +++ b/quant.ew/files/system/stevari/stevari.lua @@ -25,7 +25,7 @@ function rpc.spawn_stevari(pos_x, pos_y) end end -np.CrossCallAdd("ew_spawn_stevari", function(x, y) +util.add_cross_call("ew_spawn_stevari", function(x, y) rpc.spawn_stevari(x, y) end) diff --git a/quant.ew/files/system/streaming_sync/streaming_sync.lua b/quant.ew/files/system/streaming_sync/streaming_sync.lua index 834d14b4..66da7d8e 100644 --- a/quant.ew/files/system/streaming_sync/streaming_sync.lua +++ b/quant.ew/files/system/streaming_sync/streaming_sync.lua @@ -28,6 +28,6 @@ function rpc.remote_run_event(id) end end -np.CrossCallAdd("ew_run_streaming_event", rpc.remote_run_event) +util.add_cross_call("ew_run_streaming_event", rpc.remote_run_event) return module \ No newline at end of file diff --git a/quant.ew/files/system/wand_charm/wand_charm.lua b/quant.ew/files/system/wand_charm/wand_charm.lua index 3cc1e87a..09a1f067 100644 --- a/quant.ew/files/system/wand_charm/wand_charm.lua +++ b/quant.ew/files/system/wand_charm/wand_charm.lua @@ -8,7 +8,7 @@ function rpc.charm(gid) EntityAddComponent2(item, "LuaComponent", {script_source_file = "mods/quant.ew/files/system/wand_charm/charm.lua", remove_after_executed = true}) end end -np.CrossCallAdd("ew_charm_sync", function(id) +util.add_cross_call("ew_charm_sync", function(id) local gid = EntityGetFirstComponentIncludingDisabled(id, "VariableStorageComponent", "ew_global_item_id") rpc.charm(ComponentGetValue2(gid, "value_string")) end) diff --git a/quant.ew/files/system/world_sync_cuts/world_sync_cuts.lua b/quant.ew/files/system/world_sync_cuts/world_sync_cuts.lua index 836afd59..78ad6f74 100644 --- a/quant.ew/files/system/world_sync_cuts/world_sync_cuts.lua +++ b/quant.ew/files/system/world_sync_cuts/world_sync_cuts.lua @@ -1,4 +1,4 @@ -np.CrossCallAdd("ew_cut_through_world", function(x, min_y, max_y, radius, _radius2) +util.add_cross_call("ew_cut_through_world", function(x, min_y, max_y, radius, _radius2) if ctx.is_host then net.proxy_send("cut_through_world", x.." "..min_y.." "..max_y.." "..radius) end diff --git a/quant.ew/init.lua b/quant.ew/init.lua index 0c25bdcd..0c653dc9 100755 --- a/quant.ew/init.lua +++ b/quant.ew/init.lua @@ -36,11 +36,11 @@ ModLuaFileAppend("data/scripts/gun/gun_actions.lua", "mods/quant.ew/files/resour ModMagicNumbersFileAdd("mods/quant.ew/files/magic.xml") -np.CrossCallAdd("ew_per_peer_seed", function() +util.add_cross_call("ew_per_peer_seed", function() return tonumber(string.sub(ctx.my_id, 8, 12), 16), tonumber(string.sub(ctx.my_id, 12), 16) end) -np.CrossCallAdd("ew_spectator", function() +util.add_cross_call("ew_spectator", function() if ctx.spectating_over_peer_id == nil then return ctx.my_player.entity or EntityGetWithTag("player_unit")[1] else