mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-10 22:25:25 +00:00
perf[generate]: generate gdscript protocol
This commit is contained in:
@@ -15,7 +15,10 @@ package com.zfoo.protocol.generate;
|
||||
import com.zfoo.protocol.collection.CollectionUtils;
|
||||
import com.zfoo.protocol.collection.tree.GeneralTree;
|
||||
import com.zfoo.protocol.collection.tree.TreeNode;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
import com.zfoo.protocol.serializer.CodeLanguage;
|
||||
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
|
||||
import com.zfoo.protocol.util.AssertionUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
@@ -39,6 +42,34 @@ public abstract class GenerateProtocolPath {
|
||||
protocolPathMap = null;
|
||||
}
|
||||
|
||||
public static String protocolAbsolutePath(short protocolId, CodeLanguage language) {
|
||||
var path = getProtocolPath(protocolId);
|
||||
var name = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId);
|
||||
if (StringUtils.isBlank(path)) {
|
||||
path = name;
|
||||
} else {
|
||||
path = StringUtils.format("{}/{}", path, name);
|
||||
}
|
||||
|
||||
switch (language) {
|
||||
case Cpp:
|
||||
case Go:
|
||||
case JavaScript:
|
||||
case TypeScript:
|
||||
case CSharp:
|
||||
case Protobuf:
|
||||
break;
|
||||
case Lua:
|
||||
break;
|
||||
case GdScript:
|
||||
break;
|
||||
case Enhance:
|
||||
default:
|
||||
throw new RunException("无法识别的枚举类型[{}]", language);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取协议生成的路径
|
||||
*/
|
||||
@@ -55,7 +86,7 @@ public abstract class GenerateProtocolPath {
|
||||
|
||||
public static String getRelativePath(short protocolId, short relativeProtocolId) {
|
||||
// 不是折叠协议的话,protocolPathMap一定是空,这里返回“”,上层会解析为同一个文件下
|
||||
if(CollectionUtils.isEmpty(protocolPathMap)){
|
||||
if (CollectionUtils.isEmpty(protocolPathMap)) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
var protocolPath = protocolPathMap.get(protocolId);
|
||||
|
||||
@@ -30,8 +30,10 @@ import com.zfoo.protocol.util.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.zfoo.protocol.util.FileUtils.LS;
|
||||
import static com.zfoo.protocol.util.StringUtils.TAB_ASCII;
|
||||
@@ -98,12 +100,8 @@ public abstract class GenerateGdUtils {
|
||||
for (var protocol : protocolList) {
|
||||
var protocolId = protocol.protocolId();
|
||||
var name = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
var path = GenerateProtocolPath.getProtocolPath(protocolId);
|
||||
if (StringUtils.isBlank(path)) {
|
||||
importBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}.gd\")", name, protocolOutputRootPath, name)).append(LS);
|
||||
} else {
|
||||
importBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}/{}.gd\")", name, protocolOutputRootPath, path, name)).append(LS);
|
||||
}
|
||||
var path = GenerateProtocolPath.protocolAbsolutePath(protocolId, CodeLanguage.GdScript);
|
||||
importBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}.gd\")", name, protocolOutputRootPath, path)).append(LS);
|
||||
initList.add(StringUtils.format("{}{}: {}", TAB_ASCII, protocolId, name));
|
||||
}
|
||||
var initProtocols = StringUtils.joinWith(StringUtils.COMMA + LS, initList.toArray());
|
||||
@@ -139,18 +137,13 @@ public abstract class GenerateGdUtils {
|
||||
if (CollectionUtils.isEmpty(subProtocols)) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
var cppBuilder = new StringBuilder();
|
||||
var gdBuilder = new StringBuilder();
|
||||
for (var subProtocolId : subProtocols) {
|
||||
var protocolClassName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId);
|
||||
var subProtocolPath = StringUtils.format("{}/{}/{}.gd"
|
||||
, protocolOutputRootPath
|
||||
, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId)
|
||||
, protocolClassName);
|
||||
subProtocolPath = subProtocolPath.replaceAll("//", StringUtils.SLASH);
|
||||
subProtocolPath = StringUtils.format("const {} = preload(\"res://{}\")", protocolClassName, subProtocolPath);
|
||||
cppBuilder.append(subProtocolPath).append(LS);
|
||||
var name = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId);
|
||||
var path = GenerateProtocolPath.protocolAbsolutePath(subProtocolId, CodeLanguage.GdScript);
|
||||
gdBuilder.append(StringUtils.format("const {} = preload(\"res://{}/{}.gd\")", name, protocolOutputRootPath, path)).append(LS);
|
||||
}
|
||||
return cppBuilder.toString();
|
||||
return gdBuilder.toString();
|
||||
}
|
||||
|
||||
private static String fieldDefinition(ProtocolRegistration registration) {
|
||||
|
||||
Reference in New Issue
Block a user