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 23
/
Copy pathgamestrings.cpp
265 lines (235 loc) · 7.91 KB
/
gamestrings.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
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include "utils.h"
#include "main.h"
#include "gamestrings.h"
void unpackStrings(int game, char filePath[255])
{
//char filePath[255];
//switch (game)
// {
// case UWDEMO:
// case UW1:
// strcpy_s(filePath, UW1_STRINGS_FILE);break;
// case UW2:
// strcpy_s(filePath, UW2_STRINGS_FILE);break;
// case SHOCK:
// strcpy_s(filePath, SHOCK_STRINGS_FILE);break;
// }
huffman_node *hman;
block_dir *blocks;
unsigned char *Buffer;
long NoOfNodes; long NoOfStringBlocks;
long address_pointer=0;
short FirstLine=0;
//char *str;
FILE *file = NULL; // File pointer
//fopen_s(file,filePath, "rb");
if (fopen_s(&file,filePath, "rb") != 0)
{
printf("Could not open specified file\n");
}
else
{
// Get the size of the file in bytes
long fileSize = getFileSize(file);
// Allocate space in the buffer for the whole file
Buffer = new unsigned char[fileSize];
fread(Buffer, fileSize, 1,file);
fclose(file);
NoOfNodes=getValAtAddress(Buffer,address_pointer,16);
int i=0;
hman = new huffman_node [NoOfNodes];
address_pointer=address_pointer+2;
while (i<NoOfNodes)
{
hman[i].symbol= Buffer[address_pointer+0];
hman[i].parent= Buffer[address_pointer+1];
hman[i].left= Buffer[address_pointer+2];
hman[i].right= Buffer[address_pointer+3];
fprintf(LOGFILE, "Node:%d parent=%d, left=%d, right=%d, symbol=%c\n", i, hman[i].parent, hman[i].left, hman[i].right, hman[i].symbol);
i++;
address_pointer=address_pointer+4;
}
//next is the number of string blocks
NoOfStringBlocks=getValAtAddress(Buffer,address_pointer,16);
blocks=new block_dir[NoOfStringBlocks];
address_pointer=address_pointer+2;
i=0;
while (i<NoOfStringBlocks)
{
blocks[i].block_no = getValAtAddress(Buffer,address_pointer,16);
address_pointer=address_pointer+2;
blocks[i].address = getValAtAddress(Buffer,address_pointer,32);
address_pointer=address_pointer+4;
blocks[i].NoOfEntries = getValAtAddress(Buffer,blocks[i].address,16); //look ahead and get no of entries.
i++;
}
i=0;
while (i<NoOfStringBlocks)
{
//printf("Block %d is at address %d. It has %d entries\n",blocks[i].block_no,blocks[i].address, blocks[i].NoOfEntries );
address_pointer=2 + blocks[i].address + blocks[i].NoOfEntries *2;
//printf("It's strings begin at %d\n", address_pointer);
//printf("It should end at %d\n",blocks[i+1].address );
//fprintf(LOGFILE, "\n+=====================================+\n");
//fprintf(LOGFILE, "Block Name: %d\n", blocks[i].block_no);
long strAdd;
int blnFnd;
strAdd= address_pointer;
int iteration = 0;
for (int j=0;j< blocks[i].NoOfEntries;j++)
{
//Based on abysmal /uwadv implementations.
blnFnd=0;
char c;
int bit = 0;
int raw = 0;
int node=0;
do {
node = NoOfNodes - 1; // starting node
// huffman tree decode loop
while (char(hman[node].left) != -1
&& char(hman[node].right) != -1)
{
iteration++;
if (bit == 0) {
bit = 8;
raw = Buffer[address_pointer++]; //stream.get<uint8_t>();
}
// decide which node is next
//node = raw & 0x80 ? short (hman[node].right)
// : short (hman[node].left);
//printf("\nRaw = %d = %d", raw, raw & 0x80);
if (raw & 0x80)
{
node = short(hman[node].right);
}
else
{
node = short(hman[node].left);
}
raw <<= 1;
bit--;
}
// have a new symbol
if ((hman[node].symbol !='|')){
if (blnFnd==0)
//{printf("\nBlock %d String %d at %d:",blocks[i].block_no, j, strAdd); }
{
if (FirstLine != 0){ fprintf(LOGFILE, "\n"); }else{ FirstLine = 1; }
//printf("\n%03d=",j);
fprintf(LOGFILE, "%03d~%03d~%03d~", i, blocks[i].block_no, j);
}
if ((hman[node].symbol == 10))
{
fprintf(LOGFILE, " \\n");
}
else
{
fprintf(LOGFILE, "%c", hman[node].symbol);
// printf("%c", hman[node].symbol);
}
blnFnd = 1;
}
} while (hman[node].symbol != '|');
}
i++;
}
}
}
void unpackStringsShock(char filePath[255])
{//this looks familiar.
//long BlockNo;
unsigned char *tmp_ark ;
long chunkPackedLength;
long chunkUnpackedLength;
//char *filePath = SHOCK_STRINGS_FILE;
FILE *file = NULL; // File pointer
if (fopen_s(&file,filePath, "rb") != 0)
{
printf("Could not open specified file\n");
}
else
{
// Get the size of the file in bytes
long fileSize = getFileSize(file);
// Allocate space in the tmp_ark for the whole file
tmp_ark = new unsigned char[fileSize];
fread(tmp_ark, fileSize, 1,file);
fclose(file);
int blnLevelFound =0;
long DirectoryAddress=getValAtAddress(tmp_ark,124,32);
printf("\nThe directory is at %d\n", DirectoryAddress);
long NoOfChunks = getValAtAddress(tmp_ark,DirectoryAddress,16);
//fprintf(LOGFILE, "there are %d chunks\n", NoOfChunks);
long firstChunkAddress = getValAtAddress(tmp_ark,DirectoryAddress+2,32);
//fprintf(LOGFILE, "The first chunk is at %d\n", firstChunkAddress);
long address_pointer=DirectoryAddress+6;
long AddressOfBlockStart= firstChunkAddress;
for (int k=0; k< NoOfChunks; k++)
{
long chunkId = getValAtAddress(tmp_ark,address_pointer,16);
chunkUnpackedLength =getValAtAddress(tmp_ark,address_pointer+2,24);
long chunkType = getValAtAddress(tmp_ark,address_pointer+5,8);
chunkPackedLength = getValAtAddress(tmp_ark,address_pointer+6,24);
long chunkContentType = getValAtAddress(tmp_ark,address_pointer+9,8);
//fprintf(LOGFILE, "Index: %d, Chunk %d, Unpack size %d, compression %d, packed size %d, content type %d\t", k, chunkId, chunkUnpackedLength, chunkType, chunkPackedLength, chunkContentType);
//fprintf(LOGFILE, "Absolute address is %d\n", AddressOfBlockStart);
long NoSubChunks = getValAtAddress(tmp_ark,AddressOfBlockStart,16);
//fprintf(LOGFILE, "\nIndex: %d Chunk %d has %d sub chunks", k, chunkId, NoSubChunks);
//fprintf(LOGFILE, "\n@Chunk:%d\n{", chunkId);
//fprintf(LOGFILE, "\n+=====================================+\n");
long strPtr=2;
for (int i = 0; i<NoSubChunks;i++)
{
long subChunkStart =AddressOfBlockStart+ getValAtAddress(tmp_ark,AddressOfBlockStart+strPtr,32);
long subChunkEnd = AddressOfBlockStart+getValAtAddress(tmp_ark,AddressOfBlockStart+strPtr+4,32);
if (subChunkEnd > subChunkStart)
{
fprintf(LOGFILE, "%d€%03d€%03d€", k, chunkId, i);
//fprintf(LOGFILE, "%d€%03d€%03d€", k, chunkId, i);
fprintf(LOGFILE, "%.*s\n", subChunkEnd - subChunkStart, tmp_ark + subChunkStart);
}
strPtr+=4;
}
//fprintf(LOGFILE, "}\n");
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;
}
}
}
int lookupString(int BlockNo, int StringNo, char StringOut[255] )
{//looks up a flat file of strings to find the specified value
//
int BlockNoFnd=0;
int StringNoFnd=0;
char stringContents[255];
char filePath[255];
char line[255];
FILE *file = NULL; // File pointer
strcpy_s(filePath,255,UW1_CLEAN_STRINGS_FILE);
if (fopen_s(&file, filePath, "r") == 0)
{
while (fgets(line,255,file))
{
sscanf(line,"%d€%d€%[^\n]",&BlockNoFnd,&StringNoFnd,&stringContents);
if ((BlockNo==BlockNoFnd) && (StringNo==StringNoFnd))
{
fclose(file);
strcpy_s(StringOut ,255,stringContents);
return 1;
}
}
fclose(file);
return -1;
}
else
{
return -1;
}
}