Skip to content

Commit

Permalink
perf[protocol]: all protocol files are generated in a single protocol…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
jaysunxiao committed Nov 30, 2023
1 parent 1ca4db6 commit 2635369
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,14 @@ public static void generate(GenerateOperation generateOperation) throws IOExcept
// 生成Lua协议
if (generateLanguages.contains(CodeLanguage.Lua)) {
GenerateLuaUtils.init(generateOperation);
GenerateLuaUtils.createProtocolManager(allSortedGenerateProtocols);
for (var protocolRegistration : allSortedGenerateProtocols) {
GenerateLuaUtils.createLuaProtocolFile((ProtocolRegistration) protocolRegistration);
if (generateOperation.isOneProtocol()) {
GenerateLuaUtils.createProtocolManagerInOneFile(allSortedGenerateProtocols);
GenerateLuaUtils.createLuaProtocolsInOneFile(allSortedGenerateProtocols);
} else {
GenerateLuaUtils.createProtocolManager(allSortedGenerateProtocols);
for (var protocolRegistration : allSortedGenerateProtocols) {
GenerateLuaUtils.createLuaProtocolFile((ProtocolRegistration) protocolRegistration);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.generate.GenerateProtocolNote;
import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
Expand Down Expand Up @@ -83,6 +82,72 @@ public static void clear() {
protocolOutputPath = null;
}

// All protocol files are generated in a single protocol file.
public static void createProtocolManagerInOneFile(List<IProtocolRegistration> protocolList) throws IOException {
var list = List.of("lua/ByteBuffer.lua", "lua/Long.lua");
for (var fileName : list) {
var fileInputStream = ClassUtils.getFileFromClassPath(fileName);
var createFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, StringUtils.substringAfterFirst(fileName, "lua/")));
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
}

// 生成Protocol.lua文件
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolManagerTemplate.lua");
var importProtocols = StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath);
var importRegistrations = StringUtils.format("local Registrations = require(\"{}.Registrations\")", protocolOutputRootPath);
var protocolBuilder = new StringBuilder();
for (var protocol : protocolList) {
var protocolId = protocol.protocolId();
var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
protocolBuilder.append(TAB).append(StringUtils.format("protocols[{}] = Protocols.{}", protocolId, protocolName)).append(LS);
}
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, StringUtils.EMPTY_JSON, StringUtils.EMPTY_JSON, importProtocols, importRegistrations, protocolBuilder.toString().trim());
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.lua")), protocolManagerTemplate, true);
}

public static void createLuaProtocolsInOneFile(List<IProtocolRegistration> registrations) {
var protocolTemplate1 = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolTemplate1.lua");
var protocolTemplate2 = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolTemplate2.lua");
var builder1 = new StringBuilder();
var builder2 = new StringBuilder();

builder2.append(StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath)).append(LS + LS);

for (var protocolRegistration : registrations) {
var registration = (ProtocolRegistration) protocolRegistration;
var protocolId = registration.protocolId();
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
GenerateProtocolFile.index.set(0);

var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Lua);
var valueOfMethod = valueOfMethod(registration);
var toStringJsonTemplate = toStringJsonTemplate(registration);
var toStringParams = toStringParams(registration);
var writePacket = writePacket(registration);
var readPacket = readPacket(registration);

var body1 = StringUtils.format(protocolTemplate1, classNote, protocolClazzName, StringUtils.EMPTY_JSON, protocolClazzName, valueOfMethod.trim());
var body2 = StringUtils.format(protocolTemplate2, protocolClazzName, protocolId, protocolClazzName, protocolClazzName
, protocolClazzName, toStringJsonTemplate, toStringParams
, protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim());
builder1.append(body1).append(LS);
builder2.append(body2).append(LS);
}

builder1.append(LS).append("local Protocols = {}").append(LS);
for (var protocolRegistration : registrations) {
var registration = (ProtocolRegistration) protocolRegistration;
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
builder1.append(StringUtils.format("Protocols.{} = {}", protocolClazzName, protocolClazzName)).append(LS);
}
builder1.append("return Protocols");

FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "Protocols.lua")), builder1.toString(), true);
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "Registrations.lua")), builder2.toString(), true);
}

public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
var list = List.of("lua/ByteBuffer.lua", "lua/Long.lua");
for (var fileName : list) {
Expand All @@ -103,7 +168,7 @@ public static void createProtocolManager(List<IProtocolRegistration> protocolLis
fieldBuilder.append(TAB).append(StringUtils.format("local {} = require(\"{}.{}\")", protocolName, protocolOutputRootPath, protocolName)).append(LS);
} else {
fieldBuilder.append(TAB).append(StringUtils.format("local {} = require(\"{}.{}.{}\")"
, protocolName,protocolOutputRootPath, path.replaceAll(StringUtils.SLASH, StringUtils.PERIOD), protocolName)).append(LS);
, protocolName, protocolOutputRootPath, path.replaceAll(StringUtils.SLASH, StringUtils.PERIOD), protocolName)).append(LS);
}

protocolBuilder.append(TAB).append(StringUtils.format("protocols[{}] = {}", protocolId, protocolName)).append(LS);
Expand All @@ -112,7 +177,7 @@ public static void createProtocolManager(List<IProtocolRegistration> protocolLis
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.lua")), protocolManagerTemplate, true);
}

public static void createLuaProtocolFile(ProtocolRegistration registration) throws IOException {
public static void createLuaProtocolFile(ProtocolRegistration registration) {
// 初始化index
GenerateProtocolFile.index.set(0);

Expand Down
42 changes: 42 additions & 0 deletions protocol/src/main/resources/lua-one/ProtocolManagerTemplate.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local protocols = {}

local ProtocolManager = {}

-- table扩展方法,map的大小
function table.mapSize(map)
local size = 0
for _,_ in pairs(map) do
size = size + 1
end
return size
end

function ProtocolManager.getProtocol(protocolId)
local protocol = protocols[protocolId]
if protocol == nil then
error("[protocolId:" + protocolId + "]协议不存在")
end
return protocol
end

function ProtocolManager.write(buffer, packet)
local protocolId = packet:protocolId()
-- 写入协议号
buffer:writeShort(protocolId)
-- 写入包体
ProtocolManager.getProtocol(protocolId):write(buffer, packet)
end

function ProtocolManager.read(buffer)
local protocolId = buffer:readShort()
return ProtocolManager.getProtocol(protocolId):read(buffer)
end

function initProtocol()
{}
{}
{}
end

ProtocolManager.initProtocol = initProtocol
return ProtocolManager
10 changes: 10 additions & 0 deletions protocol/src/main/resources/lua-one/ProtocolTemplate1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{}
local {} = {}
function {}:new()
local obj = {
{}
}
setmetatable(obj, self)
self.__index = self
return obj
end
35 changes: 35 additions & 0 deletions protocol/src/main/resources/lua-one/ProtocolTemplate2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Protocols.{}:protocolId()
return {}
end

function Protocols.{}:protocolName()
return {}
end

function Protocols.{}:__tostring()
local jsonTemplate = "{}"
local result = string.format(jsonTemplate, {})
return result
end

function Protocols.{}:write(buffer, packet)
if packet == nil then
buffer:writeInt(0)
return
end
{}
end

function Protocols.{}:read(buffer)
local length = buffer:readInt()
if length == 0 then
return nil
end
local beforeReadIndex = buffer:getReadOffset()
local packet = {}:new()
{}
if length > 0 then
buffer:setReadOffset(beforeReadIndex + length)
end
return packet
end
4 changes: 2 additions & 2 deletions protocol/src/main/resources/lua/ProtocolManagerTemplate.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
protocols = {}
local protocols = {}

ProtocolManager = {}
local ProtocolManager = {}

-- table扩展方法,map的大小
function table.mapSize(map)
Expand Down

0 comments on commit 2635369

Please sign in to comment.