-
Notifications
You must be signed in to change notification settings - Fork 0
/
universal_acdc.pl
1963 lines (1922 loc) · 69.3 KB
/
universal_acdc.pl
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# S.T.A.L.K.E.R *.spawn files unpacker
# Update history:
# 1.42:
# [+] fix LA DC spawn unpacking
# 1.41:
# [+] add spawn LA DC unpacking
# 1.40:
# [!] fix line break characters when splitting all.spawn
# 1.39:
# [!] add scanning sections from system.ltx
# 1.38:
# [!] fix release LA spawn unpacking
# [!] fix parse and convert
# 1.37:
# [+] LA spawn unpacking added
# [!] all unrecognized ways during 'split' goes to 'unrecognized_ways.game'
# 1.36:
# [!] fixed scanning sections with spaces in beginning
# [!] fixed spawn splitting
# 1.35:
# [!] fixed version auto-assigning
# [!] removed game.graph reading when compiling
# 1.34:
# [+] added story ids control while compiling
# [+] added distance updating
# [!] fixed vertices updating
# 1.33:
# [+] added vertices updating
# [+] added smart way objects sorting in split mode
# [!] fixed some builds spawn unpacking
# 1.322:
# [+] added alife files comparing
# 1.321:
# [!] fixed cop spawn unpacking
# 1.32:
# [!] guids.ltx no longer nedded without -idx key
# [+] implemented sorting of way objects in ascending order
# [+] implemented detection of ways with invalid vertices
# [+] implemented sorting of alife objects in two different ways
# 1.31:
# [!] changed priority of clsids.ini querys [scan.pm]
# [!] implemented two-way config scan [scan.pm]
# [i] fixed code for new fail() syntax
# [i] fixed processing Clear Sky actor packet
# [+] added new logging system
# [+] implemented independent ini for user-defined class-section assignments
# 1.30:
# [i] fixed bug with decompiling NS spawn caused by new version of error handler
# [i] fixed bugs with unpacking level.spawns of some builds
# 1.29:
# [i] fixed bugs with unpacking level.spawns of some builds
# [i] fixed Clear Sky spawn unpacking caused by new actor packet processing
# [i] some tiny staff, can't remember it all
# 1.28:
# [i] èñïðàâëåíî èãíîðèðîâàíèå ïàðñåðîì êëþ÷à -way â ðåæèìå split.
# [i] èñïðàâëåíà îøèáêà ñêàíèðîâàíèÿ êîíôèãîâ ïðè êîìïèëÿöèè.
# [i] èñïðàâëåíà îøèáêà ÷òåíèÿ ñåêöèé íåêîòîðûõ se-êëàññîâ.
# [i] èñïðàâëåíà îøèáêà ðàçáèâêè ñïàâíà, èç-çà êîòîðîé ãåíåðèðîâàëèñü ëåâåë ñïàâíû íåïðàâèëüíîãî ôîðìàòà.
# [+] äîáàâëåí êîíòðîëü äóïëèêàòîâ àêòîðà ïðè êîìïèëÿöèè.
#
# 1.27:
# [i] èñïðàâëåíà îøèáêà ïàðñåðà, â íåêîòîðûõ ñëó÷àÿõ ïðèâîäèâøàÿ ê ïîð÷å ëîãèêè.
# [i] èñïðàâëåíî ñîçäàíèå ïàïîê ïðè ñîõðàíåíèè ðåçóëüòàòà.
# [+] äîáàâëåíà ïåðåèíèöèàëèçàöèÿ ïàðàìåòðîâ ñåêöèè ïîñëå ñìåíû êëàññà ïðè êîíâåðòàöèè. Ýòî ðàñøèðÿåò äèàïàçîí âåðñèé, äîñòóïíûõ äëÿ êîíâåðòèðîâàíèÿ.
# [+] äîáàâëåíà ïîääåðæêà ìàñê ïðè êîíâåðòàöèè.
# [+] äîáàâëåí êëþ÷ -ini â ðåæèìå êîíâåðòàöèè
#
# 1.26:
# [i] ïîïðàâëåíà ðàñïàêîâêà ñïàâíîâ ×Í.
# [+] äîáàâëåíî àâòîìàòè÷åñêîå çàïîëíåíèå âåðñèè ñïàâíà èç ïåðâîé ñåêöèè (åñëè àêòîðà â ñïàâíå íåò).
# [+] ÷òî-òî åùå ïî ìåëî÷è, íå ïîìíþ.
#
# 1.25:
# [i] îòêëþ÷åí âûâîä ïóñòîãî ïàðàìåòðà spawned_obj ïðè ðàñïàêîâêå.
# [+] ðåàëèçîâàíî àâòîìàòè÷åñêîå çàïîëíåíèå ïàðàìåòðîâ version è script_version ïðè çàïàêîâêå ñïàâíîâ ñ ñåêöèÿìè èç ðàçíûõ âåðñèé èãðû. Âåðñèÿ áåðåòñÿ èç êîíôèãà àêòîðà.
#
# 1.24:
# [i] èñïðàâëåíà ðàñïàêîâêà/çàïàêîâêà ñïàâíà áèëäà 2571.
# [i] èñïðàâëåíà çàïèñü guids.ltx
# [i] ìåëêèå ïðàâêè
#
# 1.23b:
# [+] óáðàíî ïðåäóïðåæäåíèå "state data left" ïðè ðàñïàêîâêå ñïàâíîâ ÇÏ, çàïàêîâàííûõ ðàíåå ñ ïîìîùüþ acdccop.
# [i] èñïðàâëåíû îøèáêè split, èç-çà êîòîðûõ ìîãëè ïîëó÷àòüñÿ êðèâûå level.spawn
# [i] ïåðåäåëàíà ëîãèêà ÷òåíèÿ/çàïèñè ïàêåòîâ se_stalker/se_monster
# [i] ìåëêèå èçìåíåíèÿ
#
# 1.22b:
# [+] äîáàâëåí êëþ÷ -nofatal
#
# 1.21b:
# [i] èñïðàâëåíû íåáîëüøèå îïå÷àòêè â êîäå.
# [i] ïàðñåð òåïåðü êîððåêòíî ÷èòàåò çíà÷åíèÿ ñ êîììåíòàðèÿìè.
#
# 1.2b:
# [+] íåáîëüøèå ïðàâêè ïî êîíâåðòàöèè.
# [+] äîáàâëåííûå â ìîäàõ ñîîòâåòñòâèÿ clsid -> ñåðâåðíûé êëàññ òåïåðü ðåäàêòèðóþòñÿ â îòäåëüíîì êîíôèãå (clsids.ini).
# [+] îøèáêà ïðè âñòðå÷å íåçíàêîìîãî clsid òåïåðü âûäàåòñÿ ïðè ðàñïàêîâêè ñåêöèè ñïàâíà ñ òàêèì clsid, à íå ïðè ñêàíèðîâàíèè êîíôèãîâ, êàê ðàíüøå.
#
# 1.1b:
# [+] ïðîâåðåíà ðàñïàêîâêà áèëä-ñïàâíîâ, ðåøåíà ïðîáëåìà äåêîìïèëÿöèè ñïàâíîâ áèëäîâ 25õõ.
# [+] äîáàâëåí êîíòðîëü íàëè÷èÿ ïàðàìåòðà version â ñåêöèÿõ ðàñïàêîâàííîãî ñïàâíà.
# [i] èñïðàâëåíî èñêëþ÷åíèå ôàéëà ñî spawn_id îáúåêòîâ ïðè ñêàíèðîâàíèè êîíôèãîâ.
#
# 1.0b:
# [+] îñíîâàòåëüíî ïåðåðàáîòàí êîä, ÷àñòü ñêðèïòà âûíåñåíà â îòäåëüíûå ìîäóëè.
# [i] èñïðàâëåíû âñå íåðàáîòàâøèå ôóíêöèè.
# [+] óâåëè÷åíà ñêîðîñòü âûïîëíåíèÿ êîäà, óìåíüøåíû òðåáîâàíèÿ ïî ïàìÿòè.
###########################################################
package gg_version;
use strict;
use constant build_versions => (
{ version => 128, script_version => 12, build => 'Call Of Pripyat (any patch)', graph_build => 'cop', graph_ver => 10},
{ version => 124, script_version => 8, build => 'Clear Sky (1.5.04 - 1.5.10)', graph_build => 'cop', graph_ver => 10},
{ version => 123, script_version => 8, build => 'Clear Sky (1.5.03)', graph_build => 'cop', graph_ver => 10},
{ version => 122, script_version => 8, build => 'Clear Sky (1.5.00 - 1.5.02)', graph_build => 'cop', graph_ver => 10},
{ version => 121, script_version => 7, build => 'LA DC', graph_build => 'soc', graph_ver => 8},
{ version => 118, script_version => 7, build => 'Shadow Of Chernobyl (1.0001 or higher)', graph_build => 'soc', graph_ver => 8},
{ version => 118, script_version => 6, build => 'Shadow Of Chernobyl (1.0001 or higher)', graph_build => 'soc', graph_ver => 8},
{ version => 118, script_version => 5, build => 'xrCore build 2559-2947', graph_build => 'soc', graph_ver => 8},
{ version => 117, script_version => 4, build => 'xrCore build 2571', graph_build => 'soc', graph_ver => 8},
{ version => 115, script_version => 3, build => 'xrCore build 2365', graph_build => 'soc', graph_ver => 8},
{ version => 109, script_version => 2, build => 'xrCore build 2307', graph_build => '2215', graph_ver => 8},
{ version => 104, script_version => 2, build => 'xrCore build 2217, 2232', graph_build => '2215', graph_ver => 8},
{ version => 103, script_version => 2, build => 'xrCore build 2198, 2191', graph_build => '2215', graph_ver => 8},
{ version => 102, script_version => 2, build => 'xrCore build 2221', graph_build => '2215', graph_ver => 8},
{ version => 101, script_version => 2, build => 'xrCore build 2205, 2215', graph_build => '2215', graph_ver => 8},
{ version => 95, script_version => 1, build => 'xrCore build 2218-2201', graph_build => '1935', graph_ver => 7},
{ version => 94, script_version => 1, build => 'xrCore build 2212-2217', graph_build => '1935', graph_ver => 7},
{ version => 93, script_version => 0, build => 'xrCore build 2202', graph_build => '1935', graph_ver => 7},
{ version => 92, script_version => 0, build => 'xrCore build 1994', graph_build => '1935', graph_ver => 7},
{ version => 90, script_version => 0, build => 'xrCore build 1964-1971', graph_build => '1935', graph_ver => 7},
{ version => 89, script_version => 0, build => 'xrCore build 1957', graph_build => '1935', graph_ver => 7},
{ version => 85, script_version => 0, build => 'xrCore build 1936', graph_build => '1935', graph_ver => 7},
{ version => 79, script_version => 0, build => 'xrCore build 1935', graph_build => '1935', graph_ver => 7},
{ version => 77, script_version => 0, build => 'xrCore build 1925', graph_build => '1935', graph_ver => 7},
{ version => 76, script_version => 0, build => 'xrCore build 1902-1917', graph_build => '1510', graph_ver => 7},
{ version => 75, script_version => 0, build => 'xrCore build 1893', graph_build => '1510', graph_ver => 7},
{ version => 73, script_version => 0, build => 'xrCore build 1875', graph_build => '1510', graph_ver => 7},
{ version => 72, script_version => 0, build => 'xrCore build 1865', graph_build => '1510', graph_ver => 7},
{ version => 65, script_version => 0, build => 'xrCore build 1850', graph_build => '1510', graph_ver => 7},
{ version => 63, script_version => 0, build => 'xrCore build 1842', graph_build => '1510', graph_ver => 7},
{ version => 60, script_version => 0, build => 'xrCore build 1844 (19 May 2005)', graph_build => '1510', graph_ver => 7},
{ version => 59, script_version => 0, build => 'xrCore build 1833-1835', graph_build => '1510', graph_ver => 7},
{ version => 56, script_version => 0, build => 'xrCore build 1834 (09 April 2005)', graph_build => '1510', graph_ver => 7},
{ version => 51, script_version => 0, build => 'xrCore build 1844-1849', graph_build => '1510', graph_ver => 6},
{ version => 49, script_version => 0, build => 'xrCore build 1835', graph_build => '1510', graph_ver => 5},
{ version => 47, script_version => 0, build => 'xrCore build 1834 (09 Feb 2005)', graph_build => '1510', graph_ver => 5},
{ version => 46, script_version => 0, build => 'xrCore build 1829', graph_build => '1510', graph_ver => 5},
{ version => 45, script_version => 0, build => 'xrCore build 1828', graph_build => '1510', graph_ver => 5},
{ version => 44, script_version => 0, build => 'xrCore build 1851', graph_build => '1510', graph_ver => 5},
{ version => 41, script_version => 0, build => 'xrCore build 1837', graph_build => '1510', graph_ver => 5},
{ version => 40, script_version => 0, build => 'xrCore build 1610-1638', graph_build => '1510', graph_ver => 4},
{ version => 39, script_version => 0, build => 'xrCore build 1511-1580', graph_build => '1510', graph_ver => 3},
{ version => 38, script_version => 0, build => 'xrCore build 1510', graph_build => '1510', graph_ver => 3},
{ version => 35, script_version => 0, build => 'xrCore build 1475', graph_build => '1510', graph_ver => 3},
{ version => 34, script_version => 0, build => 'xrCore build 1475', graph_build => '1510', graph_ver => 3},
{ version => 16, script_version => 0, build => 'xrCore build 1472', graph_build => '1472', graph_ver => 3},
{ version => 14, script_version => 0, build => 'xrCore build 1472', graph_build => '1472', graph_ver => 3},
{ version => 13, script_version => 0, build => 'xrCore build 1472', graph_build => '1472', graph_ver => 3},
{ version => 8, script_version => 0, build => 'xrCore build 1469', graph_build => '1469', graph_ver => 3},
{ version => 7, script_version => 0, build => 'xrCore build 1465', graph_build => '1469', graph_ver => 3},
{ version => 3, script_version => 0, build => 'xrCore build 1233-1265'},
{ version => 2, script_version => 0, build => 'xrCore build 1230-1254'},
);
sub graph_ver_by_ver {
foreach my $info (build_versions) {
if ($_[0] == $info->{version}) {
return $info->{graph_ver};
}
}
return undef;
}
sub scr_ver_by_version { #returns script_version by version
foreach my $info (build_versions) {
if ($_[0] >= $info->{version}) {
return $info->{script_version};
}
}
return undef;
}
sub build_by_version { #returns build number by version
foreach my $info (build_versions) {
if (($_[0] == $info->{version}) && ($_[1] == $info->{script_version})) {
return $info->{build};
}
}
return undef;
}
sub version_by_build { #returns version by build
foreach my $info (build_versions) {
if ($_[0] eq $info->{short_build}) {
return $info->{version}, $info->{script_version}, $info->{build};
}
}
return undef;
}
sub graph_build { #returns graph build (for graph unpacking) by version and script_version
foreach my $info (build_versions) {
if (($_[0] == $info->{version}) && ($_[1] == $info->{script_version})) {
return $info->{graph_build};
}
}
return undef;
}
#######################################################################
package convert; #contains harm classes by spawn version for converting
use strict;
use constant harm_classes => (
{name => 'se_respawn', start => 116, end => 118},
{name => 'se_smart_cover', start => 122, end => 128},
{name => 'se_sim_faction', start => 122, end => 128},
);
sub get_harm {
my @harm;
foreach my $sect (harm_classes) {
if ($_[0] < $sect->{start} || $_[0] > $sect->{end}) {
push @harm, $sect->{name};
}
}
return @harm;
}
#######################################################################
package all_spawn;
use strict;
use IO::File;
use File::Path;
use stkutils::ini_file;
use stkutils::chunked;
use stkutils::file::graph;
use stkutils::debug qw(fail);
use stkutils::file::entity;
use stkutils::level::level_game;
use stkutils::level::level_gct;
use Cwd;
# enum flags
use constant FL_LEVEL_SPAWN => 0x01;
use constant FL_IS_3120 => 0x02;
use constant FL_IS_2942 => 0x04;
use constant FL_IS_25XX => 0x08;
use constant FL_NO_FATAL => 0x10;
use constant FULL_IMPORT => 0x0;
use constant NO_VERTEX_IMPORT => 0x1;
# OOP part
sub new {
my $class = shift;
my $self = {};
$self->{spawn_version} = 0;
$self->{script_version} = 0;
# $self->{graph} = stkutils::file::graph->new();
$self->{config} = {};
$self->{flags} = 0;
bless($self, $class);
return $self;
}
sub DESTROY {
my $self = shift;
}
sub set_version {$_[0]->{spawn_version} = $_[1]};
sub get_version {return $_[0]->{spawn_version}};
sub set_script_version {$_[0]->{script_version} = $_[1]};
sub get_script_version {return $_[0]->{script_version}};
sub get_config {return $_[0]->{config}};
sub mode {return $_[0]->{config}->{mode}};
sub idx {return $_[0]->{config}->{compile}->{idx_file}};
sub use_graph {
if (defined $_[0]->{config}->{split}->{use_graph}) {
return 1;
}
return 0;
};
sub way {
if (defined $_[0]->{config}->{common}->{way}) {
return 1;
}
return 0;
};
sub graph_dir {return $_[0]->{config}->{common}->{graph_dir}};
sub get_src {return $_[0]->{config}->{common}->{src}};
sub get_out {return $_[0]->{config}->{common}->{out}};
sub get_ini {return $_[0]->{config}->{common}->{sections_ini}};
sub get_user_ini {return $_[0]->{config}->{common}->{user_ini}};
sub get_prefixes_ini {return $_[0]->{config}->{common}->{prefixes_ini}};
sub set_ini {$_[0]->{config}->{common}->{sections_ini} = $_[1]};
sub get_sort {return $_[0]->{config}->{common}->{sort}};
sub get_af {return $_[0]->{config}->{common}->{af}};
sub get_flag {return $_[0]->{flags}};
sub set_flag {$_[0]->{flags} |= $_[1]};
sub is_3120 {
if ($_[0]->{flags} & FL_IS_3120) {return 1};
return undef;
}
sub get_new_gvid {return $_[0]->{config}->{parse}->{new_gvid}};
sub get_old_gvid {return $_[0]->{config}->{parse}->{old_gvid}};
# reading
sub read {
my $self = shift;
my $cf = stkutils::chunked->new($self->get_src(), 'r') or fail($self->get_src().": $!\n");
if (!$self->level()) {
if ($self->get_version() > 79) {
while (1) {
my ($index, $size) = $cf->r_chunk_open();
defined($index) or last;
if ($index == 0) {
$self->read_header($cf);
} elsif ($index == 1) {
$self->read_alife($cf);
} elsif ($index == 2) {
$self->read_af_spawn($cf);
} elsif ($index == 3) {
$self->read_way($cf);
} elsif ($index == 4) {
$self->read_graph($cf->r_chunk_data());
} elsif ($index != 5) {
fail('unexpected chunk index '.$index);
}
$cf->r_chunk_close();
}
} else {
my $count;
my ($index, $size) = $cf->r_chunk_open();
$self->read_header($cf);
$cf->r_chunk_close();
$self->read_alife($cf);
if ($self->get_version() > 16) {
($count, $size) = $cf->r_chunk_open();
$self->read_af_spawn($cf);
$cf->r_chunk_close();
}
}
} else {
$self->read_alife($cf);
}
$cf->close();
}
sub read_header {
my $self = shift;
my ($cf) = @_;
print "reading header...\n";
if ($self->get_version() > 94) {
( $self->{graph_version},
$self->{guid},
$self->{graph_guid},
$self->{count},
$self->{level_count},
) = unpack('Va[16]a[16]VV', ${$cf->r_chunk_data()});
} else {
( $self->{graph_version},
$self->{count},
$self->{unknown},
) = unpack('VVV', ${$cf->r_chunk_data()});
}
}
sub read_alife {
my $self = shift;
my ($cf) = @_;
my $i = 0;
print "reading alife objects...\n";
if ($self->get_version() > 79 && !$self->level()) {
while (1) {
my ($index, $size) = $cf->r_chunk_open();
defined($index) or last;
if ($index == 0) {
$size == 4 or fail('unexpected alife objects count size');
my ($alife_count) = unpack('V', ${$cf->r_chunk_data()});
$alife_count == $self->{count} or fail('alife object count mismatch');
} elsif ($index == 1) {
while (1) {
($index, $size) = $cf->r_chunk_open();
defined($index) or last;
my $object = stkutils::file::entity->new();
$object->{cse_object}->{flags} = $self->get_flag();
$object->{cse_object}->{ini} = $self->get_ini();
$object->{cse_object}->{user_ini} = $self->get_user_ini();
$object->read($cf, $self->get_version());
$self->set_flag($object->{cse_object}->{flags} & 0x9F); # exclude entity specific flags
$self->set_ini($object->{cse_object}->{ini});
push @{$self->{alife_objects}}, $object;
$cf->r_chunk_close();
}
} elsif ($index == 2) {
$self->{unk_chunk} = $cf->r_chunk_data();
}
$cf->r_chunk_close();
}
} else {
while (1) {
my ($index, $size) = $cf->r_chunk_open();
defined $index or last;
$index < $self->{count} or last if defined $self->{count};
die unless $i == $index;
my $object = stkutils::file::entity->new();
$object->{cse_object}->{flags} = $self->get_flag();
$object->{cse_object}->{ini} = $self->get_ini();
$object->read($cf, $self->get_version());
$self->set_flag($object->{cse_object}->{flags});
$self->set_ini($object->{cse_object}->{ini});
$cf->r_chunk_close();
$i++;
if ($self->mode() eq 'split') {
push (@{$self->{alife_objects}}, $object) if (ref($object->{cse_object}) eq 'cse_alife_graph_point');
} else {
push @{$self->{alife_objects}}, $object;
}
}
}
}
sub read_af_spawn {
my $self = shift;
my ($cf) = @_;
print "reading artefact spawn places...\n";
$self->{af_spawn_data} = $cf->r_chunk_data();
if ($self->get_af()) {
my $packet = stkutils::data_packet->new($self->{af_spawn_data});
my ($obj_count) = $packet->unpack('V', 4);
while ($obj_count--) {
my $afsp = {};
@{$afsp->{position}} = $packet->unpack('f3', 12);
($afsp->{level_vertex_id}, $afsp->{distance}) = $packet->unpack('Vf', 8);
push @{$self->{af_spawn_places}}, $afsp;
}
}
}
sub read_way {
my $self = shift;
my ($cf) = @_;
print "reading way objects...\n";
while (1) {
my ($index, $size) = $cf->r_chunk_open();
defined($index) or last;
if ($index == 0) {
$size == 4 or fail('unexpected way objects count size');
my $way_count = unpack('V', ${$cf->r_chunk_data()});
} elsif ($index == 1) {
while (1) {
($index, $size) = $cf->r_chunk_open();
defined($index) or last;
my $object = stkutils::level::level_game->new();
$object->read($cf);
push @{$self->{way_objects}}, $object;
$cf->r_chunk_close();
}
} else {
fail('unexpected chunk index '.$index);
}
$cf->r_chunk_close();
}
}
sub read_graph {
my $self = shift;
my ($data_ref) = @_;
$self->{graph_data} = $data_ref;
$self->set_flag(FL_IS_3120) if $self->get_version() == 118;
$self->{graph} = stkutils::file::graph->new($self->{graph_data});
$self->{graph}->{gg_version} = $self->check_graph_build();
$self->{graph}->decompose();
$self->{graph}->read_vertices();
$self->{graph}->show_guids('guids.ltx');
}
# writing
sub write {
my $self = shift;
if ($#{$self->{alife_objects}} > -1) { # check if there is no objects
$self->set_version((@{$self->{alife_objects}}[0])->{cse_object}->{version});
$self->set_script_version((@{$self->{alife_objects}}[0])->{cse_object}->{script_version});
}
$self->{graph_version} = $self->check_graph_version();
my $cf = stkutils::chunked->new($self->get_out(), 'w') or fail($self->get_out().": $!\n");
if (!$self->level()) {
$self->write_header($cf);
$self->write_alife($cf);
$self->write_af_spawn($cf);
$self->write_way($cf);
$self->write_graph($cf) if !defined $_[0];
$self->write_service_chunk($cf);
} else {
$self->write_alife($cf);
}
$cf->close();
}
sub write_header {
my $self = shift;
my ($cf) = @_;
print "writing header...\n";
if ($self->get_version() > 94) {
my $data = pack('Va[16]a[16]VV',
$self->{graph_version},
$self->{guid},
$self->{graph_guid},
$#{$self->{alife_objects}} + 1,
$self->{level_count});
$cf->w_chunk(0, $data);
} else {
my $data = pack('VVV',
$self->{graph_version},
$#{$self->{alife_objects}} + 1,
$self->{unknown},);
if ($self->get_version() > 79) {
$cf->w_chunk(0, $data);
} else {
$cf->w_chunk(0xFFFF, $data);
}
}
}
sub write_alife {
my $self = shift;
my ($cf) = @_;
print "writing alife objects...\n";
if ($self->get_version() > 79 && !$self->level()) {
$cf->w_chunk_open(1);
$cf->w_chunk(0, pack('V', $#{$self->{alife_objects}} + 1));
$cf->w_chunk_open(1);
my $id = 0;
my $file = $self->idx();
$file = 'spawn_ids' if (defined $file && $file eq '');
$file .= '.ltx' if (defined $file and (substr($file, -3) ne 'ltx'));
my $log = IO::File->new($file, 'w') if defined $self->idx();
my $guids_file = stkutils::ini_file->new('guids.ltx', 'r') if defined $self->idx();
fail("guids.ltx: $!\n") if (defined $self->idx() && !defined $guids_file);
foreach my $object (@{$self->{alife_objects}}) {
next unless defined $object;
# enable this if you want inventory boxes always online
# if ($object->{cse_object}->{section_name} eq "inventory_box") {
# $object->{cse_object}->{object_flags} = 0xffffff3b;
# }
my $class_name = ref $object->{cse_object};
$cf->w_chunk_open($id);
if (defined $self->idx()) {
my $level_id = get_level_id($guids_file, $object->{cse_object}->{game_vertex_id});
print $log "\n[".$level_id."_".$object->{cse_object}->{name}."]\n";
print $log "id = $id\n";
print $log "story_id = $object->{cse_object}->{story_id}\n";
}
$object->write($cf, $id++);
$cf->w_chunk_close();
}
$log->close() if defined $self->idx();
$guids_file->close() if defined $self->idx();
$cf->w_chunk_close();
my $chunk_2 = '';
$chunk_2 = ${$self->{unk_chunk}} if $self->get_version() == 85;
$cf->w_chunk(2, $chunk_2);
$cf->w_chunk_close();
} else {
my $id = 0;
foreach my $object (@{$self->{alife_objects}}) {
$cf->w_chunk_open($id);
$object->write($cf, $id++);
$cf->w_chunk_close();
}
}
}
sub write_af_spawn {
my $self = shift;
my ($cf) = @_;
if ($self->get_af()) {
my $data = '';
foreach my $afsp (@{$self->{af_spawn_places}}) {
$data .= pack('f3Vf', @{$afsp->{position}}, $afsp->{level_vertex_id}, $afsp->{distance})
}
$self->{af_spawn_data} = \$data;
}
if (defined $self->{af_spawn_data}){
print "writing artefact spawn places...\n";
if ($self->get_version() > 79) {
$cf->w_chunk(2, ${$self->{af_spawn_data}});
} else {
$cf->w_chunk($#{$self->{alife_objects}} + 1, ${$self->{af_spawn_data}});
}
}
}
sub write_way {
my $self = shift;
my ($cf) = @_;
if (@{$self->{way_objects}}) {
print "writing way objects...\n";
$cf->w_chunk_open(3);
$cf->w_chunk(0, pack('V', $#{$self->{way_objects}} + 1));
$cf->w_chunk_open(1);
my $id = 0;
foreach my $object (@{$self->{way_objects}}) {
$cf->w_chunk_open($id++);
$object->write($cf);
$cf->w_chunk_close();
}
$cf->w_chunk_close();
$cf->w_chunk_close();
}
}
sub write_graph {
my $self = shift;
my ($cf) = @_;
my $new_graph_ver = $self->check_graph_build();
print "writing graph...\n";
if (!defined $self->{graph}->{gg_version} || $self->{graph}->{gg_version} eq $new_graph_ver) {
# -compile
return if $new_graph_ver ne 'cop';
$cf->w_chunk(4, ${$self->{graph_data}});
} elsif ($self->{graph}->{gg_version} eq 'cop') {
# convert from cs/cop spawn, so we need to split graph
# write cross tables
File::Path::mkpath('levels', 0);
my $wd = getcwd();
chdir 'levels' or fail('cannot change path to levels');
foreach my $level (values %{$self->{graph}->{level_by_guid}}) {
File::Path::mkpath($level, 0);
my $ctfh = IO::File->new($level.'/level.gct', 'w') or fail("$level/level.gct: $!\n");
my $ct = stkutils::level::level_gct->new($self->{graph}->{raw_cross_tables}{$level});
$ct->read();
$ct->set_version($self->check_graph_version());
$ct->write();
my $data = $ct->get_data();
$ctfh->write($$data, length($$data));
$ctfh->close();
}
chdir $wd or fail('cannot change path to '.$wd);
$self->{graph}->{gg_version} = $new_graph_ver;
my $graph_data = $self->{graph}->compose();
# write graph
my $fh = IO::File->new('game.graph', 'w');
binmode $fh;
$fh->write($$graph_data, length($$graph_data));
$fh->close();
} elsif ($new_graph_ver eq 'cop') {
# convert to cs/cop spawn, so we need to form graph
# read cross tables
foreach my $level (values %{$self->{graph}->{level_by_guid}}) {
my $ctfh = IO::File->new('levels/'.$level.'/level.gct', 'r') or fail("levels/$level/level.gct: $!\n");
binmode $ctfh;
my $data = '';
$ctfh->read($data, ($ctfh->stat())[7]);
$ctfh->close();
my $ct = stkutils::level::level_gct->new(\$data);
$ct->read();
$ct->set_version($self->check_graph_version());
$ct->write();
$self->{graph}->{raw_cross_tables}{$level} = $ct->get_data();
}
$self->{graph}->{gg_version} = 'cop';
my $graph_data = $self->{graph}->compose();
# write graph
$cf->w_chunk(4, $$graph_data);
}
}
# importing
sub import {
my $self = shift;
if (!$self->level()) {
my $if = stkutils::ini_file->new('all.ltx', 'r') or fail("all.ltx: $!\n");
$self->import_header($if);
$self->import_alife($if);
$self->set_version((@{$self->{alife_objects}}[0])->{cse_object}->{version});
$self->import_af_spawn($if);
$self->import_way($if);
$self->import_graph($if);
$if->close();
} else {
$self->import_level('level_spawn.ltx');
}
}
sub import_header {
my $self = shift;
my ($if) = @_;
$self->{graph_version} = $if->value('header', 'graph_version');
$self->{guid} = pack 'H*', $if->value('header', 'guid') if defined $if->value('header', 'guid');
$self->{graph_guid} = pack 'H*', $if->value('header', 'graph_guid') if defined $if->value('header', 'graph_guid');
$self->{level_count} = $if->value('header', 'level_count');
$self->{unknown} = $if->value('header', 'unknown');
$self->{flags} = $if->value('header', 'flags') if defined $if->value('header', 'flags');
}
sub import_alife {
my $self = shift;
my ($if) = @_;
unlink 'spawn_ids.log' if -e 'spawn_ids.log';
my $id = 0;
my $idx_log = IO::File->new('spawn_ids.log', 'w') if !defined $self->idx();
my $actor_flag = 0;
my $version = 0;
my $script_version = 0;
foreach my $fn (split /,/, ($if->value('alife', 'source_files') or fail('cannot find any locations in all.ltx'))) {
$fn =~ s/^\s*|\s*$//g;
my $lif;
if ($fn eq 'alife_$debug$\y_selo.ltx') {
$lif = stkutils::ini_file->new('alife_debug_y_selo.ltx', 'r') or fail("alife_debug_y_selo.ltx: $!\n");
} else {
$lif = stkutils::ini_file->new($fn, 'r') or fail("$fn: $!\n");
}
print "importing alife objects from file $fn...\n";
foreach my $section (@{$lif->{sections_list}}) {
my $object = stkutils::file::entity->new();
$object->{cse_object}->{flags} |= $self->get_flag();
$object->{cse_object}->{user_ini} = $self->get_user_ini();
$object->{cse_object}->{ini} = $self->get_ini();
$object->import_ltx($lif, $section, FULL_IMPORT);
if (($object->{cse_object}->{version} != 0) && (($version == 0) || ($script_version == 0)))
{
$version = $object->{cse_object}->{version};
$script_version = $object->{cse_object}->{script_version};
$script_version = gg_version::scr_ver_by_version($version) if !defined $script_version;
} elsif ($object->{cse_object}->{version} == 0) {
if (($version != 0) && ($script_version != 0))
{
$object = stkutils::file::entity->new();
$object->{cse_object}->{flags} |= $self->get_flag();
$object->{cse_object}->{user_ini} = $self->get_user_ini();
$object->{cse_object}->{ini} = $self->get_ini();
$object->{cse_object}->{version} = $version;
$object->{cse_object}->{script_version} = $script_version;
$object->import_ltx($lif, $section, NO_VERTEX_IMPORT);
# print "$object->{cse_object}->{version}\n";
} else {
fail('you must define version in first section');
}
}
# print "$object->{cse_object}->{game_vertex_id}\n";
$actor_flag += 1 if $object->{cse_object}->{section_name} eq 'actor';
fail('second actor object in '.$fn) if $actor_flag > 1;
if (defined $object->{cse_object}->{custom_data} && $object->{cse_object}->{custom_data} =~ /^(.*)\[(spawn_id)\]/s) {
$object->{cse_object}->{custom_data} = $1."[spawn_id]\n$object->{cse_object}->{name} = $id";
print $idx_log "\n[$object->{cse_object}->{name}]\nnew_idx = $id\n" if !defined $self->idx();
}
push @{$self->{alife_objects}}, $object;
$id++;
}
$lif->close();
}
if (defined $if->section('unk')) {
my $fn = $if->value('unk', 'binary_files');
my $bin_fh = IO::File->new($fn, 'r') or fail("$fn: $!\n");
binmode $bin_fh;
my $data = '';
$bin_fh->read($data, ($bin_fh->stat())[7]);
$self->{unk_chunk} = \$data;
$bin_fh->close();
}
$idx_log->close() if !defined $self->idx();
}
sub import_level {
my $self = shift;
my ($if) = @_;
my ($lif) = stkutils::ini_file->new($if, 'r') or fail("$if: $!\n");
print "importing alife objects from $if\n";
foreach my $section (@{$lif->{sections_list}}) {
my $object = stkutils::file::entity->new();
$object->{cse_object}->{flags} = $self->get_flag();
$object->{cse_object}->{ini} = $self->get_ini();
$object->import_ltx($lif, $section);
push @{$self->{alife_objects}}, $object;
}
$lif->close();
}
sub import_af_spawn {
my $self = shift;
my ($if) = @_;
if ($self->get_af()) {
my $fh = stkutils::ini_file->new('af_spawn_places.ltx', 'r') or fail("af_spawn_places.ltx: $!\n");
foreach my $id (@{$fh->{sections_list}}) {
my $afsp = {};
@{$afsp->{position}} = split /,\s*/, $fh->value($id, 'position');
$afsp->{level_vertex_id} = $fh->value($id, 'level_vertex_id');
$afsp->{distance} = $fh->value($id, 'distance');
push @{$self->{af_spawn_places}}, $afsp;
}
$fh->close();
} else {
return if !defined $if->value('section2', 'binary_files');
my $fn = $if->value('section2', 'binary_files');
my $bin_fh = IO::File->new($fn, 'r') or fail("$fn: $!\n");
binmode $bin_fh;
print "importing artefact spawn places data...\n";
my $data = '';
$bin_fh->read($data, ($bin_fh->stat())[7]);
$self->{af_spawn_data} = \$data;
}
}
sub import_way {
my $self = shift;
my ($if) = @_;
my $fn = $if->section('way') or return;
foreach my $fn (split /,/, ($if->value('way', 'source_files') or return)) {
$fn =~ s/^\s*|\s*$//g;
my $lif;
if ($fn eq 'way_$debug$\y_selo.ltx') {
$lif = stkutils::ini_file->new('way_debug_y_selo.ltx', 'r') or fail("way_debug_y_selo.ltx: $!\n");
} else {
$lif = stkutils::ini_file->new($fn, 'r') or fail("$fn: $!\n");
}
print "importing way objects from file $fn...\n";
foreach my $section (@{$lif->{sections_list}}) {
my $object = stkutils::level::level_game->new();
$object->importing($lif, $section);
push @{$self->{way_objects}}, $object;
}
$lif->close();
}
}
sub import_graph {
my $self = shift;
my ($if) = @_;
# return if !defined $if->section('unk');
return if !defined $if->section('graph');
print "importing graph...\n";
my $fn = $if->value('graph', 'binary_files');
my $bin_fh = IO::File->new($fn, 'r') or fail("$fn: $!\n");
binmode $bin_fh;
my $data = '';
$bin_fh->read($data, ($bin_fh->stat())[7]);
$bin_fh->close();
$self->{graph_data} = \$data;
$self->set_flag(FL_IS_3120) if $self->get_version() == 118;
}
# exporting
sub export {
my $self = shift;
my ($fn) = @_;
if (!$self->level()) {
my $if = stkutils::ini_file->new($fn, 'w') or fail("$fn: $!\n");
$self->export_header($if);
$self->export_alife($if);
$self->export_af_spawn($if);
$self->export_way($if);
$self->export_graph($if) if ($self->get_version() > 118 || $self->is_3120());
$if->close();
} else {
$self->export_level('level_spawn.ltx');
}
}
sub export_header {
my $self = shift;
my ($if) = @_;
my $fh = $if->{fh};
print $fh "[header]\n; don't touch these\n";
print $fh "graph_version = $self->{graph_version}\n";
print $fh 'guid = ', unpack('H*', $self->{guid}), "\n" if (defined $self->{guid});
print $fh 'graph_guid = ', unpack('H*', $self->{graph_guid}), "\n" if (defined $self->{graph_guid});
print $fh "level_count = $self->{level_count}\n" if (defined $self->{level_count});
print $fh "unknown = $self->{unknown}\n" if (defined $self->{unknown});
print $fh "flags = $self->{flags}\n" if $self->{flags} != 0;
print $fh "\n";
}
sub export_alife {
my $self = shift;
my ($if) = @_;
my $id = 0;
my %if_by_level;
my @levels;
my %objects_by_level_id; # key = level_id, value = array_ref
# split objects by level id
foreach my $object (@{$self->{alife_objects}}) {
my $level_name = $self->{graph}->level_name($object->{cse_object}->{game_vertex_id});
push @{$objects_by_level_id{$self->{graph}->level_id($level_name)}}, $object;
}
# sort objects in arrays by section name
my $sort = $self->get_sort();
if (defined $sort) {
if ($sort eq 'simple') {
foreach my $arr (values %objects_by_level_id) {
my @new = sort {$a->{cse_object}->{name} cmp $b->{cse_object}->{name}} @$arr;
$arr = \@new;
}
} elsif ($sort eq 'complex') {
foreach my $arr (values %objects_by_level_id) {
my @new = sort {($a->{cse_object}->{section_name} cmp $b->{cse_object}->{section_name}) || ($a->{cse_object}->{name} cmp $b->{cse_object}->{name})} @$arr;
$arr = \@new;
}
}
}
# export objects
foreach my $i (keys %objects_by_level_id) {
my $arr = $objects_by_level_id{$i};
next if !defined $arr;
my $level = $self->{graph}->level_name_by_id($i);
next if !defined $level;
my $lif = $if_by_level{$level};
if (!defined $lif) {
push @levels, "alife_$level.ltx";
if ($level eq '$debug$\y_selo') {
$lif = stkutils::ini_file->new("alife_debug_y_selo.ltx", 'w') or fail("alife_debug_y_selo.ltx: $!\n");
} else {
$lif = stkutils::ini_file->new("alife_$level.ltx", 'w') or fail("alife_$level.ltx: $!\n");
}
print "exporting alife objects on level $level...\n";
$if_by_level{$level} = $lif;
}
my $out_sects = IO::File->new($level.'.sections', 'w');
foreach my $object (@$arr) {
$object->export_ltx($lif, $id++);
print $out_sects "$object->{cse_object}->{name}\n";
}
$out_sects->close();
}
if ($self->get_version() == 85) {
my $bin_fh = IO::File->new('unk_chunk.bin', 'w') or fail("unk_chunk.bin: $!\n");
binmode $bin_fh;
$bin_fh->write(${$self->{unk_chunk}}, length(${$self->{unk_chunk}}));
$bin_fh->close();
}
my $fh = $if->{fh};
print $fh "[alife]\nsource_files = <<END\n", join(",\n", @levels), "\nEND\n\n";
print $fh "[unk]\nbinary_files = unk_chunk.bin\n\n" if $self->get_version() == 85;
foreach $if (values %if_by_level) {
$if->close();
}
}
sub export_level {
my $self = shift;
my ($if) = @_;
my $id = 0;
my $lif = stkutils::ini_file->new($if, 'w') or fail("$if: $!\n");
foreach my $object (@{$self->{alife_objects}}) {
next unless defined $object;
$object->export_ltx($lif, $id++);
}
$lif->close();
}
sub export_af_spawn {
my $self = shift;
my ($if) = @_;
if ($self->get_af()) {
my $fh = IO::File->new('af_spawn_places.ltx', 'w') or fail("af_spawn_places.ltx: $!\n");
my $id = 0;
foreach my $afsp (@{$self->{af_spawn_places}}) {
print $fh "[$id]\n";
printf $fh "position = %f,%f,%f\n", @{$afsp->{position}}[0..2];
print $fh "level_vertex_id = $afsp->{level_vertex_id}\n";
print $fh "distance = $afsp->{distance}\n";
$id++;
}
$fh->close();
} else {
my $bin_fh = IO::File->new('section2.bin', 'w') or fail("section2.bin: $!\n");
binmode $bin_fh;
print "exporting raw data...\n";
$bin_fh->write(${$self->{af_spawn_data}}, length(${$self->{af_spawn_data}}));
my $fh = $if->{fh};
print $fh "[section2]\nbinary_files = section2.bin\n\n";
}
}
use constant way_name_exceptions => {
kat_teleport_to_dark_city_orientation => 'l03u_agr_underground',
kat_teleport_to_dark_city_position => 'l03u_agr_underground',
walk_3 => 'l05_bar',
rad_heli_move => 'l10_radar',
pri_heli4_go2_path => 'l11_pripyat',
sar_teleport_0000_exit_look => 'l12u_sarcofag',
sar_teleport_0000_exit_walk => 'l12u_sarcofag',
val_ambush_dest_look => 'l04_darkvalley',
};
sub export_way {
my $self = shift;
my ($if) = @_;
# init prefixes
my $prefixes = $self->init_way_prefixes();
if (@{$self->{way_objects}}) {
my %info_by_level;
foreach my $object (@{$self->{way_objects}}) {
my $level = $self->get_level_name($object, $prefixes);
fail('unknown level of the way object '.$object->{name}) unless defined $level;
my $info = $info_by_level{$level};
if (!defined $info) {
$info = {};
if ($level eq '$debug$\y_selo') {
$info->{lif} = stkutils::ini_file->new("way_debug_y_selo.ltx", 'w') or fail("way_debug_y_selo.ltx: $!\n");
} else {
$info->{lif} = stkutils::ini_file->new("way_$level.ltx", 'w') or fail("way_$level.ltx: $!\n");
}
$info->{way_objects} = ();
$info_by_level{$level} = $info;
}
push @{$info->{way_objects}}, $object;
}
my $id = 0;
# foreach my $info (values %info_by_level) {
while (my ($level, $info) = each %info_by_level) {
print "exporting way objects on level $level...\n";
foreach my $object (sort {$a->{name} cmp $b->{name}} @{$info->{way_objects}}) {
$object->export($info->{lif}, $id++);
}
$info->{lif}->close();
}
my $fh = $if->{fh};
print $fh "[way]\nsource_files = <<END\n", join(",\n", map {"way_$_.ltx"} (sort {$a cmp $b} keys %info_by_level)), "\nEND\n\n";
}
}
sub init_way_prefixes {
my $self = shift;
my $prefixes = $self->get_prefixes_ini();
if (defined $prefixes) {
foreach my $key (keys %{$prefixes->{sections_hash}{prefixes}}) {