diff --git a/src/Luban.Job.Cfg/Source/GenArgs.cs b/src/Luban.Job.Cfg/Source/GenArgs.cs index da34138..ec25e65 100644 --- a/src/Luban.Job.Cfg/Source/GenArgs.cs +++ b/src/Luban.Job.Cfg/Source/GenArgs.cs @@ -8,7 +8,7 @@ namespace Luban.Job.Cfg [Option('s', "service", Required = true, HelpText = "service")] public string Service { get; set; } - [Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")] + [Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_python2_json,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")] public string GenType { get; set; } [Option("input_data_dir", Required = true, HelpText = "input data dir")] diff --git a/src/Luban.Job.Cfg/Source/Generate/CodeRenderBase.cs b/src/Luban.Job.Cfg/Source/Generate/CodeRenderBase.cs index 220188c..2dead80 100644 --- a/src/Luban.Job.Cfg/Source/Generate/CodeRenderBase.cs +++ b/src/Luban.Job.Cfg/Source/Generate/CodeRenderBase.cs @@ -91,7 +91,7 @@ namespace Luban.Job.Cfg.Generate var file = outputFile; var md5 = CacheFileUtil.GenMd5AndAddCache(file, string.Join("\n", fileContent)); - ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 }); + ctx.GenScatteredFiles.Add(new FileInfo() { FilePath = file, MD5 = md5 }); })); } diff --git a/src/Luban.Job.Cfg/Source/Generate/Python2CodeJsonRender.cs b/src/Luban.Job.Cfg/Source/Generate/Python2CodeJsonRender.cs new file mode 100644 index 0000000..2cd07e0 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/Generate/Python2CodeJsonRender.cs @@ -0,0 +1,37 @@ +using Luban.Common.Protos; +using Luban.Job.Cfg.Defs; +using Luban.Job.Common.Generate; +using Luban.Job.Common.Tpl; +using Luban.Job.Common.Utils; +using System.Collections.Generic; + +namespace Luban.Job.Cfg.Generate +{ + [Render("code_python2_json")] + class Python2CodeJsonRender : TemplateCodeRenderBase + { + protected override string RenderTemplateDir => "python2_json"; + + public override void Render(GenContext ctx) + { + ctx.Render = this; + ctx.Lan = Common.ELanguage.PYTHON; + DefAssembly.LocalAssebmly.CurrentLanguage = ctx.Lan; + + var lines = new List(10000); + static void PreContent(List fileContent) + { + //fileContent.Add(PythonStringTemplates.ImportTython3Enum); + //fileContent.Add(PythonStringTemplates.PythonVectorTypes); + fileContent.Add(StringTemplateManager.Ins.GetTemplateString("config/python2_json/include")); + } + + GenerateCodeMonolithic(ctx, RenderFileUtil.GetFileOrDefault(ctx.GenArgs.OutputCodeMonolithicFile, "Types.py"), lines, PreContent, null); + + //string indexFileName = "__init__.py"; + //string indexFileContent = ""; + //var md5 = CacheFileUtil.GenMd5AndAddCache(indexFileName, indexFileContent); + //ctx.GenScatteredFiles.Add(new FileInfo() { FilePath = indexFileName, MD5 = md5 }); + } + } +} diff --git a/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs b/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs index 5cb342a..221b9c4 100644 --- a/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs @@ -6,11 +6,10 @@ using System.Collections.Generic; namespace Luban.Job.Cfg.Generate { - [Render("code_python_json")] [Render("code_python3_json")] class Python3CodeJsonRender : TemplateCodeRenderBase { - protected override string RenderTemplateDir => "python_json"; + protected override string RenderTemplateDir => "python3_json"; public override void Render(GenContext ctx) { @@ -23,7 +22,7 @@ namespace Luban.Job.Cfg.Generate { //fileContent.Add(PythonStringTemplates.ImportTython3Enum); //fileContent.Add(PythonStringTemplates.PythonVectorTypes); - fileContent.Add(StringTemplateManager.Ins.GetTemplateString("config/python_json/include")); + fileContent.Add(StringTemplateManager.Ins.GetTemplateString("config/python3_json/include")); } GenerateCodeMonolithic(ctx, RenderFileUtil.GetFileOrDefault(ctx.GenArgs.OutputCodeMonolithicFile, "Types.py"), lines, PreContent, null); diff --git a/src/Luban.Job.Common/Source/Utils/FileHeaderUtil.cs b/src/Luban.Job.Common/Source/Utils/FileHeaderUtil.cs index f99e5f3..25199fa 100644 --- a/src/Luban.Job.Common/Source/Utils/FileHeaderUtil.cs +++ b/src/Luban.Job.Common/Source/Utils/FileHeaderUtil.cs @@ -2,8 +2,7 @@ namespace Luban.Job.Common.Utils { public static class FileHeaderUtil { - const string AUTO_GENERATE_C_LIKE = @" -//------------------------------------------------------------------------------ + const string AUTO_GENERATE_C_LIKE = @"//------------------------------------------------------------------------------ // // This code was generated by a tool. // Changes to this file may cause incorrect behavior and will be lost if @@ -13,8 +12,7 @@ namespace Luban.Job.Common.Utils "; - const string AUTO_GENERATE_LUA = @" ---[[------------------------------------------------------------------------------ + const string AUTO_GENERATE_LUA = @"--[[------------------------------------------------------------------------------ -- -- This code was generated by a tool. -- Changes to this file may cause incorrect behavior and will be lost if @@ -23,7 +21,7 @@ namespace Luban.Job.Common.Utils --]]------------------------------------------------------------------------------ "; - const string AUTO_GENERATE_PYTHON = @" + const string AUTO_GENERATE_PYTHON = @"#-*- coding: utf-8 -*- ''' This code was generated by a tool. diff --git a/src/Luban.Server/Luban.Server.csproj b/src/Luban.Server/Luban.Server.csproj index fe486b9..878f682 100644 --- a/src/Luban.Server/Luban.Server.csproj +++ b/src/Luban.Server/Luban.Server.csproj @@ -214,16 +214,28 @@ PreserveNewest - + PreserveNewest - + PreserveNewest - + PreserveNewest - + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + PreserveNewest diff --git a/src/Luban.Server/Templates/config/python2_json/bean.tpl b/src/Luban.Server/Templates/config/python2_json/bean.tpl new file mode 100644 index 0000000..afc7dbc --- /dev/null +++ b/src/Luban.Server/Templates/config/python2_json/bean.tpl @@ -0,0 +1,40 @@ + +{{ + name = x.py_full_name + is_abstract_type = x.is_abstract_type + parent_def_type = x.parent_def_type + export_fields = x.export_fields + hierarchy_export_fields = x.hierarchy_export_fields +}} + +class {{name}} {{if parent_def_type}}({{parent_def_type.py_full_name}}){{end}}: +{{~if x.is_abstract_type~}} + _childrenTypes = None + + @staticmethod + def fromJson(_json_): + childrenTypes = {{name}}._childrenTypes + if not childrenTypes: + childrenTypes = {{name}}._childrenTypes = { + {{~ for child in x.hierarchy_not_abstract_children~}} + '{{child.name}}': {{child.py_full_name}}, + {{~end~}} + } + type = _json_['__type__'] + child = {{name}}._childrenTypes.get(type) + if child != None: + return child(_json_) + else: + raise Exception() +{{~end~}} + + def __init__(self, _json_): + {{~if parent_def_type~}} + {{parent_def_type.py_full_name}}.__init__(self, _json_) + {{~end~}} + {{~ for field in export_fields ~}} + {{py3_deserialize_field ('self.' + field.convention_name) '_json_' field.name field.ctype}} + {{~end~}} + {{~if export_fields.empty?}} + pass + {{~end~}} \ No newline at end of file diff --git a/src/Luban.Server/Templates/config/python_json/include.tpl b/src/Luban.Server/Templates/config/python2_json/include.tpl similarity index 100% rename from src/Luban.Server/Templates/config/python_json/include.tpl rename to src/Luban.Server/Templates/config/python2_json/include.tpl diff --git a/src/Luban.Server/Templates/config/python_json/table.tpl b/src/Luban.Server/Templates/config/python2_json/table.tpl similarity index 100% rename from src/Luban.Server/Templates/config/python_json/table.tpl rename to src/Luban.Server/Templates/config/python2_json/table.tpl diff --git a/src/Luban.Server/Templates/config/python_json/tables.tpl b/src/Luban.Server/Templates/config/python2_json/tables.tpl similarity index 100% rename from src/Luban.Server/Templates/config/python_json/tables.tpl rename to src/Luban.Server/Templates/config/python2_json/tables.tpl diff --git a/src/Luban.Server/Templates/config/python_json/bean.tpl b/src/Luban.Server/Templates/config/python3_json/bean.tpl similarity index 100% rename from src/Luban.Server/Templates/config/python_json/bean.tpl rename to src/Luban.Server/Templates/config/python3_json/bean.tpl diff --git a/src/Luban.Server/Templates/config/python3_json/include.tpl b/src/Luban.Server/Templates/config/python3_json/include.tpl new file mode 100644 index 0000000..008ee4d --- /dev/null +++ b/src/Luban.Server/Templates/config/python3_json/include.tpl @@ -0,0 +1,54 @@ +from enum import Enum +import abc + +class Vector2: + def __init__(self, x, y): + self.x = x + self.y = y + def __str__(self): + return '{%g,%g}' % (self.x, self.y) + + @staticmethod + def fromJson(_json_): + x = _json_['x'] + y = _json_['y'] + if (x == None or y == None): + raise Exception() + return Vector2(x, y) + + +class Vector3: + def __init__(self, x, y, z): + self.x = x + self.y = y + self.z = z + def __str__(self): + return '{%f,%f,%f}' % (self.x, self.y, self.z) + @staticmethod + def fromJson(_json_): + x = _json_['x'] + y = _json_['y'] + z = _json_['z'] + if (x == None or y == None or z == None): + raise Exception() + return Vector3(x, y, z) + +class Vector4: + def __init__(self, x, y, z, w): + self.x = x + self.y = y + self.z = z + self.w = w + def __str__(self): + return '{%g,%g,%g,%g}' % (self.x, self.y, self.z, self.w) + + @staticmethod + def fromJson(_json_): + x = _json_['x'] + y = _json_['y'] + z = _json_['z'] + w = _json_['w'] + if (x == None or y == None or z == None or w == None): + raise Exception() + return Vector4(x, y, z, w) + diff --git a/src/Luban.Server/Templates/config/python3_json/table.tpl b/src/Luban.Server/Templates/config/python3_json/table.tpl new file mode 100644 index 0000000..700f51b --- /dev/null +++ b/src/Luban.Server/Templates/config/python3_json/table.tpl @@ -0,0 +1,54 @@ +{{ + name = x.py_full_name + key_type = x.key_ttype + key_type1 = x.key_ttype1 + key_type2 = x.key_ttype2 + value_type = x.value_ttype +}} + +class {{name}}: + {{~if x.is_map_table ~}} + + def __init__(self, _json_ ): + self._dataMap = {} + self._dataList = [] + + for _json2_ in _json_: + {{py3_deserialize_value '_v' '_json2_' value_type}} + self._dataList.append(_v) + self._dataMap[_v.{{x.index_field.convention_name}}] = _v + + def getDataMap(self) : return self._dataMap + def getDataList(self) : return self._dataList + + def get(self, key) : return self._dataMap.get(key) + {{~else if x.is_list_table ~}} + + def __init__(self, _json_ ): + self._dataList = [] + + for _json2_ in _json_: + {{py3_deserialize_value '_v' '_json2_' value_type}} + self._dataList.append(_v) + + def getDataList(self) : return self._dataList + + def get(self, index) : return self._dataList[index] + + {{~else~}} + + def __init__(self, _json_): + if (len(_json_) != 1): raise Exception('table mode=one, but size != 1') + {{py3_deserialize_value 'self._data' '_json_[0]' value_type}} + + def getData(self) : return self._data + + {{~ for field in value_type.bean.hierarchy_export_fields ~}} +{{~if field.comment != '' ~}} + ''' + {{field.escape_comment}} + ''' +{{~end~}} + def {{field.convention_name}}(self) : return self._data.{{field.convention_name}} + {{~end~}} + {{~end~}} diff --git a/src/Luban.Server/Templates/config/python3_json/tables.tpl b/src/Luban.Server/Templates/config/python3_json/tables.tpl new file mode 100644 index 0000000..9a4960a --- /dev/null +++ b/src/Luban.Server/Templates/config/python3_json/tables.tpl @@ -0,0 +1,15 @@ +{{ + name = x.name + namespace = x.namespace + tables = x.tables +}} + +class {{name}}: + {{~ for table in tables ~}} + #def {{table.name}} : return self._{{table.name}} + {{~end~}} + + def __init__(self, loader): + {{~for table in tables ~}} + self.{{table.name}} = {{table.py_full_name}}(loader('{{table.output_data_file}}')); + {{~end~}}