Skip to content

Commit

Permalink
Merge pull request owent#4 from Kill-Console/master
Browse files Browse the repository at this point in the history
蓝图接口获取Helper代码生成示例
  • Loading branch information
owent authored Feb 3, 2021
2 parents 48ad27d + dc11f4c commit 6607f92
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions tools/GenUETableFuncCode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

import os
import sys
import json

# 代码模板,包括头文件、CPP、声明、定义等
BlueprintFunctionLibraryName = 'TableBPFuncLib'
HeaderFileTemp = '''
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
/*INCLUDE_BEGIN*/
{IncludeCode}
/*INCLUDE_END*/
#include "{BpFuncLibName}.generated.h"
/**
*
*/
UCLASS()
class HXNEXT_API U{BpFuncLibName} : public UBlueprintFunctionLibrary
{{
GENERATED_BODY()
public:
/*DECLARATION_BEGIN*/
{DeclarationCode}
/*DECLARATION_END*/
}};
'''

CppFileTemp = '''
#include "{BpFuncLibName}.h"
/*DIFINITION_BEGIN*/
{DifinitionCode}
/*DIFINITION_END*/
'''

HeaderTemp = '#include "TableData/{StructName}.h"'
DeclarationTemp = '''
UFUNCTION(BlueprintCallable, Category = "XResConfig")
static {HelperName}* Get{TableName}Table();
'''
DifinitionTemp = '''
{HelperName}* UTableBPFuncLib::Get{TableName}Table()
{{
UClass* clazz = {HelperName}::StaticClass();
if (nullptr == clazz)
{{
return nullptr;
}}
return clazz->GetDefaultObject<{HelperName}>();
}}
'''

# 各种路径
UnreaImportSettingsPath = 'output/UnreaImportSettings.json'
OutputPath = 'output/TableHelperCode/'
HeaderFileName = '{BpFuncLibName}.h'
CppFileName = '{BpFuncLibName}.cpp'

# 从UnreaImportSettings.json解析出生成的UE结构名
def ParseStructFromJson(jsonPath):
with open(jsonPath, 'r') as f:
data = json.load(f)

structLst = []
if 'ImportGroups' in data:
cfgLst = data['ImportGroups']
for cfg in cfgLst:
if 'ImportSettings' in cfg:
settings = cfg['ImportSettings']
if 'ImportRowStruct' in settings:
structLst.append(settings['ImportRowStruct'])

return structLst

# 生成对应头文件和CPP文件
def GenerateCode(structLst):
includeCode = ''
declarationCode = ''
difinitionCode = ''

for structName in structLst:
helperName = f'U{structName}Helper'
tableName = structName[len('Hx2proto'):]

if len(includeCode) != 0:
includeCode += '\n'
includeCode += HeaderTemp.format(StructName = structName)

if len(declarationCode) != 0:
declarationCode += '\n'
declarationCode += DeclarationTemp.format(HelperName = helperName, TableName = tableName)

if len(difinitionCode) != 0:
difinitionCode += '\n'
difinitionCode += DifinitionTemp.format(HelperName = helperName, TableName = tableName)

bpFuncLibName = BlueprintFunctionLibraryName
headerCode = HeaderFileTemp.format(BpFuncLibName = bpFuncLibName, IncludeCode = includeCode, DeclarationCode = declarationCode)
cppCode = CppFileTemp.format(BpFuncLibName = bpFuncLibName, DifinitionCode = difinitionCode)

with open(OutputPath + HeaderFileName.format(BpFuncLibName = bpFuncLibName), 'w') as headerFile:
headerFile.write(headerCode)

with open(OutputPath + CppFileName.format(BpFuncLibName = bpFuncLibName), 'w') as cppFile:
cppFile.write(cppCode)

if __name__=='__main__':
if not os.path.exists(UnreaImportSettingsPath):
print("{} not exists".format(UnreaImportSettingsPath))
else:
structLst = ParseStructFromJson(UnreaImportSettingsPath)
GenerateCode(structLst)


0 comments on commit 6607f92

Please sign in to comment.