// File: FreeSlave[P]vhlt/ripent/ripent.cpp
// Function: WriteTextures(char const*)
static void		WriteTextures(const char* const name)
{
	char wadfilename[_MAX_PATH];
	FILE *wadfile;
#ifdef ZHLT_DEFAULTEXTENSION_FIX
	safe_snprintf(wadfilename, _MAX_PATH, "%s.wad", name);
#else
    safe_strncpy(wadfilename, name, _MAX_PATH);
    StripExtension(wadfilename);
    DefaultExtension(wadfilename, ".wad");
#endif
    _unlink(wadfilename);
	wadfile = SafeOpenWrite (wadfilename);
	Log("\nWriting %s.\n", wadfilename);

    char texfilename[_MAX_PATH];
	FILE *texfile;
#ifdef ZHLT_DEFAULTEXTENSION_FIX
	safe_snprintf(texfilename, _MAX_PATH, "%s.tex", name);
#else
    safe_strncpy(texfilename, name, _MAX_PATH);
    StripExtension(texfilename);
    DefaultExtension(texfilename, ".tex");
#endif
    _unlink(texfilename);
	if (!g_textureparse)
	{
		int dataofs = (int)(intptr_t)&((dmiptexlump_t*)NULL)->dataofs[((dmiptexlump_t*)g_dtexdata)->nummiptex];
		int wadofs = sizeof(wadinfo_t);

		wadinfo_t header;
		header.identification[0] = 'W';
		header.identification[1] = 'A';
		header.identification[2] = 'D';
		header.identification[3] = '3';
		header.numlumps = ((dmiptexlump_t*)g_dtexdata)->nummiptex;
		header.infotableofs = g_texdatasize - dataofs + wadofs;
		SafeWrite (wadfile, &header, wadofs);

		SafeWrite (wadfile, (byte *)g_dtexdata + dataofs, g_texdatasize - dataofs);

		lumpinfo_t *info;
		info = (lumpinfo_t *)malloc (((dmiptexlump_t*)g_dtexdata)->nummiptex * sizeof (lumpinfo_t));
		hlassume (info != NULL, assume_NoMemory);
		memset (info, 0, header.numlumps * sizeof(lumpinfo_t));

		for (int i = 0; i < header.numlumps; i++)
		{
			int ofs = ((dmiptexlump_t*)g_dtexdata)->dataofs[i];
			int size = 0;
			if (ofs >= 0)
			{
				size = g_texdatasize - ofs;
				for (int j = 0; j < ((dmiptexlump_t*)g_dtexdata)->nummiptex; ++j)
					if (ofs < ((dmiptexlump_t*)g_dtexdata)->dataofs[j] &&
						ofs + size > ((dmiptexlump_t*)g_dtexdata)->dataofs[j])
						size = ((dmiptexlump_t*)g_dtexdata)->dataofs[j] - ofs;
			}
			info[i].filepos = ofs - dataofs + wadofs;
			info[i].disksize = size;
			info[i].size = size;
			info[i].type = (ofs >= 0 && ((miptex_t*)(g_dtexdata+ofs))->offsets[0] > 0)? 67: 0; // prevent invalid texture from being processed by Wally
			info[i].compression = 0;
			strcpy (info[i].name, ofs >= 0? ((miptex_t*)(g_dtexdata+ofs))->name: "\rTEXTUREMISSING");
		}
		SafeWrite (wadfile, info, header.numlumps * sizeof(lumpinfo_t));
		free (info);
	}
	else
	{
		texfile = SafeOpenWrite (texfilename);
		Log("\nWriting %s.\n", texfilename);

		wadinfo_t header;
		header.identification[0] = 'W';
		header.identification[1] = 'A';
		header.identification[2] = 'D';
		header.identification[3] = '3';
		header.numlumps = 0;
		
		lumpinfo_t *info;
		info = (lumpinfo_t *)malloc (((dmiptexlump_t*)g_dtexdata)->nummiptex * sizeof (lumpinfo_t)); // might be more than needed
		hlassume (info != NULL, assume_NoMemory);

		fprintf (texfile, "%d\r\n", ((dmiptexlump_t*)g_dtexdata)->nummiptex);
		fseek (wadfile, sizeof(wadinfo_t), SEEK_SET);

		for (int itex = 0; itex < ((dmiptexlump_t*)g_dtexdata)->nummiptex; ++itex)
		{
			int ofs = ((dmiptexlump_t*)g_dtexdata)->dataofs[itex];
			miptex_t *tex = (miptex_t*)(g_dtexdata+ofs);
			if (ofs < 0)
			{
				fprintf (texfile, "[-1]\r\n");
			}
			else
			{
				int size = g_texdatasize - ofs;
				for (int j = 0; j < ((dmiptexlump_t*)g_dtexdata)->nummiptex; ++j)
					if (ofs < ((dmiptexlump_t*)g_dtexdata)->dataofs[j] &&
						ofs + size > ((dmiptexlump_t*)g_dtexdata)->dataofs[j])
						size = ((dmiptexlump_t*)g_dtexdata)->dataofs[j] - ofs;
				bool included = false;
				if (tex->offsets[0] > 0)
					included = true;
				if (included)
				{
					memset (&info[header.numlumps], 0, sizeof (lumpinfo_t));
					info[header.numlumps].filepos = ftell (wadfile);
					SafeWrite (wadfile, tex, size);
					info[header.numlumps].disksize = ftell (wadfile) - info[header.numlumps].filepos;
					info[header.numlumps].size = info[header.numlumps].disksize;
					info[header.numlumps].type = 67;
					info[header.numlumps].compression = 0;
					strcpy (info[header.numlumps].name, tex->name);
					header.numlumps++;
				}
#ifdef ZHLT_64BIT_FIX
				fprintf (texfile, "[%d]", (int)strlen(tex->name));
#else
				fprintf (texfile, "[%d]", strlen(tex->name));
#endif
				SafeWrite (texfile, tex->name, strlen(tex->name));
				fprintf (texfile, " %d %d\r\n", tex->width, tex->height);
			}
		}
		header.infotableofs = ftell (wadfile);
		SafeWrite (wadfile, info, header.numlumps * sizeof(lumpinfo_t));
		fseek (wadfile, 0, SEEK_SET);
		SafeWrite (wadfile, &header, sizeof(wadinfo_t));

		fclose (texfile);
		free (info);
	}
	fclose (wadfile);
}

