Commit 2d392edea40830da215800daa51b8e5aebde2e50
1 parent
a5b025b0
热更新 最终版
Showing
5 changed files
with
139 additions
and
17 deletions
Show diff stats
src/actions/GmAction.lua
... | ... | @@ -16,7 +16,7 @@ function _M.clientRequest(agent, data) |
16 | 16 | end |
17 | 17 | |
18 | 18 | function _M.testhotfix(role, pms) |
19 | - return csvdb["itemCsv"][1]["name"] | |
19 | + return csvdb["itemCsv"][1]["name"] .. " -=- " .. csvdb["itemCsv"][2]["name"] .. " -=- " .. role:getItemCount(123) .. " -=- " .. table.pack(next(role.heros))[2]:getBattleValue() | |
20 | 20 | end |
21 | 21 | |
22 | 22 | function _M.hero(role, pms) | ... | ... |
src/actions/HttpAction.lua
... | ... | @@ -5,6 +5,22 @@ local codecache = require "skynet.codecache" -- 清空缓存用 |
5 | 5 | |
6 | 6 | local _M = {} |
7 | 7 | |
8 | + | |
9 | +--[=[使用python 调用的话eg: | |
10 | + | |
11 | + import requests | |
12 | + | |
13 | + @params 指处理方法中的 query | |
14 | + @data 指处理方法中的 body | |
15 | + @return 'success' 成功 | |
16 | + | |
17 | + status = requests.post("http://127.0.0.1:8001/clearcache", params = {"key": "zhaolu1234dangge"}, data = "", timeout = 300).text | |
18 | + | |
19 | + if status != 'success' : | |
20 | + print("错误") | |
21 | +]=] | |
22 | + | |
23 | + | |
8 | 24 | -- 清空缓存 |
9 | 25 | function _M.clearcache(query, body) |
10 | 26 | skynet.error(string.format("clearcache time: %s", skynet.timex())) |
... | ... | @@ -12,7 +28,7 @@ function _M.clearcache(query, body) |
12 | 28 | return 'success' |
13 | 29 | end |
14 | 30 | |
15 | ---重新加载 需要修改的csvdb | |
31 | +--重新加载 需要修改的csvdb -- 单字段修改 优先使用hotfix_csvdata | |
16 | 32 | --[=[ eg: |
17 | 33 | body = """ |
18 | 34 | csvdb["itemCsv"][1]["name"] = "测试一下" |
... | ... | @@ -21,20 +37,49 @@ end |
21 | 37 | |
22 | 38 | function _M.reload_csvdata(query, body) |
23 | 39 | if not body or body == "" then |
24 | - return | |
40 | + return 'no body' | |
25 | 41 | end |
26 | 42 | |
27 | 43 | local ok = pcall(load, body) |
28 | - if not ok then return end | |
29 | - | |
30 | - skynet.error(string.format("reload_csvdata time: %s, code: %s", skynet.timex(), body)) | |
44 | + if not ok then | |
45 | + return "code error" | |
46 | + end | |
31 | 47 | |
32 | - local status = skynet.call('CSVDATA', "lua", "reload", body) | |
48 | + local ok, status = pcall(skynet.call, 'CSVDATA', "lua", "reload", body) | |
33 | 49 | if status == "ok" then |
50 | + skynet.error(string.format("reload_csvdata time: %s, code: %s", skynet.timex(), body)) | |
34 | 51 | return 'success' |
52 | + else | |
53 | + return 'error update' | |
35 | 54 | end |
36 | 55 | end |
37 | 56 | |
57 | +--指定更新某个字段值 | |
58 | +--[=[ | |
59 | + 解码后 | |
60 | + body = { | |
61 | + {"itemCsv", 1, "name", "测试一下"}, | |
62 | + {"itemCsv", 2, "name", "测试一下"} | |
63 | + } | |
64 | +]=] | |
65 | +function _M.hotfix_csvdata(query, body) | |
66 | + if not body or body == "" then | |
67 | + return 'no body' | |
68 | + end | |
69 | + | |
70 | + local ok, result = pcall(json.decode, body) | |
71 | + if not ok or type(result) ~= 'table' then | |
72 | + return "decode error" | |
73 | + end | |
74 | + local ok, status = pcall(skynet.call, 'CSVDATA', "lua", "hotfix", result) | |
75 | + | |
76 | + if status == "ok" then | |
77 | + skynet.error(string.format("hotfix_csvdata time: %s, code: %s", skynet.timex(), body)) | |
78 | + return 'success' | |
79 | + else | |
80 | + return 'error update' | |
81 | + end | |
82 | +end | |
38 | 83 | |
39 | 84 | -- 热更新代码 -- 针对 agent 执行发送过来的代码 -- 代码要规范~ |
40 | 85 | |
... | ... | @@ -59,21 +104,53 @@ end |
59 | 104 | HEHE = 123 |
60 | 105 | """ |
61 | 106 | |
62 | -3. 修改 role 方法(待定) | |
63 | - | |
107 | +3. 修改 class 类 方法 | |
108 | + 1. 修改role的方法 | |
109 | + body = """ | |
110 | + local role = ... | |
111 | + _hotfixClass = _hotfixClass or {} | |
112 | + _hotfixClass["Role"] = _hotfixClass["Role"] or {} | |
113 | + local hotfixFunc = function(hotfixclass) | |
114 | + function hotfixclass:getItemCount(itemId) | |
115 | + return self:getProperty("id") .. "__" .. self:getProperty("uid") .. "__" .. itemId | |
116 | + end | |
117 | + end | |
118 | + table.insert(_hotfixClass["Role"], hotfixFunc) | |
119 | + if role then | |
120 | + hotfixFunc(role) | |
121 | + end | |
122 | + """ | |
123 | + 2. 修改hero的方法 | |
64 | 124 | body = """ |
65 | - | |
125 | + local role = ... | |
126 | + _hotfixClass = _hotfixClass or {} | |
127 | + _hotfixClass["Hero"] = _hotfixClass["Hero"] or {} | |
128 | + local hotfixFunc = function(hotfixclass) | |
129 | + function hotfixclass:getBattleValue() | |
130 | + return self:getProperty("id") + "98700000000" | |
131 | + end | |
132 | + end | |
133 | + table.insert(_hotfixClass["Hero"], hotfixFunc) | |
134 | + if role and role.heros then | |
135 | + for _, hero in pairs(role.heros) do | |
136 | + hotfixFunc(hero) | |
137 | + end | |
138 | + end | |
66 | 139 | """ |
140 | + | |
141 | + 3. 其他的类仿照上面的写法 | |
67 | 142 | ]=] |
68 | 143 | |
69 | -function _M.hotfix(query, body) | |
144 | +function _M.hotfix_code(query, body) | |
70 | 145 | if not body or body == "" then |
71 | - return | |
146 | + return "no body" | |
72 | 147 | end |
73 | 148 | local ok = pcall(load, body) |
74 | - if not ok then return end | |
149 | + if not ok then | |
150 | + return "code error" | |
151 | + end | |
75 | 152 | |
76 | - skynet.error(string.format("hotfix time: %s, code: %s", skynet.timex(), body)) | |
153 | + skynet.error(string.format("hotfix_code time: %s, code: %s", skynet.timex(), body)) | |
77 | 154 | |
78 | 155 | pcall(skynet.call, 'WATCHDOG', "lua", "hotfix", body) |
79 | 156 | return 'success' | ... | ... |
src/agent.lua
... | ... | @@ -24,6 +24,7 @@ local agentInfo = {} -- { client_fd, role, gate_serv, open_timer} |
24 | 24 | local agent_util, cs |
25 | 25 | |
26 | 26 | _hotfixActions = _hotfixActions or {} |
27 | +_hotfixClass = _hotfixClass or {} | |
27 | 28 | |
28 | 29 | --- {{{ 定时器相关 |
29 | 30 | local function handle_timeout() |
... | ... | @@ -286,10 +287,10 @@ end |
286 | 287 | function CMD.hotfix(code) |
287 | 288 | local ok, func = pcall(load, code) |
288 | 289 | if ok then |
289 | - ok = pcall(func) | |
290 | + ok = pcall(func, agentInfo.role) | |
290 | 291 | end |
291 | 292 | if not ok then |
292 | - skynet.error("hotfix error by code " .. code) | |
293 | + skynet.error("hotfix_code error by code " .. code) | |
293 | 294 | end |
294 | 295 | end |
295 | 296 | ... | ... |
src/services/csvdatad.lua
... | ... | @@ -61,8 +61,8 @@ end |
61 | 61 | |
62 | 62 | |
63 | 63 | local CMD = {} |
64 | + | |
64 | 65 | function CMD.reload(code) |
65 | - -- 重新加载 csvdata | |
66 | 66 | csvdb = csvdb or {} |
67 | 67 | |
68 | 68 | local ok, func = pcall(load, code) |
... | ... | @@ -78,6 +78,29 @@ function CMD.reload(code) |
78 | 78 | return 'ok' |
79 | 79 | end |
80 | 80 | |
81 | +--更新已经存在的 | |
82 | +function CMD.hotfix(fieldss) | |
83 | + csvdb = csvdb or {} | |
84 | + for _, fields in ipairs(fieldss) do | |
85 | + if #fields >= 3 and (type(fields[#fields]) == 'number' or type(fields[#fields]) == 'string') then | |
86 | + local temp = csvdb | |
87 | + local ok = false | |
88 | + for i = 1, #fields - 1 do | |
89 | + if type(temp) ~= 'table' then break end | |
90 | + if type(fields[i]) ~= 'number' and type(fields[i]) ~= 'string' then break end | |
91 | + if i == #fields - 1 then | |
92 | + if type(temp[fields[i]]) ~= 'number' and type(temp[fields[i]]) ~= 'string' then break end | |
93 | + temp[fields[i]] = fields[#fields] | |
94 | + else | |
95 | + temp = temp[fields[i]] | |
96 | + end | |
97 | + end | |
98 | + end | |
99 | + end | |
100 | + sharedata.update("csvdata", csvdb) | |
101 | + return 'ok' | |
102 | +end | |
103 | + | |
81 | 104 | skynet.start(function () |
82 | 105 | travCsv("src/csvdata", file2timeMap) |
83 | 106 | sharedata.new("csvdata", csvdb) | ... | ... |
src/shared/functions.lua
... | ... | @@ -215,6 +215,17 @@ function class(classname, super) |
215 | 215 | -- copy fields from class to native object |
216 | 216 | for k,v in pairs(cls) do instance[k] = v end |
217 | 217 | instance.class = cls |
218 | + | |
219 | + -- 覆盖热更新的方法 | |
220 | + pcall(function() | |
221 | + if _hotfixClass and _hotfixClass[cls.__cname] then | |
222 | + for _, func in ipairs(_hotfixClass[cls.__cname]) do | |
223 | + func(instance) -- 绑定新的方法 | |
224 | + end | |
225 | + end | |
226 | + end) | |
227 | + | |
228 | + | |
218 | 229 | instance:ctor(...) |
219 | 230 | return instance |
220 | 231 | end |
... | ... | @@ -235,6 +246,16 @@ function class(classname, super) |
235 | 246 | function cls.new(...) |
236 | 247 | local instance = setmetatable({}, cls) |
237 | 248 | instance.class = cls |
249 | + | |
250 | + -- 覆盖热更新的方法 | |
251 | + pcall(function() | |
252 | + if _hotfixClass and _hotfixClass[cls.__cname] then | |
253 | + for _, func in ipairs(_hotfixClass[cls.__cname]) do | |
254 | + func(instance) -- 绑定新的方法 | |
255 | + end | |
256 | + end | |
257 | + end) | |
258 | + | |
238 | 259 | instance:ctor(...) |
239 | 260 | return instance |
240 | 261 | end | ... | ... |