2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
26 #if defined (__linux__) || defined (__APPLE__)
\r
32 #include "pakstuff.h"
\r
43 struct PACKDirectory pakdir;
\r
44 PACKDirPtr pakdirptr = &pakdir;
\r
46 bool pakopen = false;
\r
48 DIRECTORY *paktextures = NULL;
\r
49 UInt32 PakColormapOffset;
\r
50 UInt32 PakColormapSize;
\r
51 DIRECTORY *dirhead = NULL;
\r
52 bool g_bPK3 = false;
\r
53 char g_strBasePaths[16][1024];
\r
54 int g_numBasePaths = 0;
\r
66 bool operator ==(const PK3FileInfo& rhs) const { return strcmp(m_pName, rhs.m_pName) == 0; }
\r
69 #define __PATHSEPERATOR '/'
\r
71 //#define LOG_PAKFAIL
\r
75 #if defined (__linux__) || defined (__APPLE__)
\r
79 #include <sys/types.h>
\r
85 LogFile(const char* pName)
\r
87 #if defined (__linux__) || defined (__APPLE__)
\r
88 // leo: use ~/.q3a/radiant/paklog instead of /tmp/paklog.txt
\r
91 home = getenv("HOME");
\r
94 uid_t id = getuid();
\r
98 while( (pwd = getpwent()) != NULL )
\r
99 if( pwd->pw_uid == id )
\r
101 home = pwd->pw_dir;
\r
109 char path[PATH_MAX];
\r
110 strcpy (path, home);
\r
111 if (path[strlen(path)-1] != '/')
\r
112 strcat (path, "/");
\r
113 strcat (path, ".q3a/radiant/paklog");
\r
114 m_pFile = fopen(path, "w");
\r
118 m_pFile = fopen(pName, "w");
\r
127 void Log(const char *pFormat, ...)
\r
129 if (m_pFile == NULL)
\r
133 va_start(arg_ptr, pFormat);
\r
134 fprintf(m_pFile, pFormat, arg_ptr);
\r
139 LogFile g_LogFile("/tmp/paklog.txt");
\r
142 template <class T> class StrPtr : public Str
\r
151 StrPtr(const char *pStr, T *p) : Str(pStr)
\r
171 template <class T> class PtrList
\r
203 PtrList *pl = this;
\r
204 while (pl && pl->m_pNext)
\r
208 pl->m_pNext = new PtrList(ip);
\r
213 PtrList *p = m_pNext;
\r
216 while (p->m_pNext != this && p->m_pNext != NULL)
\r
220 if (p->m_pNext == this)
\r
222 p->m_pNext = m_pNext;
\r
227 virtual PtrList* Find(T *ip)
\r
229 PtrList *p = m_pNext;
\r
232 if (*p->m_pPtr == *ip)
\r
241 // remove vp from the list
\r
244 PtrList *p = Find(ip);
\r
263 PtrList *p = m_pNext;
\r
274 typedef PtrList<unzFile> ZFileList;
\r
275 typedef PtrList<Str> StrList;
\r
276 typedef PtrList<PK3FileInfo> PK3List;
\r
279 StrList g_PK3TexturePaths;
\r
280 PK3List g_PK3Files;
\r
281 ZFileList g_zFiles;
\r
282 #define WORK_LEN 1024
\r
283 #define TEXTURE_PATH "textures"
\r
284 #define PATH_SEPERATORS "/\\:\0"
\r
287 char* __StrDup(char* pStr)
\r
292 return strcpy(new char[strlen(pStr)+1], pStr);
\r
295 char* __StrDup(const char* pStr)
\r
300 return strcpy(new char[strlen(pStr)+1], pStr);
\r
304 #define MEM_BLOCKSIZE 4096
\r
305 void* __qblockmalloc(size_t nSize)
\r
308 // round up to threshold
\r
309 int nAllocSize = nSize % MEM_BLOCKSIZE;
\r
310 if ( nAllocSize > 0)
\r
312 nSize += MEM_BLOCKSIZE - nAllocSize;
\r
314 b = malloc(nSize + 1);
\r
315 memset (b, 0, nSize);
\r
319 void* __qmalloc (size_t nSize)
\r
322 b = malloc(nSize + 1);
\r
323 memset (b, 0, nSize);
\r
329 ====================
\r
331 ====================
\r
333 void __ExtractFilePath (const char *path, char *dest)
\r
337 src = path + strlen(path) - 1;
\r
340 // back up until a \ or the start
\r
342 while (src != path && *(src-1) != __PATHSEPERATOR)
\r
345 memcpy (dest, path, src-path);
\r
346 dest[src-path] = 0;
\r
349 void __ExtractFileName (const char *path, char *dest)
\r
353 src = path + strlen(path) - 1;
\r
356 // back up until a \ or the start
\r
358 while (src != path && *(src-1) != '/'
\r
359 && *(src-1) != '\\' )
\r
369 void __ExtractFileBase (const char *path, char *dest)
\r
373 src = path + strlen(path) - 1;
\r
376 // back up until a \ or the start
\r
378 while (src != path && *(src-1) != '/'
\r
379 && *(src-1) != '\\' )
\r
382 while (*src && *src != '.')
\r
389 void __ExtractFileExtension (const char *path, char *dest)
\r
393 src = path + strlen(path) - 1;
\r
396 // back up until a . or the start
\r
398 while (src != path && *(src-1) != '.')
\r
402 *dest = 0; // no extension
\r
410 void __ConvertDOSToUnixName( char *dst, const char *src )
\r
414 if ( *src == '\\' )
\r
427 static void AddSlash(Str& str)
\r
429 int nLen = str.GetLength();
\r
432 if (str[nLen-1] != '\\' && str[nLen-1] != '/')
\r
437 static void FindReplace(Str& strContents, const char* pTag, const char* pValue)
\r
439 if (strcmp(pTag, pValue) == 0)
\r
441 for (int nPos = strContents.Find(pTag); nPos >= 0; nPos = strContents.Find(pTag))
\r
443 int nRightLen = strContents.GetLength() - strlen(pTag) - nPos;
\r
444 Str strLeft(strContents.Left(nPos));
\r
445 Str strRight(strContents.Right(nRightLen));
\r
447 strLeft += strRight;
\r
448 strContents = strLeft;
\r
456 void ClearFileList(FILELIST **list)
\r
463 *list = (*list)->next;
\r
468 void ClearDirList(DIRLIST **list)
\r
475 *list = (*list)->next;
\r
480 DIRECTORY *FindPakDir(DIRECTORY *dir, char *name)
\r
482 DIRECTORY *currentPtr;
\r
484 for(currentPtr = dir; currentPtr; currentPtr = currentPtr->next)
\r
486 if(!stricmp(name, currentPtr->name))
\r
498 // This gets passed a file mask which we want to remove as
\r
499 // we are only interested in the directory name and any given
\r
500 // extension. Only handles explicit filenames or *.something
\r
502 bool LoadPK3FileList(FILELIST **filelist, const char *pattern)
\r
504 char cSearch[WORK_LEN];
\r
505 __ConvertDOSToUnixName( cSearch, pattern );
\r
506 char cPath[WORK_LEN];
\r
507 char cExt[WORK_LEN];
\r
508 char cFile[WORK_LEN];
\r
509 char cWork[WORK_LEN];
\r
510 __ExtractFilePath(pattern, cPath);
\r
511 __ExtractFileName(pattern, cFile);
\r
512 __ExtractFileExtension(pattern, cExt);
\r
513 const char *pCompare = (strnicmp(cFile, "*.", 2) == 0) ? cExt : cFile;
\r
515 PK3List *p = g_PK3Files.Next();
\r
518 // qualify the path
\r
519 PK3FileInfo *pKey = p->Ptr();
\r
520 if (strstr(pKey->m_pName, cPath) && strstr(pKey->m_pName, pCompare))
\r
522 __ExtractFileName(pKey->m_pName, cWork);
\r
523 AddToFileListAlphabetized(filelist, cWork, 0, 0, false);
\r
527 return (*filelist) != NULL;
\r
530 bool GetPackFileList(FILELIST **filelist, char *pattern)
\r
534 DIRECTORY *dummy = paktextures;
\r
542 return LoadPK3FileList(filelist, pattern);
\r
547 for(i = 0; pattern[i] != '\0'; i++)
\r
549 if(pattern[i] == '\\')
\r
553 while(strchr(str1, '/'))
\r
555 str2 = strchr(str1, '/');
\r
557 dummy = FindPakDir(dummy, str1);
\r
562 for(temp = dummy->files; temp; temp=temp->next)
\r
564 AddToFileListAlphabetized(filelist, temp->filename, temp->offset, 0, false);
\r
569 bool GetPackTextureDirs(DIRLIST **dirlist)
\r
579 StrList *pl = g_PK3TexturePaths.Next();
\r
582 AddToDirListAlphabetized(dirlist, pl->Ref(), 0);
\r
588 for (i = 0; i < dirsize; i++)
\r
590 if(!strnicmp(pakdirptr[i].name, "textures", 8))
\r
592 strncpy(buf, &(pakdirptr[i].name[9]), 46);
\r
593 if(strchr(buf, '\\'))
\r
594 *strchr(buf, '\\') = '\0';
\r
595 else if(strchr(buf, '/'))
\r
596 *strchr(buf, '/') = '\0';
\r
600 if(strchr(buf, '.'))
\r
603 AddToDirListAlphabetized(dirlist, buf, 0);
\r
609 bool AddToDirListAlphabetized(DIRLIST **list, char *dirname, int from)
\r
611 DIRLIST *currentPtr, *previousPtr, *newPtr;
\r
614 for(currentPtr = *list; currentPtr; currentPtr = currentPtr->next)
\r
616 if(!stricmp(dirname, currentPtr->dirname))
\r
621 previousPtr = NULL;
\r
622 currentPtr = *list;
\r
624 if((newPtr = (DIRLIST *)__qmalloc(sizeof(DIRLIST))) == NULL)
\r
627 strcpy(newPtr->dirname, dirname);
\r
628 newPtr->from = from;
\r
630 while(currentPtr != NULL && stricmp(dirname, currentPtr->dirname) > 0)
\r
632 previousPtr = currentPtr;
\r
633 currentPtr = currentPtr->next;
\r
635 if(previousPtr == NULL)
\r
637 newPtr->next = *list;
\r
642 previousPtr->next = newPtr;
\r
643 newPtr->next = currentPtr;
\r
648 bool AddToFileListAlphabetized(FILELIST **list, char *filename, UInt32 offset, UInt32 size, bool dirs)
\r
650 FILELIST *currentPtr, *previousPtr, *newPtr;
\r
652 for(currentPtr = *list; currentPtr; currentPtr = currentPtr->next)
\r
654 if(!stricmp(filename, currentPtr->filename))
\r
659 previousPtr = NULL;
\r
660 currentPtr = *list;
\r
662 if((newPtr = (FILELIST *)__qmalloc(sizeof(FILELIST))) == NULL)
\r
665 strcpy(newPtr->filename, filename);
\r
666 newPtr->offset = offset;
\r
667 newPtr->size = size;
\r
669 while(currentPtr != NULL && stricmp(filename, currentPtr->filename) > 0)
\r
671 previousPtr = currentPtr;
\r
672 currentPtr = currentPtr->next;
\r
674 if(previousPtr == NULL)
\r
676 newPtr->next = *list;
\r
681 previousPtr->next = newPtr;
\r
682 newPtr->next = currentPtr;
\r
687 int PakLoadAnyFile(const char *filename, void **bufferptr)
\r
689 char cWork[WORK_LEN];
\r
692 // leo: hack to be able to use pak files from multiple directories
\r
693 for (int i = 0; i < g_numBasePaths; i++)
\r
695 PK3FileInfo *pInfo;
\r
697 // need to lookup the file without the base/texture path on it
\r
698 Str strBase(g_strBasePaths[i]);
\r
700 __ConvertDOSToUnixName(cWork, strBase);
\r
701 Str strFile(filename);
\r
702 __ConvertDOSToUnixName(strFile, strFile);
\r
703 strFile.MakeLower();
\r
705 FindReplace(strFile, cWork, "");
\r
707 PK3FileInfo infoFind;
\r
708 infoFind.m_pName = __StrDup(strFile.GetBuffer());
\r
709 PK3List *pList = g_PK3Files.Find(&infoFind);
\r
712 pInfo = pList->Ptr();
\r
713 memcpy(pInfo->m_zFile, &pInfo->m_zInfo, sizeof(unz_s));
\r
714 if (unzOpenCurrentFile(pInfo->m_zFile) == UNZ_OK)
\r
716 void *buffer = __qblockmalloc(pInfo->m_lSize+1);
\r
717 int n = unzReadCurrentFile(pInfo->m_zFile , buffer, pInfo->m_lSize);
\r
718 *bufferptr = buffer;
\r
719 unzCloseCurrentFile(pInfo->m_zFile);
\r
726 sprintf(cWork, "PAK failed on %s\n", filename);
\r
727 g_LogFile.Log(cWork);
\r
732 for (int i = 0; i < dirsize; i++)
\r
734 if(!stricmp(filename, pakdirptr[i].name))
\r
736 if (fseek(pakfile[m_nPAKIndex], pakdirptr[i].offset, SEEK_SET) >= 0)
\r
738 void *buffer = __qmalloc (pakdirptr[i].size+1);
\r
739 ((char *)buffer)[pakdirptr[i].size] = 0;
\r
740 if (fread(buffer, 1, pakdirptr[i].size, pakfile[m_nPAKIndex]) == pakdirptr[i].size)
\r
742 *bufferptr = buffer;
\r
743 return pakdirptr[i].size;
\r
749 sprintf(cWork, "PAK failed on %s\n", filename);
\r
750 g_LogFile.Log(cWork);
\r
757 DIRECTORY *AddPakDir(DIRECTORY **dir, char *name)
\r
759 DIRECTORY *currentPtr, *previousPtr, *newPtr;
\r
761 for(currentPtr = *dir; currentPtr; currentPtr = currentPtr->next)
\r
763 if(!stricmp(name, currentPtr->name))
\r
768 previousPtr = NULL;
\r
771 if((newPtr = (DIRECTORY *)__qmalloc(sizeof(DIRECTORY))) == NULL)
\r
774 strcpy(newPtr->name, name);
\r
775 newPtr->files = NULL;
\r
777 while(currentPtr != NULL && stricmp(name, currentPtr->name) > 0)
\r
779 previousPtr = currentPtr;
\r
780 currentPtr = currentPtr->next;
\r
782 if(previousPtr == NULL)
\r
784 newPtr->next = *dir;
\r
789 previousPtr->next = newPtr;
\r
790 newPtr->next = currentPtr;
\r
798 // Opens a PK3 ( or zip ) file and creates a list of filenames
\r
799 // and zip info structures
\r
801 bool OpenPK3(const char *filename)
\r
803 char cFilename[WORK_LEN];
\r
804 char cName[WORK_LEN];
\r
805 char cWork[WORK_LEN];
\r
806 unz_file_info zInfo;
\r
807 unzFile *zFile = new unzFile(unzOpen(filename));
\r
808 g_zFiles.Add(zFile);
\r
811 int nStatus = unzGoToFirstFile(*zFile);
\r
812 while (nStatus == UNZ_OK)
\r
814 cFilename[0] = '\0';
\r
815 unzGetCurrentFileInfo(*zFile, &zInfo, cFilename, WORK_LEN, NULL, 0, NULL, 0);
\r
817 __ConvertDOSToUnixName( cWork, cFilename);
\r
818 if (strstr(cWork, ".") != NULL)
\r
820 PK3FileInfo *pInfo = new PK3FileInfo();
\r
821 pInfo->m_pName = __StrDup(cWork);
\r
822 memcpy(&pInfo->m_zInfo, (unz_s*)*zFile, sizeof(unz_s));
\r
823 pInfo->m_lSize = zInfo.uncompressed_size;
\r
824 pInfo->m_zFile = *zFile;
\r
825 g_PK3Files.Add(pInfo);
\r
827 char *p = strstr(cFilename, TEXTURE_PATH);
\r
830 // FIXME: path differences per os ?
\r
831 // catch solo directory entry
\r
832 if (strlen(p) > strlen(TEXTURE_PATH) + 1)
\r
834 // skip textures + path seperator
\r
835 p += strlen(TEXTURE_PATH) + 1;
\r
836 int nEnd = strcspn(p, PATH_SEPERATORS);
\r
837 strncpy(cName, p, nEnd);
\r
838 cName[nEnd] = '\0';
\r
840 bool bFound = false;
\r
841 StrList *pl = g_PK3TexturePaths.Next();
\r
844 if (strcmpi(pl->Ref(), cName) == 0)
\r
846 // already have this, continue
\r
854 g_PK3TexturePaths.Add(new Str(cName));
\r
858 nStatus = unzGoToNextFile(*zFile);
\r
861 return (zFile != NULL);
\r
864 void closePK3(unzFile zf)
\r
869 void OpenPakFile(const char *filename)
\r
872 paktextures = NULL;
\r
874 pakopen = g_bPK3 = OpenPK3(filename);
\r
877 void ClearPaKDir(DIRECTORY **dir)
\r
879 DIRECTORY *d1 = *dir, *d2;
\r
883 ClearFileList(&(d1->files));
\r
890 void CleanUpPakDirs()
\r
892 ClearPaKDir(&paktextures);
\r
893 paktextures = NULL;
\r
895 g_PK3TexturePaths.RemoveAll();
\r
896 g_PK3Files.RemoveAll();
\r
899 void ClosePakFile(void)
\r
905 ZFileList *p = g_zFiles.Next();
\r
908 unzFile uz = p->Ref();
\r
915 fclose(pakfile[m_nPAKIndex]);
\r
923 void WINAPI InitPakFile(const char * pBasePath, const char *pName)
\r
925 if (g_numBasePaths == 0)
\r
929 paktextures = NULL;
\r
931 strcpy(g_strBasePaths[g_numBasePaths], pBasePath);
\r
936 //++timo FIXME: use some kind of compatibility lib here!
\r
937 #if defined (__linux__) || defined (__APPLE__)
\r
938 char cWork[WORK_LEN];
\r
939 struct dirent *dirlist;
\r
942 dir = opendir (pBasePath);
\r
945 while ((dirlist = readdir (dir)) != NULL)
\r
947 if (strstr (dirlist->d_name, ".pk3") == NULL)
\r
949 sprintf(cWork, "%s/%s", pBasePath, dirlist->d_name);
\r
950 OpenPakFile(cWork);
\r
956 char cWork[WORK_LEN];
\r
957 Str strPath(pBasePath);
\r
959 strPath += "*.pk3";
\r
961 struct _finddata_t fileinfo;
\r
962 int handle = _findfirst (strPath, &fileinfo);
\r
967 sprintf(cWork, "%s/%s", pBasePath, fileinfo.name);
\r
968 OpenPakFile(cWork);
\r
969 } while (_findnext( handle, &fileinfo ) != -1);
\r
970 _findclose (handle);
\r
976 OpenPakFile(pName);
\r