【特性】新增 proto go支持

main
walon 2021-11-29 14:31:13 +08:00
parent 1de0299b17
commit a9de6b924b
33 changed files with 492 additions and 347 deletions

View File

@ -22,10 +22,6 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Luban.Common\Luban.Common.csproj" />
</ItemGroup>

View File

@ -214,8 +214,6 @@ Options:
LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.FromString(options.LogLevel));
s_logger = NLog.LogManager.GetCurrentClassLogger();
int processorCount = System.Environment.ProcessorCount;
ThreadPool.SetMinThreads(Math.Max(4, processorCount), 5);
ThreadPool.SetMaxThreads(Math.Max(16, processorCount * 4), 10);

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Bright.Net" Version="1.1.0.41" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,30 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Common.Utils
{
public static class CommandLineUtil
{
public static T ParseOptions<T>(String[] args)
{
var helpWriter = new StringWriter();
var parser = new Parser(ps =>
{
ps.HelpWriter = helpWriter;
});
var result = parser.ParseArguments<T>(args);
if (result.Tag == ParserResultType.NotParsed)
{
Console.Error.WriteLine(helpWriter.ToString());
Environment.Exit(1);
}
return ((Parsed<T>)result).Value;
}
}
}

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
var ass = DefAssembly.LocalAssebmly;
return $"#{{{DText.KEY_NAME}=>\"{type.Key}\",{DText.TEXT_NAME}=>\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
}

View File

