diff --git a/src/Luban.Job.Cfg/Source/JobController.cs b/src/Luban.Job.Cfg/Source/JobController.cs index f044c51..4aae87b 100644 --- a/src/Luban.Job.Cfg/Source/JobController.cs +++ b/src/Luban.Job.Cfg/Source/JobController.cs @@ -271,10 +271,15 @@ namespace Luban.Job.Cfg TimeZoneInfo timeZoneInfo = string.IsNullOrEmpty(args.TimeZone) ? null : TimeZoneInfo.FindSystemTimeZoneById(args.TimeZone); - var ass = new DefAssembly(args.BranchName, timeZoneInfo, args.ExportTestData, agent); + var ass = new DefAssembly(args.BranchName, timeZoneInfo, args.ExportTestData, agent) + { + UseUnityVectors = args.UseUnityVectors + }; ass.Load(args.Service, rawDefines); + DefAssemblyBase.LocalAssebmly = ass; + var targetService = ass.CfgTargetService; @@ -451,6 +456,7 @@ namespace Luban.Job.Cfg res.ErrCode = Luban.Common.EErrorCode.JOB_EXCEPTION; res.ErrMsg = $"{e.Message} \n {e.StackTrace}"; } + DefAssemblyBase.LocalAssebmly = null; timer.EndPhaseAndLog(); agent.Session.ReplyRpc(rpc, res); diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs index 616e441..0059e5f 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs @@ -105,17 +105,17 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TVector2 type, string json, string x) { - return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } {x} = new System.Numerics.Vector2(__x, __y); }}"; + return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y); }}"; } public string Accept(TVector3 type, string json, string x) { - return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } {x} = new System.Numerics.Vector3(__x, __y,__z); }}"; + return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y,__z); }}"; } public string Accept(TVector4 type, string json, string x) { - return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } float __w; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"w\")", "__w") } {x} = new System.Numerics.Vector4(__x, __y, __z, __w); }}"; + return $"{{ var _json0 = {json}; float __x; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"x\")", "__x") } float __y; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"y\")", "__y") } float __z; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"z\")", "__z") } float __w; {TFloat.Ins.Apply(this, "_json0.GetProperty(\"w\")", "__w") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; } public string Accept(TDateTime type, string json, string x) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs index e3b94a2..60eeb50 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs @@ -110,19 +110,19 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TVector2 type, string json, string x) { string tempJsonName = $"_json2"; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } {x} = new System.Numerics.Vector2(__x, __y); }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y); }}"; } public string Accept(TVector3 type, string json, string x) { string tempJsonName = $"_json2"; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } {x} = new System.Numerics.Vector3(__x, __y,__z); }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y,__z); }}"; } public string Accept(TVector4 type, string json, string x) { string tempJsonName = $"_json2"; - return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } float __w; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"w\"]", "__w") } {x} = new System.Numerics.Vector4(__x, __y, __z, __w); }}"; + return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"z\"]", "__z") } float __w; {TFloat.Ins.Apply(this, $"{tempJsonName}[\"w\"]", "__w") } {x} = new {type.Apply(CsDefineTypeName.Ins)}(__x, __y, __z, __w); }}"; } public string Accept(TDateTime type, string json, string x) diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index 03ef09a..f40ca93 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -5,6 +5,7 @@ using Luban.Server.Common; using System; using System.Collections.Generic; using System.Linq; +using System.Threading; namespace Luban.Job.Common.Defs { @@ -12,6 +13,22 @@ namespace Luban.Job.Common.Defs { private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); + private static readonly AsyncLocal _localAssembly = new(); + + public static DefAssemblyBase LocalAssebmly + { + get + { + return _localAssembly.Value; + } + set + { + _localAssembly.Value = value; + } + } + + public static bool IsUseUnityVectors => LocalAssebmly?.UseUnityVectors == true; + public Dictionary Types { get; } = new Dictionary(); public RemoteAgent Agent { get; protected set; } @@ -22,6 +39,8 @@ namespace Luban.Job.Common.Defs public bool SupportNullable { get; protected set; } = true; + public bool UseUnityVectors { get; set; } + public void AddType(DefTypeBase type) { string fullName = type.FullName; diff --git a/src/Luban.Job.Common/Source/GenArgsBase.cs b/src/Luban.Job.Common/Source/GenArgsBase.cs index 0a5cca0..2f98b23 100644 --- a/src/Luban.Job.Common/Source/GenArgsBase.cs +++ b/src/Luban.Job.Common/Source/GenArgsBase.cs @@ -27,6 +27,9 @@ namespace Luban.Job.Common [Option("embed_bright_types", Required = false, HelpText = "use puerts bytebuf class")] public bool EmbedBrightTypes { get; set; } + [Option("use_unity_vector", Required = false, HelpText = "use UnityEngine.Vector{2,3,4}")] + public bool UseUnityVectors { get; set; } + public bool ValidateOutouptCodeDir(ref string errMsg) { if (string.IsNullOrWhiteSpace(this.OutputCodeDir)) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs index 700d4b3..607d4a3 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs @@ -1,3 +1,4 @@ +using Luban.Job.Common.Defs; using Luban.Job.Common.Types; namespace Luban.Job.Common.TypeVisitors @@ -103,16 +104,28 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TVector2 type) { + if (DefAssemblyBase.IsUseUnityVectors) + { + return "UnityEngine.Vector2"; + } return "System.Numerics.Vector2"; } public string Accept(TVector3 type) { + if (DefAssemblyBase.IsUseUnityVectors) + { + return "UnityEngine.Vector3"; + } return "System.Numerics.Vector3"; } public string Accept(TVector4 type) { + if (DefAssemblyBase.IsUseUnityVectors) + { + return "UnityEngine.Vector4"; + } return "System.Numerics.Vector4"; } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index a57a4d9..25193fe 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -1,3 +1,4 @@ +using Luban.Job.Common.Defs; using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; @@ -103,19 +104,21 @@ namespace Luban.Job.Common.TypeVisitors } + public static string VectorName => (DefAssemblyBase.IsUseUnityVectors ? "UnityVector" : "Vector"); + public string Accept(TVector2 type, string bufName, string fieldName) { - return $"{fieldName} = {bufName}.ReadVector2();"; + return $"{fieldName} = {bufName}.Read{VectorName}2();"; } public string Accept(TVector3 type, string bufName, string fieldName) { - return $"{fieldName} = {bufName}.ReadVector3();"; + return $"{fieldName} = {bufName}.Read{VectorName}3();"; } public string Accept(TVector4 type, string bufName, string fieldName) { - return $"{fieldName} = {bufName}.ReadVector4();"; + return $"{fieldName} = {bufName}.Read{VectorName}4();"; } public string Accept(TDateTime type, string bufName, string fieldName) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs index 1275393..7c22271 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingSerializeVisitor.cs @@ -1,3 +1,4 @@ +using Luban.Job.Common.Defs; using Luban.Job.Common.Types; using Luban.Job.Common.TypeVisitors; @@ -102,19 +103,22 @@ namespace Luban.Job.Common.TypeVisitors return $"{{ {bufName}.WriteSize({fieldName}.Count); foreach((var _k, var _v) in {fieldName}) {{ {type.KeyType.Apply(this, bufName, "_k")} {type.ValueType.Apply(this, bufName, "_v")} }} }}"; } + + public static string VectorName => (DefAssemblyBase.IsUseUnityVectors ? "UnityVector" : "Vector"); + public string Accept(TVector2 type, string bufName, string fieldName) { - return $"{bufName}.WriteVector2({fieldName});"; + return $"{bufName}.Write{VectorName}2({fieldName});"; } public string Accept(TVector3 type, string bufName, string fieldName) { - return $"{bufName}.WriteVector3({fieldName});"; + return $"{bufName}.Write{VectorName}3({fieldName});"; } public string Accept(TVector4 type, string bufName, string fieldName) { - return $"{bufName}.WriteVector4({fieldName});"; + return $"{bufName}.Write{VectorName}4({fieldName});"; } public string Accept(TDateTime type, string bufName, string fieldName) diff --git a/src/Luban.Job.Proto/Source/JobController.cs b/src/Luban.Job.Proto/Source/JobController.cs index 609144f..36e6e9b 100644 --- a/src/Luban.Job.Proto/Source/JobController.cs +++ b/src/Luban.Job.Proto/Source/JobController.cs @@ -93,10 +93,12 @@ namespace Luban.Job.Proto var rawDefines = loader.BuildDefines(); - var ass = new DefAssembly(); + var ass = new DefAssembly() { UseUnityVectors = args.UseUnityVectors }; ass.Load(rawDefines, agent); + DefAssemblyBase.LocalAssebmly = ass; + var targetService = args.Service; @@ -215,6 +217,9 @@ namespace Luban.Job.Proto res.ErrCode = Luban.Common.EErrorCode.JOB_EXCEPTION; res.ErrMsg = $"{e.Message} \n {e.StackTrace}"; } + + DefAssemblyBase.LocalAssebmly = null; + timer.EndPhaseAndLog(); agent.Session.ReplyRpc(rpc, res);