-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
234 lines (194 loc) · 9.43 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using CppAst;
using System.Diagnostics;
namespace RytheTributary
{
class Program
{
private static string autogenHeaders;
private static string autogenImpl;
public static uint filesCreated = 0;
private static string autogenFolderPath;
static void Main(string[] args)
{
Logger.Init();
if (!CLArgs.Parse(args))
return;
Logger.Log("Enter Main", LogLevel.trace);
Logger.Log($"Module Name: {CLArgs.moduleName}", LogLevel.info);
autogenFolderPath = $@"{CLArgs.moduleRoot}\{CLArgs.moduleName}\autogen";
string autogenHeaderPath = $@"{autogenFolderPath}\autogen.hpp";
string autogenImplPath = $@"{autogenFolderPath}\autogen.cpp";
if (Directory.Exists(autogenFolderPath))
IterateFiles(autogenFolderPath, new string[] { "*" }, (string filePath) =>
{
File.Delete(filePath);
}, SearchOption.TopDirectoryOnly);
else
Directory.CreateDirectory(autogenFolderPath);
File.WriteAllText(autogenHeaderPath, "#pragma once\n#include <core/platform/platform.hpp>\n#if !defined(L_AUTOGENACTIVE)\nL_ERROR(\"Preprocessor still busy, or crashed.\");\n#endif\n");
File.WriteAllText(autogenImplPath, "#include <core/platform/platform.hpp>\n#if !defined(L_AUTOGENACTIVE)\nL_ERROR(\"Preprocessor still busy, or crashed.\");\n#endif\n");
if (ModuleAST.GenerateAST())
{
autogenHeaders = "#pragma once\n#include <core/platform/platform.hpp>\n#if __has_include_next(\"autogen.hpp\")\n#include_next \"autogen.hpp\"\n#endif\n";
autogenImpl = "#include \"autogen.hpp\"\n";
AddOverrideFiles();
SearchNamespaces(ModuleAST.Namespaces);
GenerateCode(ModuleAST.Classes);
File.WriteAllText(autogenHeaderPath, $"{autogenHeaders}\n");
File.WriteAllText(autogenImplPath, $"{autogenImpl}");
}
Logger.Log($"Scanned {ModuleAST.filesChecked} files and generated {filesCreated} files in total.", LogLevel.info);
Logger.Log("Exit Main", LogLevel.trace);
Logger.WriteToFile("PreProcessor");
}
static string GetRelativePath(string basePath, string targetPath)
{
return (new Uri(basePath)).MakeRelativeUri(new Uri(targetPath)).OriginalString;
}
static void IterateFiles(string path, string[] searchPatterns, Action<string> fileIteration, SearchOption searchOption = SearchOption.AllDirectories)
{
foreach (string patern in searchPatterns)
foreach (var filePath in Directory.GetFileSystemEntries(path, patern, searchOption))
{
FileAttributes attr = File.GetAttributes(filePath);
if (!attr.HasFlag(FileAttributes.Directory))
{
fileIteration(filePath);
}
}
}
static void AddOverrideFiles()
{
string overridePath = $@"{autogenFolderPath}\override";
if (Directory.Exists(overridePath))
{
string[] headerTypes = { "*.h", "*.hpp" };
IterateFiles(overridePath, headerTypes, (string path) =>
{
autogenHeaders += $"#include \"{GetRelativePath(overridePath, path)}\"\n";
Logger.Log($"Including override header: {path}", LogLevel.debug);
});
string[] implTypes = { "*.inl" };
IterateFiles(overridePath, implTypes, (string path) =>
{
autogenImpl += $"#include \"{GetRelativePath(overridePath, path)}\"\n";
Logger.Log($"Including override implementation: {path}", LogLevel.debug);
});
}
}
static void GenerateCode(CppContainerList<CppClass> classes, string nameSpace = "")
{
Logger.Log("Enter GenerateCode", LogLevel.trace);
foreach (var cppStruct in classes)
{
if (cppStruct.SourceFile == null)
continue;
if (!cppStruct.SourceFile.Contains(CLArgs.moduleName))
continue;
if (cppStruct.TemplateParameters.Count > 0)
continue;
bool matched = false;
foreach (Regex regex in CLArgs.excludePatterns)
if (regex.IsMatch(cppStruct.SourceFile))
{
matched = true;
break;
}
if (matched)
continue;
string depPath = " ";
if (cppStruct.SourceFile.Contains(CLArgs.moduleRoot + "\\"))
depPath = cppStruct.SourceFile.Replace(CLArgs.moduleRoot + "\\", "");
depPath = depPath.Replace("\\", "/");
string source = File.ReadAllText(cppStruct.SourceFile);
Regex removeGroupComments = new Regex(@"\/\*[\s|\S]*?\*\/");
Regex removeComments = new Regex(@"\/\/[\t| |\S]*");
source = removeComments.Replace(source, "");
source = removeGroupComments.Replace(source, "");
Regex findStruct = new Regex($@"\b(?:struct|class)\b\s*.*\s*{cppStruct.Name}\s*(?:\s*\:[\s|<|>|\w|\:]*)?\s*\{{");
if (!findStruct.IsMatch(source))
continue;
bool reflectable = false;
bool no_reflect = false;
foreach (CppAttribute attr in cppStruct.Attributes)
{
if (attr.Name.Equals("reflectable"))
{
reflectable = true;
no_reflect = false;
break;
}
else if (attr.Name.Equals("no_reflect"))
{
no_reflect = true;
break;
}
}
if (no_reflect)
continue;
if (!reflectable && (cppStruct.ClassKind == CppClassKind.Class))
continue;
WriteToHpp(cppStruct.Name, CodeGenerator.PrototypeHeader(cppStruct.Name, nameSpace), "prototype");
WriteToHpp(cppStruct.Name, CodeGenerator.ReflectorHeader(cppStruct.Name, nameSpace), "reflector");
if (reflectable)
{
WriteToInl(cppStruct.Name, CodeGenerator.PrototypeImpl(cppStruct, nameSpace, depPath), "prototype");
WriteToInl(cppStruct.Name, CodeGenerator.ReflectorImpl(cppStruct, nameSpace, depPath), "reflector");
}
else
{
WriteToInl(cppStruct.Name, CodeGenerator.DummyPrototypeImpl(cppStruct, nameSpace, depPath), "prototype");
WriteToInl(cppStruct.Name, CodeGenerator.DummyReflectorImpl(cppStruct, nameSpace, depPath), "reflector");
}
Logger.Log($"Generated files for {cppStruct.Name}", LogLevel.debug);
}
Logger.Log("Exit GenerateCode", LogLevel.trace);
}
static void SearchNamespaces(CppContainerList<CppNamespace> Namespaces, string parentNameSpace = "")
{
Logger.Log("Enter SearchNamespaces", LogLevel.trace);
string combinedNameSpace = "";
foreach (var nameSpace in Namespaces)
{
if (parentNameSpace.Length > 0 && nameSpace.Name.Length > 0)
combinedNameSpace = parentNameSpace + "::" + nameSpace.Name;
else if (parentNameSpace.Length > 0)
combinedNameSpace = parentNameSpace;
else
combinedNameSpace = nameSpace.Name;
if (nameSpace.Namespaces.Count < 1)
{
GenerateCode(nameSpace.Classes, combinedNameSpace);
}
else
{
SearchNamespaces(nameSpace.Namespaces, combinedNameSpace);
GenerateCode(nameSpace.Classes, combinedNameSpace);
}
}
Logger.Log("Exit SearchNamespaces", LogLevel.trace);
}
static void WriteToHpp(string structName, string contents, string type)
{
Logger.Log("Enter WriteToHpp", LogLevel.trace);
string writePath = $@"{autogenFolderPath}\autogen_{type}_{structName}.hpp";
autogenHeaders += $"#include \"{writePath}\"\n".Replace($@"{CLArgs.moduleRoot}\{CLArgs.moduleName}\autogen\", "");
filesCreated++;
File.WriteAllText(writePath, contents);
Logger.Log("Exit WriteToHpp", LogLevel.trace);
}
static void WriteToInl(string structName, string contents, string type)
{
Logger.Log("Enter WriteToInl", LogLevel.trace);
string writePath = $@"{autogenFolderPath}\autogen_{type}_{structName}.inl";
autogenImpl += $"#include \"autogen_{type}_{structName}.inl\"\n";
filesCreated++;
File.WriteAllText(writePath, contents);
Logger.Log("Exit WriteToInl", LogLevel.trace);
}
}
}