@ -92,7 +92,7 @@ namespace Luban.Job.Cfg.DataVisitors
public virtual string Accept(DDateTime type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
var ass = DefAssembly.LocalAssebmly;
return type.GetUnixTime(ass.TimeZone).ToString();
}
}

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
var ass = DefAssembly.LocalAssebmly;
return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}=\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
}

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
var ass = DefAssembly.LocalAssebmly;
return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}\"}}";
}

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type)
{
#if !LUBAN_LITE
var ass = DefAssembly.LocalAssebmly as DefAssembly;
var ass = DefAssembly.LocalAssebmly;
return $"\"{type.Key}#{type.GetText(ass.ExportTextTable, ass.NotConvertTextSet)}\"";
#else
return $"\"{type.Key}#{type.RawValue}\"";

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.DataVisitors
public override string Accept(DText type)
{
var ass = DefAssembly.LocalAssebmly as DefAssembly;
var ass = DefAssembly.LocalAssebmly;
return $"{{{DText.KEY_NAME}='{type.Key}',{DText.TEXT_NAME}='{DataUtil.EscapeString(type.GetText(ass.ExportTextTable, ass.NotConvertTextSet))}'}}";
}

View File

@ -32,22 +32,6 @@ namespace Luban.Job.Cfg.Defs
}
#if !LUBAN_LITE
public string GoBinImport
{
get
{
var imports = new HashSet<string>();
if (IsAbstractType)
{
imports.Add("errors");
}
foreach (var f in Fields)
{
f.CType.Apply(TypeVisitors.GoBinImport.Ins, imports);
}
return string.Join('\n', imports.Select(im => $"import \"{im}\""));
}
}
public string GoJsonImport
{

View File

@ -11,7 +11,7 @@ namespace Luban.Job.Cfg.TypeVisitors
{
if (type.IsNullable)
{
return $"{{ if {bufName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", bufName)}; {varName} = {(type.Apply(IsGoPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}";
return $"{{ if {bufName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", bufName)}; {varName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}";
}
else
{

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.TypeVisitors
if (type.IsNullable)
{
var jsonObjName = $"__json_{fieldName}__";
return $"{{ var _ok_ bool; var {jsonObjName} interface{{}}; if {jsonObjName}, _ok_ = {bufName}[\"{fieldName}\"]; !_ok_ || {jsonObjName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", jsonObjName)}; {varName} = {(type.Apply(IsGoPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}";
return $"{{ var _ok_ bool; var {jsonObjName} interface{{}}; if {jsonObjName}, _ok_ = {bufName}[\"{fieldName}\"]; !_ok_ || {jsonObjName} == nil {{ return }} else {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeJsonUndering2Visitor.Ins, "__x__", jsonObjName)}; {varName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}";
}
else
{

View File

@ -1,152 +0,0 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
{
class GoDeserializeUnderingVisitor : ITypeFuncVisitor<string, string, string>
{
public static GoDeserializeUnderingVisitor Ins { get; } = new GoDeserializeUnderingVisitor();
public string Accept(TBool type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadBool(); err != nil {{ return }} }}";
}
public string Accept(TByte type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadByte(); err != nil {{ return }} }}";
}
public string Accept(TShort type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadShort(); err != nil {{ return }} }}";
}
public string Accept(TFshort type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadFshort(); err != nil {{ return }} }}";
}
public string Accept(TInt type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadInt(); err != nil {{ return }} }}";
}
public string Accept(TFint type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadFint(); err != nil {{ return }} }}";
}
public string Accept(TLong type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadLong(); err != nil {{ return }} }}";
}
public string Accept(TFlong type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadFlong(); err != nil {{ return }} }}";
}
public string Accept(TFloat type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadFloat(); err != nil {{ return }} }}";
}
public string Accept(TDouble type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadDouble(); err != nil {{ return }} }}";
}
public string Accept(TEnum type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadInt(); err != nil {{ return }} }}";
}
public string Accept(TString type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadString(); err != nil {{ return }} }}";
}
public string Accept(TBytes type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadBytes(); err != nil {{ return }} }}";
}
public string Accept(TText type, string fieldName, string bufName)
{
return $"{{ if _, err = {bufName}.ReadString(); err != nil {{ return }}; if {fieldName}, err = {bufName}.ReadString(); err != nil {{ return }} }}";
}
public string Accept(TBean type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {($"New{type.Bean.GoFullName}({bufName})")}; err != nil {{ return }} }}";
}
private string GenList(TType elementType, string fieldName, string bufName)
{
return $@" {{
{fieldName} = make([]{elementType.Apply(GoTypeNameVisitor.Ins)}, 0)
var _n_ int
if _n_, err = {bufName}.ReadSize(); err != nil {{return}}
for i := 0 ; i < _n_ ; i++ {{
var _e_ {elementType.Apply(GoTypeNameVisitor.Ins)}
{elementType.Apply(GoDeserializeBinVisitor.Ins, "_e_", bufName)}
{fieldName} = append({fieldName}, _e_)
}}
}}
";
}
public string Accept(TArray type, string fieldName, string bufName)
{
return GenList(type.ElementType, fieldName, bufName);
}
public string Accept(TList type, string fieldName, string bufName)
{
return GenList(type.ElementType, fieldName, bufName);
}
public string Accept(TSet type, string fieldName, string bufName)
{
return GenList(type.ElementType, fieldName, bufName);
}
public string Accept(TMap type, string fieldName, string bufName)
{
return $@"{{
{fieldName} = make({type.Apply(GoTypeNameVisitor.Ins)})
var _n_ int
if _n_, err = {bufName}.ReadSize(); err != nil {{return}}
for i := 0 ; i < _n_ ; i++ {{
var _key_ {type.KeyType.Apply(GoTypeNameVisitor.Ins)}
{type.KeyType.Apply(GoDeserializeBinVisitor.Ins, "_key_", bufName)}
var _value_ {type.ValueType.Apply(GoTypeNameVisitor.Ins)}
{type.ValueType.Apply(GoDeserializeBinVisitor.Ins, "_value_", bufName)}
{fieldName}[_key_] = _value_
}}
}}";
}
public string Accept(TVector2 type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadVector2(); err != nil {{ return }} }}";
}
public string Accept(TVector3 type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadVector3(); err != nil {{ return }} }}";
}
public string Accept(TVector4 type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadVector4(); err != nil {{ return }} }}";
}
public string Accept(TDateTime type, string fieldName, string bufName)
{
return $"{{ if {fieldName}, err = {bufName}.ReadInt(); err != nil {{ return }} }}";
}
}
}

View File

