【特性】为 proto,cfg 新增 --use_unity_vector命令行参数,允许使用 UnityEngine.Vector{2,3,4} 而不是 System.Numerics.Vector{2,3,4}

main
walon 2021-08-03 16:05:43 +08:00
parent e6e88e5104
commit 05f98f3ae1
9 changed files with 67 additions and 14 deletions

View File

@ -271,10 +271,15 @@ namespace Luban.Job.Cfg
TimeZoneInfo timeZoneInfo = string.IsNullOrEmpty(args.TimeZone) ? null : TimeZoneInfo.FindSystemTimeZoneById(args.TimeZone); 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); ass.Load(args.Service, rawDefines);
DefAssemblyBase.LocalAssebmly = ass;
var targetService = ass.CfgTargetService; var targetService = ass.CfgTargetService;
@ -451,6 +456,7 @@ namespace Luban.Job.Cfg
res.ErrCode = Luban.Common.EErrorCode.JOB_EXCEPTION; res.ErrCode = Luban.Common.EErrorCode.JOB_EXCEPTION;
res.ErrMsg = $"{e.Message} \n {e.StackTrace}"; res.ErrMsg = $"{e.Message} \n {e.StackTrace}";
} }
DefAssemblyBase.LocalAssebmly = null;
timer.EndPhaseAndLog(); timer.EndPhaseAndLog();
agent.Session.ReplyRpc<GenJob, GenJobArg, GenJobRes>(rpc, res); agent.Session.ReplyRpc<GenJob, GenJobArg, GenJobRes>(rpc, res);

View File

@ -105,17 +105,17 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TVector2 type, string json, string x) 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) 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) 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) public string Accept(TDateTime type, string json, string x)

View File

@ -110,19 +110,19 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TVector2 type, string json, string x) public string Accept(TVector2 type, string json, string x)
{ {
string tempJsonName = $"_json2"; 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) public string Accept(TVector3 type, string json, string x)
{ {
string tempJsonName = $"_json2"; 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) public string Accept(TVector4 type, string json, string x)
{ {
string tempJsonName = $"_json2"; 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) public string Accept(TDateTime type, string json, string x)

View File

@ -5,6 +5,7 @@ using Luban.Server.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
namespace Luban.Job.Common.Defs 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 NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger();
private static readonly AsyncLocal<DefAssemblyBase> _localAssembly = new();
public static DefAssemblyBase LocalAssebmly
{
get
{
return _localAssembly.Value;
}
set
{
_localAssembly.Value = value;
}
}
public static bool IsUseUnityVectors => LocalAssebmly?.UseUnityVectors == true;
public Dictionary<string, DefTypeBase> Types { get; } = new Dictionary<string, DefTypeBase>(); public Dictionary<string, DefTypeBase> Types { get; } = new Dictionary<string, DefTypeBase>();
public RemoteAgent Agent { get; protected set; } public RemoteAgent Agent { get; protected set; }
@ -22,6 +39,8 @@ namespace Luban.Job.Common.Defs
public bool SupportNullable { get; protected set; } = true; public bool SupportNullable { get; protected set; } = true;
public bool UseUnityVectors { get; set; }
public void AddType(DefTypeBase type) public void AddType(DefTypeBase type)
{ {
string fullName = type.FullName; string fullName = type.FullName;

View File

@ -27,6 +27,9 @@ namespace Luban.Job.Common
[Option("embed_bright_types", Required = false, HelpText = "use puerts bytebuf class")] [Option("embed_bright_types", Required = false, HelpText = "use puerts bytebuf class")]
public bool EmbedBrightTypes { get; set; } 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) public bool ValidateOutouptCodeDir(ref string errMsg)
{ {
if (string.IsNullOrWhiteSpace(this.OutputCodeDir)) if (string.IsNullOrWhiteSpace(this.OutputCodeDir))

View File

@ -1,3 +1,4 @@
using Luban.Job.Common.Defs;
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
namespace Luban.Job.Common.TypeVisitors namespace Luban.Job.Common.TypeVisitors
@ -103,16 +104,28 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TVector2 type) public string Accept(TVector2 type)
{ {
if (DefAssemblyBase.IsUseUnityVectors)
{
return "UnityEngine.Vector2";
}
return "System.Numerics.Vector2"; return "System.Numerics.Vector2";
} }
public string Accept(TVector3 type) public string Accept(TVector3 type)
{ {
if (DefAssemblyBase.IsUseUnityVectors)
{
return "UnityEngine.Vector3";
}
return "System.Numerics.Vector3"; return "System.Numerics.Vector3";
} }
public string Accept(TVector4 type) public string Accept(TVector4 type)
{ {
if (DefAssemblyBase.IsUseUnityVectors)
{
return "UnityEngine.Vector4";
}
return "System.Numerics.Vector4"; return "System.Numerics.Vector4";
} }

View File

@ -1,3 +1,4 @@
using Luban.Job.Common.Defs;
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; 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) 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) 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) 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) public string Accept(TDateTime type, string bufName, string fieldName)

View File

@ -1,3 +1,4 @@
using Luban.Job.Common.Defs;
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; 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")} }} }}"; 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) 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) 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) 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) public string Accept(TDateTime type, string bufName, string fieldName)

View File

@ -93,10 +93,12 @@ namespace Luban.Job.Proto
var rawDefines = loader.BuildDefines(); var rawDefines = loader.BuildDefines();
var ass = new DefAssembly(); var ass = new DefAssembly() { UseUnityVectors = args.UseUnityVectors };
ass.Load(rawDefines, agent); ass.Load(rawDefines, agent);
DefAssemblyBase.LocalAssebmly = ass;
var targetService = args.Service; var targetService = args.Service;
@ -215,6 +217,9 @@ namespace Luban.Job.Proto
res.ErrCode = Luban.Common.EErrorCode.JOB_EXCEPTION; res.ErrCode = Luban.Common.EErrorCode.JOB_EXCEPTION;
res.ErrMsg = $"{e.Message} \n {e.StackTrace}"; res.ErrMsg = $"{e.Message} \n {e.StackTrace}";
} }
DefAssemblyBase.LocalAssebmly = null;
timer.EndPhaseAndLog(); timer.EndPhaseAndLog();
agent.Session.ReplyRpc<GenJob, GenJobArg, GenJobRes>(rpc, res); agent.Session.ReplyRpc<GenJob, GenJobArg, GenJobRes>(rpc, res);