-
Notifications
You must be signed in to change notification settings - Fork 21
/
HeaderParser.cs
213 lines (187 loc) · 9.34 KB
/
HeaderParser.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
using ClangSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace WowClientDB2MySQLTableGenerator
{
public sealed class HeaderParser
{
[DllImport("libclang.dll", EntryPoint = "clang_parseTranslationUnit2", CallingConvention = CallingConvention.Cdecl)]
public static extern CXErrorCode parseTranslationUnit2(CXIndex @CIdx,
[MarshalAs(UnmanagedType.LPStr)] string @source_filename,
string[] @command_line_args,
int @num_command_line_args,
[MarshalAs(UnmanagedType.LPArray)] CXUnsavedFile[] @unsaved_files,
uint @num_unsaved_files,
uint @options,
out CXTranslationUnit @out_TU);
public string FileName { get; set; }
public List<CStructureInfo> Structures { get; set; } = new List<CStructureInfo>();
private readonly StringBuilder CommonTypes = new StringBuilder()
.AppendLine("struct int64 { };")
.AppendLine("struct int32 { };")
.AppendLine("struct int16 { };")
.AppendLine("struct int8 { };")
.AppendLine("struct uint64 { };")
.AppendLine("struct uint32 { };")
.AppendLine("struct uint16 { };")
.AppendLine("struct uint8 { };")
.AppendLine("struct LocalizedString { };")
.AppendLine("struct DBCPosition2D { float X, Y; };")
.AppendLine("struct DBCPosition3D { float X, Y, Z; };")
.AppendLine("namespace Trinity { template<typename T> struct RaceMask { }; }")
.AppendLine("template<typename T> using EnumFlag = T;")
.AppendLine("enum AreaFlags { };")
.AppendLine("enum AreaFlags2 { };")
.AppendLine("enum AreaMountFlags { };")
.AppendLine("enum AreaTriggerActionSetFlag { };")
.AppendLine("enum AreaTriggerShapeType { };")
.AppendLine("using flag128 = int32[4];")
.AppendLine("using BattlegroundBracketId = uint32;")
.AppendLine("enum BattlePetSpeciesFlags { };")
.AppendLine("enum BattlemasterListFlags { };")
.AppendLine("enum CfgCategoriesCharsets { };")
.AppendLine("enum CfgCategoriesFlags { };")
.AppendLine("enum ChatChannelFlags { };")
.AppendLine("enum ChatChannelRuleset { };")
.AppendLine("enum ChrCustomizationReqFlag { };")
.AppendLine("enum ChrRacesFlag { };")
.AppendLine("enum ChrSpecializationFlag { };")
.AppendLine("enum ChrSpecializationRole { };")
.AppendLine("enum ContentTuningFlag { };")
.AppendLine("enum CreatureModelDataFlags { };")
.AppendLine("enum CriteriaFlags { };")
.AppendLine("enum CriteriaTreeFlags { };")
.AppendLine("enum CurrencyTypesFlags { };")
.AppendLine("enum CurrencyTypesFlagsB { };")
.AppendLine("enum FriendshipReputationFlags { };")
.AppendLine("enum MapFlags { };")
.AppendLine("enum MapFlags2 { };")
.AppendLine("enum MapDifficultyFlags { };")
.AppendLine("enum PathPropertyIndex { };")
.AppendLine("enum PhaseEntryFlags { };")
.AppendLine("enum PowerTypeFlags { };")
.AppendLine("enum SkillLineFlags { };")
.AppendLine("enum SkillLineAbilityFlags { };")
.AppendLine("enum SpellEffectAttributes { };")
.AppendLine("enum SpellItemEnchantmentFlags { };")
.AppendLine("enum SpellShapeshiftFormFlags { };")
.AppendLine("enum SummonPropertiesFlags { };")
.AppendLine("enum TaxiNodeFlags { };")
.AppendLine("enum TraitConditionType { };")
.AppendLine("enum TraitCurrencyType { };")
.AppendLine("enum TraitPointsOperationType { };")
.AppendLine("enum TraitNodeType { };")
.AppendLine("enum TraitNodeEntryType { };")
.AppendLine("enum TraitTreeFlag { };")
.AppendLine("enum TransmogIllusionFlags { };")
.AppendLine("enum UiMapFlag { };")
.AppendLine("enum UnitConditionFlags { };")
.AppendLine("enum VehicleSeatFlags {};")
.AppendLine("enum VehicleSeatFlagsB {};")
.AppendLine("enum VignetteFlags {};")
.AppendLine("#define MAX_ITEM_PROTO_FLAGS 5")
.AppendLine("#define MAX_ITEM_PROTO_ZONES 2")
.AppendLine("#define MAX_ITEM_PROTO_SOCKETS 3")
.AppendLine("#define MAX_ITEM_PROTO_STATS 10")
.AppendLine("#define MAX_SPELL_AURA_INTERRUPT_FLAGS 2");
private readonly Regex StdArrayRegex = new Regex(@"^[\s]*std::array<([^,]+), *([^>]+)> ([^;]+)+", RegexOptions.Multiline | RegexOptions.ECMAScript);
public void Parse()
{
var index = clang.createIndex(0, 1);
var args = new string[]
{
"-x",
"c++",
"--std=c++11"
};
// prepare input file
// comment out existing includes
// append required typedefs
var hdr = CommonTypes.Append(StdArrayRegex.Replace(File.ReadAllText(FileName).Replace("#include", "//#include"), "$1 $3[$2]")).ToString();
var unsavedFiles = new CXUnsavedFile[]
{
new CXUnsavedFile()
{
Contents = hdr,
Filename = FileName,
Length = hdr.Length
}
};
var result = parseTranslationUnit2(index, FileName, args, args.Length, unsavedFiles, (uint)unsavedFiles.Length, (uint)CXTranslationUnit_Flags.CXTranslationUnit_SkipFunctionBodies, out CXTranslationUnit translationUnit);
if (result != CXErrorCode.CXError_Success)
return;
clang.visitChildren(clang.getTranslationUnitCursor(translationUnit), VisitStruct, new CXClientData(IntPtr.Zero));
clang.disposeTranslationUnit(translationUnit);
clang.disposeIndex(index);
}
public CXChildVisitResult VisitStruct(CXCursor cursor, CXCursor parent, IntPtr data)
{
if (clang.Location_isInSystemHeader(clang.getCursorLocation(cursor)) != 0)
return CXChildVisitResult.CXChildVisit_Continue;
if (clang.getCursorKind(cursor) != CXCursorKind.CXCursor_StructDecl)
return CXChildVisitResult.CXChildVisit_Continue;
if (!clang.getCursorSpelling(cursor).ToString().Contains("Entry"))
return CXChildVisitResult.CXChildVisit_Continue;
var structure = new CStructureInfo();
structure.Name = clang.getCursorSpelling(cursor).ToString();
var suffixIndex = structure.Name.LastIndexOf("Entry", StringComparison.Ordinal);
if (suffixIndex != -1)
structure.Name = structure.Name.Remove(suffixIndex);
structure.NormalizedName = structure.Name
.Replace("GameObject", "Gameobject")
.Replace("NPC", "Npc")
.Replace("PvP", "Pvp")
.Replace("PVP", "Pvp")
.Replace("QuestXP", "QuestXp")
.Replace("WMO", "Wmo")
.Replace("AddOn", "Addon")
.Replace("LFG", "Lfg")
.Replace("POI", "Poi")
.Replace("UI", "Ui")
.Replace("_", "");
var handle = GCHandle.Alloc(structure);
clang.visitChildren(cursor, VisitField, new CXClientData(GCHandle.ToIntPtr(handle)));
handle.Free();
structure.Members.Add(new CStructureMemberInfo()
{
TypeName = "int32",
Name = "VerifiedBuild"
});
Structures.Add(structure);
var localeStruct = structure.CreateLocaleTable();
if (localeStruct != null)
Structures.Add(localeStruct);
return CXChildVisitResult.CXChildVisit_Continue;
}
public static CXChildVisitResult VisitField(CXCursor cursor, CXCursor parent, IntPtr data)
{
if (clang.getCursorKind(cursor) != CXCursorKind.CXCursor_FieldDecl)
return CXChildVisitResult.CXChildVisit_Continue;
GCHandle handle = GCHandle.FromIntPtr(data);
var structure = (CStructureInfo)handle.Target;
var typeInfo = ExtractFieldType(cursor);
structure.Members.Add(new CStructureMemberInfo()
{
TypeName = clang.getTypeSpelling(typeInfo.Type).ToString(),
Name = clang.getCursorSpelling(cursor).ToString().Replace("_lang", ""),
ArraySize = typeInfo.ArraySize
});
return CXChildVisitResult.CXChildVisit_Continue;
}
public static (CXType Type, long ArraySize) ExtractFieldType(CXCursor cursor)
{
var cxType = clang.getCursorType(cursor);
var arraySize = clang.getArraySize(cxType);
if (arraySize > 1)
cxType = clang.getArrayElementType(cxType);
var templateArguments = clang.Type_getNumTemplateArguments(cxType);
if (templateArguments > 0)
cxType = clang.Type_getTemplateArgumentAsType(cxType, 0);
return (cxType, Math.Max(arraySize, 1L));
}
}
}