@ -133,21 +133,6 @@ namespace Luban.Job.Cfg.Utils
}
}
public static string GoDefineType(TType type)
{
return type.Apply(GoTypeNameVisitor.Ins);
}
public static string GoDeserializeType(TBean type, string bufName)
{
return $"New{type.Bean.GoFullName}({bufName})";
}
public static string GoDeserializeField(TType type, string name, string bufName)
{
return type.Apply(GoDeserializeBinVisitor.Ins, name, bufName);
}
public static string GoDeserializeJsonField(TType type, string name, string fieldName, string bufName)
{
return type.Apply(GoDeserializeJsonVisitor.Ins, name, fieldName, bufName);

View File

@ -58,6 +58,25 @@ namespace Luban.Job.Common.Defs
}
}
#if !LUBAN_LITE
public string GoBinImport
{
get
{
var imports = new HashSet<string>();
if (IsAbstractType)
{
imports.Add("errors");
}
foreach (var f in Fields)
{
f.CType.Apply(Luban.Job.Common.TypeVisitors.GoBinImport.Ins, imports);
}
return string.Join('\n', imports.Select(im => $"import \"{im}\""));
}
}
#endif
protected abstract DefFieldBase CreateField(Field f, int idOffset);
public void CollectHierarchyNotAbstractChildren(List<DefBeanBase> children)

View File

