-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathagsalphahack.c
66 lines (57 loc) · 1.92 KB
/
agsalphahack.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
#define _GNU_SOURCE
#include "DataFile.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "version.h"
#define ADS ":::AGSalphahack " VERSION " by rofl0r:::"
static int usage(char *argv0) {
fprintf(stderr, ADS "\nusage:\n%s [-s spriteno] DIR\n"
"removes alphachannel flag from specified sprite in game file.\n"
"if spriteno is -1 (default), all flags are removed.\n"
"gamefile is typically ac2game.dta or game28.dta inside DIR.\n"
"this is useful when sprites have been hacked/converted to 16bit.\n"
"you should make a backup of the file before doing this.\n"
, argv0);
return 1;
}
#define SPF_ALPHACHANNEL 0x10
int main(int argc, char**argv) {
int c, flags = 0, spriteno = -1;
while ((c = getopt(argc, argv, "s:")) != EOF) switch(c) {
case 's': spriteno = atoi(optarg); break;
default: return usage(argv[0]);
}
if(!argv[optind]) return usage(argv[0]);
char *dir = argv[optind];
ADF a_b, *a = &a_b;
char fnbuf[512];
if(!ADF_find_datafile(dir, fnbuf, sizeof(fnbuf)))
return 1;
enum ADF_open_error aoe = ADF_open(a, fnbuf);
if(aoe != AOE_success && aoe <= AOE_gamebase) {
fprintf(stderr, "failed to open/process data file: %s\n", AOE2str(aoe));
return 1;
} else if (aoe != AOE_success) {
fprintf(stderr, "warning: failed to process some non-essential parts (%s) of gamefile, probably from a newer game format\n", AOE2str(aoe));
}
off_t off = ADF_get_spriteflagsstart(a);
unsigned nsprites = ADF_get_spritecount(a);
ADF_close(a);
printf("removing alpha of %u out of %u spriteflags.\n", spriteno==-1?nsprites:1, nsprites);
FILE *f = fopen(fnbuf, "r+b");
if(!f) return 1;
fseeko(f, off, SEEK_SET);
unsigned char *buf = malloc(nsprites);
fread(buf, 1, nsprites, f);
size_t i;
for(i=0;i<nsprites;++i) {
if(spriteno == -1 || spriteno == i)
buf[i] = buf[i] & (~SPF_ALPHACHANNEL);
}
fseeko(f, off, SEEK_SET);
fwrite(buf, 1, nsprites, f);
fclose(f);
free(buf);
return 0;
}