ro-webgl/ProtocolGen/GenClientProto.lua
2021-12-21 09:40:39 +08:00

151 lines
4.2 KiB
Lua

-- require("luaext")
local proto_path = "./proto/"
local client_protocal_name = "protocal.proto"
--获取文件名
local function strippath(filename)
--return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
return string.match(filename, ".+\\([^\\]*%.%w+)$") -- *nix system
end
--去除扩展名
local function stripextension(filename)
local idx = filename:match(".+()%.%w+$")
if(idx) then
return filename:sub(1, idx-1)
else
return filename
end
end
--获取扩展名
local function getextension(filename)
return filename:match(".+%.(%w+)$")
end
local function endswith(str, substr)
if str == nil or substr == nil then
return nil, "the string or the sub-string parameter is nil"
end
str_tmp = string.reverse(str)
substr_tmp = string.reverse(substr)
if string.find(str_tmp, substr_tmp) ~= 1 then
return false
else
return true
end
end
local function getfiles(path, suffix)
-- body
local t = {}
for dir in io.popen(string.format([[dir "%s" /b]], path)):lines() do
if endswith(dir, "." .. suffix) then
table.insert(t, dir)
end
end
return t
end
local function readFileString(path)
local file = io.open(path, "r")
if file == nil then
return false
end
local content = {}
for l in file:lines() do
if string.find(l, '//') ~= nil then
content[#content + 1] = l
end
end
return content
end
local function writeFileContent(path, content)
local file = io.open(path,"w")
file:write(content)
file:close()
end
local message_content
local content = [[syntax = "proto3";
package serverproto;
]]
local import_content = "\n"
local request_content = "message Request {%s}\n"
local response_content = "message Response {%s}\n"
local request_cs = "\n"
local response_sc = "\n"
local pbs = getfiles(proto_path, "proto")
for _, v in pairs(pbs) do
local k = string.sub(v, 1, -7)
if k == "messagedefclient" then
message_content = readFileString(proto_path..v)
elseif k ~= "protocal" then
import_content = import_content .. string.format([[import "%s.proto";]],k).."\n"
end
end
import_content = import_content .."\n"
for _,v in pairs(message_content) do
local idx = string.find(v, '=')
local idx1 = string.find(v, ';')
local pb_name = string.sub(v, 1, idx - 1)
local pb_id = string.sub(v, idx + 1, idx1 - 1)
local idx2 = string.find(v, '%[')
local idx3 = string.find(v, '%]')
local pb_message = string.sub(v, idx2 + 1, idx3 - 1)
local idx4 = string.find(v, "//")
local pb_desc = string.sub(v, idx4, -1)
if string.find(pb_name, "CS_") ~= nil then
request_cs = request_cs .." ".. pb_message .. " "..pb_name.." ".." = "..pb_id..";".." "..pb_desc.."\n"
elseif string.find(pb_name, "SC_") ~= nil then
response_sc = response_sc .." ".. pb_message .. " "..pb_name.." ".." = "..pb_id..";".." "..pb_desc.."\n"
end
end
request_content = string.format(request_content, request_cs)
response_content = string.format(response_content, response_sc)
content = content .. import_content .. request_content .. response_content
writeFileContent(proto_path..client_protocal_name, content)
local message = ""
local confirm_message = ""
for k, v in pairs(message_content) do
v = string.gsub(v, ';', ',')
v = string.gsub(v, '//', '--')
message = message ..v.."\n"
if string.find(v, 'confirm]') ~= nil then
confirm_message = confirm_message ..v.."\n"
end
end
local lua_content = [[
local ProtoMsgId = {
%s
}
return ProtoMsgId
]]
lua_content = string.format(lua_content, message)
writeFileContent(proto_path.."ProtoMsgId.lua", lua_content)
local lua_confirm_content = [[
local ProtoConfirmMsgId = {
%s
}
return ProtoConfirmMsgId
]]
lua_confirm_content = string.format(lua_confirm_content, confirm_message)
writeFileContent(proto_path.."ProtoConfirmMsgId.lua", lua_confirm_content)