1 #pragma warning(disable: 4267) // 'size_t' to 'unsigned int', possible loss of data
2 
3 #include "csg.h"
4 
5 #ifdef HAVE_UNISTD_E
6 #include <unistd.h>
7 #endif
8 
LoadWadincludeFile(const char * const filename)9 void            LoadWadincludeFile(const char* const filename)
10 {
11     char*           fname;
12     int             i, x;
13     char*           pData = NULL;
14     char*           pszData;
15     unsigned        len = strlen(filename) + 5;
16 
17     fname = (char*)Alloc(len);
18     safe_snprintf(fname, len, "%s.wic", filename);
19 
20     if (q_exists(fname))
21     {
22         i = LoadFile(fname, &pData);
23 
24         if (i == 0)
25         {
26             goto LoadWadincludeFileReturn;
27         }
28     }
29     else
30     {
31         Warning("WadInclude file %s does not exist", fname);
32         goto LoadWadincludeFileReturn;
33     }
34 
35     for (pszData = pData, x = 0; x < i; x++)
36     {
37         if (pData[x] == ';')
38         {
39             pData[x] = 0;
40             g_WadInclude.push_back(pszData);
41             pszData = pData + x + 1;
42         }
43     }
44 
45 LoadWadincludeFileReturn:;
46     Free(fname);
47     if (pData)
48     {
49         Free(pData);
50     }
51 }
52 
SaveWadincludeFile(const char * const filename)53 void            SaveWadincludeFile(const char* const filename)
54 {
55     char*           fname;
56     FILE*           file;
57     int             x;
58     unsigned        len = strlen(filename) + 5;
59 
60     fname = (char*)Alloc(len);
61     safe_snprintf(fname, len, "%s.wic", filename);
62 
63     _unlink(fname);
64 
65     file = SafeOpenWrite(fname);
66 
67     WadInclude_i it;
68     for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
69     {
70         x = it->size();
71         if (x)
72         {
73             SafeWrite(file, it->c_str(), x);
74             SafeWrite(file, ";", 1);
75         }
76     }
77 
78     Free(fname);
79     fclose(file);
80 }
81 
82 // this function is called in place of tex_initfromwad for onlyents compiles
HandleWadinclude()83 void            HandleWadinclude()
84 {
85     int             i;
86     char            szTmpWad[1024]; // arbitrary, but needs to be large.
87     char*           pszWadFile;
88     wadpath_t*      currentwad;
89 
90     Log("\n"); // looks cleaner
91 
92     szTmpWad[0] = 0;
93 
94 #ifdef HLCSG_AUTOWAD
95     if (g_bWadAutoDetect)
96     {
97         autowad_UpdateUsedWads();
98     }
99 #endif
100 
101     // for eachwadpath
102     for (i = 0; i < g_iNumWadPaths; i++)
103     {
104         bool            bExcludeThisWad = false;
105 
106         currentwad = g_pWadPaths[i];
107         pszWadFile = currentwad->path;
108 
109 #ifdef HLCSG_AUTOWAD/*
110     #ifdef _DEBUG
111         Log("[dbg] HandleWIC: attempting to parse wad '%s'\n", currentwad->path);
112     #endif*/
113         if (g_bWadAutoDetect && !currentwad->usedtextures)
114             continue;/*
115     #ifdef _DEBUG
116         Log("[dbg] HandleWIC: parsing wad\n");
117     #endif*/
118 #endif // HLCSG_AUTOWAD
119 
120         // look and see if we're supposed to include the textures from this WAD in the bsp.
121         WadInclude_i it;
122         for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
123         {
124             if (stristr(pszWadFile, it->c_str()))
125             {
126                 Log("Including Wadfile: %s\n", pszWadFile);
127                 bExcludeThisWad = true;             // wadincluding this one
128             }
129         }
130 
131         if (!bExcludeThisWad)
132         {
133             Log("Using Wadfile: %s\n", pszWadFile);
134             safe_snprintf(szTmpWad, 1024, "%s%s;", szTmpWad, pszWadFile);
135         }
136     }
137 
138     Log("\"wad\" is \"%s\"\n", szTmpWad);
139 
140     SetKeyValue(&g_entities[0], "wad", szTmpWad);
141 
142     Log("\n");
143     CheckFatal();
144 }
145 
146 #if 0
147 void            HandleWadinclude()
148 {
149     // Code somewhat copied from TEX_InitFromWad()
150 
151     char            szTmpPath[MAXTOKEN];
152     char            szNewWad[MAXTOKEN];
153     char*           pszWadFile;
154     bool            bExcludeThisWad;
155 
156     const char*     path = ValueForKey(&g_entities[0], "wad");
157 
158     szNewWad[0] = 0;
159     safe_strncpy(szTmpPath, path, MAXTOKEN);
160 
161     // temporary kludge so we don't have to deal with no occurances of a semicolon
162     //  in the path name ..
163     if (strchr(szTmpPath, ';') == NULL)
164     {
165         safe_strncat(szTmpPath, ";", MAXTOKEN);
166     }
167 
168     pszWadFile = strtok(szTmpPath, ";");
169 
170     while (pszWadFile)
171     {
172         bExcludeThisWad = false;
173 
174         // look and see if we're supposed to include the textures from this WAD in the bsp.
175         WadInclude_i it;
176         for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
177         {
178             if (stristr(pszWadFile, it->c_str()))
179             {
180                 Log("Embedding textures from WAD File [%s] into BSP\n", pszWadFile);
181                 bExcludeThisWad = true;
182             }
183         }
184 
185         if (!bExcludeThisWad)
186         {
187             safe_strncat(szNewWad, pszWadFile, MAXTOKEN);
188             safe_strncat(szNewWad, ";", MAXTOKEN);
189         }
190 
191         if (!bExcludeThisWad)
192         {
193             Log("Using WAD File: %s\n", pszWadFile);
194         }
195 
196         // next wad file
197         pszWadFile = strtok(NULL, ";");
198     }
199 
200     SetKeyValue(&g_entities[0], "wad", szNewWad);
201 }
202 
203 #endif