@ -184,6 +184,27 @@ namespace Luban.Job.Common.Defs
return type.Apply(ErlangDefineTypeNameVisitor.Ins);
}
public static string GoDefineType(TType type)
{
return type.Apply(GoTypeNameVisitor.Ins);
}
public static string GoDeserializeType(TBean type, string bufName)
{
return $"Deserialize{type.Bean.GoFullName}({bufName})";
}
public static string GoSerializeField(TType type, string name, string bufName)
{
return type.Apply(GoSerializeBinVisitor.Ins, name, bufName);
}
public static string GoDeserializeField(TType type, string name, string bufName, string err)
{
return type.Apply(GoDeserializeBinVisitor.Ins, name, bufName, err);
}
public static bool HasTag(dynamic obj, string attrName)
{
return obj.HasTag(attrName);

View File

@ -1,10 +1,9 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
using System.Collections.Generic;
namespace Luban.Job.Cfg.TypeVisitors
namespace Luban.Job.Common.TypeVisitors
{
class GoBinImport : DecoratorActionVisitor<HashSet<string>>
public class GoBinImport : DecoratorActionVisitor<HashSet<string>>
{
public static GoBinImport Ins { get; } = new();

View File

@ -1,21 +1,21 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
namespace Luban.Job.Common.TypeVisitors
{
class GoDeserializeBinVisitor : DecoratorFuncVisitor<string, string, string>
public class GoDeserializeBinVisitor : DecoratorFuncVisitor<string, string, string, string>
{
public static GoDeserializeBinVisitor Ins { get; } = new GoDeserializeBinVisitor();
public override string DoAccept(TType type, string fieldName, string bufName)
public override string DoAccept(TType type, string fieldName, string bufName, string err)
{
if (type.IsNullable)
{
return $"{{ var __exists__ bool; if __exists__, err = {bufName}.ReadBool(); err != nil {{ return }}; if __exists__ {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoDeserializeUnderingVisitor.Ins, "__x__", bufName)}; {fieldName} = {(type.Apply(IsGoPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}";
return $"{{ var __exists__ bool; if __exists__, {err} = {bufName}.ReadBool(); {err} != nil {{ return }}; if __exists__ {{ var __x__ {type.Apply(GoTypeUnderingNameVisitor.Ins)}; {type.Apply(GoSerializeUnderingVisitor.Ins, "__x__", bufName)}; {fieldName} = {(type.Apply(GoIsPointerTypeVisitor.Ins) ? "&" : "")}__x__ }}}}";
}
else
{
return type.Apply(GoDeserializeUnderingVisitor.Ins, (string)fieldName, bufName);
return type.Apply(GoDeserializeUnderingVisitor.Ins, fieldName, bufName, err);
}
}
}

View File

@ -0,0 +1,130 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Common.TypeVisitors
{
public class GoDeserializeUnderingVisitor : ITypeFuncVisitor<string, string, string, string>
{
public static GoDeserializeUnderingVisitor Ins { get; } = new();
public string Accept(TBool type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadBool(); {err} != nil {{ return }} }}";
}
public string Accept(TByte type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadByte(); {err} != nil {{ return }} }}";
}
public string Accept(TShort type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadShort(); {err} != nil {{ return }} }}";
}
public string Accept(TFshort type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadFshort(); {err} != nil {{ return }} }}";
}
public string Accept(TInt type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}";
}
public string Accept(TFint type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadFint(); {err} != nil {{ return }} }}";
}
public string Accept(TLong type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadLong(); {err} != nil {{ return }} }}";
}
public string Accept(TFlong type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadFlong(); {err} != nil {{ return }} }}";
}
public string Accept(TFloat type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadFloat(); {err} != nil {{ return }} }}";
}
public string Accept(TDouble type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadDouble(); {err} != nil {{ return }} }}";
}
public string Accept(TEnum type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}";
}
public string Accept(TString type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadString(); {err} != nil {{ return }} }}";
}
public string Accept(TText type, string fieldName, string bufName, string err)
{
return $"{{ if _, {err} = {bufName}.ReadString(); {err} != nil {{ return }}; if {fieldName}, {err} = {bufName}.ReadString(); {err} != nil {{ return }} }}";
}
public string Accept(TBytes type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadBytes(); {err} != nil {{ return }} }}";
}
public string Accept(TVector2 type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadVector2(); {err} != nil {{ return }} }}";
}
public string Accept(TVector3 type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadVector3(); {err} != nil {{ return }} }}";
}
public string Accept(TVector4 type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadVector4(); {err} != nil {{ return }} }}";
}
public string Accept(TDateTime type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {bufName}.ReadInt(); {err} != nil {{ return }} }}";
}
public string Accept(TBean type, string fieldName, string bufName, string err)
{
return $"{{ if {fieldName}, {err} = {($"Deserialize{type.Bean.GoFullName}({bufName})")}; {err} != nil {{ return }} }}";
}
private string GenList(TType elementType, string fieldName, string bufName, string err)
{
return $@"{{{fieldName} = make([]{elementType.Apply(GoTypeNameVisitor.Ins)}, 0); var _n_ int; if _n_, {err} = {bufName}.ReadSize(); {err} != nil {{return}}; for i := 0 ; i < _n_ ; i++ {{ var _e_ {elementType.Apply(GoTypeNameVisitor.Ins)}; {elementType.Apply(GoDeserializeBinVisitor.Ins, "_e_", bufName, err)}; {fieldName} = append({fieldName}, _e_) }} }}";
}
public string Accept(TArray type, string fieldName, string bufName, string err)
{
return GenList(type.ElementType, fieldName, bufName, err);
}
public string Accept(TList type, string fieldName, string bufName, string err)
{
return GenList(type.ElementType, fieldName, bufName, err);
}
public string Accept(TSet type, string fieldName, string bufName, string err)
{
return GenList(type.ElementType, fieldName, bufName, err);
}
public string Accept(TMap type, string fieldName, string bufName, string err)
{
return $@"{{ {fieldName} = make({type.Apply(GoTypeNameVisitor.Ins)}); var _n_ int; if _n_, {err} = {bufName}.ReadSize(); {err} != nil {{return}}; for i := 0 ; i < _n_ ; i++ {{ var _key_ {type.KeyType.Apply(GoTypeNameVisitor.Ins)}; {type.KeyType.Apply(GoDeserializeBinVisitor.Ins, "_key_", bufName, err)}; var _value_ {type.ValueType.Apply(GoTypeNameVisitor.Ins)}; {type.ValueType.Apply(GoDeserializeBinVisitor.Ins, "_value_", bufName, err)}; {fieldName}[_key_] = _value_}} }}";
}
}
}

View File

