// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/face-extract.c
// Function: main
int main( int argc, char **argv ) {
	
	char execpath[4096];
	char *namestring = NULL, dirname[128] = {0}, filename[256] = {0};
	unsigned int i, j, datsize, datoffset = 0;
	unsigned int groupnum = 0;
	unsigned char *data = NULL, *rgba = NULL;
	uint16_t *palsource = NULL;
	
	uint32_t numfaces, numframes;
	
	faceinfo *faces = NULL;
	uint32_t *simpleoffsets = NULL;
	animationinfo *fullani = NULL;
	imageinfo *image = NULL;
	
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s [FACE.DAT]\n", argv[0]);
		return 1;
	}
	
	if(strlen(argv[0]) > 4096) BREAK("Path to executable exceeds 4096 chars!\n")
	strcpy(execpath, argv[0]);
	cleanExecPath(execpath);
	
	numcommondic = loaddic(&commondic, execpath, "mgs1-face", DIC_HASH_SINGLE_EXT, StrCode16);
	
	FILE *f;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_END);
	datsize = ftell(f);
	fseek(f, 0, SEEK_SET);
	
	while(datoffset<datsize) {
		//~ printf("offset %08x\n", (unsigned int)ftell(f));
		snprintf(dirname, 128, "%04d-%08x", groupnum, (unsigned int)ftell(f));
		
		fread(&numfaces, 4, 1, f);
		if(numfaces) {
			createDirectory(dirname);
			faces = malloc(sizeof(faceinfo)*numfaces);
			fread(faces, sizeof(faceinfo)*numfaces, 1, f);
			
			for(i = 0; i < numfaces; i++) {
				printf("group %d face %02d of %02d\r", groupnum, i+1, numfaces);
				namestring = getNameFromDetails(faces[i].hash);
				
				data = malloc(faces[i].size);
				fseek(f, datoffset+faces[i].offset+4, SEEK_SET);
				fread(data, faces[i].size, 1, f);
				
				numframes = *(uint32_t *)data;
				
				if(numframes == 0x20) {
					/* simple */
					//~ printf("simple\n");
					simpleoffsets = (uint32_t *)(data+4);
					palsource = (uint16_t *)(data+32);
					for(j = 0; j < 7; j++) {
						if(simpleoffsets[j]) {
							//~ printf("x");
							image = (imageinfo *)(data+simpleoffsets[j]);
							rgba = pixelsToRGBA(data+simpleoffsets[j]+4, palsource, image->w, image->h);
							snprintf(filename, 256, "%s/%03d-%02d-x%03d-y%03d-%s", dirname, i, j, image->x, image->y, namestring);
							lodepng_encode32_file(filename, rgba, image->w, image->h);
							free(rgba);
						}
						//~ else printf("-");
					}
					//~ printf("\n");
				}
				else {
					/* full */
					fullani = (animationinfo *)(data+4);
					for(j = 0; j < numframes; j++) {
						palsource = (uint16_t *)(data+fullani[j].palette);
						image = (imageinfo *)(data+fullani[j].bitmap);
						//~ printf("off %08x - w %03x - h %03d\n", fullani[j].bitmap+4, image->w, image->h);
						rgba = pixelsToRGBA(data+fullani[j].bitmap+4, palsource, image->w, image->h);
						snprintf(filename, 256, "%s/%03d-%02d-x%03d-y%03d-%s", dirname, i, j, image->x, image->y, namestring);
						lodepng_encode32_file(filename, rgba, image->w, image->h);
						free(rgba);
					}
				}
				free(data);
				free(namestring);
			}
			printf("\n");
			free(faces);
		}
		if(ftell(f)%0x800) fseek(f, (0x800-(ftell(f)%0x800)), SEEK_CUR);
		datoffset = ftell(f);
		groupnum++;
	}
	
	
	printf("Done.\n");
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11e8, %rsp           # imm = 0x11E8
movq %rsi, %rbx
movl %edi, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
leaq 0xe0(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x1100
movq (%rbx), %r14
cmpl $0x1, %ebp
jg 0xd00d
leaq 0x2a79(%rip), %rdi      # 0xfa7a
movq %r14, %rsi
xorl %eax, %eax
callq 0x10c0
jmp 0xd029
movq %r14, %rdi
callq 0x10b0
cmpq $0x1001, %rax           # imm = 0x1001
jb 0xd040
leaq 0x2ae3(%rip), %rdi      # 0xfb07
callq 0x1080
movl $0x1, %eax
addq $0x11e8, %rsp           # imm = 0x11E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1060
movq %r15, %rdi
movl $0x2f, %esi
callq 0x10f0
movb $0x0, (%rax)
leaq 0x60a6(%rip), %rdi      # 0x13110
leaq 0x2a2e(%rip), %rdx      # 0xfa9f
leaq -0xbd9c(%rip), %r8      # 0x12dc
movq %r15, %rsi
movl $0x2, %ecx
callq 0xcb0c
movl %eax, 0x6095(%rip)      # 0x13120
movq 0x8(%rbx), %rdi
leaq 0x28c6(%rip), %rsi      # 0xf95c
callq 0x11a0
testq %rax, %rax
je 0xd427
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1180
movq %r12, %rdi
callq 0x1140
movq %rax, %rbx
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1180
movq %rbx, 0x38(%rsp)
testl %ebx, %ebx
je 0xd40c
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
movq %r12, 0x40(%rsp)
movq %r12, %rdi
callq 0x1140
movl $0x80, %esi
leaq 0x60(%rsp), %rdi
leaq 0x29bc(%rip), %rdx      # 0xfabf
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x10e0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x24(%rsp), %rdi
movq %r12, %rcx
callq 0x1090
movl 0x24(%rsp), %r14d
testq %r14, %r14
je 0xd3b1
leaq 0x60(%rsp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1070
leaq (,%r14,4), %rax
leaq (%rax,%rax,2), %rbx
movq %rbx, %rdi
callq 0x1160
movl $0x1, %edx
movq %rax, 0x28(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x1090
movq 0x8(%rsp), %rax
addl $0x4, %eax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
movl %ebp, 0x20(%rsp)
movq %r14, 0x48(%rsp)
movq %rax, %rbx
leaq 0x1(%rax), %rdx
leaq 0x2934(%rip), %rdi      # 0xfac9
movl %ebp, %esi
movq %rdx, 0x50(%rsp)
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10c0
movq %rbx, 0x30(%rsp)
leaq (%rbx,%rbx,2), %r15
movq 0x28(%rsp), %r13
movzwl 0x2(%r13,%r15,4), %edi
callq 0xce1f
movq %rax, 0x10(%rsp)
movl 0x4(%r13,%r15,4), %ebx
movq %rbx, %rdi
callq 0x1160
movq %rax, %r14
movl 0x8(%r13,%r15,4), %esi
addl 0x8(%rsp), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1180
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x1090
movl (%r14), %eax
testq %rax, %rax
je 0xd36f
cmpl $0x20, %eax
jne 0xd2c1
leaq 0x20(%r14), %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movl 0x4(%r14,%r15,4), %ebp
testq %rbp, %rbp
je 0xd2af
leaq (%r14,%rbp), %rdi
addq $0x4, %rdi
movzbl 0x2(%r14,%rbp), %r13d
movzbl 0x3(%r14,%rbp), %r12d
movq 0x18(%rsp), %rsi
movl %r13d, %edx
movl %r12d, %ecx
callq 0xcf29
movq %rax, %rbx
movzbl (%r14,%rbp), %r10d
movzbl 0x1(%r14,%rbp), %r11d
subq $0x8, %rsp
movl $0x100, %esi            # imm = 0x100
leaq 0xe8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x286f(%rip), %rdx      # 0xfae5
leaq 0x68(%rsp), %rcx
movq 0x38(%rsp), %r8
movl %r15d, %r9d
xorl %eax, %eax
pushq 0x18(%rsp)
pushq %r11
pushq %r10
callq 0x10e0
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbx, %rsi
movl %r13d, %edx
movl %r12d, %ecx
callq 0xa220
movq %rbx, %rdi
callq 0x1030
incq %r15
cmpq $0x7, %r15
jne 0xd21b
jmp 0xd36f
leaq 0x8(%r14), %rbp
xorl %r12d, %r12d
movq %rax, 0x58(%rsp)
movl -0x4(%rbp), %esi
movl (%rbp), %r15d
addq %r14, %rsi
leaq (%r14,%r15), %rdi
addq $0x4, %rdi
movzbl 0x2(%r14,%r15), %edx
movl %edx, 0x18(%rsp)
movzbl 0x3(%r14,%r15), %r13d
movl %r13d, %ecx
callq 0xcf29
movq %rax, %rbx
movzbl (%r14,%r15), %r10d
movzbl 0x1(%r14,%r15), %r11d
subq $0x8, %rsp
movl $0x100, %esi            # imm = 0x100
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
leaq 0x27c5(%rip), %rdx      # 0xfae5
leaq 0x68(%rsp), %rcx
movq 0x38(%rsp), %r8
movl %r12d, %r9d
xorl %eax, %eax
pushq 0x18(%rsp)
pushq %r11
pushq %r10
callq 0x10e0
addq $0x20, %rsp
movq %r15, %rdi
movq %rbx, %rsi
movl 0x18(%rsp), %edx
movl %r13d, %ecx
callq 0xa220
movq %rbx, %rdi
callq 0x1030
movq 0x58(%rsp), %rax
incq %r12
addq $0xc, %rbp
cmpq %r12, %rax
jne 0xd2cd
movq %r14, %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
callq 0x1030
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %rax
cmpq %r14, %rax
movq 0x40(%rsp), %r12
movl 0x20(%rsp), %ebp
jne 0xd187
movl $0xa, %edi
callq 0x1040
movq 0x28(%rsp), %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1140
shlq $0x35, %rax
je 0xd3f3
movq %r12, %rdi
callq 0x1140
leaq 0x7ff(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
andq $-0x800, %rsi           # imm = 0xF800
subq %rax, %rsi
addq $0x800, %rsi            # imm = 0x800
movq %r12, %rdi
movl $0x1, %edx
callq 0x1180
movq %r12, %rdi
callq 0x1140
incl %ebp
movq %rax, 0x8(%rsp)
cmpl 0x38(%rsp), %eax
jb 0xd0ea
leaq 0x26ee(%rip), %rdi      # 0xfb01
callq 0x1080
movq %r12, %rdi
callq 0x10a0
xorl %eax, %eax
jmp 0xd02e
movq 0x8(%rbx), %rsi
leaq 0x2677(%rip), %rdi      # 0xfaa9
jmp 0xd004