GenClientProto.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. -- require("luaext")
  2. local proto_path = "./proto/"
  3. local client_protocal_name = "protocal.proto"
  4. --获取文件名
  5. local function strippath(filename)
  6. --return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
  7. return string.match(filename, ".+\\([^\\]*%.%w+)$") -- *nix system
  8. end
  9. --去除扩展名
  10. local function stripextension(filename)
  11. local idx = filename:match(".+()%.%w+$")
  12. if(idx) then
  13. return filename:sub(1, idx-1)
  14. else
  15. return filename
  16. end
  17. end
  18. --获取扩展名
  19. local function getextension(filename)
  20. return filename:match(".+%.(%w+)$")
  21. end
  22. local function endswith(str, substr)
  23. if str == nil or substr == nil then
  24. return nil, "the string or the sub-string parameter is nil"
  25. end
  26. str_tmp = string.reverse(str)
  27. substr_tmp = string.reverse(substr)
  28. if string.find(str_tmp, substr_tmp) ~= 1 then
  29. return false
  30. else
  31. return true
  32. end
  33. end
  34. local function getfiles(path, suffix)
  35. -- body
  36. local t = {}
  37. for dir in io.popen(string.format([[dir "%s" /b]], path)):lines() do
  38. if endswith(dir, "." .. suffix) then
  39. table.insert(t, dir)
  40. end
  41. end
  42. return t
  43. end
  44. local function readFileString(path)
  45. local file = io.open(path, "r")
  46. if file == nil then
  47. return false
  48. end
  49. local content = {}
  50. for l in file:lines() do
  51. if string.find(l, '//') ~= nil then
  52. content[#content + 1] = l
  53. end
  54. end
  55. return content
  56. end
  57. local function writeFileContent(path, content)
  58. local file = io.open(path,"w")
  59. file:write(content)
  60. file:close()
  61. end
  62. local message_content
  63. local content = [[syntax = "proto3";
  64. package serverproto;
  65. ]]
  66. local import_content = "\n"
  67. local request_content = "message Request {%s}\n"
  68. local response_content = "message Response {%s}\n"
  69. local request_cs = "\n"
  70. local response_sc = "\n"
  71. local pbs = getfiles(proto_path, "proto")
  72. for _, v in pairs(pbs) do
  73. local k = string.sub(v, 1, -7)
  74. if k == "messagedefclient" then
  75. message_content = readFileString(proto_path..v)
  76. elseif k ~= "protocal" then
  77. import_content = import_content .. string.format([[import "%s.proto";]],k).."\n"
  78. end
  79. end
  80. import_content = import_content .."\n"
  81. for _,v in pairs(message_content) do
  82. local idx = string.find(v, '=')
  83. local idx1 = string.find(v, ';')
  84. local pb_name = string.sub(v, 1, idx - 1)
  85. local pb_id = string.sub(v, idx + 1, idx1 - 1)
  86. local idx2 = string.find(v, '%[')
  87. local idx3 = string.find(v, '%]')
  88. local pb_message = string.sub(v, idx2 + 1, idx3 - 1)
  89. local idx4 = string.find(v, "//")
  90. local pb_desc = string.sub(v, idx4, -1)
  91. if string.find(pb_name, "CS_") ~= nil then
  92. request_cs = request_cs .." ".. pb_message .. " "..pb_name.." ".." = "..pb_id..";".." "..pb_desc.."\n"
  93. elseif string.find(pb_name, "SC_") ~= nil then
  94. response_sc = response_sc .." ".. pb_message .. " "..pb_name.." ".." = "..pb_id..";".." "..pb_desc.."\n"
  95. end
  96. end
  97. request_content = string.format(request_content, request_cs)
  98. response_content = string.format(response_content, response_sc)
  99. content = content .. import_content .. request_content .. response_content
  100. writeFileContent(proto_path..client_protocal_name, content)
  101. local message = ""
  102. local confirm_message = ""
  103. for k, v in pairs(message_content) do
  104. v = string.gsub(v, ';', ',')
  105. v = string.gsub(v, '//', '--')
  106. message = message ..v.."\n"
  107. if string.find(v, 'confirm]') ~= nil then
  108. confirm_message = confirm_message ..v.."\n"
  109. end
  110. end
  111. local lua_content = [[
  112. local ProtoMsgId = {
  113. %s
  114. }
  115. return ProtoMsgId
  116. ]]
  117. lua_content = string.format(lua_content, message)
  118. writeFileContent(proto_path.."ProtoMsgId.lua", lua_content)
  119. local lua_confirm_content = [[
  120. local ProtoConfirmMsgId = {
  121. %s
  122. }
  123. return ProtoConfirmMsgId
  124. ]]
  125. lua_confirm_content = string.format(lua_confirm_content, confirm_message)
  126. writeFileContent(proto_path.."ProtoConfirmMsgId.lua", lua_confirm_content)