Merge pull request #615 from isRyven/master

Fix q3map2 pak sorting
This commit is contained in:
Timothee "TTimo" Besset 2018-04-21 13:59:49 -05:00 committed by GitHub
commit 1219ad4115
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -164,13 +164,56 @@ static void vfsInitPakFile( const char *filename ){
}
}
static int vfsPakSort(const void *a, const void *b) {
char *s1, *s2;
int c1, c2;
s1 = (char*)a;
s2 = (char*)b;
do {
c1 = *s1++;
c2 = *s2++;
if (c1 >= 'a' && c1 <= 'z') {
c1 -= ('a' - 'A');
}
if (c2 >= 'a' && c2 <= 'z') {
c2 -= ('a' - 'A');
}
if (c1 == '\\' || c1 == ':') {
c1 = '/';
}
if (c2 == '\\' || c2 == ':') {
c2 = '/';
}
// Arnout: note - sort pakfiles in reverse order. This ensures that
// later pakfiles override earlier ones. This because the vfs module
// returns a filehandle to the first file it can find (while it should
// return the filehandle to the file in the most overriding pakfile, the
// last one in the list that is).
if (c1 < c2) {
//return -1; // strings not equal
return 1; // strings not equal
}
if (c1 > c2) {
//return 1;
return -1;
}
} while (c1);
return 0; // strings are equal
}
// =============================================================================
// Global functions
// reads all pak files from a dir
void vfsInitDirectory( const char *path ){
char filename[PATH_MAX];
char *dirlist;
GSList *dirlist = NULL;
GDir *dir;
if ( g_numDirs == ( VFS_MAXDIRS - 1 ) ) {
@ -188,38 +231,48 @@ void vfsInitDirectory( const char *path ){
dir = g_dir_open( path, 0, NULL );
if ( dir != NULL ) {
while ( 1 )
while(1)
{
const char* name = g_dir_read_name( dir );
const char* name = g_dir_read_name(dir);
if (name == NULL) {
break;
}
char* direntry = g_strdup(name);
dirlist = g_slist_append(dirlist, direntry);
}
dirlist = g_slist_sort(dirlist, vfsPakSort);
while ( dirlist )
{
GSList *cur = dirlist;
char* name = (char*)cur->data;
if ( name == NULL ) {
break;
}
dirlist = g_strdup( name );
char *ext = strrchr(name, '.' );
{
char *ext = strrchr( dirlist, '.' );
if ( ext != NULL && ( !Q_stricmp( ext, ".pk3dir" ) || !Q_stricmp( ext, ".dpkdir" ) ) ) {
if ( g_numDirs == VFS_MAXDIRS ) {
continue;
}
snprintf( g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name );
g_strDirs[g_numDirs][PATH_MAX-1] = '\0';
vfsFixDOSName( g_strDirs[g_numDirs] );
vfsAddSlash( g_strDirs[g_numDirs] );
++g_numDirs;
}
if ( ext == NULL || ( Q_stricmp( ext, ".pk3" ) != 0 && Q_stricmp( ext, ".dpk" ) != 0 ) ) {
if ( ext != NULL && ( !Q_stricmp( ext, ".pk3dir" ) || !Q_stricmp( ext, ".dpkdir" ) ) ) {
if ( g_numDirs == VFS_MAXDIRS ) {
continue;
}
snprintf( g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name );
g_strDirs[g_numDirs][PATH_MAX-1] = '\0';
vfsFixDOSName( g_strDirs[g_numDirs] );
vfsAddSlash( g_strDirs[g_numDirs] );
++g_numDirs;
}
sprintf( filename, "%s/%s", path, dirlist );
vfsInitPakFile( filename );
if ( ext != NULL && ( !Q_stricmp( ext, ".pk3" ) || !Q_stricmp( ext, ".dpk" )) ) {
sprintf(filename, "%s/%s", path, name);
vfsInitPakFile(filename);
}
g_free( dirlist );
g_free(name);
dirlist = g_slist_remove(cur, name);
}
g_dir_close( dir );
}