This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.cpp
1938 lines (1737 loc) · 55.6 KB
/
utils.cpp
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
#include <fstream>
#include "main.h"
#include "utils.h"
#include "textures.h"
long getFileSize(FILE *file)
{
long lCurPos, lEndPos;
lCurPos = ftell(file);
fseek(file, 0, 2);
lEndPos = ftell(file);
fseek(file, lCurPos, 0);
return lEndPos;
}
long ConvertInt16(unsigned char Byte1, unsigned char Byte2)
{
//return Byte1 << 8 | Byte2 ;
return Byte2 << 8 | Byte1 ;
}
long ConvertInt24(unsigned char Byte1, unsigned char Byte2, unsigned char Byte3)
{
return Byte3 << 16 | Byte2 << 8 | Byte1 ;
}
long ConvertInt32(unsigned char Byte1, unsigned char Byte2, unsigned char Byte3, unsigned char Byte4)
{
return Byte4 << 24 | Byte3 << 16 | Byte2 << 8 | Byte1 ; //24 was 32
}
long getValAtAddress(unsigned char *buffer, long Address, int size)
{//Gets contents of bytes the the specific integer address. int(8), int(16), int(32) per uw-formats.txt
if (Address < 0)
{
printf("Invalid Address Error\n");
}
switch (size)
{
case 8:
{return buffer[Address];}
case 16:
{return ConvertInt16(buffer[Address],buffer[Address+1]);}
case 24:
{return ConvertInt24(buffer[Address],buffer[Address+1],buffer[Address+2]);}
case 32: //may be buggy!
{return ConvertInt32(buffer[Address], buffer[Address + 1], buffer[Address + 2], buffer[Address + 3]); }
default:
{
printf("Invalid size entered!");
return -1;
}
}
}
int getValAtCoordinate(int x, int y, int BlockStart,unsigned char *buffer,int size)
{
int val = getValAtAddress(buffer, BlockStart + (x*4) + (y * (4 * 64)),size);
return val;
}
// ua_uw2_import methods
/*! the function reads in compressed blocks from uw2 .ark files and creates
a SDL_RWops struct to transparently access it.
The code was adapted from the LoW project: http://low.sourceforge.net/
\param fd an open .ark file
\param blocknum block number to read in
\param destsize number of bytes which should be read from the block
*/
unsigned char *get_rwops_uw2dec(unsigned char *buffer, unsigned int blocknum, int *datalen)
{
// read in number of blocks
*datalen = 0;
long addressPointer = 0;
int nblocks = getValAtAddress(buffer, 0, 16); //fread16(fd);
// read in offset
addressPointer += blocknum * 4 + 6;
int offset = getValAtAddress(buffer, addressPointer, 32);
if (offset == 0)
return NULL; // no block
// read in flags
addressPointer = (blocknum + nblocks) * 4 + 6;
int flags = getValAtAddress(buffer, addressPointer, 32);
// read in real block size
addressPointer = (blocknum + nblocks * 2) * 4 + 6;
int blocksize = getValAtAddress(buffer, addressPointer, 32);
// read in available block size
addressPointer = (blocknum + nblocks * 3) * 4 + 6;
int avail = getValAtAddress(buffer, addressPointer, 32); //fread32(fd);
// read in source data
unsigned char *src = &buffer[offset]; //= new unsigned char[blocksize];
//addressPointer = offset;
//for (int i = 0; i < blocksize; i++)
// {
// src[i] = buffer[addressPointer + i];
// }
long destsize = getValAtAddress(src, 0, 32) + 100;
printf("uw2dec: reading in block %u:\n offset=%06x, flags=%u, blocksize=%04x, avail=%04x, destsize=%04x\n",
blocknum, offset, flags, blocksize, avail, destsize);
// allocate destination array
unsigned char *dest = new unsigned char[destsize];
//for (int i = 0; i < destsize; i++)
// {
// dest[i] = 0;
// }
// decode uw2 compression scheme
if ((flags & 2) != 0)
{
// compressed data
unsigned char * ubuf = dest;
unsigned char * cp = &src[4];
unsigned char * ce = &src[blocksize];
unsigned char * up = ubuf;
unsigned char * ue = ubuf + destsize;
while (up<ue && cp<ce)
{
unsigned char bits = *cp++;
for (int i = 0; i<8; i++, bits >>= 1)
{
if (bits & 1)
{
*up++ = *cp++;
*datalen = *datalen + 1;
}
else
{
signed int m1 = *cp++; // m1: pos
signed int m2 = *cp++; // m2: run
m1 |= (m2 & 0xF0) << 4;
// correct for sign bit
if (m1 & 0x800)
m1 |= 0xFFFFF000;
// add offsets
m2 = (m2 & 0x0F) + 3;
m1 += 18;
if (m1 > up - ubuf)
printf("uw2dec: pos exceeds buffer!");
// adjust pos to current 4k segment
while (m1 < (up - ubuf - 0x1000))
m1 += 0x1000;
while (m2-- && up < ue)
{
*up++ = ubuf[m1++];
*datalen = *datalen + 1;
}
}
if (up >= ue || cp >= ce)
break;
}
}
printf(" decoded %04x compressed bytes to %04x uncompressed data.\n",
cp - &src[4], up - ubuf);
}
else
{
// uncompressed
memcpy(dest, &src[0], blocksize);
*datalen = blocksize;
printf(" block was uncompressed.\n");
}
return dest;
}
unsigned char* unpackUW2(unsigned char *tmp, int address_pointer, int *datalen)
{
//Robbed and changed slightly from the Labyrinth of Worlds implementation project.
//This decompresses UW2 blocks.
fprintf(LOGFILE, "Unpacking block");
int len = getValAtAddress(tmp,address_pointer,32); //lword(base);
unsigned char* buf = new unsigned char[len+100];
unsigned char* up = buf;
*datalen = 0;
address_pointer += 4;
int upPtr = 0;
int bitBlock = 0;
while(up < buf+len)
{
int bits = tmp[address_pointer++];
//fprintf(LOGFILE, "\n\tBit Block %d at %d is %d", bitBlock++, address_pointer, bits);
for(int r=0; r<8; r++)
{
if(bits&1)
{
//printf("transfer %d\ at %d\n", byte(base),base);
//fprintf(LOGFILE, "\n\t\tTransferring from %d to %d Value %d", address_pointer, upPtr++, tmp[address_pointer]);
*up++ = tmp[address_pointer++];
*datalen = *datalen+1;
}
else
{
int o = tmp[address_pointer++];
int c = tmp[address_pointer++];
o |= (c&0xF0)<<4;
c = (c&15) + 3;
o = o+18;
int OrigOffset = o;
int uplessbuf = up - buf;
if(o > (up-buf))
o -= 0x1000;
while(o < (up-buf-0x1000))
o += 0x1000;
//fprintf(LOGFILE, "\n\t\tCopy Record C= %d O=%d (orig offset=%d)", c, o, OrigOffset);
while(c--)
{
//if (o < 0)
// {
// printf("Offset is less than 0!");
// }
//fprintf(LOGFILE, "\n\t\t\tCopying Value %d from %d to %d ", buf[o], o, upPtr++);
// fprintf(LOGFILE, "%d ", buf[o]);
*up++ = buf[o++];
*datalen = *datalen+1;
}
}
bits >>= 1;
}
}
return buf;
}
//****************************************************************************
/* The following procedure comes straight from Jim Cameron
http://madeira.physiol.ucl.ac.uk/people/jim
Specifically, this procedure can be found on his "Unofficial System Shock
Specifications" page at
http://madeira.physiol.ucl.ac.uk/people/jim/games/ss-res.txt
*/
void unpack_data (unsigned char *pack, unsigned char *unpack,
unsigned long unpacksize)
{
unsigned char *byteptr;
unsigned char *exptr;
unsigned long word = 0; /* initialise to stop "might be used before set" */
int nbits;
/* int type; */
int val;
int ntokens = 0;
static int offs_token [16384];
static int len_token [16384];
static int org_token [16384];
int i;
for (i = 0; i < 16384; ++i)
{
len_token [i] = 1;
org_token [i] = -1;
}
memset (unpack, 0, unpacksize);
byteptr = pack;
exptr = unpack;
nbits = 0;
while (exptr - unpack < unpacksize)
{
while (nbits < 14)
{
word = (word << 8) + *byteptr++;
nbits += 8;
}
nbits -= 14;
val = (word >> nbits) & 0x3FFF;
if (val == 0x3FFF)
{
break;
}
if (val == 0x3FFE)
{
for (i = 0; i < 16384; ++i)
{
len_token [i] = 1;
org_token [i] = -1;
}
ntokens = 0;
continue;
}
if (ntokens < 16384)
{
offs_token [ntokens] = exptr - unpack;
if (val >= 0x100)
{
org_token [ntokens] = val - 0x100;
}
}
++ntokens;
if (val < 0x100)
{
*exptr++ = val;
}
else
{
val -= 0x100;
if (len_token [val] == 1)
{
if (org_token [val] != -1)
{
len_token [val] += len_token [org_token [val]];
}
else
{
len_token [val] += 1;
}
}
for (i = 0; i < len_token [val]; ++i)
{
if (i + offs_token[val]<unpacksize)
{
*exptr++ = unpack[i + offs_token[val]];
}
else
{
printf("Oh shit");
}
}
}
}
}
// End of Jim Cameron's procedure
//****************************************************************************
long getShockBlockAddress(long BlockNo, unsigned char *tmp_ark , long *chunkPackedLength,long *chunkUnpackedLength, long *chunkType)
{
//Finds the address of the block based on the directory block no.
//Justs loops through until it finds a match.
int blnLevelFound =0;
long DirectoryAddress=getValAtAddress(tmp_ark,124,32);
//printf("\nThe directory is at %d\n", DirectoryAddress);
int NoOfChunks = getValAtAddress(tmp_ark,DirectoryAddress,16);
//printf("there are %d chunks\n",NoOfChunks);
long firstChunkAddress = getValAtAddress(tmp_ark,DirectoryAddress+2,32);
//printf("The first chunk is at %d\n", firstChunkAddress);
long address_pointer=DirectoryAddress+6;
long AddressOfBlockStart= firstChunkAddress;
for (int k=0; k< NoOfChunks; k++)
{
int chunkId = getValAtAddress(tmp_ark,address_pointer,16);
*chunkUnpackedLength =getValAtAddress(tmp_ark,address_pointer+2,24);
*chunkType = getValAtAddress(tmp_ark,address_pointer+5,8); //Compression.
*chunkPackedLength = getValAtAddress(tmp_ark,address_pointer+6,24);
short chunkContentType = getValAtAddress(tmp_ark,address_pointer+9,8);
//printf("Index: %d, Chunk %d, Unpack size %d, compression %d, packed size %d, content type %d\t",
// k,chunkId, *chunkUnpackedLength, *chunkType,*chunkPackedLength,chunkContentType);
//printf("Absolute address is %d\n",AddressOfBlockStart);
//target chunk id is 4005 + level no * 100 for levels
if (chunkId== BlockNo) //4005+ LevelNo*100
{
blnLevelFound=1;
address_pointer=0;
break;
}
AddressOfBlockStart=AddressOfBlockStart+ *chunkPackedLength;
if ((AddressOfBlockStart % 4) != 0)
AddressOfBlockStart = AddressOfBlockStart + 4 - (AddressOfBlockStart % 4); // chunk offsets always fall on 4-byte boundaries
address_pointer=address_pointer+10;
}
if (blnLevelFound == 0)
{
//printf("Level not found");
return -1;
}
else
{
return AddressOfBlockStart;
}
}
int LoadShockChunk(long AddressOfBlockStart, int chunkType, unsigned char *archive_ark, unsigned char *OutputChunk,long chunkPackedLength,long chunkUnpackedLength)
{
//Util to return an uncompressed shock block. Will use this for all future lookups and replace old ones
int chunkId;
// long chunkUnpackedLength;
//int chunkType=Compressed;//compression type
//long chunkPackedLength;
//long chunkContentType;
long filepos;
// long AddressOfBlockStart=0;
long address_pointer=4;
//int blnLevelFound=0;
//Find the address of the block. This will also return the file size.
//AddressOfBlockStart = getShockBlockAddress(ChunkNo,archive_ark,&chunkPackedLength,&chunkUnpackedLength,&chunkType);
if (AddressOfBlockStart == -1) {return -1;}
//if (chunkType ==1)
switch (chunkType)
{
case 0:
{//Flat uncompressed
for (long k=0; k< chunkUnpackedLength; k++)
{
OutputChunk[k] = archive_ark[AddressOfBlockStart+k];
}
return chunkUnpackedLength;
break;
}
case 1:
{//flat Compressed
//printf("\nCompressed chunk");
unsigned char *temp_ark = new unsigned char[chunkPackedLength];
for (long k=0; k< chunkPackedLength; k++)
{
temp_ark[k] = archive_ark[AddressOfBlockStart+k];
}
unpack_data(temp_ark,OutputChunk,chunkUnpackedLength);
return chunkUnpackedLength;
break;
}
case 3://Subdir compressed //Just return the compressed data and unpack the sub chunks individually?
{
//uncompressed the sub chunks
int NoOfEntries=getValAtAddress(archive_ark,AddressOfBlockStart,16);
int SubDirLength=(NoOfEntries+1) * 4 + 2;
unsigned char *temp_ark = new unsigned char[chunkPackedLength];
unsigned char *tmpchunk = new unsigned char[chunkUnpackedLength];
for (long k=0; k< chunkPackedLength; k++)
{
temp_ark[k] = archive_ark[AddressOfBlockStart+k+SubDirLength];
}
unpack_data(temp_ark,tmpchunk,chunkUnpackedLength);
//Merge my subdir and uncompressed subdir data back together.
for (long k=0;k<SubDirLength;k++)
{//Subdir
OutputChunk[k]=archive_ark[AddressOfBlockStart+k];
}
for (long k=SubDirLength;k<chunkUnpackedLength;k++)
{//Subdir
OutputChunk[k]=tmpchunk[k-SubDirLength];
}
return chunkUnpackedLength;
break;
}
case 2://Subdir uncompressed
//{
//printf("Uncompressed subdir!");
//}
default:
{//Uncompressed.
//printf("\nUncompressed chunk");
//OutputChunk = new unsigned char[chunkUnpackedLength];
for (long k=0; k< chunkUnpackedLength; k++)
{
OutputChunk[k] = archive_ark[AddressOfBlockStart+k];
}
return chunkUnpackedLength;
break;
}
}
}
/*
void RepackUW2(char InputFile[255], char OutputFile[255],int BlocksToUnpack)
{
//hopefully repacks a compressed level file into one containing uncompressed chuncks that I can edit to test object properties.
FILE *file = NULL;
char *filePath;
//char *filePathO;
long fileSize;
unsigned char *lev_ark;
int NoOfBlocks;
if ((file = fopen(InputFile, "rb")) == NULL)
{
printf("Could not open specified file\n");
return;
}
fileSize = getFileSize(file);
lev_ark = new unsigned char[fileSize];
fread(lev_ark, fileSize, 1, file);
fclose(file);
NoOfBlocks = getValAtAddress(lev_ark,0,16);
//tables that I have to write.
long *BlockAddress;
BlockAddress=new long[NoOfBlocks];
// long BlockAddress[NoOfBlocks]; //No of blocks in UW2
long PreviousUsedLength;
//int CompressionFlags[NoOfBlocks];
//int DataSize[NoOfBlocks];
//int AvailableSpace[NoOfBlocks];
long currAddress;
long address_pointer = 6;
int compressionFlag ;
int DataLen;
int isCompressed;
long *mapSize;
mapSize=new long[NoOfBlocks];
//long mapSize[BlocksToUnpack];// = 0x7c08;
fprintf(LOGFILE, "Original Blocks\n");
for (int x = 0; x < NoOfBlocks; x++)
{//Just dump out some original data for info.
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
compressionFlag = getValAtAddress(lev_ark, address_pointer + (UW2_COMPRESS_BLOCK *(NoOfBlocks * 4)) + (x * 4), 32);
long DataSizeVal = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
long DataAvail = getValAtAddress(lev_ark, address_pointer + (UW2_SPACE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
fprintf(LOGFILE, "Block no %d\n", x);
fprintf(LOGFILE, "\tAddress %d", currAddress);
fprintf(LOGFILE, "\tFlags %d\n", compressionFlag);
fprintf(LOGFILE, "\tData Size %d", DataSizeVal);
fprintf(LOGFILE, "\tAvailable %d\n", DataAvail);
fprintf(LOGFILE, "\tNext if using datasize %d\n", currAddress + DataSizeVal);
fprintf(LOGFILE, "\tNext if using available %d\n", currAddress + DataAvail);
//if (x < BlocksToUnpack)
// {//This default size covers most of the map data except for the animation overlays + other stuff I know nothing about.
// mapSize[x]=0x7c08;//default size
mapSize[x]= DataSizeVal;
// }
}
//Initial values for the loop for calculating block addresses.
BlockAddress[0] = getValAtAddress(lev_ark, 6 , 32);//The first block stays the same
int FoundFirst=0;
long LastBlockAddress = BlockAddress[0];
int firstBlock;//Containing Data.
PreviousUsedLength = 0;
for (int x = 0; x < NoOfBlocks; x++)
{
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
isCompressed = (compressionFlag >> 1) & 0x01;
if (x < BlocksToUnpack)
{
if (x == 80)
{
printf("");
}
if (currAddress != 0)
{//Map blocks are always compressed in UW2.
if (isCompressed == 1)
{
unpackUW2(lev_ark, currAddress, &DataLen); //Unpack and see how much space I need to allow for.
mapSize[x]=DataLen;
if (FoundFirst == 0)
{//The first block containing data stays the same.
if (REPACK_INTO_UW1_HACK == 1)
{
BlockAddress[x] = 1286;//Conversation start //currAddress;
}
else
{
BlockAddress[x]=currAddress;
}
firstBlock=x;
FoundFirst = 1;
}
else
{
BlockAddress[x] = LastBlockAddress + PreviousUsedLength; //Size of a map block
}
LastBlockAddress = BlockAddress[x];
PreviousUsedLength = mapSize[x];
}
else
{
if (FoundFirst == 0)
{
BlockAddress[x] = currAddress;
firstBlock = x;
FoundFirst = 1;
}
else
{
BlockAddress[x] = LastBlockAddress + PreviousUsedLength;
}
LastBlockAddress = BlockAddress[x];
PreviousUsedLength = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
//Set mapsize here???
}
}
else
{
BlockAddress[x] = 0;
mapSize[x]=0;
}
}
else
{
if (currAddress != 0)
{//is this correct for all compression flags???
BlockAddress[x] = LastBlockAddress + PreviousUsedLength;
LastBlockAddress = BlockAddress[x];
PreviousUsedLength = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
}
else
{
BlockAddress[x] = 0;
}
}
fprintf(LOGFILE, "\nBlock Address %d = %d", x, BlockAddress[x]);
}
if ((file = fopen(OutputFile, "w+b")) == NULL)
{
fprintf(LOGFILE, "Could not open specified file\n");
return;
}
//Write the number of blocks
WriteInt16(file,NoOfBlocks);
//write 4x0
//was wrrite int 32. conversations have 2? maps have 4?
WriteInt32(file, 0);
//write my block addresses. increment by the size of the decompressed data.
for (int x = 0; x < NoOfBlocks; x++)
{
//long currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
fprintf(LOGFILE, "\nBlock %d at %d",x, BlockAddress[x]);
WriteInt32(file, BlockAddress[x]);
}
if (REPACK_INTO_UW1_HACK==0)
{
//write my compression flags. each shoudld be 0 0 0
for (int x = 0; x < NoOfBlocks; x++)
{
if (x == 80)
{
printf("");
}
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
isCompressed = (compressionFlag >> 1) & 0x01;
if (x<BlocksToUnpack)
{
WriteInt32(file, 0);
}
else
{//copy the existing flags.
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
WriteInt32(file,compressionFlag);
}
}
//write the space used
for (int x = 0; x < NoOfBlocks; x++)
{
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
if (x <BlocksToUnpack)
{
if (currAddress != 0)
{
WriteInt32(file, mapSize[x]);
}
else
{
DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
WriteInt32(file, DataLen);
}
}
else
{//copy the existing values
DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
WriteInt32(file, DataLen);
}
}
//write my available space should not matter for decompression so I'll just put the block size here (?)
for (int x = 0; x < NoOfBlocks; x++)
{
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
if (x <BlocksToUnpack)
{
if (currAddress != 0)
{
WriteInt32(file, mapSize[x]);
}
else
{
int DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
WriteInt32(file, DataLen);
}
}
else
{//copy the existing values
int DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
WriteInt32(file, DataLen);
}
}
}
//write my uncompressed blocks.
for (int x = 0; x < NoOfBlocks; x++)
{
if (x == 80)
{
printf("");
}
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
isCompressed = (compressionFlag >> 1) & 0x01;
if (x < BlocksToUnpack)
{
if (currAddress!=0)
{
switch (isCompressed)
{
case 0:
//DataSize[x] = DataLen;
for (int y = currAddress; y < currAddress + DataLen; y++)
{//Copy the bytes
fputc(lev_ark[y], file);
}
break;
case 1://compressed block
case 2:
unsigned char *tmp_ark;
tmp_ark = unpackUW2(lev_ark, currAddress, &DataLen);
//DataSize[x] = DataLen;
for (int y = 0; y < mapSize[x]; y++)
{//Copy the bytes
fputc(tmp_ark[y], file);
}
break;
}
}
}
else
{ //non map. just copy the bytes
if (currAddress != 0)
{
for (int y = currAddress; y < currAddress+DataLen; y++)
{//Copy the bytes
fputc(lev_ark[y], file);
}
}
}
}
fclose(file);
if ((file = fopen(OutputFile, "rb")) == NULL)
{
fprintf(LOGFILE, "Could not open specified file\n");
return;
}
fileSize = getFileSize(file);
lev_ark = new unsigned char[fileSize];
fread(lev_ark, fileSize, 1, file);
fclose(file);
fprintf(LOGFILE, "New Blocks\n");
for (int x = 0; x < NoOfBlocks; x++)
{
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
long DataSizeVal = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
long DataAvail = getValAtAddress(lev_ark, address_pointer + (UW2_SPACE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
fprintf(LOGFILE, "Block no %d\n", x);
fprintf(LOGFILE, "\tAddress %d", currAddress);
fprintf(LOGFILE, "\tFlags %d\n", compressionFlag);
fprintf(LOGFILE, "\tData Size %d", DataSizeVal);
fprintf(LOGFILE, "\tAvailable %d\n", DataAvail);
fprintf(LOGFILE, "\tNext if using datasize %d\n", currAddress + DataSizeVal);
fprintf(LOGFILE, "\tNext if using available %d\n", currAddress + DataAvail);
}
}*/
/*
void RepackUW2(char InputFile[255], char OutputFile[255], int BlocksToUnpack)
{
//hopefully repacks a compressed level file into one containing uncompressed chuncks that I can edit to test object properties.
FILE *file = NULL;
char *filePath;
//char *filePathO;
long fileSize;
unsigned char *lev_ark;
int NoOfBlocks;
if ((file = fopen(InputFile, "rb")) == NULL)
{
printf("Could not open specified file\n");
return;
}
fileSize = getFileSize(file);
lev_ark = new unsigned char[fileSize];
fread(lev_ark, fileSize, 1, file);
fclose(file);
NoOfBlocks = getValAtAddress(lev_ark, 0, 16);
//tables that I have to write.
long *BlockAddress;
BlockAddress = new long[NoOfBlocks];
// long BlockAddress[NoOfBlocks]; //No of blocks in UW2
long PreviousUsedLength;
//int CompressionFlags[NoOfBlocks];
//int DataSize[NoOfBlocks];
//int AvailableSpace[NoOfBlocks];
long currAddress;
long address_pointer = 6;
int compressionFlag;
int DataLen;
int isCompressed;
long *mapSize;
mapSize = new long[NoOfBlocks];
//long mapSize[BlocksToUnpack];// = 0x7c08;
printf("Original Blocks\n");
for (int x = 0; x < NoOfBlocks; x++)
{//Just dump out some original data for info.
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
compressionFlag = getValAtAddress(lev_ark, address_pointer + (UW2_COMPRESS_BLOCK *(NoOfBlocks * 4)) + (x * 4), 32);
long DataSizeVal = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
long DataAvail = getValAtAddress(lev_ark, address_pointer + (UW2_SPACE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
printf("Block no %d\n", x);
printf("\tAddress %d", currAddress);
printf("\tFlags %d\n", compressionFlag);
printf("\tData Size %d", DataSizeVal);
printf("\tAvailable %d\n", DataAvail);
printf("\tNext if using datasize %d\n", currAddress + DataSizeVal);
printf("\tNext if using available %d\n", currAddress + DataAvail);
//if (x < BlocksToUnpack)
// {//This default size covers most of the map data except for the animation overlays + other stuff I know nothing about.
// mapSize[x]=0x7c08;//default size
mapSize[x] = DataSizeVal;
// }
}
//Initial values for the loop for calculating block addresses.
BlockAddress[0] = getValAtAddress(lev_ark, 6, 32);//The first block stays the same
long LastBlockAddress = BlockAddress[0];
PreviousUsedLength = 0;
for (int x = 0; x < NoOfBlocks; x++)
{
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
isCompressed = (compressionFlag >> 1) & 0x01;
if (x < BlocksToUnpack)
{
if (currAddress != 0)
{//Map blocks are always compressed in UW2.
if (isCompressed == 1)
{
unpackUW2(lev_ark, currAddress, &DataLen); //Unpack and see how much space I need to allow for.
mapSize[x] = DataLen;
BlockAddress[x] = LastBlockAddress + PreviousUsedLength; //Size of a map block
LastBlockAddress = BlockAddress[x];
PreviousUsedLength = mapSize[x];
}
else
{
BlockAddress[x] = LastBlockAddress + PreviousUsedLength;
LastBlockAddress = BlockAddress[x];
PreviousUsedLength = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
//Set mapsize here???
}
}
else
{
BlockAddress[x] = 0;
mapSize[x] = 0;
}
}
else
{
if (currAddress != 0)
{//is this correct for all compression flags???
BlockAddress[x] = LastBlockAddress + PreviousUsedLength;
LastBlockAddress = BlockAddress[x];
PreviousUsedLength = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
}
else
{
BlockAddress[x] = 0;
}
}
printf("\nBlock Address %d = %d", x, BlockAddress[x]);
}
if ((file = fopen(OutputFile, "w+b")) == NULL)
{
printf("Could not open specified file\n");
return;
}
//Write the number of blocks
WriteInt16(file, NoOfBlocks);
//write 4x0
WriteInt32(file, 0);
//write my block addresses. increment by the size of the decompressed data.
for (int x = 0; x < NoOfBlocks; x++)
{
//long currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
WriteInt32(file, BlockAddress[x]);
}
//write my compression flags. each shoudld be 0 0 0
for (int x = 0; x < NoOfBlocks; x++)
{
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
DataLen = getValAtAddress(lev_ark, address_pointer + (UW2_SIZE_BLOCK * (NoOfBlocks * 4)) + (x * 4), 32);
isCompressed = (compressionFlag >> 1) & 0x01;
if (x<BlocksToUnpack)
{//TODO:Bit mask the flag out
WriteInt32(file, 0);
}
else
{//copy the existing flags.
compressionFlag = getValAtAddress(lev_ark, address_pointer + (NoOfBlocks * 4) + (x * 4), 32);
WriteInt32(file, compressionFlag);
}
}
//write the space used
for (int x = 0; x < NoOfBlocks; x++)
{
currAddress = getValAtAddress(lev_ark, 6 + (x * 4), 32);
if (x <BlocksToUnpack)
{
if (currAddress != 0)