-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmadconv.c
138 lines (132 loc) · 2.78 KB
/
gmadconv.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
/*
* Modifications by Ben 'Cue' Woodford
* With Credit to SiPlus (original author)
* Also with Credit to zQaX for fixing an earlier issue
*/
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *name;
unsigned int size;
} addonfile;
char *ztstr(FILE *file)
{
int size;
char chr, *dest;
for (size = 1;; size++)
{
fread(&chr, 1, 1, file);
if (chr == '\0')
break;
}
fseek(file, -size, SEEK_CUR);
dest = malloc(size);
fread(dest, size, 1, file);
return dest;
}
FILE *fopenwb(char *path)
{
int len = strlen(path) + 1, i;
char *dirpath;
if (len == 1)
{
fputs("File in addon has no name\n", stderr);
exit(1);
}
dirpath = malloc(len);
for (i = 0; i < len; i++)
{
if (path[i] == '/')
{
dirpath[i] = '\0';
mkdir(dirpath, 0755);
}
dirpath[i] = path[i];
}
free(dirpath);
return fopen(path, "wb");
}
int main(const int argc, const char *argv[])
{
FILE *in, *out;
unsigned int filecnt = 0, filenum, i, j;
char *addonname, *addondesc, *addonauthor;
int curbyte;
addonfile *addonfiles = NULL, *curfile;
puts("GMADConv v2 by SiPlus");
if (argc < 2)
{
fputs("No file specified\n", stderr);
return 1;
}
in = fopen(argv[1], "rb");
if (in == NULL)
{
fprintf(stderr, "Error opening input file %s: %s\n", argv[1], strerror(errno));
return errno;
}
fread(&i, 4, 1, in);
if (i != 0x44414d47)
{
fputs("Input file is not addon\n", stderr);
return 1;
}
fseek(in, 18, SEEK_CUR);
addonname = ztstr(in);
addondesc = ztstr(in);
addonauthor = ztstr(in);
fseek(in, 4, SEEK_CUR);
puts("Extracting file names");
for (;;)
{
fread(&filenum, 4, 1, in);
if (filenum == 0)
break;
filecnt++;
addonfiles = realloc(addonfiles, filecnt * sizeof(addonfile));
curfile = &addonfiles[filecnt - 1];
curfile->name = ztstr(in);
fread(&curfile->size, 4, 1, in);
fseek(in, 8, SEEK_CUR);
}
if (addonfiles == NULL)
{
puts("Addon is empty");
return 0;
}
mkdir(addonname, 0755);
chdir(addonname);
for (i = 0; i < filecnt; i++)
{
curfile = &addonfiles[i];
printf("Extracting %s (%uB)\n", curfile->name, curfile->size);
out = fopenwb(curfile->name);
if (out == NULL)
{
fprintf(stderr, "Error opening output file %s: %s\n", curfile->name, strerror(errno));
return errno;
}
for (j = 0; j < curfile->size; j++)
{
fread(&curbyte, 1, 1, in);
fputc(curbyte, out);
}
fflush(out);
fclose(out);
}
puts("Writing info.txt");
out = fopen("info.txt", "wb");
if (out == NULL)
{
fprintf(stderr, "Error opening info.txt: %s\n", strerror(errno));
return errno;
}
fprintf(out,
"\"AddonInfo\"\r\n{\r\n\t\"name\" \"%s\"\r\n\t\"author_name\" \"%s\"\r\n\t\"info\" \"%s\"\r\n}", addonname, addonauthor, addondesc);
printf("Finished extracting addon %s\n", addonname);
chdir("..");
return 0;
}