Skip to content

Commit

Permalink
[refactor] 将root schema改为global conf,不再使用原始__root__.xml之类的根配置,而使用luba…
Browse files Browse the repository at this point in the history
…n.conf全局配置
  • Loading branch information
pirunxi committed Aug 30, 2023
1 parent 04cd564 commit 4f05aa5
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 221 deletions.
7 changes: 5 additions & 2 deletions src/Luban.Core/GenerationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -116,7 +119,7 @@ private List<DefTypeBase> CalculateExportTypes()

public static string GetInputDataPath()
{
return EnvManager.Current.GetOption("", BuiltinOptionNames.InputDataDir, true);
return GlobalConf.InputDataDir;
}

public void AddDataTable(DefTable table, List<Record> mainRecords, List<Record> patchRecords)
Expand Down
88 changes: 88 additions & 0 deletions src/Luban.Core/GlobalConfigLoader.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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<string> Groups { get; set; }

public string TopModule { get; set; }
}

private class LubanConf
{
public List<Group> Groups { get; set; }

public List<SchemaFile> SchemaFiles { get; set; }

public string DataDir { get; set; }

public List<Target> 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<LubanConf>(File.ReadAllText(fileName, Encoding.UTF8), options);

List<RawGroup> groups = globalConf.Groups.Select(g => new RawGroup(){ Names = g.Names, IsDefault = g.Default }).ToList();
List<RawTarget> targets = globalConf.Targets.Select(t => new RawTarget() { Name = t.Name, Manager = t.Manager, Groups = t.Groups, TopModule = t.TopModule }).ToList();

List<SchemaFileInfo> 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,
};
}

}
9 changes: 7 additions & 2 deletions src/Luban.Core/Pipeline/DefaultPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Luban.Core/Pipeline/PipelineArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PipelineArguments

public string SchemaCollector { get; set; }

public string SchemaPath { get; set; }
public string ConfFile { get; set; }

public List<string> OutputTables { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Luban.Core/RawDefs/RawGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Luban.RawDefs;

public class RawGroup
{
public bool Default { get; set; }
public bool IsDefault { get; set; }

public List<string> Names { get; set; }
}
2 changes: 1 addition & 1 deletion src/Luban.Core/Schema/ISchemaCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Luban.Schema;

public interface ISchemaCollector
{
void Load(string configFile);
void Load(LubanConfig config);

RawAssembly CreateRawAssembly();

Expand Down
2 changes: 2 additions & 0 deletions src/Luban.Core/Schema/LubanConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public class LubanConfig
public List<RawTarget> Targets { get; set; }

public List<SchemaFileInfo> Imports { get; set; }

public string InputDataDir { get; set; }
}
2 changes: 1 addition & 1 deletion src/Luban.Core/Schema/SchemaCollectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class SchemaCollectorBase : ISchemaCollector

protected List<RawTable> Tables => _tables;

public abstract void Load(string schemaPath);
public abstract void Load(LubanConfig config);

public abstract RawAssembly CreateRawAssembly();

Expand Down
10 changes: 2 additions & 8 deletions src/Luban.Schema.Builtin/DefaultSchemaCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
101 changes: 0 additions & 101 deletions src/Luban.Schema.Builtin/JsonConfigLoader.cs

This file was deleted.

Loading

0 comments on commit 4f05aa5

Please sign in to comment.