// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlcsg/textures.cpp
// Function: TEX_InitFromWad()
bool            TEX_InitFromWad()
{
    int             i, j;
    wadinfo_t       wadinfo;
#ifndef HLCSG_AUTOWAD_NEW
    char            szTmpWad[1024]; // arbitrary, but needs to be large.
#endif
    char*           pszWadFile;
    const char*     pszWadroot;
    wadpath_t*      currentwad;

    Log("\n"); // looks cleaner
#ifdef HLCSG_AUTOWAD_NEW
	// update wad inclusion
	for (i = 0; i < g_iNumWadPaths; i++)
	{
        currentwad = g_pWadPaths[i];
		if (!g_wadtextures) // '-nowadtextures'
		{
			currentwad->usedbymap = false; // include this wad
		}
		for (WadInclude_i it = g_WadInclude.begin (); it != g_WadInclude.end (); it++) // '-wadinclude xxx.wad'
		{
			if (stristr (currentwad->path, it->c_str ()))
			{
				currentwad->usedbymap = false; // include this wad
			}
		}
	}
#endif

#ifndef HLCSG_AUTOWAD_NEW
    szTmpWad[0] = 0;
#endif
    pszWadroot = getenv("WADROOT");

#ifndef HLCSG_AUTOWAD_NEW
#ifdef HLCSG_AUTOWAD
    autowad_UpdateUsedWads();
#endif
#endif

    // for eachwadpath
    for (i = 0; i < g_iNumWadPaths; i++)
    {
        FILE*           texfile;                           // temporary used in this loop
#ifndef HLCSG_AUTOWAD_NEW
        bool            bExcludeThisWad = false;
#endif

        currentwad = g_pWadPaths[i];
        pszWadFile = currentwad->path;


#ifndef HLCSG_AUTOWAD_NEW
#ifdef HLCSG_AUTOWAD
        #ifdef _DEBUG
        Log("[dbg] Attempting to parse wad: '%s'\n", pszWadFile);
        #endif

        if (g_bWadAutoDetect && !currentwad->usedtextures)
            continue;

        #ifdef _DEBUG
        Log("[dbg] Parsing wad\n");
        #endif
#endif
#endif

#ifdef HLCSG_AUTOWAD_NEW
		texwadpathes[nTexFiles] = currentwad;
#endif
        texfiles[nTexFiles] = fopen(pszWadFile, "rb");

        #ifdef SYSTEM_WIN32
        if (!texfiles[nTexFiles])
        {
            // cant find it, maybe this wad file has a hard code drive
            if (pszWadFile[1] == ':')
            {
                pszWadFile += 2;                           // skip past the drive
                texfiles[nTexFiles] = fopen(pszWadFile, "rb");
            }
        }
        #endif

        if (!texfiles[nTexFiles] && pszWadroot)
        {
            char            szTmp[_MAX_PATH];
            char            szFile[_MAX_PATH];
            char            szSubdir[_MAX_PATH];

            ExtractFile(pszWadFile, szFile);

            ExtractFilePath(pszWadFile, szTmp);
            ExtractFile(szTmp, szSubdir);

            // szSubdir will have a trailing separator
            safe_snprintf(szTmp, _MAX_PATH, "%s" SYSTEM_SLASH_STR "%s%s", pszWadroot, szSubdir, szFile);
            texfiles[nTexFiles] = fopen(szTmp, "rb");

            #ifdef SYSTEM_POSIX
            if (!texfiles[nTexFiles])
            {
                // if we cant find it, Convert to lower case and try again
                strlwr(szTmp);
                texfiles[nTexFiles] = fopen(szTmp, "rb");
            }
            #endif
        }

		#ifdef HLCSG_SEARCHWADPATH_VL
        #ifdef SYSTEM_WIN32
		if (!texfiles[nTexFiles] && pszWadFile[0] == '\\')
		{
			char tmp[_MAX_PATH];
			int l;
			for (l = 'C'; l <= 'Z'; ++l)
			{
				safe_snprintf (tmp, _MAX_PATH, "%c:%s", l, pszWadFile);
				texfiles[nTexFiles] = fopen (tmp, "rb");
				if (texfiles[nTexFiles])
				{
					Developer (DEVELOPER_LEVEL_MESSAGE, "wad file found in drive '%c:' : %s\n", l, pszWadFile);
					break;
				}
			}
		}
		#endif
		#endif

        if (!texfiles[nTexFiles])
        {
#ifdef HLCSG_SEARCHWADPATH_VL
			pszWadFile = currentwad->path; // correct it back
#endif
            // still cant find it, error out
            Fatal(assume_COULD_NOT_FIND_WAD, "Could not open wad file %s", pszWadFile);
            continue;
        }

#ifdef HLCSG_WADCFG_NEW
		pszWadFile = currentwad->path; // correct it back
#endif
#ifndef HLCSG_AUTOWAD_NEW
        // look and see if we're supposed to include the textures from this WAD in the bsp.
        WadInclude_i it;
        for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
        {
            if (stristr(pszWadFile, it->c_str()))
            {
                Log("Including Wadfile: %s\n", pszWadFile);
                bExcludeThisWad = true;             // wadincluding this one
                s_WadIncludeMap[nTexFiles] = true;
                break;
            }
        }
#ifdef HLCSG_ONLYENTS_NOWADCHANGE
		if (!bExcludeThisWad && !g_wadtextures) // -nowadtextures
		{
			Log("Including Wadfile: %s\n", pszWadFile);
			bExcludeThisWad = true;
		}
#endif

        if (!bExcludeThisWad)
        {
            Log("Using Wadfile: %s\n", pszWadFile);
#ifdef HLCSG_STRIPWADPATH
			char tmp[_MAX_PATH];
			ExtractFile (pszWadFile, tmp);
            safe_snprintf(szTmpWad, 1024, "%s%s;", szTmpWad, tmp);
#else
            safe_snprintf(szTmpWad, 1024, "%s%s;", szTmpWad, pszWadFile);
#endif
        }
#endif

        // temp assignment to make things cleaner:
        texfile = texfiles[nTexFiles];

        // read in this wadfiles information
        SafeRead(texfile, &wadinfo, sizeof(wadinfo));

        // make sure its a valid format
        if (strncmp(wadinfo.identification, "WAD2", 4) && strncmp(wadinfo.identification, "WAD3", 4))
        {
            Log(" - ");
            Error("%s isn't a Wadfile!", pszWadFile);
        }

        wadinfo.numlumps        = LittleLong(wadinfo.numlumps);
        wadinfo.infotableofs    = LittleLong(wadinfo.infotableofs);

        // read in lump
        if (fseek(texfile, wadinfo.infotableofs, SEEK_SET))
            Warning("fseek to %d in wadfile %s failed\n", wadinfo.infotableofs, pszWadFile);

        // memalloc for this lump
        lumpinfo = (lumpinfo_t*)realloc(lumpinfo, (nTexLumps + wadinfo.numlumps) * sizeof(lumpinfo_t));

        // for each texlump
        for (j = 0; j < wadinfo.numlumps; j++, nTexLumps++)
        {
            SafeRead(texfile, &lumpinfo[nTexLumps], (sizeof(lumpinfo_t) - sizeof(int)) );  // iTexFile is NOT read from file

            if (!TerminatedString(lumpinfo[nTexLumps].name, MAXWADNAME))
            {
                lumpinfo[nTexLumps].name[MAXWADNAME - 1] = 0;
                Log(" - ");
                Warning("Unterminated texture name : wad[%s] texture[%d] name[%s]\n", pszWadFile, nTexLumps, lumpinfo[nTexLumps].name);
            }

            CleanupName(lumpinfo[nTexLumps].name, lumpinfo[nTexLumps].name);

            lumpinfo[nTexLumps].filepos = LittleLong(lumpinfo[nTexLumps].filepos);
            lumpinfo[nTexLumps].disksize = LittleLong(lumpinfo[nTexLumps].disksize);
            lumpinfo[nTexLumps].iTexFile = nTexFiles;

            if (lumpinfo[nTexLumps].disksize > MAX_TEXTURE_SIZE)
            {
                Log(" - ");
                Warning("Larger than expected texture (%d bytes): '%s'",
                    lumpinfo[nTexLumps].disksize, lumpinfo[nTexLumps].name);
            }

        }

        // AJM: this feature is dependant on autowad. :(
        // CONSIDER: making it standard?
#ifdef HLCSG_AUTOWAD_NEW
		currentwad->totaltextures = wadinfo.numlumps;
#else
#ifdef HLCSG_AUTOWAD
        {
            double percused = ((float)(currentwad->usedtextures) / (float)(g_numUsedTextures)) * 100;
            Log(" - Contains %i used texture%s, %2.2f percent of map (%d textures in wad)\n",
                currentwad->usedtextures, currentwad->usedtextures == 1 ? "" : "s", percused, wadinfo.numlumps);
        }
#endif
#endif

        nTexFiles++;
        hlassume(nTexFiles < MAX_TEXFILES, assume_MAX_TEXFILES);
    }

    //Log("num of used textures: %i\n", g_numUsedTextures);

#ifndef HLCSG_STRIPWADPATH
	// This is absurd. --vluzacn
    // AJM: Tommy suggested i add this warning message in, and  it certianly doesnt
    //  hurt to be cautious. Especially one of the possible side effects he mentioned was svc_bad
    if (nTexFiles > 8)
    {
        Log("\n");
        Warning("More than 8 wadfiles are in use. (%i)\n"
                "This may be harmless, and if no strange side effects are occurring, then\n"
                "it can safely be ignored. However, if your map starts exhibiting strange\n"
                "or obscure errors, consider this as suspect.\n"
                , nTexFiles);
    }
#endif

    // sort texlumps in memory by name
    qsort((void*)lumpinfo, (size_t) nTexLumps, sizeof(lumpinfo[0]), lump_sorter_by_name);

#ifndef HLCSG_AUTOWAD_NEW
#ifdef HLCSG_CHART_FIX
    Log("\n");
#ifdef HLCSG_ONLYENTS_NOWADCHANGE
	if (*szTmpWad)
	{
		Log ("Wad files required to run the map: \"%s\"\n", szTmpWad);
	}
	else
	{
		Log ("Wad files required to run the map: (None)\n");
	}
#else
    Log("\"wad\" is \"%s\"\n", szTmpWad);
#endif
#endif
    SetKeyValue(&g_entities[0], "wad", szTmpWad);

    Log("\n");
#endif
    CheckFatal();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3038, %rsp           # imm = 0x3038
leaq 0x4ab8(%rip), %rdi      # 0x1dca8
xorl %eax, %eax
callq 0x9e76
leaq 0x63d9172(%rip), %rax   # 0x63f2370
movl (%rax), %ebp
testl %ebp, %ebp
jle 0x1968d
xorl %r14d, %r14d
leaq 0x1023e(%rip), %r15     # 0x29450
leaq 0x63d8d57(%rip), %rax   # 0x63f1f70
movq (%rax,%r14,8), %rbx
cmpb $0x0, (%r15)
jne 0x1922a
movb $0x0, 0x1000(%rbx)
movq 0x61b44cf(%rip), %r12   # 0x61cd700
cmpq 0x61b44e8(%rip), %r12   # 0x61cd720
je 0x1928b
movq 0x61b44d7(%rip), %r13   # 0x61cd718
movq 0x61b44c8(%rip), %rbp   # 0x61cd710
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x8552
testq %rax, %rax
je 0x19260
movb $0x0, 0x1000(%rbx)
addq $0x20, %r12
cmpq %rbp, %r12
jne 0x19279
movq 0x8(%r13), %r12
addq $0x8, %r13
leaq 0x200(%r12), %rbp
cmpq 0x61b44a0(%rip), %r12   # 0x61cd720
jne 0x19248
leaq 0x63d90e7(%rip), %rax   # 0x63f2370
movl (%rax), %ebp
incq %r14
movslq %ebp, %rax
cmpq %rax, %r14
jl 0x19212
leaq 0x8a3f(%rip), %rdi      # 0x21ce0
callq 0x43c0
testl %ebp, %ebp
jle 0x19699
movq %rax, %rbx
leaq 0x46fb(%rip), %r14      # 0x1d9b3
leaq 0x61b4891(%rip), %r15   # 0x61cdb50
xorl %ecx, %ecx
movq %rax, 0x20(%rsp)
leaq 0x63d8ca3(%rip), %rax   # 0x63f1f70
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rdi
movslq 0x61b4863(%rip), %rax   # 0x61cdb40
leaq 0x61b445c(%rip), %rcx   # 0x61cd740
movq %rdi, (%rcx,%rax,8)
movq %rdi, 0x8(%rsp)
movq %r14, %rsi
callq 0x4380
movq %rax, %r12
movslq 0x61b4841(%rip), %rax   # 0x61cdb40
movq %r12, (%r15,%rax,8)
testq %rbx, %rbx
je 0x193b3
testq %r12, %r12
jne 0x193b3
movq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x2030(%rsp), %r12
movq %r12, %rsi
callq 0x87f2
movq %r13, %rdi
leaq 0x30(%rsp), %r13
movq %r13, %rsi
callq 0x874d
movq %r13, %rdi
leaq 0x1030(%rsp), %rbp
movq %rbp, %rsi
callq 0x87f2
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rdi
leaq 0x8989(%rip), %rdx      # 0x21ce8
movq %rbx, %rcx
movq %rbp, %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x8a47
movq %r13, %rdi
movq %r14, %rsi
callq 0x4380
movq %rax, %r12
movslq 0x61b47bc(%rip), %rax   # 0x61cdb40
movq %r12, (%r15,%rax,8)
testq %r12, %r12
jne 0x193b3
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x8514
movq %r12, %rdi
movq %r14, %rsi
callq 0x4380
movq %rax, %r12
movslq 0x61b4791(%rip), %rax   # 0x61cdb40
movq %r12, (%r15,%rax,8)
testq %r12, %r12
je 0x19658
movq %r12, %rdi
leaq 0x14(%rsp), %rsi
movl $0xc, %edx
callq 0x9a4c
movl 0x14(%rsp), %eax
movl $0x1000000, %ecx        # imm = 0x1000000
orl %ecx, %eax
cmpl $0x33444157, %eax       # imm = 0x33444157
je 0x19401
leaq 0x4577(%rip), %rdi      # 0x1d95e
xorl %eax, %eax
callq 0x9e76
leaq 0x8916(%rip), %rdi      # 0x21d0b
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0xa164
movl 0x18(%rsp), %edi
callq 0x8a38
movl %eax, 0x18(%rsp)
movl 0x1c(%rsp), %edi
callq 0x8a38
movl %eax, 0x1c(%rsp)
movslq %eax, %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x4350
testl %eax, %eax
je 0x19443
movl 0x1c(%rsp), %esi
leaq 0x88e8(%rip), %rdi      # 0x21d1f
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0xa4d6
movq 0x61b4b06(%rip), %rdi   # 0x61cdf50
movslq 0x61b4b07(%rip), %rbx   # 0x61cdf58
movslq 0x18(%rsp), %rax
addq %rbx, %rax
shlq $0x2, %rax
leaq (%rax,%rax,8), %rsi
callq 0x4540
movq %rax, 0x61b4ae3(%rip)   # 0x61cdf50
movl 0x18(%rsp), %ecx
testl %ecx, %ecx
jle 0x19616
xorl %r13d, %r13d
movslq %ebx, %rcx
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rsi
movq %r12, %rdi
movl $0x20, %edx
callq 0x9a4c
movq 0x61b4ab5(%rip), %rax   # 0x61cdf50
movslq 0x61b4ab6(%rip), %rcx   # 0x61cdf58
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rdi
addq $0x10, %rdi
movl $0x10, %esi
callq 0x8b3b
testb %al, %al
jne 0x1950e
movq 0x61b4a8d(%rip), %rax   # 0x61cdf50
movslq 0x61b4a8e(%rip), %rcx   # 0x61cdf58
leaq (%rcx,%rcx,8), %rcx
movb $0x0, 0x1f(%rax,%rcx,4)
leaq 0x4484(%rip), %rdi      # 0x1d95e
xorl %eax, %eax
callq 0x9e76
movslq 0x61b4a70(%rip), %rdx   # 0x61cdf58
movq 0x61b4a61(%rip), %rax   # 0x61cdf50
leaq (%rdx,%rdx,8), %rcx
leaq (%rax,%rcx,4), %rcx
addq $0x10, %rcx
leaq 0x883f(%rip), %rdi      # 0x21d41
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0xa4d6
movq 0x61b4a3b(%rip), %rbp   # 0x61cdf50
movslq 0x61b4a3c(%rip), %rax   # 0x61cdf58
leaq (%rax,%rax,8), %r15
leaq 0x10(,%r15,4), %rbx
addq %rbp, %rbx
xorl %r14d, %r14d
movsbl (%rbx,%r14), %edi
testl %edi, %edi
je 0x1954b
callq 0x45e0
movb %al, (%rbx,%r14)
incq %r14
cmpq $0x10, %r14
jne 0x1952e
jmp 0x19566
cmpq $0xf, %r14
ja 0x19566
addq %r14, %rbx
movl $0x10, %edx
subq %r14, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41c0
movl (%rbp,%r15,4), %edi
callq 0x8a38
movq 0x61b49d9(%rip), %rcx   # 0x61cdf50
movslq 0x61b49da(%rip), %rdx   # 0x61cdf58
leaq (%rdx,%rdx,8), %rdx
movl %eax, (%rcx,%rdx,4)
movl 0x4(%rcx,%rdx,4), %edi
callq 0x8a38
movl %eax, %ecx
movq 0x61b49b9(%rip), %rax   # 0x61cdf50
movslq 0x61b49ba(%rip), %rbx   # 0x61cdf58
leaq (%rbx,%rbx,8), %rdx
movl %ecx, 0x4(%rax,%rdx,4)
movl 0x61b4594(%rip), %esi   # 0x61cdb40
movl %esi, 0x20(%rax,%rdx,4)
cmpl $0x5532d, %ecx          # imm = 0x5532D
jl 0x195fe
leaq 0x439f(%rip), %rdi      # 0x1d95e
xorl %eax, %eax
callq 0x9e76
movq 0x61b4983(%rip), %rax   # 0x61cdf50
movslq 0x61b4984(%rip), %rcx   # 0x61cdf58
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rdx
addq $0x10, %rdx
movl -0xc(%rdx), %esi
leaq 0x8791(%rip), %rdi      # 0x21d7b
xorl %eax, %eax
callq 0xa4d6
movq 0x61b4958(%rip), %rax   # 0x61cdf50
movl 0x61b495a(%rip), %ebx   # 0x61cdf58
incl %r13d
incl %ebx
movl %ebx, 0x61b494f(%rip)   # 0x61cdf58
movl 0x18(%rsp), %ecx
cmpl %ecx, %r13d
jl 0x1947c
movq 0x8(%rsp), %rax
movl %ecx, 0x1008(%rax)
movl 0x61b4519(%rip), %eax   # 0x61cdb40
leal 0x1(%rax), %ecx
movl %ecx, 0x61b4510(%rip)   # 0x61cdb40
xorl %edi, %edi
cmpl $0x7f, %eax
setl %dil
movl $0x10, %esi
callq 0xa91d
movq 0x20(%rsp), %rbx
leaq 0x4364(%rip), %r14      # 0x1d9b3
leaq 0x61b44fa(%rip), %r15   # 0x61cdb50
jmp 0x19670
movl $0xd, %edi
leaq 0x868c(%rip), %rsi      # 0x21cf0
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0xa253
movq 0x28(%rsp), %rcx
incq %rcx
leaq 0x63d8cf1(%rip), %rax   # 0x63f2370
movslq (%rax), %rax
cmpq %rax, %rcx
jl 0x192c6
jmp 0x19699
leaq 0x864c(%rip), %rdi      # 0x21ce0
callq 0x43c0
movq 0x61b48b0(%rip), %rdi   # 0x61cdf50
movslq 0x61b48b1(%rip), %rsi   # 0x61cdf58
leaq 0x23(%rip), %rcx        # 0x196d1
movl $0x24, %edx
callq 0x4600
callq 0xa14f
movb $0x1, %al
addq $0x3038, %rsp           # imm = 0x3038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq