2024-04-29 01:07:26 +08:00
|
|
|
|
using Fluid;
|
2024-06-20 22:32:20 +08:00
|
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
|
|
using System.IO;
|
2024-04-29 01:07:26 +08:00
|
|
|
|
using System.Text;
|
2024-04-29 14:58:45 +08:00
|
|
|
|
using System.Xml.Linq;
|
2024-04-29 01:07:26 +08:00
|
|
|
|
|
|
|
|
|
|
namespace refl
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class Gen
|
|
|
|
|
|
{
|
|
|
|
|
|
public static Dictionary<string, StringBuilder> FileList = new Dictionary<string, StringBuilder>();
|
2024-06-20 22:32:20 +08:00
|
|
|
|
public static StringBuilder Meta = new StringBuilder();
|
2024-04-29 01:07:26 +08:00
|
|
|
|
public static StringBuilder Output = new StringBuilder();
|
2024-06-20 22:32:20 +08:00
|
|
|
|
private ClassMetaData data = new ClassMetaData();
|
2024-04-29 01:07:26 +08:00
|
|
|
|
private GenMeta meta = new GenMeta();
|
2024-06-20 22:32:20 +08:00
|
|
|
|
private GenMetaImpl metaImpl = new GenMetaImpl();
|
2024-04-29 01:07:26 +08:00
|
|
|
|
private GenMultyMeta multyMeta = new GenMultyMeta();
|
2024-06-20 22:32:20 +08:00
|
|
|
|
private Stack<string> nameSpaces = new Stack<string>();
|
2024-04-29 01:07:26 +08:00
|
|
|
|
public Gen() { }
|
|
|
|
|
|
|
2024-06-20 22:32:20 +08:00
|
|
|
|
public static string PreprocessTemplate(string template, string indent, string newline)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 替换占位符
|
|
|
|
|
|
return template.Replace("{{ Indent }}", indent);
|
|
|
|
|
|
}
|
2024-04-29 01:07:26 +08:00
|
|
|
|
public static bool InitTemplate(string dir)
|
|
|
|
|
|
{
|
|
|
|
|
|
string meta_file = Path.Combine(dir, "meta.liquid");
|
2024-06-20 22:32:20 +08:00
|
|
|
|
string meta_impl_file = Path.Combine(dir, "meta_impl.liquid");
|
2024-04-29 14:58:45 +08:00
|
|
|
|
string multy_meta_file = Path.Combine(dir, "multy_meta.liquid");
|
2024-04-29 01:07:26 +08:00
|
|
|
|
var parser = new FluidParser();
|
2024-06-20 22:32:20 +08:00
|
|
|
|
return GenMeta.InitTemplate(meta_file, parser)
|
|
|
|
|
|
&& GenMetaImpl.InitTemplate(meta_impl_file, parser)
|
|
|
|
|
|
&& GenMultyMeta.InitTemplate(multy_meta_file, parser);
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
public void CheckDir(string? dir)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dir != null && !Directory.Exists(dir))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void InitFile(string file_name)
|
|
|
|
|
|
{
|
2024-06-20 22:32:20 +08:00
|
|
|
|
Meta.AppendLine("#pragma once");
|
|
|
|
|
|
Meta.AppendLine("namespace refl_impl{");
|
2024-04-29 01:07:26 +08:00
|
|
|
|
foreach (var name in ModuleMeta.NameSet)
|
|
|
|
|
|
{
|
|
|
|
|
|
var build = new StringBuilder();
|
|
|
|
|
|
build.AppendLine("#pragma once");
|
2024-06-20 22:32:20 +08:00
|
|
|
|
build.AppendLine("namespace refl_impl{");
|
2024-04-29 01:07:26 +08:00
|
|
|
|
FileList.Add(name, build);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void GenCppMeta(ModuleMeta module, string target)
|
|
|
|
|
|
{
|
|
|
|
|
|
string file_name = Path.GetFileName(target);
|
|
|
|
|
|
string? dir = Path.GetDirectoryName(target);
|
|
|
|
|
|
CheckDir(dir);
|
|
|
|
|
|
InitFile(file_name);
|
|
|
|
|
|
GenModuleMeta(module, null);
|
|
|
|
|
|
OutFile(file_name, dir, target);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void OutFile(string file_name, string? dir,string target)
|
|
|
|
|
|
{
|
2024-06-26 13:44:35 +08:00
|
|
|
|
Meta.AppendLine("");
|
|
|
|
|
|
Output.AppendLine("}");
|
2024-04-29 01:07:26 +08:00
|
|
|
|
foreach (var pair in FileList)
|
|
|
|
|
|
{
|
|
|
|
|
|
string path = $"{pair.Key}_{file_name}".ToLower();
|
|
|
|
|
|
if (dir != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
path = Path.Combine(dir, path);
|
|
|
|
|
|
}
|
2024-06-26 13:44:35 +08:00
|
|
|
|
pair.Value.AppendLine("}");
|
2024-04-29 01:07:26 +08:00
|
|
|
|
File.WriteAllText(path, pair.Value.ToString());
|
2024-06-20 22:32:20 +08:00
|
|
|
|
if (pair.Key == "Meta")
|
|
|
|
|
|
{
|
|
|
|
|
|
path = $"Meta_{file_name}".ToLower();
|
|
|
|
|
|
Output.AppendLine($"#include \"{path}\"");
|
|
|
|
|
|
}
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
2024-06-20 22:32:20 +08:00
|
|
|
|
using (StreamWriter writer = new StreamWriter(target, false, Encoding.UTF8))
|
|
|
|
|
|
{
|
|
|
|
|
|
writer.Write(Meta.ToString());
|
|
|
|
|
|
writer.Write(Output.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
public void GenModuleMeta(ModuleMeta module, string? prefix)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!module.HasMeta())
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (prefix != null)
|
|
|
|
|
|
{
|
2024-06-20 22:32:20 +08:00
|
|
|
|
nameSpaces.Push(module.NameSpace);
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
foreach (var cls in module.ClassList)
|
|
|
|
|
|
{
|
|
|
|
|
|
GenClassMeta(cls, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
string new_prefix = prefix == null ? "" : "\t";
|
|
|
|
|
|
foreach (var child in module.ChildList)
|
|
|
|
|
|
{
|
|
|
|
|
|
GenModuleMeta(child, new_prefix);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (prefix != null)
|
|
|
|
|
|
{
|
2024-06-20 22:32:20 +08:00
|
|
|
|
nameSpaces.Pop();
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-20 22:32:20 +08:00
|
|
|
|
public string GenPrefix()
|
2024-04-29 01:07:26 +08:00
|
|
|
|
{
|
2024-06-20 22:32:20 +08:00
|
|
|
|
if (nameSpaces.Count > 0)
|
2024-04-29 01:07:26 +08:00
|
|
|
|
{
|
2024-06-20 22:32:20 +08:00
|
|
|
|
return string.Join("::", nameSpaces) + "::";
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
2024-06-20 22:32:20 +08:00
|
|
|
|
return "";
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
2024-06-20 22:32:20 +08:00
|
|
|
|
public void GenClassMeta(ClassMeta cls, ClassMetaData data)
|
2024-04-29 01:07:26 +08:00
|
|
|
|
{
|
2024-06-20 22:32:20 +08:00
|
|
|
|
string fullName = GenPrefix() + cls.Name;
|
|
|
|
|
|
data.FullName = fullName;
|
|
|
|
|
|
bool isMulty = meta.GenClassMeta(cls, data);
|
|
|
|
|
|
Meta.Append(metaImpl.GenClassMeta(cls, fullName, isMulty));
|
|
|
|
|
|
Meta.AppendLine("");
|
|
|
|
|
|
if (isMulty)
|
2024-04-29 01:07:26 +08:00
|
|
|
|
{
|
2024-06-20 22:32:20 +08:00
|
|
|
|
var res = multyMeta.GenClassMeta(cls, fullName);
|
|
|
|
|
|
Output.Append(res);
|
|
|
|
|
|
Output.AppendLine("");
|
2024-04-29 01:07:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|