-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_indy_ini.php
322 lines (299 loc) · 8.18 KB
/
make_indy_ini.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
$debug = false;
$offset=0x22b;
$length = 15;
$fh = fopen($argv[1], 'rb');
$offsets = array(
0x68, 0x149
);
$lengths = array(
$length * $length, $length * $length
);
$output = array(
'', ''
);
foreach($offsets as $i => $v) {
fseek($fh, $v);
$output[$i] = fread($fh, $lengths[$i]);
}
fseek($fh, $offset);
/*
* 0x434a06 header
* reveal text
* 0x05
* check txt
* 0x0d
* reveal letter text
* 0x00
* 0x2f
* Congrats text
* 0x02
* 0x0f - width 0x0f = 15, 0x0d = 13 - both grid and clue must fit
* 0x0f - height
* 0x01
* 0x17
* 0x00
* 0x00000000
* 0x00000000
* 0x00000000
* 0x00c0c0c0
* 0x00ffff00
* Grid layout :
* From 0x68 for width*height, ? is a space and # is a block
* Clue layout :
* From 0x68 + width * height for width * height,
* bitmasks!
* 00000000 empty (white or black determined by grid)
* 1 clue start
* 1 separator left
* 1 separator top
* 1 make it come out green!
* 1 white again
* 1 circle!
* 1 Won't load
* 1 white
* 0x01
* answers
* Fills out the ? in grid layout from left to right, top to bottom
* 0x0200ff00
* 0x000b0103
* 0x000000c8
* 0x00000005
* 0xnum bytes to A of Across (includes 3x 0x2e)
*
* Clue format : 2 byte header (checksum?)
* 1 byte number length BCD
* number
* 2 byte clue length BCD
* clue
* Some bytes between across and down.
*/
$answers=0;
$setter=1;
$clues=2;
$data = array(
'',
'',
array(),
);
$pointer=0;
$clue = '';
$across = true;
$acrossClues = array();
$downClues = array();
$multis = array();
while (false !== ($b = fgetc($fh))) {
if ($pointer == $answers) {
if (bin2hex($b) == '02') {
//skip 15
fread($fh, 15);
$pointer = $setter;
continue;
}
}
if ($pointer == $setter) {
$setterLength = hexdec(bin2hex($b));
fread($fh, $setterLength+3);
$b1 = fgetc($fh);
if ($debug)echo 'b1 : ' . hexdec(bin2hex($b1)) . PHP_EOL;
if (bin2hex($b1) == '0b') {
if($debug)echo 'skipping three times';
fgetc($fh);
fgetc($fh);
fgetc($fh);
}
$pointer = $clues;
continue;
}
if ($pointer == $clues) {
if (bin2hex($b) == '04') {
if (fgetc($fh) == 'D') {
//Down
$across = false;
fread($fh, 8);
} else {
if($debug)echo ftell($fh) . PHP_EOL;
fseek($fh, -1, SEEK_CUR);
if($debug)echo ftell($fh) . PHP_EOL;
}
}
if($debug)echo 'b : ' . hexdec(bin2hex($b)) . PHP_EOL;
if (bin2hex($b) == '8b'
|| bin2hex($b) == '81') {
if ($debug) echo 'skipping three times';
fgetc($fh);
fgetc($fh);
$b = fgetc($fh);
}
$currentClue = array();
$currentClue['header'] = $b . fgetc($fh);
$currentClue['numLength'] = fgetc($fh);
$currentClue['num'] = fread($fh, hexdec(bin2hex($currentClue['numLength'])));
$currentClue['clueLength'] = fread($fh, 2);
$currentClue['clue'] = fread($fh, hexdec(bin2hex($currentClue['clueLength'])));
if (strpos($currentClue['num'], '/') !== false) {
$tmp = explode('/', $currentClue['num']);
$key = array_shift($tmp);
foreach($tmp as $k) {
$multis[$k.'-'. (($across) ? 'across' : 'down')] = $key.'-'. (($across) ? 'across' : 'down');
}
}
if ($debug) {
foreach($currentClue as $k => $v) {
echo $k . '=' . bin2hex($v) . PHP_EOL;
}
}
$data[$pointer][] = $currentClue['clue'];
if ($across) {
$acrossClues[] = $currentClue['clue'];
} else {
$downClues[] = $currentClue['clue'];
}
$clue = '';
} else {
$data[$pointer] .= $b;
}
}
$clueTexts = $data[$clues];
$starts = '';
foreach(str_split($output[1]) as $c) {
$starts .= substr(bin2hex($c), 1, 1);
}
$grid = str_split($output[0], 1);
$answerChar = str_split($data[$answers], 1);
foreach($grid as $i => $char) {
if ($char == '?') {
$grid[$i] = array_shift($answerChar);
}
}
if ($debug)print_r($multis);
$grid2 = join("", $grid);
if ($debug)print_r(chunk_split($grid2, $length, PHP_EOL));
$across = array();
$down = array();
$markers = str_split($starts, 1);
//print_r($markers);
$row = 0;
$clue = 1;
if ($debug)print_r($grid);
foreach($grid as $i => $letter) {
if ($i > 0 && $i % $length == 0) {
$row++;
//echo "$i : row to $row\n";
}
if ($markers[$i] & 0x01 == 0x01) {
$found = false;
if ($debug)echo "i:$i\n";
if ($debug)echo "length:$length\n";
if ($debug)echo "i % length - 1:" . $i % ($length - 1) . "\n";
if ($debug)echo "grid[i+1]:{$grid[$i+1]}\n";
if ($debug)echo "i % length : ". $i % $length . "\n";
if (array_key_exists($i + 1, $grid)
&& $grid[$i+1] != '#'
&& (($i % $length >= 1 && $grid[$i-1] == '#') || $i % $length == 0)
&& $i % $length != ($length - 1)) {
//if (($i % ($length - 1) != 0 || $i == 0)
// && $grid[$i+1] != '#'
// && (($i > 1 && $grid[$i-1] == '#') || $i < 1)) {
$cluePos = array(
'x' => $i % $length,
'y' => $row,
);
if (array_key_exists($clue.'-across', $multis)) {
$cluePos['clue'] = 'See ' . $multis[$clue.'-across'];
} else {
$cluePos['clue'] = array_shift($acrossClues);
}
$across[$clue] = $cluePos;
$found = true;
if ($debug)echo "Across : ";
} else {
if ($debug)echo "Doesn't think it's an across:\n";
}
if (array_key_exists($i + $length, $grid)
&& $grid[$i+$length] != '#'
&& (($i > $length && $grid[$i-$length] == '#') || $i < $length)) {
$cluePos = array(
'x' => $i % $length,
'y' => $row,
);
if (array_key_exists($clue.'-down', $multis)) {
$cluePos['clue'] = 'See ' . $multis[$clue.'-down'];
} else {
$cluePos['clue'] = array_shift($downClues);
}
$down[$clue] = $cluePos;
$found = true;
if ($debug) echo "Down : ";
}
if ($found) {
if ($debug) echo "$clue : $row, " . $i % ($length) . "\n";
$clue++;
}
}
}
//print_r($down);
//print_r($across);
//
$clues = array();
foreach($down as $index=> $pos) {
$solution = "";
$x = $pos['x']; $y = $pos['y'];
$mark = $x + ($length * $y);
while ($grid[$mark] != '#') {
$solution .= $grid[$mark];
$mark += $length;
if ($mark % ($length) == 0 && $x != 0) {
break;
}
if ($mark >= count($grid)) {
break;
}
}
$clue = array(
'x' => $x,
'y' => $y,
'length' => strlen($solution),
'clue' => '"' . str_replace('"', '\"', $pos['clue']) . '"',
'solution' => '"' . $solution . '"'
);
if (array_key_exists($index.'-down', $multis)) {
$clue['extra'] = $multis[$index.'-down'];
}
$clues[$index.'-down'] = $clue;
}
foreach($across as $index=> $pos) {
$solution = "";
$x = $pos['x']; $y = $pos['y'];
$mark = $x + ($length * $y);
while ($grid[$mark] != '#') {
$solution .= $grid[$mark];
$mark += 1;
if ($mark % $length == 0) {
break;
}
if ($mark >= count($grid)) {
break;
}
}
$clue = array(
'x' => $x,
'y' => $y,
'length' => strlen($solution),
'clue' => '"' . str_replace('"', '\"', $pos['clue']) . '"',
'solution' => '"' . $solution . '"'
);
if (array_key_exists($index.'-across', $multis)) {
$clue['extra'] = $multis[$index.'-across'];
}
$clues[$index.'-across'] = $clue;
}
$ini = '';
foreach($clues as $index => $data) {
$ini.= '[' . $index . ']' . PHP_EOL;
foreach($data as $key => $value) {
$ini .= $key . '=' . $value . PHP_EOL;
}
$ini .= PHP_EOL;
}
echo $ini;