/
processDBD.php
368 lines (307 loc) · 9.8 KB
/
processDBD.php
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
if(php_sapi_name() != "cli") die("This script cannot be run outside of CLI.");
require_once("../../inc/config.php");
$versionCacheByID = [];
foreach($pdo->query("SELECT id, version FROM wow_builds") as $version){
$versionCacheByID[$version['id']] = $version['version'];
}
$tableCacheByID = [];
foreach($pdo->query("SELECT id, name FROM wow_dbc_tables") as $table){
$tableCacheByID[$table['id']] = $table['name'];
}
$versionTableCache = [];
foreach($pdo->query("SELECT versionid, tableid FROM wow_dbc_table_versions WHERE hasDefinition = 0") as $tv){
$versionTableCache[$tv['versionid']][] = $tv['tableid'];
}
$dbdCache = [];
foreach(glob("/home/wow/dbd/WoWDBDefs/definitions/*.dbd") as $dbd){
$reader = new DBDReader();
$dbdCache[strtolower(basename(str_replace(".dbd", "", $dbd)))] = $reader->read($dbd);
}
$defq = $pdo->prepare("UPDATE wow_dbc_table_versions SET hasDefinition = ? WHERE versionid = ? AND tableid = ?");
foreach($versionTableCache as $versionID => $versions){
$version = $versionCacheByID[$versionID];
$build = new Build($version);
foreach($versions as $tableID){
$versionCompat = false;
$table = $tableCacheByID[$tableID];
if(!array_key_exists(strtolower($table), $dbdCache)){
continue;
}
foreach($dbdCache[strtolower($table)]['versionDefinitions'] as $versionDef){
if(in_array($build, $versionDef['builds'])){
$versionCompat = true;
}
foreach($versionDef['buildRanges'] as $range){
if($range->Contains($build)){
$versionCompat = true;
}
}
}
if($versionCompat){
$defq->execute([1, $versionID, $tableID]);
}
}
}
class Build
{
public $expansion;
public $major;
public $minor;
public $build;
public function __construct($build)
{
$splitBuild = explode(".", $build);
$this->expansion = $splitBuild[0];
$this->major = $splitBuild[1];
$this->minor = $splitBuild[2];
$this->build = $splitBuild[3];
}
public function __toString()
{
return $this->expansion.".".$this->major.".".$this->minor.".".$this->build;
}
}
class BuildRange
{
public $minBuild;
public $maxBuild;
public function __construct($minBuild, $maxBuild)
{
$this->minBuild = new Build($minBuild);
$this->maxBuild = new Build($maxBuild);
}
public function __toString()
{
return $this->minBuild."-".$this->maxBuild;
}
public function Contains(Build $build) : bool
{
return
$build->expansion >= $this->minBuild->expansion && $build->expansion <= $this->maxBuild->expansion &&
$build->major >= $this->minBuild->major && $build->major <= $this->maxBuild->major &&
$build->build >= $this->minBuild->build && $build->build <= $this->maxBuild->build;
}
}
class DBDReader
{
public function Read(string $file, bool $validate = false){
if(!file_exists($file)){
throw new Exception("Unable to find definitions file " . $file);
}
$dbd['columnDefinitions'] = array();
$lines = explode("\n", file_get_contents($file));
$lineNumber = 0;
if(substr($lines[0], 0, 7) == "COLUMNS"){
$lineNumber++;
while(true){
$line = $lines[$lineNumber++];
// Column definitions are done after encountering a newline
if (empty($line)) break;
// Create a new column definition to store information in
$columnDefinition = array();
/* TYPE READING */
// List of valid types, uint should be removed soon-ish
$validTypes = array("uint", "int", "float", "string", "locstring");
// Read line up to space (end of type) or < (foreign key)
$spacePos = strpos($line, ' ');
$larrPos = strpos($line, '<');
if(!empty($larrPos) && $spacePos > $larrPos){
$type = substr($line, 0, $larrPos);
}else{
$type = substr($line, 0, $spacePos);
}
// Check if type is valid, throw exception if not!
if (!in_array($type, $validTypes))
{
throw new Exception("Invalid type: " . $type . " on line " . $lineNumber);
}
else
{
$columnDefinition['type'] = $type;
}
/* FOREIGN KEY READING */
// Only read foreign key if foreign key identifier is found right after type (it could also be in comments)
if (substr($line, 0, strlen($columnDefinition['type']) + 1) == $columnDefinition['type']."<")
{
// Read foreign key info between < and > without < and > in result, then split on :: to separate table and field
$foreignKey = substr($line, strpos($line, '<') + 1, strpos($line, '>') - strpos($line, '<') - 1);
$foreignKey = explode("::", $foreignKey);
// There should only be 2 values in foreignKey (table and col)
if(count($foreignKey) != 2)
{
throw new Exception("Invalid foreign key length: " . strlen($foreignKey));
}
else
{
$columnDefinition['foreignTable'] = $foreignKey[0];
$columnDefinition['foreignColumn'] = $foreignKey[1];
}
}
/* NAME READING */
$name = "";
// If there's only one space on the line at the same locaiton as the first one, assume a simple line like "uint ID", this can be better
if(strrpos($line, ' ') == strpos($line, ' '))
{
$name = substr($line, strpos($line, ' ') + 1);
}
else
{
// Location of first space (after type)
$start = strpos($line, ' ');
// Second space (after name)
$end = strpos($line, ' ', $start + 1) - $start - 1;
$name = substr($line, $start + 1, $end);
}
// If name ends in ? it's unverified
if ($name[strlen($name) - 1] == "?")
{
$columnDefinition['verified'] = false;
$name = str_replace("?", "", $name);
}
else
{
$columnDefinition['verified'] = true;
}
/* COMMENT READING */
if (strpos($line, "//") !== false)
{
$columnDefinition['comment'] = trim(substr($line, strpos($line, "//") + 2));
}
// Add to dictionary
if (array_key_exists($name, $dbd['columnDefinitions']))
{
throw new Exception("Collision with existing column name while adding new column name!");
}
else
{
$dbd['columnDefinitions'][$name] = $columnDefinition;
}
}
}else{
throw new Exception("File does not start with column definitions!");
}
$versionDefinitions = array();
$definitions = array();
$layoutHashes = array();
$comment = "";
$builds = array();
$buildRanges = array();
for($i = $lineNumber; $i < count($lines); $i++)
{
$line = $lines[$i];
if (empty($line))
{
$dbd['versionDefinitions'][] = array(
"builds" => $builds,
"buildRanges" => $buildRanges,
"layoutHashes" => $layoutHashes,
"comment" => $comment,
"definitions" => $definitions
);
$definitions = array();
$layoutHashes = array();
$comment = "";
$builds = array();
$buildRanges = array();
}
if (substr($line, 0, 6) == "LAYOUT")
{
$splitLayoutHashes = str_replace("LAYOUT ", "", $line);
$splitLayoutHashes = explode(", ", $splitLayoutHashes);
$layoutHashes = array_merge($layoutHashes, $splitLayoutHashes);
}
if (substr($line, 0, 5) == "BUILD")
{
$splitBuilds = str_replace("BUILD ", "", $line);
$splitBuilds = explode(", ", $splitBuilds);
foreach($splitBuilds as $splitBuild)
{
if (strpos($splitBuild, "-") !== false)
{
$splitRange = explode("-", $splitBuild);
$buildRanges[] = new BuildRange($splitRange[0], $splitRange[1]);
}
else{
$builds[] = new Build($splitBuild);
}
}
}
if (substr($line, 0, 7) == "COMMENT")
{
$comment = trim(str_replace($line, "COMMENT", ""));
}
if (substr($line, 0, 6) != "LAYOUT" && substr($line, 0, 5) != "BUILD" && substr($line, 0, 7) != "COMMENT" && !empty($line))
{
$definition = array();
// Default to everything being inline
$definition['isNonInline'] = false;
if (strpos($line, "$") !== false)
{
$annotationStart = strpos($line, "$");
$annotationEnd = strpos($line, "$", 1);
$annotations = substr($line, $annotationStart + 1, $annotationEnd - $annotationStart - 1);
$annotations = explode(",", $annotations);
if (in_array("id", $annotations))
{
$definition['isID'] = true;
}
if (in_array("noninline", $annotations))
{
$definition['isNonInline'] = true;
}
if (in_array("relation", $annotations))
{
$definition['isRelation'] = true;
}
$line = substr($line, $annotationEnd + 1);
}
if (strpos($line, "<") !== false)
{
$size = substr($line, strpos($line, '<') + 1, strpos($line, '>') - strpos($line, '<') - 1);
if ($size[0] == 'u')
{
$definition['isSigned'] = false;
$definition['size'] = (int)str_replace("u", "", $size);
$line = str_replace("<u" . $definition['size'] . ">", "", $line);
}
else
{
$definition['isSigned'] = true;
$definition['size'] = (int)$size;
$line = str_replace("<" . $definition['size'] . ">", "", $line);
}
}
if (strpos($line, "[") !== false)
{
$definition['arrLength'] = substr($line, strpos($line, '[') + 1, strpos($line, ']') - strpos($line, '[') - 1);
$line = str_replace("[" . $definition['arrLength'] . "]", "", $line);
}
if (strpos($line, "//") !== false)
{
$definition['comment'] = trim(substr($line, strpos($line, "//") + 2));
$line = substr($line, 0, strpos($line, "//"));
}
$definition['name'] = trim($line);
// Check if this column name is known in column definitions, if not throw exception
if (!array_key_exists($definition['name'], $dbd['columnDefinitions']))
{
throw new Exception("Unable to find " . $definition['name'] . " (".basename($file).") in column definitions: " . print_r($dbd['columnDefinitions'], true));
}
$definitions[] = $definition;
}
if (count($lines) == ($i + 1) && !empty($definitions))
{
$dbd['versionDefinitions'][] = array(
"builds" => $builds,
"buildRanges" => $buildRanges,
"layoutHashes" => $layoutHashes,
"comment" => $comment,
"definitions" => $definitions
);
}
}
return $dbd;
}
}
?>