mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-19 08:15:34 +00:00
perf[protocol]: all protocol files are generated in a single protocol file
This commit is contained in:
1 parent
c00cab0e53
commit
bba3f5e69d
8 files changed
+76
-64
No files matched your search
@@ -71,7 +71,8 @@ public class ProtocolAnalysis {
|
|||||||
, "ByteBufUtils", "ArrayUtils", "CollectionUtils"
|
, "ByteBufUtils", "ArrayUtils", "CollectionUtils"
|
||||||
, "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "String", "Character", "Object"
|
, "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "String", "Character", "Object"
|
||||||
, "Collections", "Iterator", "List", "ArrayList", "Map", "HashMap", "Set", "HashSet"
|
, "Collections", "Iterator", "List", "ArrayList", "Map", "HashMap", "Set", "HashSet"
|
||||||
, "DecodedPacketInfo", "EncodedPacketInfo");
|
, "DecodedPacketInfo", "EncodedPacketInfo"
|
||||||
|
, "Protocols", "ProtocolBase", "ProtocolWriter", "ProtocolReader");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* EN: Temp field, unsupported type
|
* EN: Temp field, unsupported type
|
||||||
@@ -133,24 +134,24 @@ public class ProtocolAnalysis {
|
|||||||
LinkedHashSet<Class> tempProtocolClassSet = new LinkedHashSet<>(protocolClassList);
|
LinkedHashSet<Class> tempProtocolClassSet = new LinkedHashSet<>(protocolClassList);
|
||||||
//去重
|
//去重
|
||||||
protocolClassList = new ArrayList<>();
|
protocolClassList = new ArrayList<>();
|
||||||
for(Class clazz : tempProtocolClassSet){
|
for (Class clazz : tempProtocolClassSet) {
|
||||||
protocolClassList.add(clazz);
|
protocolClassList.add(clazz);
|
||||||
}
|
}
|
||||||
Set<Class> relevantClassList = new LinkedHashSet<>(protocolClassList);
|
Set<Class> relevantClassList = new LinkedHashSet<>(protocolClassList);
|
||||||
for (var clazz : protocolClassList) {
|
for (var clazz : protocolClassList) {
|
||||||
Set<Class<?>> classSet = ClassUtils.relevantClass(clazz);
|
Set<Class<?>> classSet = ClassUtils.relevantClass(clazz);
|
||||||
for(Class cls :classSet){
|
for (Class cls : classSet) {
|
||||||
if( !relevantClassList.contains(cls)){
|
if (!relevantClassList.contains(cls)) {
|
||||||
int protocolId = getProtocolIdAndCheckClass(cls);
|
int protocolId = getProtocolIdAndCheckClass(cls);
|
||||||
if(protocolId >= 0){
|
if (protocolId >= 0) {
|
||||||
relevantClassList.add(cls);
|
relevantClassList.add(cls);
|
||||||
}else{
|
} else {
|
||||||
throw new AssertException("[class:{}]类型必须声明", cls.getCanonicalName());
|
throw new AssertException("[class:{}]类型必须声明", cls.getCanonicalName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//var relevantClassList = relevantClassSet;
|
//var relevantClassList = relevantClassSet;
|
||||||
/* var relevantClassList = relevantClassSet.stream()
|
/* var relevantClassList = relevantClassSet.stream()
|
||||||
.sorted((a, b) -> a.getCanonicalName().compareTo(b.getCanonicalName()))
|
.sorted((a, b) -> a.getCanonicalName().compareTo(b.getCanonicalName()))
|
||||||
.toList();*/
|
.toList();*/
|
||||||
|
|||||||
@@ -93,25 +93,35 @@ public abstract class GenerateLuaUtils {
|
|||||||
|
|
||||||
// 生成Protocol.lua文件
|
// 生成Protocol.lua文件
|
||||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolManagerTemplate.lua");
|
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolManagerTemplate.lua");
|
||||||
var importProtocols = StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath);
|
var builderImports = new StringBuilder();
|
||||||
var importRegistrations = StringUtils.format("local Registrations = require(\"{}.Registrations\")", protocolOutputRootPath);
|
builderImports.append(TAB).append(StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath)).append(LS);
|
||||||
|
builderImports.append(TAB).append(StringUtils.format("local ProtocolBase = require(\"{}.ProtocolBase\")", protocolOutputRootPath)).append(LS);
|
||||||
|
builderImports.append(TAB).append(StringUtils.format("local ProtocolWriter = require(\"{}.ProtocolWriter\")", protocolOutputRootPath)).append(LS);
|
||||||
|
builderImports.append(TAB).append(StringUtils.format("local ProtocolReader = require(\"{}.ProtocolReader\")", protocolOutputRootPath)).append(LS);
|
||||||
var protocolBuilder = new StringBuilder();
|
var protocolBuilder = new StringBuilder();
|
||||||
for (var protocol : protocolList) {
|
for (var protocol : protocolList) {
|
||||||
var protocolId = protocol.protocolId();
|
var protocolId = protocol.protocolId();
|
||||||
var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
|
var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||||
protocolBuilder.append(TAB).append(StringUtils.format("protocols[{}] = Protocols.{}", protocolId, protocolName)).append(LS);
|
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());
|
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, StringUtils.EMPTY_JSON, StringUtils.EMPTY_JSON, builderImports.toString().trim(), protocolBuilder.toString().trim());
|
||||||
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.lua")), protocolManagerTemplate, true);
|
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.lua")), protocolManagerTemplate, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createLuaProtocolsInOneFile(List<IProtocolRegistration> registrations) {
|
public static void createLuaProtocolsInOneFile(List<IProtocolRegistration> registrations) {
|
||||||
var protocolTemplate1 = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolTemplate1.lua");
|
var protocolTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolTemplate.lua");
|
||||||
var protocolTemplate2 = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolTemplate2.lua");
|
var protocolBaseTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolBaseTemplate.lua");
|
||||||
var builder1 = new StringBuilder();
|
var protocolWriterTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolWriterTemplate.lua");
|
||||||
var builder2 = new StringBuilder();
|
var protocolReaderTemplate = ClassUtils.getFileFromClassPathToString("lua-one/ProtocolReaderTemplate.lua");
|
||||||
|
var builderProtocol = new StringBuilder();
|
||||||
|
var builderProtocolBase = new StringBuilder();
|
||||||
|
var builderProtocolWriter = new StringBuilder();
|
||||||
|
var builderProtocolReader = new StringBuilder();
|
||||||
|
|
||||||
builder2.append(StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath)).append(LS + LS);
|
var requireProtocols = StringUtils.format("local Protocols = require(\"{}.Protocols\")", protocolOutputRootPath);
|
||||||
|
builderProtocolBase.append(requireProtocols).append(LS).append(LS);
|
||||||
|
builderProtocolWriter.append(requireProtocols).append(LS).append(LS);
|
||||||
|
builderProtocolReader.append(requireProtocols).append(LS).append(LS);
|
||||||
|
|
||||||
for (var protocolRegistration : registrations) {
|
for (var protocolRegistration : registrations) {
|
||||||
var registration = (ProtocolRegistration) protocolRegistration;
|
var registration = (ProtocolRegistration) protocolRegistration;
|
||||||
@@ -127,25 +137,29 @@ public abstract class GenerateLuaUtils {
|
|||||||
var writePacket = writePacket(registration);
|
var writePacket = writePacket(registration);
|
||||||
var readPacket = readPacket(registration);
|
var readPacket = readPacket(registration);
|
||||||
|
|
||||||
var body1 = StringUtils.format(protocolTemplate1, classNote, protocolClazzName, StringUtils.EMPTY_JSON, protocolClazzName, valueOfMethod.trim());
|
var protocol = StringUtils.format(protocolTemplate, classNote, protocolClazzName, StringUtils.EMPTY_JSON, protocolClazzName, valueOfMethod.trim());
|
||||||
var body2 = StringUtils.format(protocolTemplate2, protocolClazzName, protocolId, protocolClazzName, protocolClazzName
|
var protocolBase = StringUtils.format(protocolBaseTemplate, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName, toStringJsonTemplate, toStringParams);
|
||||||
, protocolClazzName, toStringJsonTemplate, toStringParams
|
var protocolWriter = StringUtils.format(protocolWriterTemplate, protocolClazzName, writePacket.trim(), protocolClazzName);
|
||||||
, protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim());
|
var protocolReader = StringUtils.format(protocolReaderTemplate, protocolClazzName, readPacket.trim());
|
||||||
builder1.append(body1).append(LS);
|
|
||||||
builder2.append(body2).append(LS);
|
builderProtocol.append(protocol).append(LS);
|
||||||
|
builderProtocolBase.append(protocolBase).append(LS).append(LS);
|
||||||
|
builderProtocolWriter.append(protocolWriter).append(LS).append(LS);
|
||||||
|
builderProtocolReader.append(protocolReader).append(LS).append(LS);
|
||||||
}
|
}
|
||||||
|
|
||||||
builder1.append(LS).append("local Protocols = {}").append(LS);
|
builderProtocol.append(LS).append("local Protocols = {}").append(LS);
|
||||||
for (var protocolRegistration : registrations) {
|
for (var protocolRegistration : registrations) {
|
||||||
var registration = (ProtocolRegistration) protocolRegistration;
|
var registration = (ProtocolRegistration) protocolRegistration;
|
||||||
var registrationConstructor = registration.getConstructor();
|
var registrationConstructor = registration.getConstructor();
|
||||||
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
|
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
|
||||||
builder1.append(StringUtils.format("Protocols.{} = {}", protocolClazzName, protocolClazzName)).append(LS);
|
builderProtocol.append(StringUtils.format("Protocols.{} = {}", protocolClazzName, protocolClazzName)).append(LS);
|
||||||
}
|
}
|
||||||
builder1.append("return Protocols");
|
builderProtocol.append("return Protocols");
|
||||||
|
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "Protocols.lua")), builderProtocol.toString(), true);
|
||||||
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "Protocols.lua")), builder1.toString(), true);
|
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolBase.lua")), builderProtocolBase.toString(), true);
|
||||||
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "Registrations.lua")), builder2.toString(), true);
|
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolWriter.lua")), builderProtocolWriter.toString(), true);
|
||||||
|
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolReader.lua")), builderProtocolReader.toString(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
|
public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
function Protocols.{}:protocolId()
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
function Protocols.{}:protocolName()
|
||||||
|
return Protocols.{}
|
||||||
|
end
|
||||||
|
|
||||||
|
function Protocols.{}:__tostring()
|
||||||
|
local jsonTemplate = "{}"
|
||||||
|
local result = string.format(jsonTemplate, {})
|
||||||
|
return result
|
||||||
|
end
|
||||||
@@ -35,7 +35,6 @@ end
|
|||||||
function initProtocol()
|
function initProtocol()
|
||||||
{}
|
{}
|
||||||
{}
|
{}
|
||||||
{}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ProtocolManager.initProtocol = initProtocol
|
ProtocolManager.initProtocol = initProtocol
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
function Protocols.{}:write(buffer, packet)
|
||||||
|
if packet == nil then
|
||||||
|
buffer:writeInt(0)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
{}
|
||||||
|
end
|
||||||
File renamed without changes.
@@ -1,35 +0,0 @@
|
|||||||
function Protocols.{}:protocolId()
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
|
|
||||||
function Protocols.{}:protocolName()
|
|
||||||
return Protocols.{}
|
|
||||||
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 = Protocols.{}:new()
|
|
||||||
{}
|
|
||||||
if length > 0 then
|
|
||||||
buffer:setReadOffset(beforeReadIndex + length)
|
|
||||||
end
|
|
||||||
return packet
|
|
||||||
end
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
function Protocols.{}:read(buffer)
|
||||||
|
local length = buffer:readInt()
|
||||||
|
if length == 0 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
local beforeReadIndex = buffer:getReadOffset()
|
||||||
|
local packet = Protocols.{}:new()
|
||||||
|
{}
|
||||||
|
if length > 0 then
|
||||||
|
buffer:setReadOffset(beforeReadIndex + length)
|
||||||
|
end
|
||||||
|
return packet
|
||||||
|
end
|
||||||
Reference in new issue
Block a user