Refactor: added rpc machinery to reduce the amount of glue code

This commit is contained in:
IQuant 2024-05-20 15:03:16 +03:00
parent 26fa799c4a
commit a283827e1c
5 changed files with 51 additions and 12 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"Lua.runtime.version": "LuaJIT"
}

View file

@ -14,6 +14,7 @@ ctx.init = function()
ctx.item_prevent_localize = {}
ctx.events = {}
ctx.is_inventory_open = false
ctx.rpc_peer_id = nil
end
function ctx.dofile_and_add_hooks(path)

View file

@ -16,6 +16,44 @@ end
local string_split = util.string_split
net._rpc_inner = {
rpcs = {},
opts = {},
}
net.rpc = {}
local rpc_inner = net._rpc_inner
local rpc_base = {}
function rpc_base.opts_reliable()
rpc_inner.opts.reliable = true
end
local rpc_meta = {
__newindex = function (t, k, v)
table.insert(rpc_inner.rpcs, v)
local index = #rpc_inner.rpcs
local reliable = rpc_inner.opts.reliable == true
rawset(t, k, function(...)
net.send(index, {...}, reliable)
end)
net_handling.mod[index] = function(peer_id, args)
ctx.rpc_peer_id = peer_id
v(unpack(args))
ctx.rpc_peer_id = nil
end
rpc_inner.opts = {}
end,
__index = rpc_base,
}
function net.new_rcp_namespace()
local ret = {}
setmetatable(ret, rpc_meta)
return ret
end
function net.init()
local ready = false
local addr = os.getenv("NP_NOITA_ADDR") or "127.0.0.1:21251"
@ -136,10 +174,6 @@ function net.send_enemy_death_data(death_data)
net.send("edeath", death_data, true)
end
function net.send_world_data(world_data)
net.send("world", world_data)
end
function net.send_host_player_info(player_info)
net.send("host_player", player_info)
end

View file

@ -2,7 +2,6 @@ local player_fns = dofile_once("mods/quant.ew/files/src/player_fns.lua")
local ctx = dofile_once("mods/quant.ew/files/src/ctx.lua")
local util = dofile_once("mods/quant.ew/files/src/util.lua")
local enemy_sync = dofile_once("mods/quant.ew/files/src/enemy_sync.lua")
local world_sync = dofile_once("mods/quant.ew/files/src/world_sync.lua")
local perk_fns = dofile_once("mods/quant.ew/files/src/perk_fns.lua")
local inventory_helper = dofile_once("mods/quant.ew/files/src/inventory_helper.lua")
local item_sync = dofile_once("mods/quant.ew/files/src/item_sync.lua")
@ -85,12 +84,6 @@ function net_handling.mod.edeath(peer_id, death_data)
end
end
function net_handling.mod.world(peer_id, world_data)
if peer_id == ctx.host_id then
world_sync.handle_world_data(world_data)
end
end
function net_handling.mod.host_player(peer_id, player_infos)
if peer_id ~= ctx.host_id then
return

View file

@ -4,6 +4,9 @@ local rect = require("noitapatcher.nsew.rect")
local ffi = require("ffi")
local ctx = dofile_once("mods/quant.ew/files/src/ctx.lua")
local net = dofile_once("mods/quant.ew/files/src/net.lua")
local rpc = net.new_rcp_namespace()
local rect_optimiser = rect.Optimiser_new()
local encoded_area = world.EncodedArea()
@ -31,7 +34,7 @@ local function send_pending()
end
if #will_send > 0 then
-- GamePrint(#pending_send_wd.." "..#will_send.." "..total_len)
ctx.lib.net.send_world_data(will_send)
rpc.send_world_data(will_send)
end
end
@ -116,5 +119,10 @@ function world_sync.handle_world_data(world_data)
end
end
function rpc.send_world_data(world_data)
if ctx.rpc_peer_id == ctx.host_id then
world_sync.handle_world_data(world_data)
end
end
return world_sync