@ -1,11 +1,10 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
namespace Luban.Job.Common.TypeVisitors
{
class IsGoPointerTypeVisitor : DecoratorFuncVisitor<bool>
public class GoIsPointerTypeVisitor : DecoratorFuncVisitor<bool>
{
public static IsGoPointerTypeVisitor Ins { get; } = new();
public static GoIsPointerTypeVisitor Ins { get; } = new();
public override bool DoAccept(TType type)
{

View File

@ -0,0 +1,22 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Common.TypeVisitors
{
public class GoSerializeBinVisitor : DecoratorFuncVisitor<string, string, string>
{
public static GoSerializeBinVisitor Ins { get; } = new();
public override string DoAccept(TType type, string fieldName, string bufName)
{
if (type.IsNullable)
{
return $"if {bufName} != nil {{ {bufName}.WriteBool(true); {type.Apply(GoSerializeUnderingVisitor.Ins, (type.Apply(GoIsPointerTypeVisitor.Ins) ? $"*{fieldName}" : fieldName), bufName)} }} else {{ {bufName}.WriteBool(false) }}";
}
else
{
return type.Apply(GoSerializeUnderingVisitor.Ins, fieldName, bufName);
}
}
}
}

View File

@ -0,0 +1,130 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Common.TypeVisitors
{
public class GoSerializeUnderingVisitor : ITypeFuncVisitor<string, string, string>
{
public static GoSerializeUnderingVisitor Ins { get; } = new();
public string Accept(TBool type, string fieldName, string bufName)
{
return $"{bufName}.WriteBool({fieldName})";
}
public string Accept(TByte type, string fieldName, string bufName)
{
return $"{bufName}.WriteByte({fieldName})";
}
public string Accept(TShort type, string fieldName, string bufName)
{
return $"{bufName}.WriteShort({fieldName})";
}
public string Accept(TFshort type, string fieldName, string bufName)
{
return $"{bufName}.WriteFshort({fieldName})";
}
public string Accept(TInt type, string fieldName, string bufName)
{
return $"{bufName}.WriteInt({fieldName})";
}
public string Accept(TFint type, string fieldName, string bufName)
{
return $"{bufName}.WriteFint({fieldName})";
}
public string Accept(TLong type, string fieldName, string bufName)
{
return $"{bufName}.WriteLong({fieldName})";
}
public string Accept(TFlong type, string fieldName, string bufName)
{
return $"{bufName}.WriteFlong({fieldName})";
}
public string Accept(TFloat type, string fieldName, string bufName)
{
return $"{bufName}.WriteFloat({fieldName})";
}
public string Accept(TDouble type, string fieldName, string bufName)
{
return $"{bufName}.WriteDouble({fieldName})";
}
public string Accept(TEnum type, string fieldName, string bufName)
{
return $"{bufName}.WriteInt({fieldName})";
}
public string Accept(TString type, string fieldName, string bufName)
{
return $"{bufName}.WriteString({fieldName})";
}
public string Accept(TText type, string fieldName, string bufName)
{
throw new System.NotSupportedException();
}
public string Accept(TBytes type, string fieldName, string bufName)
{
return $"{bufName}.WriteBytes({fieldName})";
}
public string Accept(TVector2 type, string fieldName, string bufName)
{
return $"{bufName}.WriteVector2({fieldName})";
}
public string Accept(TVector3 type, string fieldName, string bufName)
{
return $"{bufName}.WriteVector3({fieldName})";
}
public string Accept(TVector4 type, string fieldName, string bufName)
{
return $"{bufName}.WriteVector4({fieldName})";
}
public string Accept(TDateTime type, string fieldName, string bufName)
{
throw new System.NotSupportedException();
}
public string Accept(TBean type, string fieldName, string bufName)
{
return $"Serialize{type.Bean.GoFullName}({fieldName}, {bufName})";
}
private string GenList(TType elementType, string fieldName, string bufName)
{
return $@"{{ {bufName}.WriteSize(len({fieldName})); for _, _e_ := range({fieldName}) {{ {elementType.Apply(GoSerializeBinVisitor.Ins, "_e_", bufName)} }} }}";
}
public string Accept(TArray type, string fieldName, string bufName)
{
return GenList(type.ElementType, fieldName, bufName);
}
public string Accept(TList type, string fieldName, string bufName)
{
return GenList(type.ElementType, fieldName, bufName);
}
public string Accept(TSet type, string fieldName, string bufName)
{
return GenList(type.ElementType, fieldName, bufName);
}
public string Accept(TMap type, string fieldName, string bufName)
{
return $@"{{{bufName}.WriteSize(len({fieldName})); for _k_, _v_ := range({fieldName}) {{ {type.KeyType.Apply(GoSerializeBinVisitor.Ins, "_k_", bufName)}; {type.ValueType.Apply(GoSerializeBinVisitor.Ins, "_v_", bufName)} }} }}";
}
}
}

View File

@ -1,16 +1,16 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
namespace Luban.Job.Common.TypeVisitors
{
class GoTypeNameVisitor : DecoratorFuncVisitor<string>
public class GoTypeNameVisitor : DecoratorFuncVisitor<string>
{
public static GoTypeNameVisitor Ins { get; } = new GoTypeNameVisitor();
public override string DoAccept(TType type)
{
var s = type.Apply(GoTypeUnderingNameVisitor.Ins);
return type.Apply(IsGoPointerTypeVisitor.Ins) ? "*" + s : s;
return type.Apply(GoIsPointerTypeVisitor.Ins) ? "*" + s : s;
}
}
}

View File

@ -1,9 +1,9 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
namespace Luban.Job.Common.TypeVisitors
{
class GoTypeUnderingNameVisitor : ITypeFuncVisitor<string>
public class GoTypeUnderingNameVisitor : ITypeFuncVisitor<string>
{
public static GoTypeUnderingNameVisitor Ins { get; } = new GoTypeUnderingNameVisitor();
@ -79,7 +79,7 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TBean type)
{
return type.Bean.IsAbstractType ? $"interface{{}}" : $"*{type.Bean.GoFullName}";
return type.Bean.IsAbstractType ? $"serialization.ISerializable" : $"*{type.Bean.GoFullName}";
}
public string Accept(TArray type)

View File

@ -28,9 +28,17 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TString type);
TR Accept(TText type);
TR Accept(TBytes type);
TR Accept(TText type);
TR Accept(TVector2 type);
TR Accept(TVector3 type);
TR Accept(TVector4 type);
TR Accept(TDateTime type);
TR Accept(TBean type);
@ -41,14 +49,6 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TSet type);
TR Accept(TMap type);
TR Accept(TVector2 type);
TR Accept(TVector3 type);
TR Accept(TVector4 type);
TR Accept(TDateTime type);
}
public interface ITypeFuncVisitor<T, TR>
@ -77,9 +77,17 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TString type, T x);
TR Accept(TText type, T x);
TR Accept(TBytes type, T x);
TR Accept(TText type, T x);
TR Accept(TVector2 type, T x);
TR Accept(TVector3 type, T x);
TR Accept(TVector4 type, T x);
TR Accept(TDateTime type, T x);
TR Accept(TBean type, T x);
@ -90,14 +98,6 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TSet type, T x);
TR Accept(TMap type, T x);
TR Accept(TVector2 type, T x);
TR Accept(TVector3 type, T x);
TR Accept(TVector4 type, T x);
TR Accept(TDateTime type, T x);
}
public interface ITypeFuncVisitor<T, T2, TR>
@ -126,9 +126,17 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TString type, T x, T2 y);
TR Accept(TText type, T x, T2 y);
TR Accept(TBytes type, T x, T2 y);
TR Accept(TText type, T x, T2 y);
TR Accept(TVector2 type, T x, T2 y);
TR Accept(TVector3 type, T x, T2 y);
TR Accept(TVector4 type, T x, T2 y);
TR Accept(TDateTime type, T x, T2 y);
TR Accept(TBean type, T x, T2 y);
@ -139,14 +147,6 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TSet type, T x, T2 y);
TR Accept(TMap type, T x, T2 y);
TR Accept(TVector2 type, T x, T2 y);
TR Accept(TVector3 type, T x, T2 y);
TR Accept(TVector4 type, T x, T2 y);
TR Accept(TDateTime type, T x, T2 y);
}
public interface ITypeFuncVisitor<T, T2, T3, TR>
@ -175,9 +175,17 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TString type, T x, T2 y, T3 z);
TR Accept(TText type, T x, T2 y, T3 z);
TR Accept(TBytes type, T x, T2 y, T3 z);
TR Accept(TText type, T x, T2 y, T3 z);
TR Accept(TVector2 type, T x, T2 y, T3 z);
TR Accept(TVector3 type, T x, T2 y, T3 z);
TR Accept(TVector4 type, T x, T2 y, T3 z);
TR Accept(TDateTime type, T x, T2 y, T3 z);
TR Accept(TBean type, T x, T2 y, T3 z);
@ -188,13 +196,5 @@ namespace Luban.Job.Common.TypeVisitors
TR Accept(TSet type, T x, T2 y, T3 z);
TR Accept(TMap type, T x, T2 y, T3 z);
TR Accept(TVector2 type, T x, T2 y, T3 z);
TR Accept(TVector3 type, T x, T2 y, T3 z);
TR Accept(TVector4 type, T x, T2 y, T3 z);
TR Accept(TDateTime type, T x, T2 y, T3 z);
}
}

