Blame view

src/services/csvdatad.lua 2.83 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
7
8
9
10
  local sharedata = require "skynet.sharedata"
  local skynet = require "skynet"
  local lfs = require "lfs"
  local redisproxy = require "shared.redisproxy"
  require "shared.init"
  require "utils.init"
  require "csvdata.init"
  require "skynet.manager"
  require "RedisKeys"
  
8dce6908   zhouahaihai   配表由两个位置读取
11
  -- local csvdb = {}
314bc5df   zhengshouren   提交服务器初始代码
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  
  local function formatFileName(filename)
  	filename = string.trim(filename)
  	local basename = filename:match("([^/]+)%.lua$")
  	if not basename then return end
  	local loadname = filename:match("^src/([^.]+)%.lua$")
  	loadname = loadname:gsub('/', '.')
  	return basename, loadname
  end
  
  local function travCsv(rootPath, pathes)
  	pathes = pathes or {}
  	local modified = false
  	local ok, files, iter = pcall(lfs.dir, rootPath)
  	if not ok then return modified end
  	for entry in files, iter do
  		-- 过滤 . 开始的字符串包括 . .. .git .开头的文件名
  		if string.byte(entry, 1) ~= 46 then
  			local pathfile = rootPath .. '/' .. entry
  			local attrs = lfs.attributes(pathfile)
  			if attrs.mode == 'directory' then
  				modified = travCsv(pathfile, pathes) or modified
  			else
  				local basename, loadname = formatFileName(pathfile)
8dce6908   zhouahaihai   配表由两个位置读取
36
37
38
39
40
41
  				if basename then
  					if tonum(pathes[loadname]) < attrs.modification then
  						modified = true
  						pathes[loadname] = attrs.modification
  					end
  					if basename == "init" or basename == "init_adv" then
314bc5df   zhengshouren   提交服务器初始代码
42
  						require(loadname)
314bc5df   zhengshouren   提交服务器初始代码
43
  					end
314bc5df   zhengshouren   提交服务器初始代码
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  				end
  			end
  		end
  	end
  	return modified
  end
  
  -- 每分钟检查是否有更改
  local file2timeMap = {}
  local function handle_timeout()
  	if travCsv("src/csvdata", file2timeMap) then
  		sharedata.update("csvdata", csvdb)
  	end
  	skynet.timeout(100*5, handle_timeout)
  end
  
3fe4471e   zhouhaihai   热更新 demo
60
61
62
63
  -- 重新加载csvdb
  
  
  local CMD = {}
2d392ede   zhouhaihai   热更新 最终版
64
  
5e5d7680   zhouhaihai   热更新 优化
65
  function CMD.reload(code)
5e5d7680   zhouhaihai   热更新 优化
66
67
68
69
70
71
72
73
74
  	csvdb = csvdb or {}
  	
  	local ok, func = pcall(load, code)
  	if ok then
  		ok = pcall(func)
  	end
  	if not ok then
  		skynet.error("reload_csvdata error by code " .. code)
  		return 'error'
3fe4471e   zhouhaihai   热更新 demo
75
  	end
3fe4471e   zhouhaihai   热更新 demo
76
77
  
  	sharedata.update("csvdata", csvdb)
3fe4471e   zhouhaihai   热更新 demo
78
79
80
  	return 'ok'
  end
  
2d392ede   zhouhaihai   热更新 最终版
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  --更新已经存在的
  function CMD.hotfix(fieldss)
  	csvdb = csvdb or {}
  	for _, fields in ipairs(fieldss) do
  		if #fields >= 3 and (type(fields[#fields]) == 'number' or type(fields[#fields]) == 'string') then
  			local temp = csvdb
  			local ok = false
  			for i = 1, #fields - 1 do
  				if type(temp) ~= 'table' then break end
  				if type(fields[i]) ~= 'number' and type(fields[i]) ~= 'string' then break end
  				if i == #fields - 1 then
  					if type(temp[fields[i]]) ~= 'number' and type(temp[fields[i]]) ~= 'string' then break end
  					temp[fields[i]] = fields[#fields]
  				else
  					temp = temp[fields[i]]
  				end
  			end
  		end
  	end
  	sharedata.update("csvdata", csvdb)
  	return 'ok'
  end
  
314bc5df   zhengshouren   提交服务器初始代码
104
105
106
107
  skynet.start(function ()
  	travCsv("src/csvdata", file2timeMap)
  	sharedata.new("csvdata", csvdb)
  	-- handle_timeout()
3fe4471e   zhouhaihai   热更新 demo
108
109
110
111
112
113
114
  
  	skynet.dispatch("lua", function(_, _, command, ...)
  		local f = CMD[command]
  		skynet.ret(skynet.pack(f(...)))
  	end)
  
  	skynet.register "CSVDATA"
314bc5df   zhengshouren   提交服务器初始代码
115
  end)