-
Notifications
You must be signed in to change notification settings - Fork 108
/
unnpk.c
280 lines (260 loc) · 9.56 KB
/
unnpk.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <zlib.h>
#include <magic.h>
int main(int argc, char **argv)
{
printf("#unnpk!\n");
if (argc != 3)
{
printf("\tHelp: unnpk npk_file unnpk_direcory\n");
return 0;
}
char *npk_path = argv[1];
char *out_path = argv[2];
//打开npk
FILE *npk = fopen(npk_path, "rb");
if (npk == NULL)
{
fprintf(stderr, "E: npk file open failed\n");
return 1;
}
//输出文件夹
if (out_path[strlen(out_path) - 1] == '/')
out_path[strlen(out_path) - 1] = 0;
if (mkdir(out_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
{
fprintf(stderr, "W: mkdir failed\n");
}
//读取文件大小
fseek(npk, 0L, SEEK_END);
size_t npk_size = ftell(npk);
//读取map偏移量
fseek(npk, 0x14, SEEK_SET);
uint32_t map_offset;
fread(&map_offset, 4, 1, npk);
//准备读取
uint32_t file_info[7];
FILE *file_out = NULL;
char *file_out_name = 0;
if (!(file_out_name = malloc(strlen(out_path) + 1 /* / */ + 30 /* 类型文件夹 */ + 1 /* / */ + 8 /* 文件名 */ + 20 /* .扩展名 */ + 1 /* 0 */)))
{
fprintf(stderr, "E: No enough memory!\n");
exit(1);
}
char *file_read_buf = NULL;
char *file_out_buf = NULL;
uLongf file_destLen = 0;
char *file_out_type = NULL; //存储Mime类型
char *file_out_type_p = NULL; //处理Mime类型文件夹用
char *file_out_extension = NULL; //存储扩展名
printf("| Index\t\t | Offset\t | Size\t\t | Unzip size\t | zip\t | MIME Type\t | Extension\t |\n| -\t\t | -\t\t | -\t\t | -\t\t | -\t | -\t\t | -\t\t |\n");
for (int file_offset = map_offset; file_offset < npk_size; file_offset += 7 * 4)
{
//map读取文件信息
fseek(npk, file_offset, SEEK_SET);
fread(&file_info, 4, 7, npk);
//读取数据
if (!(file_read_buf = malloc(file_info[2])))
{
fprintf(stderr, "E: No enough memory!\n");
exit(1);
}
fseek(npk, file_info[1], SEEK_SET);
fread(file_read_buf, 1, file_info[2], npk);
// file_info[2] 和 file_info[3] 大小不匹配说明数据压缩了
if (file_info[6] || file_info[2] != file_info[3])
{
//解压
if (!(file_out_buf = malloc(file_info[3])))
{
fprintf(stderr, "E: No enough memory!\n");
exit(1);
}
file_destLen = file_info[3];
switch (uncompress((uint8_t*)file_out_buf, &file_destLen, (uint8_t*)file_read_buf, file_info[2]))
{
case Z_OK:
//clear
free(file_read_buf);
file_read_buf = 0;
break;
case Z_MEM_ERROR:
fprintf(stderr, "E: Uncompress failed!\n");
fprintf(stderr, "Z_MEM_ERROR: No enough memory\n");
exit(1);
break;
case Z_BUF_ERROR:
free(file_out_buf);
file_out_buf = 0;
file_out_buf = file_read_buf;
file_read_buf = 0;
file_info[3] = file_info[2];
fprintf(stderr, "W: Uncompress failed!\n");
fprintf(stderr, "Z_BUF_ERROR: Map is not right, The raw data will be output\n");
break;
case Z_DATA_ERROR:
free(file_out_buf);
file_out_buf = 0;
file_out_buf = file_read_buf;
file_read_buf = 0;
file_info[3] = file_info[2];
fprintf(stderr, "W: Uncompress failed!\n");
fprintf(stderr, "Z_DATA_ERROR: Data is not zlib, The raw data will be output\n");
break;
}
}
else
{
file_out_buf = file_read_buf;
file_read_buf = 0;
}
//获取MIME类型辅助判断
magic_t cookie;
cookie = magic_open(MAGIC_MIME_TYPE);
magic_load(cookie, NULL);
file_out_type = (char *)magic_buffer(cookie, file_out_buf, file_info[3]);
//判断文件扩展名
if (strstr(file_out_type, "image/png"))
{
file_out_extension = ".png";
}
else if (strstr(file_out_type, "image/jpeg"))
{
file_out_extension = ".jpg";
}
else if (strstr(file_out_type, "image/vnd.adobe.photoshop"))
{
file_out_extension = ".psd";
}
else if (strstr(file_out_type, "video/mp4"))
{
file_out_extension = ".mp4";
}
else if (strstr(file_out_type, "application/zip"))
{
file_out_extension = ".zip";
}
else if (strstr(file_out_type, "application/font-sfnt"))
{
file_out_extension = ".ttf";
}
else if (strstr(file_out_type, "application/vnd.ms-excel"))
{
file_out_extension = ".xls";
}
else if (strstr(file_out_type, "xml"))
{
file_out_extension = ".xml";
}
else if (strcmp(file_out_buf + 1, "KTX") == 0)
{
file_out_extension = ".ktx";
}
else if (strcmp(file_out_buf, "RGIS") == 0)
{
file_out_extension = ".RGIS";
}
else if (strcmp(file_out_buf, "PKM") == 0)
{
file_out_extension = ".PKM";
}
else if (strstr(file_out_type, "text"))
{
if (strcmp(file_out_buf, "<NeoX") == 0 || strcmp(file_out_buf, "<Neox") == 0)
{
file_out_extension = ".NeoX.xml";
}
else if (strcmp(file_out_buf, "<FxGroup") == 0)
{
file_out_extension = ".FxGroup.xml";
}
else if (strcmp(file_out_buf, "<SceneMusic") == 0)
{
file_out_extension = ".SceneMusic.xml";
}
else if (strcmp(file_out_buf, "<MusicTriggers") == 0)
{
file_out_extension = ".MusicTriggers.xml";
}
else if (strcmp(file_out_buf, "<cinematic") == 0)
{
file_out_extension = ".cinematic.xml";
}
else if (strcmp(file_out_buf, "<EquipList") == 0)
{
file_out_extension = ".EquipList.xml";
}
else if (strcmp(file_out_buf, "<SceneConfig") == 0)
{
file_out_extension = ".SceneConfig.xml";
}
else if (strcmp(file_out_buf, "<SceneRoad") == 0)
{
file_out_extension = ".SceneRoad.xml";
}
else if (file_out_buf[0] == '{' && file_out_buf[file_info[3] - 1] == '}')
{
file_out_extension = ".json";
}
else if (memmem(file_out_buf, file_info[3], "vec4", 4) || memmem(file_out_buf, file_info[3], "vec2", 4) || memmem(file_out_buf, file_info[3], "tex2D", 5) || memmem(file_out_buf, file_info[3], "tex3D", 5) || memmem(file_out_buf, file_info[3], "float", 5) || memmem(file_out_buf, file_info[3], "define", 5) || memmem(file_out_buf, file_info[3], "incloud", 5))
{
file_out_extension = ".glsl";
}
else if (memmem(file_out_buf, file_info[3], "v ", 2) && memmem(file_out_buf, file_info[3], "vt ", 3) && memmem(file_out_buf, file_info[3], "f ", 2))
{
file_out_extension = ".obj";
}
else
{
file_out_extension = ".txt";
}
}
else
{
file_out_extension = "";
}
//输出文件名
sprintf(file_out_name, "%s/%s/%08X%s", out_path, file_out_type, file_info[0], file_out_extension);
//创建mime分类文件夹
file_out_type_p = file_out_type;
while ((file_out_type_p = strchr(file_out_type_p, '/')) != NULL)
{
file_out_type_p ++;
file_out_name[strlen(out_path) + (file_out_type_p - file_out_type)] = '\0';
mkdir(file_out_name, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
file_out_name[strlen(out_path) + 1 + (file_out_type_p - 1 - file_out_type)] = '/';
}
file_out_name[strlen(out_path) + 1 + strlen(file_out_type)] = '\0';
mkdir(file_out_name, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
file_out_name[strlen(out_path) + 1 + strlen(file_out_type)] = '/';
//打开并写入数据
file_out = fopen(file_out_name, "w+");
fwrite(file_out_buf, 1, file_info[3], file_out);
fclose(file_out);
//控制台输出文件信息
printf("| %08X\t | 0x%08X\t | %.3g %s\t | %.3g %s\t | %s\t | %s\t | %s\t |\n",
file_info[0],
file_info[1],
(file_info[2] > 1000000) ? (float)file_info[2] / 1000000 : (file_info[2] > 1000) ? (float)file_info[2] / 1000 : (float)file_info[2],
(file_info[2] > 1000000) ? "MB" : (file_info[2] > 1000) ? "KB" : "Byte",
(file_info[3] > 1000000) ? (float)file_info[3] / 1000000 : (file_info[3] > 1000) ? (float)file_info[3] / 1000 : (float)file_info[3],
(file_info[3] > 1000000) ? "MB" : (file_info[3] > 1000) ? "KB" : "Byte",
file_info[6] ? "Yes" : "No",
file_out_type,
strlen(file_out_extension)?file_out_extension:"None");
//clear
free(file_out_buf);
file_out_buf = 0;
magic_close(cookie);
}
//clear
fclose(npk);
free(file_out_name);
file_out_name = 0;
return 0;
}