Skip to content

Commit

Permalink
优化sharedata_service 热更支持加载新的文件
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Jan 10, 2025
1 parent 0b7ff71 commit 528b091
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions service/sharedata_service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local ENUM = {

local g_watch_server = nil
local g_file_changetime_map = {}
local g_load_dir_map = {}
local g_modes = {}

g_modes[ENUM.sharedata] = {
Expand All @@ -45,24 +46,28 @@ g_modes[ENUM.sharetable] = {

local CMD = {}

local function load_new_file(file_path, file_info, cur_time, mode)
local m = g_modes[mode]
m.load(file_path)
g_file_changetime_map[file_path] = {
version = 1,
mode = mode,
last_change_time = file_info.modification,
}
g_watch_server:register(file_path, g_file_changetime_map[file_path].version .. '-' .. cur_time)
log.info("sharedata load loadfile:", file_path)
end

function CMD.load(dir_list, mode)
assert(g_modes[mode], "not exists mode:" .. tostring(mode))
local m = g_modes[mode]
local cur_time = time_util.time()
for _,dir in pairs(dir_list) do
g_load_dir_map[dir] = mode
for file_name,file_path,file_info in file_util.diripairs(dir) do
file_path = file_util.convert_windows_to_linux_relative(file_path)
log.info("sharedata load loadfile:", file_path)

if string.find(file_name, '.lua', nil, true) then
m.load(file_path)

g_file_changetime_map[file_path] = {
version = 1,
mode = mode,
last_change_time = file_info.modification,
}

g_watch_server:register(file_path, g_file_changetime_map[file_path].version .. '-' .. cur_time)
load_new_file(file_path, file_info, cur_time, mode)
end
end
end
Expand All @@ -71,23 +76,35 @@ end
function CMD.check_reload()
local reload_list = {}
local cur_time = time_util.time()
for file_path, info in pairs(g_file_changetime_map) do
local file_info, errinfo, errno = lfs.attributes(file_path)
if not file_info then
log.warn("check_reload file can`t get info:", file_path, errinfo, errno)
else
if file_info.modification ~= info.last_change_time then
local m = g_modes[info.mode]
log.info("sharedata check_reload reloadfile:", file_path)
m.reload(file_path)
info.last_change_time = file_info.modification
info.version = info.version + 1
table.insert(reload_list, file_path)

g_watch_server:publish(file_path, info.version .. '-' .. cur_time)
for dir, mode in pairs(g_load_dir_map) do
for file_name, file_path, file_info, errmsg, errno in file_util.diripairs(dir) do
file_path = file_util.convert_windows_to_linux_relative(file_path)
if not file_info then
log.warn("check_reload file can`t get info:", file_path, errmsg, errno)
else
if g_file_changetime_map[file_path] then
local info = g_file_changetime_map[file_path]
if file_info.modification ~= info.last_change_time then
local m = g_modes[info.mode]
log.info("sharedata check_reload reloadfile:", file_path)
m.reload(file_path)
info.last_change_time = file_info.modification
info.version = info.version + 1
table.insert(reload_list, file_path)

g_watch_server:publish(file_path, info.version .. '-' .. cur_time)
end
else
if string.find(file_name, '.lua', nil, true) then
load_new_file(file_path, file_info, cur_time, mode)
table.insert(reload_list, file_path)
end
end
end
end
end

return json.encode(reload_list)
end

Expand Down

0 comments on commit 528b091

Please sign in to comment.