View File

@ -17,7 +17,7 @@ namespace Luban.Job.Proto.Generate
protected override string Render(DefEnum e)
{
var template = StringTemplateUtil.GetTemplate($"common/{RenderTemplateDir}/enum");
var result = template.Render(e);
var result = template.RenderCode(e);
return result;
}

View File

@ -35,7 +35,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="ExcelDataReader" Version="3.6.0" />
</ItemGroup>

View File

@ -21,25 +21,8 @@ namespace Luban.Server
[Option('t', "template_search_path", Required = false, HelpText = "additional template search path")]
public string TemplateSearchPath { get; set; }
[Option("timezone", Required = false, HelpText = "default timezone")]
public string DefaultTimeZone { get; set; } = "Asia/Shanghai";
}
private static CommandLineOptions ParseOptions(String[] args)
{
var helpWriter = new StringWriter();
var parser = new Parser(ps =>
{
ps.HelpWriter = helpWriter;
});
var result = parser.ParseArguments<CommandLineOptions>(args);
if (result.Tag == ParserResultType.NotParsed)
{
Console.Error.WriteLine(helpWriter.ToString());
Environment.Exit(1);
}
return ((Parsed<CommandLineOptions>)result).Value;
[Option("i10n:default_timezone", Required = false, HelpText = "default timezone")]
public string L10nDefaultTimeZone { get; set; } = "Asia/Shanghai";
}
static void Main(string[] args)
@ -47,7 +30,7 @@ namespace Luban.Server
ConsoleWindow.EnableQuickEditMode(false);
Console.OutputEncoding = System.Text.Encoding.UTF8;
var options = ParseOptions(args);
var options = CommandLineUtil.ParseOptions<CommandLineOptions>(args);
if (!string.IsNullOrEmpty(options.TemplateSearchPath))
{
@ -59,7 +42,7 @@ namespace Luban.Server
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
TimeZoneUtil.DefaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(options.DefaultTimeZone);
TimeZoneUtil.DefaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(options.L10nDefaultTimeZone);
GenServer.Ins.Start(false, options.Port, ProtocolStub.Factories);

View File

@ -2,7 +2,7 @@
go_full_name = x.go_full_name
parent_def_type = x.parent_def_type
is_abstract_type = x.is_abstract_type
export_fields = x.export_fields
hierarchy_fields = x.hierarchy_fields
hierarchy_not_abstract_children = x.hierarchy_not_abstract_children
-}}
@ -15,59 +15,59 @@ import (
{{x.go_bin_import}}
type {{go_full_name}} struct {
{{~if parent_def_type~}}
{{parent_def_type.go_full_name}}
{{~end~}}
{{~for field in export_fields ~}}
{{~for field in hierarchy_fields ~}}
{{field.convention_name}} {{go_define_type field.ctype}}
{{~end~}}
}
{{~if !is_abstract_type~}}
func ({{go_full_name}}) GetTypeId() int {
const TypeId_{{go_full_name}} = {{x.id}}
func ({{go_full_name}}) GetTypeId() int32 {
return {{x.id}}
}
{{~end~}}
func (_v {{go_full_name}})Serialize(_buf *serialization.ByteBuf) {
{{~for field in hierarchy_fields ~}}
{{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
}
func (_v {{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) {
{{~for field in hierarchy_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}}
{{~end~}}
return
}
{{~if is_abstract_type~}}
func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v interface{}, err error) {
func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) {
_buf.WriteInt(_v.GetTypeId())
_v.Serialize(_buf)
}
func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) {
var id int32
if id, err = _buf.ReadInt() ; err != nil {
return
}
switch id {
{{~for child in hierarchy_not_abstract_children~}}
case {{child.id}}: return New{{child.go_full_name}}(_buf)
case {{child.id}}: _v = {{child.go_full_name}}{}; if err = _v.Deserialize(_buf); err != nil { return nil, err } else { return }
{{~end~}}
default: return nil, errors.New("unknown type id")
}
return
}
func New{{go_full_name}}_Body(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) {
_v = &{{go_full_name}}{}
{{~if parent_def_type~}}
var _p *{{parent_def_type.go_full_name}}
if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return }
_v.{{parent_def_type.go_full_name}} = *_p
{{~end~}}
{{~for field in export_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
return
}
{{~else~}}
func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) {
_v = &{{go_full_name}}{}
{{~if parent_def_type~}}
var _p *{{parent_def_type.go_full_name}}
if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return }
_v.{{parent_def_type.go_full_name}} = *_p
{{~end~}}
{{~for field in export_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
return
func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) {
_v.Serialize(_buf)
}
func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (*{{go_full_name}}, error) {
v := &{{go_full_name}}{}
if err := v.Deserialize(_buf); err == nil {
return v, nil
} else {
return nil, err
}
}
{{~end~}}

View File

@ -2,7 +2,7 @@
go_full_name = x.go_full_name
parent_def_type = x.parent_def_type
is_abstract_type = x.is_abstract_type
export_fields = x.export_fields
hierarchy_fields = x.hierarchy_fields
hierarchy_not_abstract_children = x.hierarchy_not_abstract_children
-}}
@ -15,59 +15,59 @@ import (
{{x.go_bin_import}}
type {{go_full_name}} struct {
{{~if parent_def_type~}}
{{parent_def_type.go_full_name}}
{{~end~}}
{{~for field in export_fields ~}}
{{~for field in hierarchy_fields ~}}
{{field.convention_name}} {{go_define_type field.ctype}}
{{~end~}}
}
{{~if !is_abstract_type~}}
func ({{go_full_name}}) GetTypeId() int {
const TypeId_{{go_full_name}} = {{x.id}}
func ({{go_full_name}}) GetTypeId() int32 {
return {{x.id}}
}
{{~end~}}
func (_v {{go_full_name}})Serialize(_buf *serialization.ByteBuf) {
{{~for field in hierarchy_fields ~}}
{{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
}
func (_v {{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) {
{{~for field in hierarchy_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}}
{{~end~}}
return
}
{{~if is_abstract_type~}}
func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v interface{}, err error) {
func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) {
_buf.WriteInt(_v.GetTypeId())
_v.Serialize(_buf)
}
func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) {
var id int32
if id, err = _buf.ReadInt() ; err != nil {
return
}
switch id {
{{~for child in hierarchy_not_abstract_children~}}
case {{child.id}}: return New{{child.go_full_name}}(_buf)
case {{child.id}}: _v = {{child.go_full_name}}{}; if err = _v.Deserialize(_buf); err != nil { return nil, err } else { return }
{{~end~}}
default: return nil, errors.New("unknown type id")
}
return
}
func New{{go_full_name}}_Body(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) {
_v = &{{go_full_name}}{}
{{~if parent_def_type~}}
var _p *{{parent_def_type.go_full_name}}
if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return }
_v.{{parent_def_type.go_full_name}} = *_p
{{~end~}}
{{~for field in export_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
return
}
{{~else~}}
func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) {
_v = &{{go_full_name}}{}
{{~if parent_def_type~}}
var _p *{{parent_def_type.go_full_name}}
if _p, err = New{{parent_def_type.go_full_name}}_Body(_buf) ; err != nil { return }
_v.{{parent_def_type.go_full_name}} = *_p
{{~end~}}
{{~for field in export_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
return
func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) {
_v.Serialize(_buf)
}
func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (*{{go_full_name}}, error) {
v := &{{go_full_name}}{}
if err := v.Deserialize(_buf); err == nil {
return v, nil
} else {
return nil, err
}
}
{{~end~}}

View File

@ -1 +1,2 @@
package {{x.top_module}}
// rpc {{x.full_name}}

View File

@ -1 +1 @@
// stub
package {{namespace}}