-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCM_PngFR.pas
476 lines (447 loc) · 13.4 KB
/
CCM_PngFR.pas
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
unit CCM_PngFR;
interface
uses Classes, Windows, Forms, SysUtils, CCM_Png, CCM_Zip;
function OpenPNG(filename:string):boolean;
procedure ClosePNG;
function ReadPagePNG(idpointer:longint):TPAGE;
function getIDPointer(idpointer:longint):longint;
function fixedNavBar(item_id:smallint; actualmachines_page, actualrelated_principles_popup, actualtimeline_page, actualinventors_page:IDPOINTER):IDPOINTER;
const CCMWorkshop:IDPOINTER=978;
CCMindex:IDPOINTER=808;
CCMScrollBoxPos:TPOS=(327,73,258,300);
implementation
{$OVERFLOWCHECKS ON}
{$RANGECHECKS ON}
{ BigEndian reading functions }
function typeReadCardinalBE:cardinal;
var buf:array[0..3] of byte;
begin
PNG.read(buf,4);
typeReadCardinalBE:=buf[0]*256*256*256+buf[1]*256*256+buf[2]*256+buf[3];
end;
function typeReadWordBE:smallint;
var buf:array[0..1] of byte;
val:longint;
begin
PNG.read(buf,2);
val:=buf[0]*256+buf[1];
if (val > 32767) then val:=val-65536;
typeReadWordBE:=val;
end;
procedure SkipBytes(n:integer);
begin
PNG.seek(n, soFromCurrent);
end;
{ Structs reading functions }
procedure readHeader;
begin
PNG.seek(0,soFromBeginning);
header.pages_start:=typeReadCardinalBE;
header.idstrings_size:=typeReadCardinalBE;
header.idpointers_start:=typeReadCardinalBE;
end;
function readStrings:boolean;
var i,j,l:word;
s:string;
buf:array[0..127] of char;
begin
readStrings:=false;
strings.nbstrings:=typeReadWordBE;
strings.strings[strings.nbstrings]:='';
for i:=1 to strings.nbstrings do begin
l:=typeReadWordBE;
if (l > 127) then exit;
buf[l]:=' ';
PNG.Read(buf,l);
s:='';
for j:=1 to l do s:=s+buf[pred(j)];
strings.strings[i]:=s;
end;
readStrings:=true;
end;
procedure readFrame2(framenum:word;infonum:word);
begin
with info.frames[framenum].infos[infonum] do begin
typeframe2:=typeReadWordBE;
id_frame2:=typeReadWordBE;
if (typeframe2=201) then begin
SkipBytes(1*2);
idpointer_frame:=typeReadCardinalBE;
SkipBytes(1*2);
if (id_frame2 > 1) then begin
SkipBytes(2*2);
end;
size_x:=typeReadWordBE;
size_y:=typeReadWordBE;
offset_x:=typeReadWordBE;
offset_y:=typeReadWordBE;
imageoffset_x:=offset_x;
imageoffset_y:=offset_y;
end else
if (typeframe2=202) then begin
SkipBytes(4*2);
size_x:=typeReadWordBE;
size_y:=typeReadWordBE;
offset_x:=typeReadWordBE;
offset_y:=typeReadWordBE;
end else
if (typeframe2=203) then begin
SkipBytes(1*2);
idpointer_frame:=typeReadCardinalBE;
SkipBytes(3*2);
offset_x:=typeReadWordBE;
offset_y:=typeReadWordBE;
imageoffset_x:=typeReadWordBE;
imageoffset_y:=typeReadWordBE;
end else raise Exception.Create('Unknown typeframe2');
end;
end;
procedure readFrame(framenum:word);
var i:word;
begin
with info.frames[framenum] do begin
typeframe:=typeReadWordBE;
id_frame:=typeReadWordBE;
if (typeframe=101) or (typeframe=103) then begin
SkipBytes(16*2);
numids:=typeReadWordBE;
if numids > 0 then
for i:=0 to numids-1 do ids[i]:=typeReadWordBE;
SkipBytes(1*2);
numinfos:=typeReadWordBE;
if numinfos > 0 then
for i:=0 to numinfos-1 do readFrame2(framenum,i);
end else
if (typeframe=102) then begin
SkipBytes(2*2);
size_x:=typeReadWordBE;
size_y:=typeReadWordBE;
SkipBytes(12*2);
numinfos:=typeReadWordBE;
if numinfos > 0 then
for i:=0 to numinfos-1 do readFrame2(framenum,i);
end else raise Exception.Create('Unknown typeframe');
SkipBytes(1*2);
end;
end;
procedure readInfo;
var i:word;
begin
SkipBytes(1*2);
info.screenwidth:=typeReadWordBE;
info.screenheight:=typeReadWordBE;
SkipBytes(6*2);
info.page_start:=typeReadCardinalBE;
info.numframes:=typeReadWordBE;
if info.numframes > 0 then
for i:=0 to info.numframes-1 do readFrame(i);
end;
procedure readIndexitem(indexnum:word);
var i:word;
begin
with index.indexitems[indexnum] do begin
indexword:=typeReadWordBE;
SkipBytes(2*2);
idpage:=typeReadCardinalBE;
nbpopups:=typeReadWordBE;
if nbpopups > 0 then
for i:=0 to nbpopups-1 do begin
popups[i]:=typeReadCardinalBE;
end;
end;
end;
procedure readIndex;
var i:word;
begin
index.indexlen:=typeReadCardinalBE;
if index.indexlen > 0 then
for i:=0 to index.indexlen-1 do readIndexitem(i);
end;
procedure readPointers;
var i:word;
begin
pointers.nbpointers:=typeReadCardinalBE;
if pointers.nbpointers > 0 then
for i:=0 to pointers.nbpointers-1 do pointers.pointers[i]:=typeReadCardinalBE;
end;
function readAction:TACTION;
var action:TACTION;
begin
with action do begin
typeaction:=typeReadWordBE;
if (typeaction = 0) then begin
popup_id:=typeReadWordBE;
SkipBytes(2*2);
end else
if (typeaction = 1) then begin
popup_id:=typeReadCardinalBE;
popuplevel:=typeReadWordBE;
if (typeReadWordBE <> 0) then SkipBytes(-2);
end else
if (typeaction = 2) then begin
item_id:=typeReadWordBE;
SkipBytes(1*2);
if (typeReadWordBE <> 0) then SkipBytes(-2);
end else
if (typeaction = 3) then begin
linkto:=typeReadCardinalBE;
SkipBytes(1*2);
if (typeReadWordBE <> 0) then SkipBytes(-2);
end else
if (typeaction = 4) then begin
soundtoplay:=typeReadWordBE;
SkipBytes(1*4);
SkipBytes(3*2);
if (typeReadWordBE <> 0) then SkipBytes(-2);
end else
if (typeaction = 7) then begin
anim:=typeReadWordBE;
SkipBytes(5*2);
if (typeReadWordBE <> 0) then SkipBytes(-2);
end else
if (typeaction = 11) then begin
command:=typeReadWordBE;
if (typeReadWordBE <> 0) then SkipBytes(-2);
end else
if (typeaction = 12) then begin
popup_to_close:=typeReadCardinalBE;
end else
if (typeaction = 36) then begin
SkipBytes(2*2);
end else raise Exception.Create('Unknown typeaction');
end;
result:=action;
end;
function readItem:TITEM;
var item:TITEM;
i:smallint;
begin
with item do begin
itemtype:=typeReadWordBE;
item_id:=typeReadWordBE;
item_props:=typeReadWordBE;
if (itemtype = 601) then begin
SkipBytes(2*2);
imgwidth:=typeReadWordBE;
imgheight:=typeReadWordBE;
SkipBytes(2*2);
image:=typeReadWordBE;
end else
if (itemtype = 602) then begin
x1:=typeReadWordBE;
y1:=typeReadWordBE;
x2:=typeReadWordBE;
y2:=typeReadWordBE;
cursor:=typeReadWordBE;
SkipBytes(2*2);
x1:=typeReadWordBE;
y1:=typeReadWordBE;
x2:=typeReadWordBE;
y2:=typeReadWordBE;
anim:=typeReadWordBE;
cursor:=typeReadWordBE;
SkipBytes(1*2);
autostart:=typeReadWordBE;
SkipBytes(1*2);
end else
if (itemtype = 603) then begin
x1:=typeReadWordBE;
y1:=typeReadWordBE;
x2:=typeReadWordBE;
y2:=typeReadWordBE;
cursor:=typeReadWordBE;
SkipBytes(2*2);
numactions:=typeReadWordBE;
for i:=0 to numactions-1 do actions[i]:=readAction;
end else
if (itemtype = 604) then begin
x1:=typeReadWordBE;
y1:=typeReadWordBE;
x2:=typeReadWordBE;
y2:=typeReadWordBE;
cursor:=typeReadWordBE;
SkipBytes(2*2);
letter:=typeReadWordBE;
end else
if (itemtype = 605) then begin
SkipBytes(6*2);
end else
if (itemtype = 606) then begin
x1:=typeReadWordBE;
y1:=typeReadWordBE;
x2:=typeReadWordBE;
y2:=typeReadWordBE;
cursor:=typeReadWordBE;
SkipBytes(1*2);
anim:=typeReadWordBE;
SkipBytes(1*2);
end else
if (itemtype = 607) then begin
x1:=typeReadWordBE;
y1:=typeReadWordBE;
x2:=typeReadWordBE;
y2:=typeReadWordBE;
cursor:=typeReadWordBE;
page_skip:=typeReadWordBE;
if (page_skip = 1) then begin
SkipBytes(3*2);
nextpage:=typeReadCardinalBE;
SkipBytes(1*2);
end;
anim:=typeReadWordBE;
SkipBytes(1*2);
end else
if (itemtype = 608) then begin
x1:=typeReadWordBE;
y1:=typeReadWordBE;
x2:=typeReadWordBE;
y2:=typeReadWordBE;
cursor:=typeReadWordBE;
SkipBytes(5*2);
popup_id:=typeReadWordBE;
SkipBytes(3*2);
end else
if (itemtype = 609) then begin
SkipBytes(1*2);
x1:=typeReadWordBE;
x2:=typeReadWordBE;
SkipBytes(6*2);
image:=typeReadWordBE;
end else
if (itemtype = 610) then begin
SkipBytes(1*2);
x1:=typeReadWordBE;
x2:=typeReadWordBE;
cursor:=typeReadWordBE;
SkipBytes(5*2);
SkipBytes(8*2);
end else raise Exception.Create('Unknown itemtype');
end;
result:=item;
end;
function readPage(idpage:longint):TPAGE;
var page:TPAGE;
i,j:smallint;
nblinks,tmp:smallint;
tmp2:cardinal;
begin
if (idpage >= longint(pointers.nbpointers)) then PNG.Seek(cardinal(idpage)+header.pages_start,soFromBeginning) else
if (idpage >= 0) then PNG.Seek(pointers.pointers[idpage]+header.pages_start,soFromBeginning);
with page do begin
typepage:=typeReadWordBE;
while (typepage=-1) do typepage:=typeReadWordBE; // Sert de NOP - N'EXISTE PAS DANS LE FICHIER ORIGINAl!
if (typepage=0) then begin
id_frame:=typeReadWordBE;
numitems:=typeReadWordBE;
nblinks:=0;
for i:=0 to numitems-1 do begin
items[i]:=readItem;
if (items[i].itemtype = 606) then inc(nblinks);
end;
SkipBytes(1*2);
for i:=1 to nblinks do begin
tmp:=typeReadWordBE;
SkipBytes((nblinks-i)*2+((i-1)*4));
for j:=0 to numitems-1 do if (items[j].item_id = tmp) then items[j].page_skip:=typeReadCardinalBE;
SkipBytes(0-((nblinks-i)*2+(i*4)));
end;
SkipBytes(nblinks*4);
if (nblinks > 0) then SkipBytes(1*2);
end else
if (typepage=101) or (typepage=102) or (typepage=103) then begin
id_frame:=typeReadWordBE;
xoffset:=typeReadWordBE;
yoffset:=typeReadWordBE;
basedirectory:=typeReadWordBE;
SkipBytes(1*2);
tmp:=typeReadWordBE;
SkipBytes(1*2);
if (tmp < 201) then SkipBytes(tmp*3);
numitems:=typeReadWordBE;
for i:=0 to numitems-1 do items[i]:=readItem;
links_infos:=typeReadWordBE;
while ((links_infos > 600) and (links_infos < 610)) do begin
SkipBytes(-2);
inc(numitems);
items[numitems-1]:=readItem;
links_infos:=typeReadWordBE;
end;
tmp:=typeReadWordBE;
more_infos:=0;
if (tmp <> 4) then begin
more_infos:=typeReadWordBE;
if (more_infos = 1) then begin
if (links_infos = 0) then begin
related_principles_popup:=typeReadCardinalBE;
machines_page:=typeReadCardinalBE;
inventors_page:=typeReadCardinalBE;
timeline_page:=typeReadCardinalBE;
end else begin
SkipBytes(5*2);
end;
end;
if (more_infos = 73) then begin
SkipBytes(-4);
for i:=0 to 25 do begin
tmp2:=typeReadCardinalBE;
for j:=0 to numitems-1 do if (items[j].letter = i) then items[j].page_skip:=tmp2;
end;
SkipBytes(26*2); // Don't know what this is...
SkipBytes(2*2);
end;
end;
end else raise Exception.Create('Unknown itemtype');
end;
result:=page;
end;
{ Exported functions }
function OpenPNG(filename:string):boolean;
begin
OpenPNG:=false;
PNG:=OpenFile(filename);
if PNG <> nil then begin
readHeader;
if (not readStrings) then exit;
readInfo;
readIndex;
readPointers;
while (PNG.Position <> PNG.Size) do readPage(-1);
OpenPNG:=true;
end;
end;
procedure ClosePNG;
begin
if PNG <> nil then PNG.Free;
end;
function ReadPagePNG(idpointer:longint):TPAGE;
begin
try
result:=readPage(idpointer);
except
MessageBox(Application.Handle,'Fichier TWTW.ANI corrompu.','Erreur',0);
Application.Terminate;
exit;
end;
end;
function getIDPointer(idpointer:longint):longint;
//var i:longint;
begin
getIDPointer:=idpointer;
// for i:=0 to longint(pointers.nbpointers)-1 do if pointers.pointers[i] = cardinal(idpointer) then getIDPointer:=i;
end;
function fixedNavBar(item_id:smallint; actualmachines_page, actualrelated_principles_popup, actualtimeline_page, actualinventors_page:IDPOINTER):IDPOINTER;
begin
fixedNavBar:=0;
case item_id of
46: fixedNavBar:=978; // Atelier
-47,47: if actualmachines_page = -1 then fixedNavBar:=246 else fixedNavBar:=actualmachines_page; // Machines
-48,48: if actualrelated_principles_popup = -1 then fixedNavBar:=979 else fixedNavBar:=actualrelated_principles_popup; // Grands Principes
-49,49: if actualtimeline_page = -1 then fixedNavBar:=980 else fixedNavBar:=actualtimeline_page; // Histoire
-50,50: if actualinventors_page = -1 then fixedNavBar:=754 else fixedNavBar:=actualinventors_page; // Inventeurs
51: fixedNavBar:=-51; // Retour, géré plus bas
52: fixedNavBar:=808; // Index
53: fixedNavBar:=981; // Options
54: fixedNavBar:=670; // Aide
end;
end;
end.