Passed
Push — master ( 96b3cc...8ff2ce )
by smiley
01:25
created

LTEXReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 25
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 24 3
A init() 0 15 2
1
<?php
2
/**
3
 * Class LTEXReader
4
 *
5
 * @filesource   LTEXReader.php
6
 * @created      05.01.2019
7
 * @package      codemasher\WildstarDB
8
 * @author       smiley <[email protected]>
9
 * @copyright    2019 smiley
10
 * @license      MIT
11
 */
12
13
namespace codemasher\WildstarDB;
14
15
class LTEXReader extends ReaderAbstract{
16
17
	protected $FORMAT_HEADER = 'a4Signature/LVersion/LLanguage/LLCID/QTagNameStringLength/QTagNameStringPtr/QShortNameStringLength/QShortNameStringPtr/QLongNameStringLength/QLongNameStringPtr/QEntryCount/QEntryIndexPtr/QNameStoreLength/QNameStorePtr';
18
19
	/**
20
	 * @return void
21
	 * @throws \codemasher\WildstarDB\WSDBException
22
	 */
23
	protected function init():void{
24
25
		if($this->header['Signature'] !== "\x58\x45\x54\x4c"){ // XETL
26
			throw new WSDBException('invalid LTEX');
27
		}
28
29
		fseek($this->fh, 0x60 + $this->header['LongNameStringPtr']);
30
31
		$this->name = $this->decodeString(fread($this->fh, $this->header['LongNameStringLength'] * 2));
32
		$this->cols = [
33
			['name' => 'ID',            'header' => ['DataType' =>   3]],
34
			['name' => 'LocalizedText', 'header' => ['DataType' => 130]],
35
		];
36
37
		$this->logger->info($this->name.', rows: '.$this->header['EntryCount']);
38
	}
39
40
	/**
41
	 * @param string $filename
42
	 *
43
	 * @return \codemasher\WildstarDB\ReaderInterface
44
	 */
45
	public function read(string $filename):ReaderInterface{
46
		$this->loadFile($filename);
47
		$this->init();
48
49
		fseek($this->fh, 0x60 + $this->header['EntryIndexPtr']);
50
51
		for($i = 0; $i < $this->header['EntryCount']; $i++){
52
			$a = unpack('Lid/Lpos', fread($this->fh, 8));
53
			$p = ftell($this->fh);
54
			$v = '';
55
56
			fseek($this->fh, 0x60 + $this->header['NameStorePtr'] + ($a['pos'] * 2));
57
58
			do{
59
				$s = fread($this->fh, 2);
60
				$v .= $s;
61
			}
62
			while($s !== "\x00\x00" && $s !== '');
63
64
			$this->data[$i] = ['ID' => $a['id'], 'LocalizedText' => $this->decodeString($v)];
65
			fseek($this->fh, $p);
66
		}
67
68
		return $this;
69
	}
70
71
}
72