PACKReaderAbstract::read()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class PACKReaderAbstract
4
 *
5
 * @filesource   PACKReaderAbstract.php
6
 * @created      27.04.2019
7
 * @package      codemasher\WildstarDB\Archive
8
 * @author       smiley <[email protected]>
9
 * @copyright    2019 smiley
10
 * @license      MIT
11
 */
12
13
namespace codemasher\WildstarDB\Archive;
14
15
use chillerlan\Database\Database;
16
use codemasher\WildstarDB\WSDBException;
17
18
use function fread, fseek, unpack;
19
20
/**
21
 * @property array $blocktable
22
 */
23
abstract class PACKReaderAbstract extends ReaderAbstract{
24
25
	/**
26
	 * 4+4+512+8+8+8+4+4+4+8 = 564 bytes
27
	 *
28
	 * @var string
29
	 * @internal
30
	 */
31
	protected $FORMAT_HEADER = 'a4Signature/LVersion/x512/QFilesize/x8/QBlockTableOffset/LBlockCount/x4/LRootInfoIndex/x8';
32
33
	/**
34
	 * @var int
35
	 * @internal
36
	 */
37
	protected $headerSize = 564;
38
39
	/**
40
	 * @var array
41
	 */
42
	protected $blocktable = [];
43
44
	/**
45
	 * @return void
46
	 * @throws \codemasher\WildstarDB\WSDBException
47
	 */
48
	abstract protected function readData():void;
49
50
	/**
51
	 * @param string $filename
52
	 *
53
	 * @return \codemasher\WildstarDB\Archive\ReaderInterface
54
	 * @throws \codemasher\WildstarDB\WSDBException
55
	 */
56
	public function read(string $filename):ReaderInterface{
57
		$this->loadFile($filename);
58
		$this->blocktable = [];
59
60
		if($this->header['Signature'] !== "\x4b\x43\x41\x50"){ // KCAP
61
			throw new WSDBException('invalid PACK');
62
		}
63
64
		// read the block info table
65
		fseek($this->fh, $this->header['BlockTableOffset']);
66
67
		for($i = 0; $i < $this->header['BlockCount']; $i++){
68
			$this->blocktable[$i] = unpack('QOffset/QSize', fread($this->fh, 16));
69
		}
70
71
		// seek forward to the root index block for convenience
72
		fseek($this->fh, $this->blocktable[$this->header['RootInfoIndex']]['Offset']);
73
74
		$this->readData();
75
76
		return $this;
77
	}
78
79
	/**
80
	 * @param string|null $file
81
	 * @param string      $delimiter
82
	 * @param string      $enclosure
83
	 * @param string      $escapeChar
84
	 *
85
	 * @return string
86
	 * @throws \codemasher\WildstarDB\WSDBException
87
	 */
88
	public function toCSV(string $file = null, string $delimiter = ',', string $enclosure = '"', string $escapeChar = '\\'):string{
89
		// @todo
90
		throw new WSDBException('not implemented');
91
92
#		return '';
93
	}
94
95
	/**
96
	 * ugh!
97
	 *
98
	 * @param string|null $file
99
	 *
100
	 * @return string
101
	 * @throws \codemasher\WildstarDB\WSDBException
102
	 */
103
	public function toXML(string $file = null):string{
104
		// @todo
105
		throw new WSDBException('not implemented');
106
107
#		return '';
108
	}
109
110
	/**
111
	 * @param \chillerlan\Database\Database $db
112
	 *
113
	 * @return \codemasher\WildstarDB\Archive\ReaderInterface
114
	 * @throws \codemasher\WildstarDB\WSDBException
115
	 */
116
	public function toDB(Database $db):ReaderInterface{
117
		// @todo
118
		throw new WSDBException('not implemented');
119
120
#		return $this;
121
	}
122
123
}
124