|
1
|
|
|
<?php |
|
2
|
|
|
require_once(dirname(__FILE__).'/../support/IReader.php'); |
|
3
|
|
|
require_once(dirname(__FILE__).'/../support/StringReader.php'); |
|
4
|
|
|
require_once(dirname(__FILE__).'/COB/AgentBlock.php'); |
|
5
|
|
|
require_once(dirname(__FILE__).'/COB/FileBlock.php'); |
|
6
|
|
|
require_once(dirname(__FILE__).'/COB/AuthorBlock.php'); |
|
7
|
|
|
require_once(dirname(__FILE__).'/COB/UnknownBlock.php'); |
|
8
|
|
|
|
|
9
|
|
|
///@{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @name C1 format cob |
|
12
|
|
|
* Value: C1 |
|
13
|
|
|
*/ |
|
14
|
|
|
define('COB_FORMAT_C1', 'C1'); |
|
15
|
|
|
/** |
|
16
|
|
|
* @name C2 format COB |
|
17
|
|
|
* Value: C2 |
|
18
|
|
|
*/ |
|
19
|
|
|
define('COB_FORMAT_C2', 'C2'); |
|
20
|
|
|
///@} |
|
21
|
|
|
|
|
22
|
|
|
/// @brief Class that interacts with COB files (c1 and c2 formats) |
|
23
|
|
|
class COB { |
|
24
|
|
|
|
|
25
|
|
|
/// @cond INTERNAL_DOCS |
|
26
|
|
|
|
|
27
|
|
|
private $format; |
|
28
|
|
|
private $blocks; |
|
29
|
|
|
|
|
30
|
|
|
/// @endcond |
|
31
|
|
|
|
|
32
|
|
|
/// @brief Creates a new COB file |
|
33
|
|
|
/** |
|
34
|
|
|
* If you want to create a COB file from scratch, simply don't |
|
35
|
|
|
* pass anything to the constructor. \n\n |
|
36
|
|
|
* Alternatively, if you know which kind of COB file you are |
|
37
|
|
|
* reading, or only want to deal with a specific kind of COB |
|
38
|
|
|
* file, you can call the LoadC1COB and LoadC2COB functions |
|
39
|
|
|
* after creating a blank cob file. E.g. ($reader is a IReader) \n\n |
|
40
|
|
|
* $cob = new COB; \n |
|
41
|
|
|
* $cob->LoadC1COB($reader); \n |
|
42
|
|
|
* This code will only parse C1 cobs. |
|
43
|
|
|
* @param $reader The reader which contains the cob to read from. Can be null. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function COB(IReader $reader = null) { |
|
46
|
|
|
if ($reader != null) { |
|
47
|
|
|
$this->LoadCOB($reader); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/// @cond INTERNAL_DOCS |
|
52
|
|
|
|
|
53
|
|
|
/// @brief Loads the COB from the IReader. |
|
54
|
|
|
/** |
|
55
|
|
|
* Used internally, this function is not for the general public! \n |
|
56
|
|
|
* This function first identifies which type of COB is in the IReader |
|
57
|
|
|
* Then decompresses if necessary, then calls LoadC1COB or LoadC2COB. |
|
58
|
|
|
* @param $reader The reader to read from |
|
59
|
|
|
*/ |
|
60
|
|
|
private function LoadCOB(IReader $reader) { |
|
61
|
|
|
if ($reader->Read(4) == 'cob2') { |
|
62
|
|
|
$reader->Seek(0); |
|
63
|
|
|
$this->LoadC2COB($reader); |
|
64
|
|
|
} else { |
|
65
|
|
|
$string = $reader->GetSubString(0); |
|
66
|
|
|
$data = @gzuncompress($string); |
|
67
|
|
|
if ($data === false) { |
|
68
|
|
|
$reader->Seek(0); |
|
69
|
|
|
$this->LoadC1COB($reader); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->LoadC2COB(new StringReader($data)); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/// @endcond |
|
77
|
|
|
|
|
78
|
|
|
/// @brief Loads a C2 COB from the IReader given |
|
79
|
|
|
/** |
|
80
|
|
|
* C2 COBs have multiple blocks, which are accessible via the |
|
81
|
|
|
* COB::GetBlocks function. |
|
82
|
|
|
* @param $reader The IReader to load from |
|
83
|
|
|
*/ |
|
84
|
|
|
public function LoadC2COB(IReader $reader) { |
|
85
|
|
|
$this->format = COB_FORMAT_C2; |
|
86
|
|
|
if ($reader->Read(4) == 'cob2') { |
|
87
|
|
|
while ($block = $this->ReadBlock($reader)) { |
|
88
|
|
|
$this->blocks[] = $block; |
|
89
|
|
|
} |
|
90
|
|
|
} else { |
|
91
|
|
|
throw new Exception('Not a valid C2 COB file!'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/// @brief Loads a C1 COB from the specified reader |
|
96
|
|
|
/** |
|
97
|
|
|
* C1 COBs have just one block, which is a COBAgentBlock. |
|
98
|
|
|
* This is accessible by calling COB::GetBlocks |
|
99
|
|
|
* @param $reader the reader to load from |
|
100
|
|
|
*/ |
|
101
|
|
|
public function LoadC1COB(IReader $reader) { |
|
102
|
|
|
$this->format = COB_FORMAT_C1; |
|
103
|
|
|
$version = $reader->ReadInt(2); |
|
104
|
|
|
if ($version > 4) { |
|
105
|
|
|
throw new Exception('Invalid cob file.'); |
|
106
|
|
|
} else { |
|
107
|
|
|
$this->blocks[] = COBAgentBlock::CreateFromReaderC1($reader); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
/// @brief Adds a COBBlock to this COB |
|
111
|
|
|
/** |
|
112
|
|
|
* @param $block the block to add. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function AddBlock(COBBlock $block) { |
|
115
|
|
|
//TODO: Check that this block works for this COB type? |
|
116
|
|
|
$this->blocks[] = $block; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/// @cond INTERNAL_DOCS |
|
120
|
|
|
|
|
121
|
|
|
/// @brief Underlying block reader used by C2 COBs |
|
122
|
|
|
/** |
|
123
|
|
|
* Reads a block from the specified reader, then instanitates |
|
124
|
|
|
* a representative COBBlock, and returns it. |
|
125
|
|
|
*/ |
|
126
|
|
|
private function ReadBlock(IReader $reader) { |
|
127
|
|
|
if (!($type = $reader->Read(4))) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
$size = $reader->ReadInt(4); |
|
131
|
|
|
switch ($type) { |
|
132
|
|
|
case 'agnt': |
|
133
|
|
|
//we read the entire thing so that if there are errors we can still go on with the other blocks. |
|
134
|
|
|
return COBAgentBlock::CreateFromReaderC2($reader); |
|
135
|
|
|
break; |
|
|
|
|
|
|
136
|
|
|
case 'auth': |
|
137
|
|
|
return COBAuthorBlock::CreateFromReader(new StringReader($reader->Read($size))); |
|
138
|
|
|
break; |
|
|
|
|
|
|
139
|
|
|
case 'file': |
|
140
|
|
|
return COBFileBlock::CreateFromReader(new StringReader($reader->Read($size))); |
|
141
|
|
|
break; |
|
|
|
|
|
|
142
|
|
|
default: |
|
143
|
|
|
//throw new Exception('Invalid block type: Probably a bug or an invalid COB file: '.$type); |
|
|
|
|
|
|
144
|
|
|
//simply ignore unknown block types, in case people add their own |
|
145
|
|
|
return new COBUnknownBlock($type, $reader->Read($size)); |
|
146
|
|
|
break; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/// @endcond |
|
151
|
|
|
|
|
152
|
|
|
/// @brief Accessor method to get blocks of the given type |
|
153
|
|
|
/** |
|
154
|
|
|
* If $type is false, will return all blocks in this agent. \n |
|
155
|
|
|
* In a C1 COB, there is only one block and it is of the agnt |
|
156
|
|
|
* type. |
|
157
|
|
|
* @param $type The type of block to get (agnt, auth, file). False by default. |
|
158
|
|
|
* @return An array of COBBlocks. |
|
159
|
|
|
*/ |
|
160
|
|
|
public function GetBlocks($type = false) { |
|
161
|
|
|
$blocks = array(); |
|
162
|
|
|
foreach ($this->blocks as $block) { |
|
163
|
|
|
if ($type === false || $type == $block->GetType()) { |
|
164
|
|
|
$blocks[] = $block; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
return $blocks; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/// @brief Compiles the COB in the given format |
|
171
|
|
|
/** |
|
172
|
|
|
* @param $format The format of the COB. If null, assumed it's a creatures 2 COB |
|
173
|
|
|
* @return A binary string containing the COB. |
|
174
|
|
|
*/ |
|
175
|
|
|
public function Compile($format = null) { |
|
176
|
|
|
if ($format == null) { |
|
177
|
|
|
$format = $this->GetType(); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
if ($format != FORMAT_C1) { |
|
180
|
|
|
$format = FORMAT_C2; |
|
181
|
|
|
} |
|
182
|
|
|
switch ($format) { |
|
183
|
|
|
case FORMAT_C1: |
|
184
|
|
|
$this->CompileC1(); |
|
185
|
|
|
case FORMAT_C2: |
|
186
|
|
|
$this->CompileC2(); |
|
187
|
|
|
default: |
|
188
|
|
|
throw new Exception('Non-understood COB format - sorry!'); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/// @brief Compiles to a C1 COB. <b>Unimplemented</b> |
|
193
|
|
|
// TODO: implement this. |
|
194
|
|
|
public function CompileC1() { |
|
195
|
|
|
throw new Exception('C1 COB Compilation not yet ready.'); |
|
196
|
|
|
} |
|
197
|
|
|
/// @brief Compiles a C2 COB. <b>May not actually work.</b> |
|
198
|
|
|
// TODO: Check accuracy |
|
199
|
|
|
public function CompileC2() { |
|
200
|
|
|
$data = 'cob2'; |
|
201
|
|
|
foreach ($this->blocks as $block) { |
|
202
|
|
|
$data .= $block->Compile(); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
?> |
|
|
|
|
|
|
207
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.