// Assembly:
subq $0x20b8, %rsp           # imm = 0x20B8
movq %rdi, 0x20b0(%rsp)
leaq 0x10b0(%rsp), %rdi
movq 0x20b0(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x5e09(%rip), %rdx      # 0x18e04
movb $0x0, %al
callq 0x8350
leaq 0x10b0(%rsp), %rdi
callq 0x3140
leaq 0x10b0(%rsp), %rdi
callq 0x9990
movq %rax, 0x10a8(%rsp)
leaq 0x10b0(%rsp), %rsi
leaq 0x5d78(%rip), %rdi      # 0x18dab
movb $0x0, %al
callq 0x9f40
leaq 0xa0(%rsp), %rdi
movq 0x20b0(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x5db5(%rip), %rdx      # 0x18e0b
movb $0x0, %al
callq 0x8350
leaq 0xa0(%rsp), %rdi
callq 0x3140
testb $0x1, 0x208e731(%rip)   # 0x20a17a2
jne 0x133ad
leaq 0x8132aa(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq (%rax), %rcx
xorl %eax, %eax
addq $0x4, %rax
shlq $0x2, %rcx
addq %rcx, %rax
movl %eax, 0x94(%rsp)
movl $0xc, 0x90(%rsp)
movb $0x57, 0x84(%rsp)
movb $0x41, 0x85(%rsp)
movb $0x44, 0x86(%rsp)
movb $0x33, 0x87(%rsp)
leaq 0x81325e(%rip), %rax    # 0x826328
movq (%rax), %rax
movl (%rax), %eax
movl %eax, 0x88(%rsp)
leaq 0x813247(%rip), %rax    # 0x826324
movl (%rax), %eax
subl 0x94(%rsp), %eax
addl 0x90(%rsp), %eax
movl %eax, 0x8c(%rsp)
movq 0x10a8(%rsp), %rdi
movl 0x90(%rsp), %edx
leaq 0x84(%rsp), %rsi
callq 0x9aa0
movq 0x10a8(%rsp), %rdi
leaq 0x813209(%rip), %rax    # 0x826328
movq (%rax), %rsi
movslq 0x94(%rsp), %rax
addq %rax, %rsi
leaq 0x8131f0(%rip), %rax    # 0x826324
movl (%rax), %edx
subl 0x94(%rsp), %edx
callq 0x9aa0
leaq 0x8131df(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq (%rax), %rdi
shlq $0x5, %rdi
callq 0x3370
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xad70
movq 0x78(%rsp), %rdi
movslq 0x88(%rsp), %rdx
shlq $0x5, %rdx
xorl %esi, %esi
callq 0x3170
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
cmpl 0x88(%rsp), %eax
jge 0x1337e
leaq 0x81317a(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x74(%rsp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
cmpl $0x0, 0x70(%rsp)
jl 0x1325e
leaq 0x81314c(%rip), %rax    # 0x826324
movl (%rax), %eax
subl 0x70(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
leaq 0x813133(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
cmpl (%rcx), %eax
jge 0x1325c
movl 0x70(%rsp), %eax
leaq 0x813121(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
movslq 0x68(%rsp), %rdx
cmpl 0x4(%rcx,%rdx,4), %eax
jge 0x1324d
movl 0x70(%rsp), %eax
addl 0x6c(%rsp), %eax
leaq 0x813104(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
movslq 0x68(%rsp), %rdx
cmpl 0x4(%rcx,%rdx,4), %eax
jle 0x1324d
leaq 0x8130ef(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x68(%rsp), %rcx
movl 0x4(%rax,%rcx,4), %eax
subl 0x70(%rsp), %eax
movl %eax, 0x6c(%rsp)
jmp 0x1324f
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x131ea
jmp 0x1325e
movl 0x70(%rsp), %ecx
subl 0x94(%rsp), %ecx
addl 0x90(%rsp), %ecx
movq 0x78(%rsp), %rax
movslq 0x74(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl 0x6c(%rsp), %ecx
movq 0x78(%rsp), %rax
movslq 0x74(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl 0x6c(%rsp), %ecx
movq 0x78(%rsp), %rax
movslq 0x74(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
xorl %eax, %eax
cmpl $0x0, 0x70(%rsp)
movb %al, 0x36(%rsp)
jl 0x132db
leaq 0x813061(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x70(%rsp), %rcx
cmpl $0x0, 0x18(%rax,%rcx)
seta %al
movb %al, 0x36(%rsp)
movb 0x36(%rsp), %dl
xorl %eax, %eax
movl $0x43, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movb %al, %cl
movq 0x78(%rsp), %rax
movslq 0x74(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movb %cl, 0xc(%rax)
movq 0x78(%rsp), %rax
movslq 0x74(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x0, 0xd(%rax)
movq 0x78(%rsp), %rax
movslq 0x74(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
addq $0x10, %rax
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x70(%rsp)
jl 0x13351
leaq 0x812fe9(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x70(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
jmp 0x1335f
leaq 0x5aeb(%rip), %rax      # 0x18e43
movq %rax, 0x20(%rsp)
jmp 0x1335f
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x3270
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x13196
movq 0x10a8(%rsp), %rdi
movq 0x78(%rsp), %rsi
movslq 0x88(%rsp), %rax
shlq $0x5, %rax
movl %eax, %edx
callq 0x9aa0
movq 0x78(%rsp), %rdi
callq 0x3310
jmp 0x13787
leaq 0xa0(%rsp), %rdi
callq 0x9990
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rsi
leaq 0x59da(%rip), %rdi      # 0x18dab
movb $0x0, %al
callq 0x9f40
movb $0x57, 0x5c(%rsp)
movb $0x41, 0x5d(%rsp)
movb $0x44, 0x5e(%rsp)
movb $0x33, 0x5f(%rsp)
movl $0x0, 0x60(%rsp)
leaq 0x812f2d(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq (%rax), %rdi
shlq $0x5, %rdi
callq 0x3370
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xad70
movq 0x98(%rsp), %rdi
leaq 0x812ef1(%rip), %rax    # 0x826328
movq (%rax), %rax
movl (%rax), %edx
leaq 0x5a1b(%rip), %rsi      # 0x18e5e
movb $0x0, %al
callq 0x33b0
movq 0x10a8(%rsp), %rdi
movl $0xc, %esi
xorl %edx, %edx
callq 0x32e0
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
leaq 0x812eb7(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
cmpl (%rcx), %eax
jge 0x13718
leaq 0x812ea5(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x4c(%rsp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x48(%rsp)
leaq 0x812e8e(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x48(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x48(%rsp)
jge 0x134cc
movq 0x98(%rsp), %rdi
leaq 0x5993(%rip), %rsi      # 0x18e53
movb $0x0, %al
callq 0x33b0
jmp 0x13706
leaq 0x812e51(%rip), %rax    # 0x826324
movl (%rax), %eax
subl 0x48(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
leaq 0x812e38(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
cmpl (%rcx), %eax
jge 0x13557
movl 0x48(%rsp), %eax
leaq 0x812e26(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
movslq 0x38(%rsp), %rdx
cmpl 0x4(%rcx,%rdx,4), %eax
jge 0x13548
movl 0x48(%rsp), %eax
addl 0x3c(%rsp), %eax
leaq 0x812e09(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
movslq 0x38(%rsp), %rdx
cmpl 0x4(%rcx,%rdx,4), %eax
jle 0x13548
leaq 0x812df4(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x38(%rsp), %rcx
movl 0x4(%rax,%rcx,4), %eax
subl 0x48(%rsp), %eax
movl %eax, 0x3c(%rsp)
jmp 0x1354a
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x134e5
movb $0x0, 0x37(%rsp)
movq 0x40(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jbe 0x1356c
movb $0x1, 0x37(%rsp)
testb $0x1, 0x37(%rsp)
je 0x13682
movq 0x50(%rsp), %rdi
movslq 0x60(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3170
movq 0x10a8(%rsp), %rdi
callq 0x3030
movl %eax, %ecx
movq 0x50(%rsp), %rax
movslq 0x60(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq 0x10a8(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl 0x3c(%rsp), %edx
callq 0x9aa0
movq 0x10a8(%rsp), %rdi
callq 0x3030
movq 0x50(%rsp), %rcx
movslq 0x60(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq 0x50(%rsp), %rax
movslq 0x60(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq 0x50(%rsp), %rax
movslq 0x60(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq 0x50(%rsp), %rax
movslq 0x60(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq 0x50(%rsp), %rax
movslq 0x60(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x43, 0xc(%rax)
movq 0x50(%rsp), %rax
movslq 0x60(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x0, 0xd(%rax)
movq 0x50(%rsp), %rdi
movslq 0x60(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rdi
addq $0x10, %rdi
movq 0x40(%rsp), %rsi
callq 0x3270
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x40(%rsp), %rdi
callq 0x3130
movq 0x8(%rsp), %rdi
movl %eax, %edx
leaq 0x576b(%rip), %rsi      # 0x18e12
movb $0x0, %al
callq 0x33b0
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rdi
callq 0x3130
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %eax, %edx
callq 0x9aa0
movq 0x98(%rsp), %rdi
movq 0x40(%rsp), %rax
movl 0x10(%rax), %edx
movq 0x40(%rsp), %rax
movl 0x14(%rax), %ecx
leaq 0x575b(%rip), %rsi      # 0x18e5a
movb $0x0, %al
callq 0x33b0
jmp 0x13708
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x13466
movq 0x10a8(%rsp), %rdi
callq 0x3030
movl %eax, 0x64(%rsp)
movq 0x10a8(%rsp), %rdi
movq 0x50(%rsp), %rsi
movslq 0x60(%rsp), %rax
shlq $0x5, %rax
movl %eax, %edx
callq 0x9aa0
movq 0x10a8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x32e0
movq 0x10a8(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
movl $0xc, %edx
callq 0x9aa0
movq 0x98(%rsp), %rdi
callq 0x3290
movq 0x50(%rsp), %rdi
callq 0x3310
movq 0x10a8(%rsp), %rdi
callq 0x3290
addq $0x20b8, %rsp           # imm = 0x20B8
retq
nopl (%rax)