// File: 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:
subq $0x30a8, %rsp           # imm = 0x30A8
leaq 0x548a(%rip), %rdi      # 0x2bd38
movb $0x0, %al
callq 0xb5d0
movl $0x0, 0x30a4(%rsp)
movl 0x30a4(%rsp), %eax
leaq 0x63d9b42(%rip), %rcx   # 0x6400410
cmpl (%rcx), %eax
jge 0x269c9
movslq 0x30a4(%rsp), %rcx
leaq 0x63d972b(%rip), %rax   # 0x6400010
movq (%rax,%rcx,8), %rax
movq %rax, 0x3078(%rsp)
leaq 0x10bb8(%rip), %rax     # 0x374b0
testb $0x1, (%rax)
jne 0x2690c
movq 0x3078(%rsp), %rax
movb $0x0, 0x1000(%rax)
leaq 0x3058(%rsp), %rdi
leaq 0x61b4e75(%rip), %rsi   # 0x61db790
callq 0x25830
leaq 0x3038(%rsp), %rdi
leaq 0x61b4e61(%rip), %rsi   # 0x61db790
callq 0x25890
leaq 0x3058(%rsp), %rdi
leaq 0x3038(%rsp), %rsi
callq 0x25860
testb $0x1, %al
jne 0x2694f
jmp 0x269b1
movq 0x3078(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x3058(%rsp), %rdi
callq 0x258c0
movq %rax, %rdi
callq 0x41a0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x9520
cmpq $0x0, %rax
je 0x26993
movq 0x3078(%rsp), %rax
movb $0x0, 0x1000(%rax)
jmp 0x26995
leaq 0x3018(%rsp), %rdi
leaq 0x3058(%rsp), %rsi
xorl %edx, %edx
callq 0x258d0
jmp 0x26920
jmp 0x269b3
movl 0x30a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30a4(%rsp)
jmp 0x268c0
leaq 0x93a0(%rip), %rdi      # 0x2fd70
callq 0x44a0
movq %rax, 0x3080(%rsp)
movl $0x0, 0x30a4(%rsp)
movl 0x30a4(%rsp), %eax
leaq 0x63d9a1a(%rip), %rcx   # 0x6400410
cmpl (%rcx), %eax
jge 0x26f57
movslq 0x30a4(%rsp), %rcx
leaq 0x63d9603(%rip), %rax   # 0x6400010
movq (%rax,%rcx,8), %rax
movq %rax, 0x3078(%rsp)
movq 0x3078(%rsp), %rax
movq %rax, 0x3088(%rsp)
movq 0x3078(%rsp), %rdx
movslq 0x61b51a8(%rip), %rcx   # 0x61dbbe0
leaq 0x61b4da1(%rip), %rax   # 0x61db7e0
movq %rdx, (%rax,%rcx,8)
movq 0x3088(%rsp), %rdi
leaq 0x4ec1(%rip), %rsi      # 0x2b913
callq 0x4460
movq %rax, %rdx
movslq 0x61b517f(%rip), %rcx   # 0x61dbbe0
leaq 0x61b5188(%rip), %rax   # 0x61dbbf0
movq %rdx, (%rax,%rcx,8)
movslq 0x61b516d(%rip), %rcx   # 0x61dbbe0
leaq 0x61b5176(%rip), %rax   # 0x61dbbf0
cmpq $0x0, (%rax,%rcx,8)
jne 0x26b76
cmpq $0x0, 0x3080(%rsp)
je 0x26b76
movq 0x3088(%rsp), %rdi
leaq 0x1010(%rsp), %rsi
callq 0x9730
movq 0x3088(%rsp), %rdi
leaq 0x2010(%rsp), %rsi
callq 0x96d0
leaq 0x2010(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x9730
leaq 0x2010(%rsp), %rdi
movq 0x3080(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x1010(%rsp), %r9
movl $0x1000, %esi           # imm = 0x1000
leaq 0x927f(%rip), %rdx      # 0x2fd78
movb $0x0, %al
callq 0x99e0
leaq 0x2010(%rsp), %rdi
leaq 0x4e04(%rip), %rsi      # 0x2b913
callq 0x4460
movq %rax, %rdx
movslq 0x61b50c2(%rip), %rcx   # 0x61dbbe0
leaq 0x61b50cb(%rip), %rax   # 0x61dbbf0
movq %rdx, (%rax,%rcx,8)
movslq 0x61b50b0(%rip), %rcx   # 0x61dbbe0
leaq 0x61b50b9(%rip), %rax   # 0x61dbbf0
cmpq $0x0, (%rax,%rcx,8)
jne 0x26b74
leaq 0x2010(%rsp), %rdi
callq 0x94b0
leaq 0x2010(%rsp), %rdi
leaq 0x4db9(%rip), %rsi      # 0x2b913
callq 0x4460
movq %rax, %rdx
movslq 0x61b5077(%rip), %rcx   # 0x61dbbe0
leaq 0x61b5080(%rip), %rax   # 0x61dbbf0
movq %rdx, (%rax,%rcx,8)
jmp 0x26b76
movslq 0x61b5063(%rip), %rcx   # 0x61dbbe0
leaq 0x61b506c(%rip), %rax   # 0x61dbbf0
cmpq $0x0, (%rax,%rcx,8)
jne 0x26bbb
movq 0x3078(%rsp), %rax
movq %rax, 0x3088(%rsp)
movq 0x3088(%rsp), %rdx
movl $0xd, %edi
leaq 0x91d1(%rip), %rsi      # 0x2fd80
movb $0x0, %al
callq 0xbbb0
jmp 0x26f41
movq 0x3078(%rsp), %rax
movq %rax, 0x3088(%rsp)
movslq 0x61b500e(%rip), %rcx   # 0x61dbbe0
leaq 0x61b5017(%rip), %rax   # 0x61dbbf0
movq (%rax,%rcx,8), %rax
movq %rax, 0x3010(%rsp)
movq 0x3010(%rsp), %rdi
leaq 0x3094(%rsp), %rsi
movl $0xc, %edx
callq 0xb0e0
leaq 0x3094(%rsp), %rdi
leaq 0x918d(%rip), %rsi      # 0x2fd9b
movl $0x4, %edx
callq 0x41f0
cmpl $0x0, %eax
je 0x26c5f
leaq 0x3094(%rsp), %rdi
leaq 0x9174(%rip), %rsi      # 0x2fda0
movl $0x4, %edx
callq 0x41f0
cmpl $0x0, %eax
je 0x26c5f
leaq 0x4c7c(%rip), %rdi      # 0x2b8be
movb $0x0, %al
callq 0xb5d0
movq 0x3088(%rsp), %rsi
leaq 0x914d(%rip), %rdi      # 0x2fda5
movb $0x0, %al
callq 0xba80
movl 0x3098(%rsp), %edi
callq 0x9980
movl %eax, 0x3098(%rsp)
movl 0x309c(%rsp), %edi
callq 0x9980
movl %eax, 0x309c(%rsp)
movq 0x3010(%rsp), %rdi
movslq 0x309c(%rsp), %rsi
xorl %edx, %edx
callq 0x4440
cmpl $0x0, %eax
je 0x26cbe
movl 0x309c(%rsp), %esi
movq 0x3088(%rsp), %rdx
leaq 0x9102(%rip), %rdi      # 0x2fdb9
movb $0x0, %al
callq 0xbef0
movq 0x61b532b(%rip), %rdi   # 0x61dbff0
movl 0x61b532d(%rip), %eax   # 0x61dbff8
addl 0x3098(%rsp), %eax
cltq
imulq $0x24, %rax, %rsi
callq 0x4620
movq %rax, 0x61b530c(%rip)   # 0x61dbff0
movl $0x0, 0x30a0(%rsp)
movl 0x30a0(%rsp), %eax
cmpl 0x3098(%rsp), %eax
jge 0x26f00
movq 0x3010(%rsp), %rdi
movq 0x61b52de(%rip), %rsi   # 0x61dbff0
movslq 0x61b52df(%rip), %rax   # 0x61dbff8
imulq $0x24, %rax, %rax
addq %rax, %rsi
movl $0x20, %edx
callq 0xb0e0
movq 0x61b52bf(%rip), %rdi   # 0x61dbff0
movslq 0x61b52c0(%rip), %rax   # 0x61dbff8
imulq $0x24, %rax, %rax
addq %rax, %rdi
addq $0x10, %rdi
movl $0x10, %esi
callq 0x9ba0
testb $0x1, %al
jne 0x26dad
movq 0x61b5298(%rip), %rax   # 0x61dbff0
movslq 0x61b5299(%rip), %rcx   # 0x61dbff8
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x1f(%rax)
leaq 0x4b4d(%rip), %rdi      # 0x2b8be
movb $0x0, %al
callq 0xb5d0
movq 0x3088(%rsp), %rsi
movl 0x61b5272(%rip), %edx   # 0x61dbff8
movq 0x61b5263(%rip), %rcx   # 0x61dbff0
movslq 0x61b5264(%rip), %rax   # 0x61dbff8
imulq $0x24, %rax, %rax
addq %rax, %rcx
addq $0x10, %rcx
leaq 0x9035(%rip), %rdi      # 0x2fddb
movb $0x0, %al
callq 0xbef0
movq 0x61b523c(%rip), %rdi   # 0x61dbff0
movslq 0x61b523d(%rip), %rax   # 0x61dbff8
imulq $0x24, %rax, %rax
addq %rax, %rdi
addq $0x10, %rdi
movq 0x61b5223(%rip), %rsi   # 0x61dbff0
movslq 0x61b5224(%rip), %rax   # 0x61dbff8
imulq $0x24, %rax, %rax
addq %rax, %rsi
addq $0x10, %rsi
callq 0x26f90
movq 0x61b5205(%rip), %rax   # 0x61dbff0
movslq 0x61b5206(%rip), %rcx   # 0x61dbff8
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edi
callq 0x9980
movl %eax, %ecx
movq 0x61b51e7(%rip), %rax   # 0x61dbff0
movslq 0x61b51e8(%rip), %rdx   # 0x61dbff8
imulq $0x24, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq 0x61b51d0(%rip), %rax   # 0x61dbff0
movslq 0x61b51d1(%rip), %rcx   # 0x61dbff8
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
callq 0x9980
movl %eax, %ecx
movq 0x61b51b1(%rip), %rax   # 0x61dbff0
movslq 0x61b51b2(%rip), %rdx   # 0x61dbff8
imulq $0x24, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl 0x61b4d8a(%rip), %ecx   # 0x61dbbe0
movq 0x61b5193(%rip), %rax   # 0x61dbff0
movslq 0x61b5194(%rip), %rdx   # 0x61dbff8
imulq $0x24, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x20(%rax)
movq 0x61b517b(%rip), %rax   # 0x61dbff0
movslq 0x61b517c(%rip), %rcx   # 0x61dbff8
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
cmpl $0x5532c, 0x4(%rax)     # imm = 0x5532C
jle 0x26ed9
leaq 0x4a2b(%rip), %rdi      # 0x2b8be
movb $0x0, %al
callq 0xb5d0
movq 0x61b514f(%rip), %rax   # 0x61dbff0
movslq 0x61b5150(%rip), %rcx   # 0x61dbff8
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
movq 0x61b5137(%rip), %rdx   # 0x61dbff0
movslq 0x61b5138(%rip), %rax   # 0x61dbff8
imulq $0x24, %rax, %rax
addq %rax, %rdx
addq $0x10, %rdx
leaq 0x8f43(%rip), %rdi      # 0x2fe15
movb $0x0, %al
callq 0xbef0
jmp 0x26edb
movl 0x30a0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30a0(%rsp)
movl 0x61b5106(%rip), %eax   # 0x61dbff8
addl $0x1, %eax
movl %eax, 0x61b50fd(%rip)   # 0x61dbff8
jmp 0x26cef
movl 0x3098(%rsp), %ecx
movq 0x3078(%rsp), %rax
movl %ecx, 0x1008(%rax)
movl 0x61b4cc5(%rip), %eax   # 0x61dbbe0
addl $0x1, %eax
movl %eax, 0x61b4cbc(%rip)   # 0x61dbbe0
cmpl $0x80, 0x61b4cb2(%rip)  # 0x61dbbe0
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x10, %esi
callq 0xc400
movl 0x30a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30a4(%rsp)
jmp 0x269e8
movq 0x61b5092(%rip), %rdi   # 0x61dbff0
movslq 0x61b5093(%rip), %rsi   # 0x61dbff8
movl $0x24, %edx
leaq 0xaf(%rip), %rcx        # 0x27020
callq 0x4710
callq 0xba60
movb $0x1, %al
andb $0x1, %al
addq $0x30a8, %rsp           # imm = 0x30A8
retq
nopw (%rax,%rax)