Msg.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. --[[
  2. 协议模板规则如下
  3. 1.在module文件夹下面建立的所有文件夹都是一个一个的模块,里面至少要有一个proto.lua和handler.lua文件
  4. 2.handler.lua里面填写该模块的cg_xxx函数
  5. 3.proto.lua里面填写该模块的cg和gc协议
  6. 4.对于proto.lua里面的不是以cg和gc开头的table,是作为子结构,其中,如果没有local开头的,那么子结构的名字将作为客户端对应的类的名字,如果有local开头或者是直接在table里面展开的子结构,客户端对应的类的名字将根据一层一层的字段自动生成
  7. 5.协议的每个字段{名字,容量,类型}
  8. 名字:只能用string,不能用number,并且不能以下划线开头(避免生成的跟客户端的私有变量冲突)
  9. 容量:为预分配的最大空间,当容量大于1的时候,该字段就必需包含长度,例如字段名为aa,aa就是一个table,那么长度就是aa[0],aa[0]必需小于等于容量
  10. 类型:
  11. {"aa", 1, "int"} --32位整型
  12. {"aa", 1, "double"} --64位浮点数
  13. {"aa", 1, "string", 128} --一个字符串,里面最多有128个字节,字符串长度上限不填的话,默认是64
  14. {"aa", 1, sub} --aa是一个table,里面的字段由子结构的模板sub决定
  15. {"aa", 2, "int"} --数组,里面最多有2个整型
  16. {"aa", 2, "double"} --数组,里面最多有2个64位浮点数
  17. {"aa", 2, "string", 128} --字符串数组,每个字符串里面最多有128个字节,字符串长度上限不填的话,默认是64
  18. {"aa", 2, sub} --aa是一个table,aa[1]也是一个table,aa[1]里面的字段由子结构的模板sub决定
  19. --]]
  20. local msg_ex = _G.msg_ex
  21. cg = cg or {}
  22. gc = gc or {}
  23. proto_handler = proto_handler or {}
  24. proto_id = proto_id or {}
  25. proto_name = proto_name or {}
  26. proto_template = proto_template or {}
  27. list = list or {} -- 消息发送对象列表 第0为长度 后面1-x为obj
  28. local Config = require("Config")
  29. local mt = {
  30. __index =
  31. function(t, k)
  32. local id = t[1] or ""
  33. assert(nil, id .. " " .. k .. " not exist")
  34. end
  35. }
  36. local function proto_container_metatable(msg)
  37. if not Config.IS_WINDOWS then
  38. return
  39. end
  40. setmetatable(msg, mt)
  41. for _, v in pairs(msg) do
  42. if type(v) == "table" then
  43. proto_container_metatable(v)
  44. end
  45. end
  46. end
  47. local function proto_container_resize(msg, template)
  48. for _, v in ipairs(template) do
  49. if v[2] < 2 then
  50. if type(v[3]) == "table" then
  51. cnt = cnt + 1
  52. msg[v[1]] = {}
  53. proto_container_resize(msg[v[1]], v[3])
  54. end
  55. else
  56. if type(v[3]) == "table" then
  57. local v2 = v[2]
  58. if v2 == 2 and (v[1] == "items" or v[1] == "equips" or v[1] == "gems") then
  59. v2 = 1
  60. end
  61. cnt = cnt + 1
  62. msg[v[1]] = {}
  63. for i = 1, v2 do
  64. msg[v[1]][i] = {}
  65. proto_container_resize(msg[v[1]][i], v[3])
  66. end
  67. else
  68. cnt = cnt + 1
  69. msg[v[1]] = {}
  70. end
  71. end
  72. end
  73. end
  74. function register(name, id, template, handler)
  75. if not proto_name[id] then
  76. msg_ex.register_template(id, template)
  77. if string.sub(name, 1, 2) == "CG" then
  78. cg[id] = {[1] = id}
  79. proto_container_resize(cg[id], template)
  80. proto_container_metatable(cg[id])
  81. elseif string.sub(name, 1, 2) == "GC" then
  82. gc[name] = {[1] = id}
  83. cnt = 0
  84. proto_container_resize(gc[name], template)
  85. if 8000 < cnt then
  86. end
  87. end
  88. end
  89. proto_handler[id] = handler
  90. proto_name[id] = name
  91. proto_template[id] = template
  92. end
  93. if msg_ex then
  94. send = msg_ex.unicast
  95. sendMulti = msg_ex.multicast
  96. sendWorld = msg_ex.broadcast
  97. end
  98. local out = {}
  99. local out_len = 0
  100. local function add_space(step)
  101. for i = 1, step do
  102. out_len = out_len + 1
  103. out[out_len] = " "
  104. end
  105. end
  106. local set = {}
  107. function trace(m, template, step, print_on_err)
  108. if not step then
  109. template = proto_template[m[1]]
  110. step = 0
  111. out_len = 0
  112. end
  113. for k in pairs(set) do
  114. set[k] = nil
  115. end
  116. for _, v in ipairs(template) do
  117. set[v[1]] = true
  118. end
  119. for k in pairs(m) do
  120. if k ~= 1 and not set[k] then
  121. print(table.concat(out, nil, 1, out_len))
  122. assert(nil, k .. " not need")
  123. end
  124. end
  125. for _, v in ipairs(template) do
  126. add_space(step)
  127. out_len = out_len + 1
  128. out[out_len] = v[1]
  129. out_len = out_len + 1
  130. out[out_len] = "="
  131. if v[2] < 2 then
  132. if type(v[3]) == "table" then
  133. if type(m[v[1]]) ~= "table" then
  134. print(table.concat(out, nil, 1, out_len))
  135. assert(nil, v[1] .. " is not table")
  136. end
  137. out_len = out_len + 1
  138. out[out_len] = "\n"
  139. trace(m[v[1]], v[3], step + 1, print_on_err)
  140. else
  141. if (v[3] == "byte" or v[3] == "short" or v[3] == "int" or v[3] == "double" or v[3] == "uint") and type(m[v[1]]) ~= "number" then
  142. print(table.concat(out, nil, 1, out_len))
  143. assert(nil, v[1] .. " is not number")
  144. end
  145. if v[3] == "string" and type(m[v[1]]) ~= "string" then
  146. print(table.concat(out, nil, 1, out_len))
  147. assert(nil, v[1] .. " is not string")
  148. end
  149. out_len = out_len + 1
  150. out[out_len] = m[v[1]]
  151. out_len = out_len + 1
  152. out[out_len] = "\n"
  153. end
  154. else
  155. if type(m[v[1]]) ~= "table" then
  156. print(table.concat(out, nil, 1, out_len))
  157. assert(nil, v[1] .. " is not table")
  158. end
  159. out_len = out_len + 1
  160. out[out_len] = "\n"
  161. if type(m[v[1]][0]) ~= "number" then
  162. print(table.concat(out, nil, 1, out_len))
  163. assert(nil, v[1] .. " len is not number")
  164. end
  165. add_space(step + 1)
  166. out_len = out_len + 1
  167. out[out_len] = "0="
  168. out_len = out_len + 1
  169. out[out_len] = m[v[1]][0]
  170. out_len = out_len + 1
  171. out[out_len] = "\n"
  172. for i = 1, m[v[1]][0] do
  173. if type(v[3]) == "table" then
  174. if type(m[v[1]][i]) ~= "table" then
  175. print(table.concat(out, nil, 1, out_len))
  176. assert(nil, v[1] .. " " .. i .. " is not table")
  177. end
  178. add_space(step + 1)
  179. out_len = out_len + 1
  180. out[out_len] = i
  181. out_len = out_len + 1
  182. out[out_len] = "="
  183. out_len = out_len + 1
  184. out[out_len] = "\n"
  185. trace(m[v[1]][i], v[3], step + 2, print_on_err)
  186. else
  187. if (v[3] == "byte" or v[3] == "short" or v[3] == "int" or v[3] == "double" or v[3] == "uint") and type(m[v[1]][i]) ~= "number" then
  188. print(table.concat(out, nil, 1, out_len))
  189. assert(nil, v[1] .. " " .. i .. " is not number")
  190. end
  191. if v[3] == "string" and type(m[v[1]][i]) ~= "string" then
  192. print(table.concat(out, nil, 1, out_len))
  193. assert(nil, v[1] .. " " .. i .. " is not string")
  194. end
  195. add_space(step + 1)
  196. out_len = out_len + 1
  197. out[out_len] = i
  198. out_len = out_len + 1
  199. out[out_len] = "="
  200. out_len = out_len + 1
  201. out[out_len] = m[v[1]][i]
  202. out_len = out_len + 1
  203. out[out_len] = "\n"
  204. end
  205. end
  206. end
  207. end
  208. if step == 0 and not print_on_err then
  209. print(table.concat(out, nil, 1, out_len))
  210. end
  211. end