diff --git a/src/Luban.Core/GenerationContext.cs b/src/Luban.Core/GenerationContext.cs index bcd6c449..379de905 100644 --- a/src/Luban.Core/GenerationContext.cs +++ b/src/Luban.Core/GenerationContext.cs @@ -6,6 +6,7 @@ using Luban.Datas; using Luban.Defs; using Luban.RawDefs; +using Luban.Schema; using Luban.Types; using Luban.TypeVisitors; using Luban.Utils; @@ -32,7 +33,9 @@ public class GenerationContext public static ICodeTarget CurrentCodeTarget { get; set; } - public DefAssembly Assembly { get; set; } + public static LubanConfig GlobalConf { get; set; } + + public DefAssembly Assembly { get; } public RawTarget Target => Assembly.Target; @@ -116,7 +119,7 @@ private List CalculateExportTypes() public static string GetInputDataPath() { - return EnvManager.Current.GetOption("", BuiltinOptionNames.InputDataDir, true); + return GlobalConf.InputDataDir; } public void AddDataTable(DefTable table, List mainRecords, List patchRecords) diff --git a/src/Luban.Core/GlobalConfigLoader.cs b/src/Luban.Core/GlobalConfigLoader.cs new file mode 100644 index 00000000..952e0059 --- /dev/null +++ b/src/Luban.Core/GlobalConfigLoader.cs @@ -0,0 +1,88 @@ +using System.Text; +using System.Text.Json; +using Luban.RawDefs; +using Luban.Schema; +using Luban.Utils; + +namespace Luban; + +public class GlobalConfigLoader : IConfigLoader +{ + private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); + + private string _curDir; + + public GlobalConfigLoader() + { + + } + + + private class Group + { + public List Names { get; set; } + + public bool Default { get; set; } + } + + private class SchemaFile + { + public string FileName { get; set; } + + public string Type { get; set; } + } + + private class Target + { + public string Name { get; set; } + + public string Manager { get; set; } + + public List Groups { get; set; } + + public string TopModule { get; set; } + } + + private class LubanConf + { + public List Groups { get; set; } + + public List SchemaFiles { get; set; } + + public string DataDir { get; set; } + + public List Targets { get; set; } + } + + public LubanConfig Load(string fileName) + { + s_logger.Debug("load config file:{}", fileName); + _curDir = Directory.GetParent(fileName).FullName; + + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + }; + var globalConf = JsonSerializer.Deserialize(File.ReadAllText(fileName, Encoding.UTF8), options); + + List groups = globalConf.Groups.Select(g => new RawGroup(){ Names = g.Names, IsDefault = g.Default }).ToList(); + List targets = globalConf.Targets.Select(t => new RawTarget() { Name = t.Name, Manager = t.Manager, Groups = t.Groups, TopModule = t.TopModule }).ToList(); + + List importFiles = new(); + foreach (var schemaFile in globalConf.SchemaFiles) + { + foreach (var subFile in FileUtil.GetFileOrDirectory(Path.Combine(_curDir, schemaFile.FileName))) + { + importFiles.Add(new SchemaFileInfo(){ FileName = subFile, Type = schemaFile.Type}); + } + } + return new LubanConfig() + { + InputDataDir = Path.Combine(_curDir, globalConf.DataDir), + Groups = groups, + Targets = targets, + Imports = importFiles, + }; + } + +} \ No newline at end of file diff --git a/src/Luban.Core/Pipeline/DefaultPipeline.cs b/src/Luban.Core/Pipeline/DefaultPipeline.cs index b44ee9d1..99df5f3d 100644 --- a/src/Luban.Core/Pipeline/DefaultPipeline.cs +++ b/src/Luban.Core/Pipeline/DefaultPipeline.cs @@ -15,6 +15,8 @@ public class DefaultPipeline : IPipeline { private static readonly Logger s_logger = LogManager.GetCurrentClassLogger(); + private LubanConfig _config; + private PipelineArguments _args; private RawAssembly _rawAssembly; @@ -37,10 +39,13 @@ public void Run(PipelineArguments args) protected void LoadSchema() { + IConfigLoader rootLoader = new GlobalConfigLoader(); + GenerationContext.GlobalConf = _config = rootLoader.Load(_args.ConfFile); + string schemaCollectorName = _args.SchemaCollector; - s_logger.Info("load schema. collector: {} path:{}", schemaCollectorName, _args.SchemaPath); + s_logger.Info("load schema. collector: {} path:{}", schemaCollectorName, _args.ConfFile); var schemaCollector = SchemaManager.Ins.CreateSchemaCollector(schemaCollectorName); - schemaCollector.Load(_args.SchemaPath); + schemaCollector.Load(_config); _rawAssembly = schemaCollector.CreateRawAssembly(); } diff --git a/src/Luban.Core/Pipeline/PipelineArguments.cs b/src/Luban.Core/Pipeline/PipelineArguments.cs index 22b1df9a..e9f6441e 100644 --- a/src/Luban.Core/Pipeline/PipelineArguments.cs +++ b/src/Luban.Core/Pipeline/PipelineArguments.cs @@ -14,7 +14,7 @@ public class PipelineArguments public string SchemaCollector { get; set; } - public string SchemaPath { get; set; } + public string ConfFile { get; set; } public List OutputTables { get; set; } diff --git a/src/Luban.Core/RawDefs/RawGroup.cs b/src/Luban.Core/RawDefs/RawGroup.cs index a9a5eadc..d1b67be4 100644 --- a/src/Luban.Core/RawDefs/RawGroup.cs +++ b/src/Luban.Core/RawDefs/RawGroup.cs @@ -2,7 +2,7 @@ namespace Luban.RawDefs; public class RawGroup { - public bool Default { get; set; } + public bool IsDefault { get; set; } public List Names { get; set; } } \ No newline at end of file diff --git a/src/Luban.Core/Schema/ISchemaCollector.cs b/src/Luban.Core/Schema/ISchemaCollector.cs index ce2292a4..d0566192 100644 --- a/src/Luban.Core/Schema/ISchemaCollector.cs +++ b/src/Luban.Core/Schema/ISchemaCollector.cs @@ -4,7 +4,7 @@ namespace Luban.Schema; public interface ISchemaCollector { - void Load(string configFile); + void Load(LubanConfig config); RawAssembly CreateRawAssembly(); diff --git a/src/Luban.Core/Schema/LubanConfig.cs b/src/Luban.Core/Schema/LubanConfig.cs index 466e59bb..13cb0262 100644 --- a/src/Luban.Core/Schema/LubanConfig.cs +++ b/src/Luban.Core/Schema/LubanConfig.cs @@ -9,4 +9,6 @@ public class LubanConfig public List Targets { get; set; } public List Imports { get; set; } + + public string InputDataDir { get; set; } } \ No newline at end of file diff --git a/src/Luban.Core/Schema/SchemaCollectorBase.cs b/src/Luban.Core/Schema/SchemaCollectorBase.cs index 16343f40..4ed98266 100644 --- a/src/Luban.Core/Schema/SchemaCollectorBase.cs +++ b/src/Luban.Core/Schema/SchemaCollectorBase.cs @@ -15,7 +15,7 @@ public abstract class SchemaCollectorBase : ISchemaCollector protected List Tables => _tables; - public abstract void Load(string schemaPath); + public abstract void Load(LubanConfig config); public abstract RawAssembly CreateRawAssembly(); diff --git a/src/Luban.Schema.Builtin/DefaultSchemaCollector.cs b/src/Luban.Schema.Builtin/DefaultSchemaCollector.cs index 2f87d5a7..2e22cf0e 100644 --- a/src/Luban.Schema.Builtin/DefaultSchemaCollector.cs +++ b/src/Luban.Schema.Builtin/DefaultSchemaCollector.cs @@ -10,15 +10,9 @@ public class DefaultSchemaCollector : SchemaCollectorBase private LubanConfig _config; - public override void Load(string configFile) + public override void Load(LubanConfig config) { - IConfigLoader rootLoader = Path.GetExtension(configFile) switch - { - ".xml" => new XmlConfigLoader(), - ".json" => new JsonConfigLoader(), - _ => throw new Exception($"unsupported config file:{configFile}"), - }; - _config = rootLoader.Load(configFile); + _config = config; foreach (var importFile in _config.Imports) { diff --git a/src/Luban.Schema.Builtin/JsonConfigLoader.cs b/src/Luban.Schema.Builtin/JsonConfigLoader.cs deleted file mode 100644 index 1c78f7e4..00000000 --- a/src/Luban.Schema.Builtin/JsonConfigLoader.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System.Xml.Linq; -using Luban.Defs; -using Luban.RawDefs; -using Luban.Utils; - -namespace Luban.Schema.Builtin; - -public class JsonConfigLoader : IConfigLoader -{ - private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); - - private readonly Dictionary> _tagHandlers = new(); - - private readonly List _groups = new(); - private readonly List _targets = new(); - private readonly List _importFiles = new(); - - private string _xmlFileName; - private string _curDir; - - public JsonConfigLoader() - { - _tagHandlers.Add("import", AddImport); - _tagHandlers.Add("target", AddTarget); - _tagHandlers.Add("group", AddGroup); - } - - public LubanConfig Load(string fileName) - { - s_logger.Debug("load root json config file:{}", fileName); - _xmlFileName = fileName; - _curDir = Directory.GetParent(fileName).FullName; - XElement doc = XmlUtil.Open(fileName); - - foreach (XElement e in doc.Elements()) - { - var tagName = e.Name.LocalName; - if (_tagHandlers.TryGetValue(tagName, out var handler)) - { - handler(e); - } - else - { - throw new LoadDefException($"定义文件:{fileName} 非法 tag:{tagName}"); - } - } - - return new LubanConfig() - { - Groups = _groups, - Targets = _targets, - Imports = _importFiles, - }; - } - - private static readonly List _importRequireAttrs = new() { "name" }; - private static readonly List _importOptionalAttrs = new() { "type" }; - - private void AddImport(XElement e) - { - XmlSchemaUtil.ValidAttrKeys(_xmlFileName, e, _importOptionalAttrs, _importRequireAttrs); - var importName = XmlUtil.GetRequiredAttribute(e, "name"); - if (string.IsNullOrWhiteSpace(importName)) - { - throw new Exception("import 属性name不能为空"); - } - var type = XmlUtil.GetOptionalAttribute(e, "type"); - foreach (var subFile in FileUtil.GetFileOrDirectory(Path.Combine(_curDir, importName))) - { - // ignore root.xml self - if (Path.GetFileName(subFile) != Path.GetFileName(_xmlFileName)) - { - _importFiles.Add(new SchemaFileInfo(){ FileName = subFile, Type = type}); - } - } - } - - private static readonly List _groupOptionalAttrs = new() { "default" }; - private static readonly List _groupRequireAttrs = new() { "name" }; - - private void AddGroup(XElement e) - { - XmlSchemaUtil.ValidAttrKeys(_xmlFileName, e, _groupOptionalAttrs, _groupRequireAttrs); - List groupNames = SchemaLoaderUtil.CreateGroups(XmlUtil.GetRequiredAttribute(e, "name")); - bool isDefault = XmlUtil.GetOptionBoolAttribute(e, "default"); - _groups.Add(new RawGroup(){ Names = groupNames, Default = isDefault}); - } - - private readonly List _targetAttrs = new() { "name", "manager", "group", "topModule" }; - - private void AddTarget(XElement e) - { - var name = XmlUtil.GetRequiredAttribute(e, "name"); - var manager = XmlUtil.GetRequiredAttribute(e, "manager"); - var topModule = XmlUtil.GetOptionalAttribute(e, "topModule"); - List groups = SchemaLoaderUtil.CreateGroups(XmlUtil.GetOptionalAttribute(e, "group")); - XmlSchemaUtil.ValidAttrKeys(_xmlFileName, e, _targetAttrs, _targetAttrs); - _targets.Add(new RawTarget() { Name = name, Manager = manager, Groups = groups, TopModule = topModule}); - } - -} \ No newline at end of file diff --git a/src/Luban.Schema.Builtin/XmlConfigLoader.cs b/src/Luban.Schema.Builtin/XmlConfigLoader.cs deleted file mode 100644 index 9479e9ab..00000000 --- a/src/Luban.Schema.Builtin/XmlConfigLoader.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System.Xml.Linq; -using Luban.Defs; -using Luban.RawDefs; -using Luban.Utils; - -namespace Luban.Schema.Builtin; - -public class XmlConfigLoader : IConfigLoader -{ - private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); - - private readonly Dictionary> _tagHandlers = new(); - - private readonly List _groups = new(); - private readonly List _targets = new(); - private readonly List _importFiles = new(); - - private string _xmlFileName; - private string _curDir; - - public XmlConfigLoader() - { - _tagHandlers.Add("import", AddImport); - _tagHandlers.Add("target", AddTarget); - _tagHandlers.Add("group", AddGroup); - } - - public LubanConfig Load(string fileName) - { - s_logger.Debug("load root config file:{}", fileName); - _xmlFileName = fileName; - _curDir = Directory.GetParent(fileName).FullName; - XElement doc = XmlUtil.Open(fileName); - - foreach (XElement e in doc.Elements()) - { - var tagName = e.Name.LocalName; - if (_tagHandlers.TryGetValue(tagName, out var handler)) - { - handler(e); - } - else - { - throw new LoadDefException($"定义文件:{fileName} 非法 tag:{tagName}"); - } - } - - return new LubanConfig() - { - Groups = _groups, - Targets = _targets, - Imports = _importFiles, - }; - } - - private static readonly List _importRequireAttrs = new() { "name" }; - private static readonly List _importOptionalAttrs = new() { "type" }; - - private void AddImport(XElement e) - { - XmlSchemaUtil.ValidAttrKeys(_xmlFileName, e, _importOptionalAttrs, _importRequireAttrs); - var importName = XmlUtil.GetRequiredAttribute(e, "name"); - if (string.IsNullOrWhiteSpace(importName)) - { - throw new Exception("import 属性name不能为空"); - } - var type = XmlUtil.GetOptionalAttribute(e, "type"); - foreach (var subFile in FileUtil.GetFileOrDirectory(Path.Combine(_curDir, importName))) - { - // ignore root.xml self - if (Path.GetFileName(subFile) != Path.GetFileName(_xmlFileName)) - { - _importFiles.Add(new SchemaFileInfo(){ FileName = subFile, Type = type}); - } - } - } - - private static readonly List _groupOptionalAttrs = new() { "default" }; - private static readonly List _groupRequireAttrs = new() { "name" }; - - private void AddGroup(XElement e) - { - XmlSchemaUtil.ValidAttrKeys(_xmlFileName, e, _groupOptionalAttrs, _groupRequireAttrs); - List groupNames = SchemaLoaderUtil.CreateGroups(XmlUtil.GetRequiredAttribute(e, "name")); - bool isDefault = XmlUtil.GetOptionBoolAttribute(e, "default"); - _groups.Add(new RawGroup(){ Names = groupNames, Default = isDefault}); - } - - private readonly List _targetAttrs = new() { "name", "manager", "group", "topModule" }; - - private void AddTarget(XElement e) - { - var name = XmlUtil.GetRequiredAttribute(e, "name"); - var manager = XmlUtil.GetRequiredAttribute(e, "manager"); - var topModule = XmlUtil.GetOptionalAttribute(e, "topModule"); - List groups = SchemaLoaderUtil.CreateGroups(XmlUtil.GetOptionalAttribute(e, "group")); - XmlSchemaUtil.ValidAttrKeys(_xmlFileName, e, _targetAttrs, _targetAttrs); - _targets.Add(new RawTarget() { Name = name, Manager = manager, Groups = groups, TopModule = topModule}); - } - -} \ No newline at end of file diff --git a/src/Luban/Program.cs b/src/Luban/Program.cs index a9f6955e..bcc23bfd 100644 --- a/src/Luban/Program.cs +++ b/src/Luban/Program.cs @@ -25,8 +25,8 @@ private class CommandOptions [Option('s', "schemaCollector", Required = false, HelpText = "schema collector name")] public string SchemaCollector { get; set; } = "default"; - [Option("schemaPath", Required = true, HelpText = "schema path")] - public string SchemaPath { get; set; } + [Option("conf", Required = true, HelpText = "luban conf file")] + public string ConfigFile { get; set; } [Option('t', "target", Required = true, HelpText = "target name")] public string Target { get; set; } @@ -197,7 +197,7 @@ private static PipelineArguments CreatePipelineArgs(CommandOptions opts) { Target = opts.Target, SchemaCollector = opts.SchemaCollector, - SchemaPath = opts.SchemaPath, + ConfFile = opts.ConfigFile, OutputTables = opts.OutputTables?.ToList() ?? new List(), CodeTargets = opts.CodeTargets?.ToList() ?? new List(), DataTargets = opts.DataTargets?.ToList() ?? new List(), diff --git a/src/Luban/Properties/launchSettings.json b/src/Luban/Properties/launchSettings.json new file mode 100644 index 00000000..5a6c44b9 --- /dev/null +++ b/src/Luban/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "cs-bin": { + "commandName": "Project", + "commandLineArgs": "-t\r\nserver\r\n-c\r\ncs-bin\r\n-d\r\nbin\r\n--conf\r\nD:\\workspace2\\luban_examples\\DesignerConfigs\\luban.conf\r\n--validationFailAsError\r\n-x\r\noutputCodeDir=D:\\workspace2\\luban_examples\\Projects\\Csharp_Unity_bin\\Assets\\Gen\r\n-x\r\noutputDataDir=D:\\workspace2\\luban_examples\\Projects\\GenerateDatas\\bytes\r\n-x\r\npathValidator.rootDir=D:\\workspace2\\luban_examples\\Projects\\Csharp_Unity_bin\r\n-x\r\nl10n.textProviderFile=*@D:\\workspace2\\luban_examples\\DesignerConfigs\\Datas\\l10n\\texts.json" + } + } +} \ No newline at end of file