bayuMIR/client/dev/GUILayout/ssrgame/net/Message.lua

105 lines
3 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--- ssrMessage模块用于处理网络消息的发送和接收
local ssrMessage = {}
local dispatch_handler = {}
---* 注册公共模块
for i, v in ipairs(SL:GetFilesByPath("GUILayout/public") or {}) do
local game_file = string.format("GUILayout/public/%s", v)
local file_name = SL:Split(game_file, ".")[1]
SL:Require(file_name, true)
end
---* 注册所有game文件
for i, v in ipairs(SL:GetFilesByPath("GUILayout/game") or {}) do
local game_file = string.format("GUILayout/game/%s", v)
for _, flie in ipairs(SL:GetFilesByPath(game_file) or {}) do
local file_name = SL:Split(flie, ".")[1]
dispatch_handler[file_name] = SL:Require(game_file .. "/" .. file_name, true)
end
end
---* 客户端延迟加载 解决GUIUtil加载时官方节点无法获取问题的功能加载
SL:ScheduleOnce(function()
for i, v in ipairs(SL:GetFilesByPath("GUILayout/delay") or {}) do
local game_file = string.format("GUILayout/delay/%s", v)
local file_name = SL:Split(game_file, ".")[1]
local obj_name = SL:Split(v, ".")[1]
dispatch_handler[obj_name] = SL:Require(file_name, true)
local target = dispatch_handler[obj_name]
if target and target["main"] then
target["main"](target)
end
end
-- ssrPrint("延迟加载完成")
end, 0.5)
---* 统一发送网络消息处理
function ssrMessage:SubLink(Objcfg_info, ...)
local data = { ... }
local sync = { sync_obj = Objcfg_info, sync_cfg = #data > 0 and data or nil }
SL:SendLuaNetMsg(ssrNetMsgCfg.npc_sync, 0, 0, 0, SL:JsonEncode(sync))
end
--- 发送网络消息
---@param msgID number 消息ID
---@param arg1 any 参数1
---@param arg2 any 参数2
---@param arg3 any 参数3
---@param msgData table 消息数据
function ssrMessage:sendmsg(msgID, arg1, arg2, arg3, msgData)
if msgData then msgData = SL:JsonEncode(msgData) end
SL:SendLuaNetMsg(msgID, arg1, arg2, arg3, msgData)
end
---* 接收NPC网络消息
---@param msgID number 消息ID
---@param arg1 any 参数1
---@param arg2 any 参数2
---@param arg3 any 参数3
---@param jsonstr table 消息数据
function Npc_dispatchex(msgID, arg1, arg2, arg3, jsonstr)
local msgData = jsonstr and SL:JsonDecode(jsonstr) or nil
if not msgData or not msgData.sync_obj then
return
end
local msgName = msgData.sync_obj
local module, method = msgName:match "([^.]*)_(.*)"
local target = dispatch_handler[module]
if not target or not target[method] then return end
target[method](target, arg1, arg2, arg3, msgData.sync_cfg)
end
SL:RegisterLuaNetMsg(ssrNetMsgCfg.npc_sync, Npc_dispatchex)
---* 同步数据表
local function synData(msgID, arg1, arg2, arg3, jsonstr)
local msgData = jsonstr and SL:JsonDecode(jsonstr) or nil
if not msgData then
return
end
local msgName = msgData.name
local target = dispatch_handler[msgName]
if not target or not target.cfg then return end
target.cfg = msgData.cfg
end
SL:RegisterLuaNetMsg(ssrNetMsgCfg.sync, synData)
return ssrMessage