@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * @property array $cols |
| 26 | 26 | * @property array $data |
| 27 | 27 | */ |
| 28 | -class DTBLReader{ |
|
| 28 | +class DTBLReader { |
|
| 29 | 29 | use LoggerAwareTrait; |
| 30 | 30 | |
| 31 | 31 | protected const FORMAT_HEADER = 'a4Signature/LVersion/QTableNameLength/QUnknown1/QRecordSize/QFieldCount/QDescriptionOffset/QRecordCount/QFullRecordSize/QEntryOffset/QNextId/QIDLookupOffset/QUnknown2'; |
@@ -68,9 +68,9 @@ discard block |
||
| 68 | 68 | * |
| 69 | 69 | * @throws \codemasher\WildstarDB\WSDBException |
| 70 | 70 | */ |
| 71 | - public function __construct(LoggerInterface $logger = null){ |
|
| 71 | + public function __construct(LoggerInterface $logger = null) { |
|
| 72 | 72 | |
| 73 | - if(PHP_INT_SIZE < 8){ |
|
| 73 | + if (PHP_INT_SIZE < 8) { |
|
| 74 | 74 | throw new WSDBException('64-bit PHP required'); |
| 75 | 75 | } |
| 76 | 76 | |
@@ -80,9 +80,9 @@ discard block |
||
| 80 | 80 | /** |
| 81 | 81 | * @return void |
| 82 | 82 | */ |
| 83 | - public function __destruct(){ |
|
| 83 | + public function __destruct() { |
|
| 84 | 84 | |
| 85 | - if(is_resource($this->fh)){ |
|
| 85 | + if (is_resource($this->fh)) { |
|
| 86 | 86 | fclose($this->fh); |
| 87 | 87 | } |
| 88 | 88 | |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | * |
| 94 | 94 | * @return mixed|null |
| 95 | 95 | */ |
| 96 | - public function __get(string $name){ |
|
| 96 | + public function __get(string $name) { |
|
| 97 | 97 | return property_exists($this, $name) && $name !== 'fh' ? $this->{$name} : null; |
| 98 | 98 | } |
| 99 | 99 | |
@@ -118,13 +118,13 @@ discard block |
||
| 118 | 118 | $this->cols = []; |
| 119 | 119 | $this->data = []; |
| 120 | 120 | |
| 121 | - if(strlen($header) !== 0x60){ |
|
| 121 | + if (strlen($header) !== 0x60) { |
|
| 122 | 122 | throw new WSDBException('cannot read DTBL header'); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | $this->header = unpack($this::FORMAT_HEADER, $header); |
| 126 | 126 | |
| 127 | - if($this->header['Signature'] !== "\x4c\x42\x54\x44"){ // LBTD |
|
| 127 | + if ($this->header['Signature'] !== "\x4c\x42\x54\x44") { // LBTD |
|
| 128 | 128 | throw new WSDBException('invalid DTBL'); |
| 129 | 129 | } |
| 130 | 130 | |
@@ -134,17 +134,17 @@ discard block |
||
| 134 | 134 | |
| 135 | 135 | fseek($this->fh, $this->header['DescriptionOffset'] + 0x60); |
| 136 | 136 | |
| 137 | - for($i = 0; $i < $this->header['FieldCount']; $i++){ |
|
| 137 | + for ($i = 0; $i < $this->header['FieldCount']; $i++) { |
|
| 138 | 138 | $this->cols[$i]['header'] = unpack($this::FORMAT_COLUMN, fread($this->fh, 0x18)); |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | $offset = $this->header['FieldCount'] * 0x18 + $this->header['DescriptionOffset'] + 0x60; |
| 142 | 142 | |
| 143 | - if($this->header['FieldCount'] % 2){ |
|
| 143 | + if ($this->header['FieldCount'] % 2) { |
|
| 144 | 144 | $offset += 8; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - foreach($this->cols as $i => $col){ |
|
| 147 | + foreach ($this->cols as $i => $col) { |
|
| 148 | 148 | fseek($this->fh, $offset + $col['header']['NameOffset']); |
| 149 | 149 | |
| 150 | 150 | $this->cols[$i]['name'] = $this->decodeString(fread($this->fh, $col['header']['NameLength'] * 2)); |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | */ |
| 161 | 161 | public function read(string $dtbl):DTBLReader{ |
| 162 | 162 | |
| 163 | - if(!is_file($dtbl) || !is_readable($dtbl)){ |
|
| 163 | + if (!is_file($dtbl) || !is_readable($dtbl)) { |
|
| 164 | 164 | throw new WSDBException('DTBL not readable'); |
| 165 | 165 | } |
| 166 | 166 | |
@@ -172,19 +172,19 @@ discard block |
||
| 172 | 172 | |
| 173 | 173 | fseek($this->fh, $offset); |
| 174 | 174 | |
| 175 | - for($i = 0; $i < $this->header['RecordCount']; $i++){ |
|
| 175 | + for ($i = 0; $i < $this->header['RecordCount']; $i++) { |
|
| 176 | 176 | $data = fread($this->fh, $this->header['RecordSize']); |
| 177 | 177 | $row = []; |
| 178 | 178 | $j = 0; |
| 179 | 179 | $skip = false; |
| 180 | 180 | |
| 181 | - foreach($this->cols as $c => $col){ |
|
| 181 | + foreach ($this->cols as $c => $col) { |
|
| 182 | 182 | |
| 183 | - if($skip === true && ($c > 0 && $this->cols[$c - 1]['header']['DataType'] === 130) && $col['header']['DataType'] !== 130){ |
|
| 183 | + if ($skip === true && ($c > 0 && $this->cols[$c - 1]['header']['DataType'] === 130) && $col['header']['DataType'] !== 130) { |
|
| 184 | 184 | $j += 4; |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - switch($col['header']['DataType']){ |
|
| 187 | + switch ($col['header']['DataType']) { |
|
| 188 | 188 | case 3: // uint32 |
| 189 | 189 | case 11: // booleans (stored as uint32 0/1) |
| 190 | 190 | $v = unpack('L', substr($data, $j, 4))[1]; $j += 4; break; |
@@ -203,12 +203,12 @@ discard block |
||
| 203 | 203 | $j += 8; |
| 204 | 204 | $v = ''; |
| 205 | 205 | |
| 206 | - do{ |
|
| 206 | + do { |
|
| 207 | 207 | $s = fread($this->fh, 2); |
| 208 | 208 | |
| 209 | 209 | $v .= $s; |
| 210 | 210 | } |
| 211 | - while($s !== "\x00\x00" && $s !== ''); |
|
| 211 | + while ($s !== "\x00\x00" && $s !== ''); |
|
| 212 | 212 | |
| 213 | 213 | $v = $this->decodeString($v); |
| 214 | 214 | fseek($this->fh, $p); |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | $row[$col['name']] = $v; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - if(count($row) !== $this->header['FieldCount']){ |
|
| 224 | + if (count($row) !== $this->header['FieldCount']) { |
|
| 225 | 225 | throw new WSDBException('invalid field count'); |
| 226 | 226 | } |
| 227 | 227 | |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | |
| 231 | 231 | fclose($this->fh); |
| 232 | 232 | |
| 233 | - if(count($this->data) !== $this->header['RecordCount']){ |
|
| 233 | + if (count($this->data) !== $this->header['RecordCount']) { |
|
| 234 | 234 | throw new WSDBException('invalid row count'); |
| 235 | 235 | } |
| 236 | 236 | |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | protected function checkData():void{ |
| 245 | 245 | |
| 246 | - if(empty($this->data)){ |
|
| 246 | + if (empty($this->data)) { |
|
| 247 | 247 | throw new WSDBException('empty data, run DTBLReader::read() first'); |
| 248 | 248 | } |
| 249 | 249 | |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | */ |
| 259 | 259 | protected function saveToFile(string $data, string $file):bool{ |
| 260 | 260 | |
| 261 | - if(!is_writable(dirname($file))){ |
|
| 261 | + if (!is_writable(dirname($file))) { |
|
| 262 | 262 | throw new WSDBException('cannot write data to file: '.$file.', target directory is not writable'); |
| 263 | 263 | } |
| 264 | 264 | |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | |
| 277 | 277 | $json = json_encode($this->data, $jsonOptions); |
| 278 | 278 | |
| 279 | - if($file !== null){ |
|
| 279 | + if ($file !== null) { |
|
| 280 | 280 | $this->saveToFile($json, $file); |
| 281 | 281 | } |
| 282 | 282 | |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | |
| 299 | 299 | fputcsv($mh, array_column($this->cols, 'name'), $delimiter, $enclosure, $escapeChar); |
| 300 | 300 | |
| 301 | - foreach($this->data as $row){ |
|
| 301 | + foreach ($this->data as $row) { |
|
| 302 | 302 | fputcsv($mh, array_values($row), $delimiter, $enclosure, $escapeChar); |
| 303 | 303 | } |
| 304 | 304 | |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | |
| 309 | 309 | fclose($mh); |
| 310 | 310 | |
| 311 | - if($file !== null){ |
|
| 311 | + if ($file !== null) { |
|
| 312 | 312 | $this->saveToFile($csv, $file); |
| 313 | 313 | } |
| 314 | 314 | |
@@ -329,10 +329,10 @@ discard block |
||
| 329 | 329 | |
| 330 | 330 | $types = [3 => 'uint32', 4 => 'float', 11 => 'bool', 20 => 'uint64', 130 => 'string']; |
| 331 | 331 | |
| 332 | - foreach($this->data as $row){ |
|
| 332 | + foreach ($this->data as $row) { |
|
| 333 | 333 | $item = $sxe->addChild('item'); |
| 334 | 334 | |
| 335 | - foreach(array_values($row) as $i => $value){ |
|
| 335 | + foreach (array_values($row) as $i => $value) { |
|
| 336 | 336 | $item |
| 337 | 337 | ->addChild($this->cols[$i]['name'], $value) |
| 338 | 338 | ->addAttribute('dataType', $types[$this->cols[$i]['header']['DataType']]); |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | |
| 343 | 343 | $xml = $sxe->asXML(); |
| 344 | 344 | |
| 345 | - if($file !== null){ |
|
| 345 | + if ($file !== null) { |
|
| 346 | 346 | $this->saveToFile($xml, $file); |
| 347 | 347 | } |
| 348 | 348 | |
@@ -361,9 +361,9 @@ discard block |
||
| 361 | 361 | ->ifNotExists() |
| 362 | 362 | ; |
| 363 | 363 | |
| 364 | - foreach($this->cols as $i => $col){ |
|
| 364 | + foreach ($this->cols as $i => $col) { |
|
| 365 | 365 | |
| 366 | - switch($col['header']['DataType']){ |
|
| 366 | + switch ($col['header']['DataType']) { |
|
| 367 | 367 | case 3: $createTable->int($col['name'], 10, null, null, 'UNSIGNED'); break; |
| 368 | 368 | case 4: $createTable->decimal($col['name'], '7,3', 0); break; |
| 369 | 369 | case 11: $createTable->field($col['name'], 'BOOLEAN'); break; |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | |
| 376 | 376 | $createTable->query(); |
| 377 | 377 | |
| 378 | - if(count($this->data) < 1){ |
|
| 378 | + if (count($this->data) < 1) { |
|
| 379 | 379 | $this->logger->notice('no records available for table '.$this->name); |
| 380 | 380 | return; |
| 381 | 381 | } |