|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of GameQ. |
|
4
|
|
|
* |
|
5
|
|
|
* GameQ is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* GameQ is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Lesser General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace GameQ\Protocols; |
|
20
|
|
|
|
|
21
|
|
|
use GameQ\Protocol; |
|
22
|
|
|
use GameQ\Buffer; |
|
23
|
|
|
use GameQ\Result; |
|
24
|
|
|
use \GameQ\Exception\Protocol as Exception; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* GameSpy Protocol class |
|
28
|
|
|
* |
|
29
|
|
|
* @author Austin Bischoff <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class Gamespy extends Protocol |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Array of packets we want to look up. |
|
36
|
|
|
* Each key should correspond to a defined method in this or a parent class |
|
37
|
|
|
* |
|
38
|
|
|
* @type array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $packets = [ |
|
41
|
|
|
self::PACKET_STATUS => "\x5C\x73\x74\x61\x74\x75\x73\x5C", |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The query protocol used to make the call |
|
46
|
|
|
* |
|
47
|
|
|
* @type string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $protocol = 'gamespy'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* String name of this protocol class |
|
53
|
|
|
* |
|
54
|
|
|
* @type string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $name = 'gamespy'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Longer string name of this protocol class |
|
60
|
|
|
* |
|
61
|
|
|
* @type string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $name_long = "GameSpy Server"; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The client join link |
|
67
|
|
|
* |
|
68
|
|
|
* @type string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $join_link = null; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Process the response for this protocol |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
* @throws Exception |
|
77
|
|
|
*/ |
|
78
|
5 |
|
public function processResponse() |
|
79
|
|
|
{ |
|
80
|
|
|
// Holds the processed packets so we can sort them in case they come in an unordered |
|
81
|
5 |
|
$processed = []; |
|
82
|
|
|
|
|
83
|
|
|
// Iterate over the packets |
|
84
|
5 |
|
foreach ($this->packets_response as $response) { |
|
85
|
|
|
// Check to see if we had a preg_match error |
|
86
|
5 |
|
if (($match = preg_match("#^(.*)\\\\queryid\\\\([^\\\\]+)(\\\\|$)#", $response, $matches)) === false |
|
87
|
5 |
|
|| $match != 1 |
|
88
|
5 |
|
) { |
|
89
|
2 |
|
throw new Exception(__METHOD__ . " An error occurred while parsing the packets for 'queryid'"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Multiply so we move the decimal point out of the way, if there is one |
|
93
|
3 |
|
$key = (int)(floatval($matches[2]) * 1000); |
|
94
|
|
|
|
|
95
|
|
|
// Add this packet to the processed |
|
96
|
3 |
|
$processed[$key] = $matches[1]; |
|
97
|
3 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Sort the new array to make sure the keys (query ids) are in the proper order |
|
100
|
3 |
|
ksort($processed, SORT_NUMERIC); |
|
101
|
|
|
|
|
102
|
|
|
// Create buffer and offload processing |
|
103
|
3 |
|
return $this->processStatus(new Buffer(implode('', $processed))); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/* |
|
107
|
|
|
* Internal methods |
|
108
|
|
|
*/ |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Handle processing the status buffer |
|
112
|
|
|
* |
|
113
|
|
|
* @param Buffer $buffer |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
3 |
|
protected function processStatus(Buffer $buffer) |
|
118
|
|
|
{ |
|
119
|
|
|
// Set the result to a new result instance |
|
120
|
3 |
|
$result = new Result(); |
|
121
|
|
|
|
|
122
|
|
|
// By default dedicted |
|
123
|
3 |
|
$result->add('dedicated', true); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
// Lets peek and see if the data starts with a \ |
|
126
|
3 |
|
if ($buffer->lookAhead(1) == '\\') { |
|
127
|
|
|
// Burn the first one |
|
128
|
3 |
|
$buffer->skip(1); |
|
129
|
3 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Explode the data |
|
132
|
3 |
|
$data = explode('\\', $buffer->getBuffer()); |
|
133
|
|
|
|
|
134
|
|
|
// No longer needed |
|
135
|
3 |
|
unset($buffer); |
|
136
|
|
|
|
|
137
|
|
|
// Init some vars |
|
138
|
3 |
|
$numPlayers = 0; |
|
139
|
3 |
|
$numTeams = 0; |
|
140
|
|
|
|
|
141
|
|
|
// Now lets loop the array |
|
142
|
3 |
|
for ($x = 0; $x < count($data); $x += 2) { |
|
|
|
|
|
|
143
|
|
|
// Set some local vars |
|
144
|
3 |
|
$key = $data[$x]; |
|
145
|
3 |
|
$val = $data[$x + 1]; |
|
146
|
|
|
|
|
147
|
|
|
// Check for <variable>_<count> variable (i.e players) |
|
148
|
3 |
|
if (($suffix = strrpos($key, '_')) !== false && is_numeric(substr($key, $suffix + 1))) { |
|
149
|
|
|
// See if this is a team designation |
|
150
|
2 |
|
if (substr($key, 0, $suffix) == 'teamname') { |
|
151
|
|
|
$result->addTeam('teamname', $val); |
|
152
|
|
|
$numTeams++; |
|
153
|
|
|
} else { |
|
154
|
|
|
// Its a player |
|
155
|
2 |
|
if (substr($key, 0, $suffix) == 'playername') { |
|
156
|
|
|
$numPlayers++; |
|
157
|
|
|
} |
|
158
|
2 |
|
$result->addPlayer(substr($key, 0, $suffix), utf8_encode($val)); |
|
159
|
|
|
} |
|
160
|
2 |
|
} else { |
|
161
|
|
|
// Regular variable so just add the value. |
|
162
|
3 |
|
$result->add($key, $val); |
|
163
|
|
|
} |
|
164
|
3 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Add the player and team count |
|
167
|
3 |
|
$result->add('num_players', $numPlayers); |
|
168
|
3 |
|
$result->add('num_teams', $numTeams); |
|
169
|
|
|
|
|
170
|
|
|
// Unset some stuff to free up memory |
|
171
|
3 |
|
unset($data, $key, $val, $suffix, $x); |
|
172
|
|
|
|
|
173
|
|
|
// Return the result |
|
174
|
3 |
|
return $result->fetch(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: