// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/txp-convert.c
// Function: main
int main( int argc, char **argv ) {
	
	unsigned char headerdata[32], *readyframe = NULL;
	uint32_t *gamecheck = NULL;
	
	unsigned int i, game;
	
	txpHeader *header = NULL;
	textureHeader *textures = NULL;
	frameHeader *frames = NULL;
	
	char *outputname = NULL;
	
	FILE *f;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s TXP-file\n", argv[0]);
		return 1;
	}
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	
	outputname = malloc( strlen( argv[1] ) + 7 + 9 + 4 + 1 ); // will contain: infilename + "-tex###" + "-frame###" + ".png" + NULL
	
	fread( headerdata, 32, 1, f );
	gamecheck = (uint32_t *)(headerdata+4);
	
	game = (*gamecheck < 256) ? mgspo : mgspw;
	printf("TXP is %s compatible\n", (*gamecheck < 256) ? "portableops" : "peacewalker");
	
	header = getTxpHeader(headerdata, game);
	textures = getTextureInfos(f, *header, game);
	frames = getFrameInfos(f, *header, textures, game);
	
	getTexturePixels(f, *header, textures);
	getFramePixels(f, *header, textures, frames);
	
	for(i = 0; i < header->numframes; i++) {
	//~ for(i = 0; i < 1; i++) {
		printf("Processing frame %02d of %02d (texture %02d)...", i, header->numframes-1, frames[i].sourcetexture);
		fflush(stdout);
		readyframe = buildSingleFrameRgba(textures, frames[i]);
		printf("built ");
		fflush(stdout);
		sprintf(outputname, "%s-tex%03d-frame%03d.png", argv[1], frames[i].sourcetexture, i);
		lodepng_encode32_file(outputname, readyframe, frames[i].width, frames[i].height);
		printf("encoded ");
		fflush(stdout);
		free(readyframe);
		printf("managed\n");
		fflush(stdout);
	}
	
	fclose(f);
	
	for(i = 0; i < header->numtextures; i++) {
		free(textures[i].texpixels);
		if(textures[i].palpixels) free(textures[i].palpixels);
	}
	if(game == mgspw) for(i = 0; i < header->numframes; i++) free(frames[i].palpixels);
	
	free(header);
	free(textures);
	free(frames);
	free(outputname);
	
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbx
cmpl $0x1, %edi
jg 0xd79f
movq (%rbx), %rsi
leaq 0x24b1(%rip), %rdi      # 0xfc4b
jmp 0xdac5
movq 0x8(%rbx), %rdi
leaq 0x21b2(%rip), %rsi      # 0xf95c
callq 0x1130
movq 0x8(%rbx), %rsi
testq %rax, %rax
je 0xdabe
movq %rax, %r15
movq %rsi, %rdi
callq 0x1090
leaq 0x15(%rax), %rdi
callq 0x10f0
movq %rax, 0x60(%rsp)
leaq 0x80(%rsp), %r14
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x1060
movl 0x4(%r14), %eax
xorl %ebp, %ebp
movl %eax, 0x48(%rsp)
cmpl $0x100, %eax            # imm = 0x100
setae %bpl
leaq 0x249a(%rip), %rax      # 0xfca6
leaq 0x2487(%rip), %rsi      # 0xfc9a
cmovaeq %rax, %rsi
leaq 0x2466(%rip), %rdi      # 0xfc84
xorl %eax, %eax
callq 0x10a0
movq %r14, %rdi
movl %ebp, %esi
callq 0xca1b
movq %rbx, 0x78(%rsp)
movq %rax, %rbx
movl 0x10(%rax), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %ebp, %esi
callq 0xcb58
movq %rax, %r12
movl 0x10(%rbx), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0xce1c
movq %rax, %r14
movl 0x10(%rbx), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0xd32b
movl 0x10(%rbx), %eax
movl %eax, 0x40(%rsp)
movups (%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, 0x68(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r14, 0x50(%rsp)
movq %r14, %rdx
callq 0xd56d
movq %rbx, 0x70(%rsp)
movl 0xc(%rbx), %eax
testq %rax, %rax
movq %rax, 0x58(%rsp)
je 0xda0e
decl %eax
movl %eax, 0x4c(%rsp)
movq 0x50(%rsp), %r13
xorl %ebp, %ebp
movl 0x14(%r13), %r14d
leaq 0x238d(%rip), %rdi      # 0xfcb2
movl %ebp, %esi
movl 0x4c(%rsp), %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10a0
movq 0x66a4(%rip), %rax      # 0x13fe0
movq (%rax), %rdi
movq %rax, %rbx
callq 0x1100
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r12, %rdi
callq 0xd6aa
movq %rax, %r15
leaq 0x236c(%rip), %rdi      # 0xfce2
xorl %eax, %eax
callq 0x10a0
movq (%rbx), %rdi
callq 0x1100
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rdx
movq %r12, %rbx
movq 0x60(%rsp), %r12
movq %r12, %rdi
leaq 0x2349(%rip), %rsi      # 0xfce9
movl %r14d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x1140
movl 0x4(%r13), %edx
movl 0x8(%r13), %ecx
movq %r12, %rdi
movq %rbx, %r12
movq %r15, %rsi
callq 0xa104
leaq 0x2338(%rip), %rdi      # 0xfd02
xorl %eax, %eax
callq 0x10a0
movq 0x6608(%rip), %rbx      # 0x13fe0
movq (%rbx), %rdi
callq 0x1100
movq %r15, %rdi
callq 0x1030
leaq 0x2650(%rip), %rdi      # 0x1003f
callq 0x1050
movq (%rbx), %rdi
callq 0x1100
incq %rbp
addq $0x30, %r13
cmpq %rbp, 0x58(%rsp)
jne 0xd91a
movq 0x68(%rsp), %rdi
callq 0x1070
movq 0x70(%rsp), %r13
movl 0x4(%r13), %eax
testq %rax, %rax
movq 0x60(%rsp), %r15
movq 0x50(%rsp), %rbp
je 0xda5d
shlq $0x3, %rax
leaq (%rax,%rax,4), %rbx
xorl %r14d, %r14d
movq 0x18(%r12,%r14), %rdi
callq 0x1030
movq 0x20(%r12,%r14), %rdi
testq %rdi, %rdi
je 0xda54
callq 0x1030
addq $0x28, %r14
cmpq %r14, %rbx
jne 0xda3b
cmpl $0x100, 0x48(%rsp)      # imm = 0x100
movq 0x58(%rsp), %rax
jb 0xda8e
testl %eax, %eax
je 0xda8e
shlq $0x4, %rax
leaq (%rax,%rax,2), %rbx
xorl %r14d, %r14d
movq 0x28(%rbp,%r14), %rdi
callq 0x1030
addq $0x30, %r14
cmpq %r14, %rbx
jne 0xda7b
movq %r13, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
leaq 0x2584(%rip), %rdi      # 0x10039
callq 0x1050
xorl %eax, %eax
jmp 0xdad1
leaq 0x21a9(%rip), %rdi      # 0xfc6e
xorl %eax, %eax
callq 0x10a0
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq