mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-12 16:15:42 +00:00
ref[es]: refactor es protocol
This commit is contained in:
1 parent
52b2bfbd82
commit
6651154869
6 files changed
+80
-94
No files matched your search
+41
-29
@@ -83,11 +83,12 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolManagerTemplate.mjs");
|
||||
var protocol_imports = new StringBuilder();
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
protocol_imports.append("import Protocols from './Protocols.mjs';").append(LS);
|
||||
protocol_imports.append("import * as Protocols from './Protocols.mjs';").append(LS);
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, Protocols.{});", protocol_id, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new Protocols.{}Registration());", protocol_id, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set(Protocols.{}, {});", protocol_name, protocol_id)).append(LS);
|
||||
}
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
|
||||
@@ -103,8 +104,8 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
// protocol
|
||||
protocol_class.append(formatProtocolClassTemplate(registration)).append(LS);
|
||||
protocol_registration.append(StringUtils.format("Protocols.{} = {}", protocol_name, protocol_name)).append(LS);
|
||||
protocol_class.append(protocol_class(registration).replace("class ", "export class ")).append(LS);
|
||||
protocol_registration.append(protocol_registration(registration)).append(LS);
|
||||
}
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolsTemplate.mjs");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
@@ -129,8 +130,11 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
for (var protocol : registrations) {
|
||||
var protocol_id = protocol.protocolId();
|
||||
var protocol_name = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
|
||||
protocol_imports.append(StringUtils.format("import {} from './{}/{}.mjs';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol.protocolId()), protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, {});", protocol_id, protocol_name)).append(LS);
|
||||
protocol_imports.append(StringUtils.format("import { {}Registration } from './{}/{}.mjs';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol.protocolId()), protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new {}Registration());", protocol_id, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set({}, {});", protocol_name, protocol_id)).append(LS);
|
||||
}
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
|
||||
@@ -143,7 +147,12 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var formatProtocolTemplate = formatProtocolTemplate(registration);
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_name, protocol_name
|
||||
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
|
||||
));
|
||||
var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
@@ -155,16 +164,17 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
public void defaultProtocol(List<ProtocolRegistration> registrations) throws IOException {
|
||||
createTemplateFile();
|
||||
|
||||
|
||||
// 生成ProtocolManager.mjs文件
|
||||
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolManagerTemplate.mjs");
|
||||
var protocol_imports = new StringBuilder();
|
||||
var protocol_manager_registrations = new StringBuilder();
|
||||
for (var protocol : registrations) {
|
||||
var protocol_id = protocol.protocolId();
|
||||
var protocol_name = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
protocol_imports.append(StringUtils.format("import {} from './{}.mjs';", protocol_name, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, {});", protocol_id, protocol_name)).append(LS);
|
||||
protocol_imports.append(StringUtils.format("import { {}Registration } from './{}.mjs';", protocol_name, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new {}Registration());", protocol_id, protocol_name)).append(LS);
|
||||
protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set({}, {});", protocol_name, protocol_id)).append(LS);
|
||||
}
|
||||
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
|
||||
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
|
||||
@@ -177,8 +187,13 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
for (var registration : registrations) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var formatProtocolTemplate = formatProtocolTemplate(registration);
|
||||
var outputPath = StringUtils.format("{}/{}.mjs", protocolOutputPath, protocol_name, protocol_name);
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs");
|
||||
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
|
||||
CodeTemplatePlaceholder.protocol_name, protocol_name
|
||||
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
|
||||
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
|
||||
));
|
||||
var outputPath = StringUtils.format("{}/{}.mjs", protocolOutputPath, protocol_name);
|
||||
var file = new File(outputPath);
|
||||
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
|
||||
logger.info("Generated EcmaScript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
|
||||
@@ -195,22 +210,7 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
}
|
||||
}
|
||||
|
||||
public String formatProtocolTemplate(ProtocolRegistration registration) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs");
|
||||
var placeholderMap = Map.of(
|
||||
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.EcmaScript)
|
||||
, CodeTemplatePlaceholder.protocol_name, protocol_name
|
||||
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
|
||||
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
|
||||
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
|
||||
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
|
||||
);
|
||||
return CodeTemplatePlaceholder.formatTemplate(protocolTemplate, placeholderMap);
|
||||
}
|
||||
|
||||
public String formatProtocolClassTemplate(ProtocolRegistration registration) {
|
||||
public String protocol_class(ProtocolRegistration registration) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolClassTemplate.mjs");
|
||||
@@ -219,6 +219,18 @@ public class CodeGenerateEcmaScript implements ICodeGenerate {
|
||||
, CodeTemplatePlaceholder.protocol_name, protocol_name
|
||||
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
|
||||
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
|
||||
);
|
||||
return CodeTemplatePlaceholder.formatTemplate(protocolTemplate, placeholderMap);
|
||||
}
|
||||
|
||||
public String protocol_registration(ProtocolRegistration registration) {
|
||||
var protocol_id = registration.protocolId();
|
||||
var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName();
|
||||
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolRegistrationTemplate.mjs");
|
||||
var placeholderMap = Map.of(
|
||||
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.EcmaScript)
|
||||
, CodeTemplatePlaceholder.protocol_name, protocol_name
|
||||
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
|
||||
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
|
||||
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
|
||||
);
|
||||
|
||||
@@ -1,32 +1,4 @@
|
||||
${protocol_note}
|
||||
class ${protocol_name} {
|
||||
${protocol_field_definition}
|
||||
|
||||
static PROTOCOL_ID = ${protocol_id};
|
||||
|
||||
protocolId() {
|
||||
return ${protocol_name}.PROTOCOL_ID;
|
||||
}
|
||||
|
||||
static write(buffer, packet) {
|
||||
if (packet === null) {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
${protocol_write_serialization}
|
||||
}
|
||||
|
||||
static read(buffer) {
|
||||
const length = buffer.readInt();
|
||||
if (length === 0) {
|
||||
return null;
|
||||
}
|
||||
const beforeReadIndex = buffer.getReadOffset();
|
||||
const packet = new ${protocol_name}();
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(beforeReadIndex + length);
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,19 @@
|
||||
${protocol_imports}
|
||||
|
||||
const protocols = new Map();
|
||||
const protocolIdMap = new Map();
|
||||
|
||||
// initProtocol
|
||||
${protocol_manager_registrations}
|
||||
|
||||
class ProtocolManager {
|
||||
static getProtocolId(clazz) {
|
||||
const protocolId = protocolIdMap.get(clazz);
|
||||
if (protocolId === null || protocolId === undefined) {
|
||||
throw '[protocol:' + clazz + '] not exist';
|
||||
}
|
||||
return protocolId;
|
||||
}
|
||||
static getProtocol(protocolId) {
|
||||
const protocol = protocols.get(protocolId);
|
||||
if (protocol === null) {
|
||||
@@ -15,7 +23,7 @@ class ProtocolManager {
|
||||
}
|
||||
|
||||
static write(buffer, packet) {
|
||||
const protocolId = packet.protocolId();
|
||||
const protocolId = ProtocolManager.getProtocolId(packet.constructor);
|
||||
buffer.writeShort(protocolId);
|
||||
const protocol = ProtocolManager.getProtocol(protocolId);
|
||||
protocol.write(buffer, packet);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
export class ${protocol_name}Registration {
|
||||
protocolId() {
|
||||
return ${protocol_id};
|
||||
}
|
||||
|
||||
write(buffer, packet) {
|
||||
if (packet === null) {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
${protocol_write_serialization}
|
||||
}
|
||||
|
||||
read(buffer) {
|
||||
const length = buffer.readInt();
|
||||
if (length === 0) {
|
||||
return null;
|
||||
}
|
||||
const beforeReadIndex = buffer.getReadOffset();
|
||||
const packet = new ${protocol_name}();
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(beforeReadIndex + length);
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,5 @@
|
||||
${protocol_note}
|
||||
class ${protocol_name} {
|
||||
${protocol_field_definition}
|
||||
${protocol_class}
|
||||
|
||||
static PROTOCOL_ID = ${protocol_id};
|
||||
${protocol_registration}
|
||||
|
||||
protocolId() {
|
||||
return ${protocol_name}.PROTOCOL_ID;
|
||||
}
|
||||
|
||||
static write(buffer, packet) {
|
||||
if (packet === null) {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
${protocol_write_serialization}
|
||||
}
|
||||
|
||||
static read(buffer) {
|
||||
const length = buffer.readInt();
|
||||
if (length === 0) {
|
||||
return null;
|
||||
}
|
||||
const beforeReadIndex = buffer.getReadOffset();
|
||||
const packet = new ${protocol_name}();
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(beforeReadIndex + length);
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
export default ${protocol_name};
|
||||
@@ -1,8 +1,3 @@
|
||||
class Protocols {
|
||||
}
|
||||
|
||||
${protocol_class}
|
||||
|
||||
${protocol_registration}
|
||||
|
||||
export default Protocols;
|
||||
${protocol_registration}
|
||||
Reference in new issue
Block a user