- moved most basic utility code without any dependencies on the rest of the engine to 'common' directory.

Again the objective is easier sharing with Raze.
This commit is contained in:
Christoph Oelckers 2020-04-11 12:59:55 +02:00
parent ace3e29473
commit fb1a7679ec
28 changed files with 293 additions and 227 deletions

View file

@ -1127,8 +1127,8 @@ set (PCH_SOURCES
common/utility/utf8.cpp common/utility/utf8.cpp
utility/palette.cpp utility/palette.cpp
utility/palettecontainer.cpp utility/palettecontainer.cpp
utility/files.cpp common/utility/files.cpp
utility/files_decompress.cpp common/utility/files_decompress.cpp
utility/m_png.cpp utility/m_png.cpp
utility/m_random.cpp utility/m_random.cpp
utility/memarena.cpp utility/memarena.cpp
@ -1140,15 +1140,15 @@ set (PCH_SOURCES
utility/nodebuilder/nodebuild_utility.cpp utility/nodebuilder/nodebuild_utility.cpp
utility/sc_man.cpp utility/sc_man.cpp
utility/stats.cpp utility/stats.cpp
utility/cmdlib.cpp common/utility/cmdlib.cpp
utility/configfile.cpp common/utility/configfile.cpp
utility/i_time.cpp utility/i_time.cpp
utility/m_argv.cpp utility/m_argv.cpp
utility/m_bbox.cpp utility/m_bbox.cpp
utility/name.cpp utility/name.cpp
utility/s_playlist.cpp utility/s_playlist.cpp
utility/v_collection.cpp utility/v_collection.cpp
utility/zstrformat.cpp common/utility/zstrformat.cpp
common/thirdparty/md5.cpp common/thirdparty/md5.cpp
common/thirdparty/superfasthash.cpp common/thirdparty/superfasthash.cpp
) )
@ -1175,7 +1175,8 @@ add_executable( zdoom WIN32 MACOSX_BUNDLE
${PCH_SOURCES} ${PCH_SOURCES}
utility/x86.cpp utility/x86.cpp
common/thirdparty/strnatcmp.c common/thirdparty/strnatcmp.c
utility/zstring.cpp common/utility/zstring.cpp
common/utility/findfile.cpp
common/thirdparty/math/asin.c common/thirdparty/math/asin.c
common/thirdparty/math/atan.c common/thirdparty/math/atan.c
common/thirdparty/math/const.c common/thirdparty/math/const.c

View file

@ -35,7 +35,7 @@
#include "cmdlib.h" #include "cmdlib.h"
#include "i_system.h" #include "findfile.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>

View file

@ -0,0 +1,190 @@
/*
** findfile.cpp
** Warpper around the native directory scanning API
**
**---------------------------------------------------------------------------
** Copyright 1998-2016 Randy Heit
** Copyright 2005-2020 Christoph Oelckers
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include "findfile.h"
#include "zstring.h"
#ifndef _WIN32
#include <fnmatch.h>
static const char *pattern;
#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1080
static int matchfile(struct dirent *ent)
#else
static int matchfile(const struct dirent *ent)
#endif
{
return fnmatch(pattern, ent->d_name, FNM_NOESCAPE) == 0;
}
void *I_FindFirst(const char *const filespec, findstate_t *const fileinfo)
{
FString dir;
const char *const slash = strrchr(filespec, '/');
if (slash)
{
pattern = slash + 1;
dir = FString(filespec, slash - filespec + 1);
fileinfo->path = dir;
}
else
{
pattern = filespec;
dir = ".";
}
fileinfo->current = 0;
fileinfo->count = scandir(dir.GetChars(), &fileinfo->namelist, matchfile, alphasort);
if (fileinfo->count > 0)
{
return fileinfo;
}
return (void *)-1;
}
int I_FindNext(void *const handle, findstate_t *const fileinfo)
{
findstate_t *const state = static_cast<findstate_t *>(handle);
if (state->current < fileinfo->count)
{
return ++state->current < fileinfo->count ? 0 : -1;
}
return -1;
}
int I_FindClose(void *const handle)
{
findstate_t *const state = static_cast<findstate_t *>(handle);
if (handle != (void *)-1 && state->count > 0)
{
for (int i = 0; i < state->count; ++i)
{
free(state->namelist[i]);
}
free(state->namelist);
state->namelist = nullptr;
state->count = 0;
}
return 0;
}
int I_FindAttr(findstate_t *const fileinfo)
{
dirent *const ent = fileinfo->namelist[fileinfo->current];
const FString path = fileinfo->path + ent->d_name;
bool isdir;
if (DirEntryExists(path, &isdir))
{
return isdir ? FA_DIREC : 0;
}
return 0;
}
#else
#include <windows.h>
//==========================================================================
//
// I_FindFirst
//
// Start a pattern matching sequence.
//
//==========================================================================
void *I_FindFirst(const char *filespec, findstate_t *fileinfo)
{
static_assert(sizeof(WIN32_FIND_DATAW) == sizeof(fileinfo->FindData), "FindData size mismatch");
auto widespec = WideString(filespec);
fileinfo->UTF8Name = "";
return FindFirstFileW(widespec.c_str(), (LPWIN32_FIND_DATAW)&fileinfo->FindData);
}
//==========================================================================
//
// I_FindNext
//
// Return the next file in a pattern matching sequence.
//
//==========================================================================
int I_FindNext(void *handle, findstate_t *fileinfo)
{
fileinfo->UTF8Name = "";
return !FindNextFileW((HANDLE)handle, (LPWIN32_FIND_DATAW)&fileinfo->FindData);
}
//==========================================================================
//
// I_FindClose
//
// Finish a pattern matching sequence.
//
//==========================================================================
int I_FindClose(void *handle)
{
return FindClose((HANDLE)handle);
}
//==========================================================================
//
// I_FindName
//
// Returns the name for an entry
//
//==========================================================================
const char *I_FindName(findstate_t *fileinfo)
{
if (fileinfo->UTF8Name.IsEmpty()) fileinfo->UTF8Name = fileinfo->FindData.Name;
return fileinfo->UTF8Name.GetChars();
}
#endif

View file

@ -0,0 +1,85 @@
#pragma once
// Directory searching routines
#include <stdint.h>
#include "zstring.h"
#ifndef _WIN32
struct findstate_t
{
private:
FString path;
struct dirent **namelist;
int current;
int count;
friend void *I_FindFirst(const char *filespec, findstate_t *fileinfo);
friend int I_FindNext(void *handle, findstate_t *fileinfo);
friend const char *I_FindName(findstate_t *fileinfo);
friend int I_FindAttr(findstate_t *fileinfo);
friend int I_FindClose(void *handle);
};
int I_FindAttr (findstate_t *fileinfo);
inline const char *I_FindName(findstate_t *fileinfo)
{
return (fileinfo->namelist[fileinfo->current]->d_name);
}
#define FA_RDONLY 1
#define FA_HIDDEN 2
#define FA_SYSTEM 4
#define FA_DIREC 8
#define FA_ARCH 16
#else
// Mirror WIN32_FIND_DATAW in <winbase.h>
struct findstate_t
{
private:
struct FileTime
{
uint32_t lo, hi;
};
struct WinData
{
uint32_t Attribs;
FileTime Times[3];
uint32_t Size[2];
uint32_t Reserved[2];
wchar_t Name[260];
wchar_t AltName[14];
};
WinData FindData;
FString UTF8Name;
friend void *I_FindFirst(const char *filespec, findstate_t *fileinfo);
friend int I_FindNext(void *handle, findstate_t *fileinfo);
friend const char *I_FindName(findstate_t *fileinfo);
friend int I_FindAttr(findstate_t *fileinfo);
};
const char *I_FindName(findstate_t *fileinfo);
inline int I_FindAttr(findstate_t *fileinfo)
{
return fileinfo->FindData.Attribs;
}
#define FA_RDONLY 0x00000001
#define FA_HIDDEN 0x00000002
#define FA_SYSTEM 0x00000004
#define FA_DIREC 0x00000010
#define FA_ARCH 0x00000020
#endif
void *I_FindFirst (const char *filespec, findstate_t *fileinfo);
int I_FindNext (void *handle, findstate_t *fileinfo);
int I_FindClose (void *handle);

View file

@ -70,6 +70,7 @@
#include "g_levellocals.h" #include "g_levellocals.h"
#include "v_video.h" #include "v_video.h"
#include "md5.h" #include "md5.h"
#include "findfile.h"
extern FILE *Logfile; extern FILE *Logfile;
extern bool insave; extern bool insave;

View file

@ -47,6 +47,7 @@
#include "version.h" #include "version.h"
#include "engineerrors.h" #include "engineerrors.h"
#include "v_text.h" #include "v_text.h"
#include "findfile.h"
CVAR (Bool, queryiwad, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG); CVAR (Bool, queryiwad, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
CVAR (String, defaultiwad, "", CVAR_ARCHIVE|CVAR_GLOBALCONFIG); CVAR (String, defaultiwad, "", CVAR_ARCHIVE|CVAR_GLOBALCONFIG);

View file

@ -102,6 +102,7 @@
#include "r_data/r_vanillatrans.h" #include "r_data/r_vanillatrans.h"
#include "s_music.h" #include "s_music.h"
#include "swrenderer/r_swcolormaps.h" #include "swrenderer/r_swcolormaps.h"
#include "findfile.h"
EXTERN_CVAR(Bool, hud_althud) EXTERN_CVAR(Bool, hud_althud)
EXTERN_CVAR(Int, vr_mode) EXTERN_CVAR(Int, vr_mode)

View file

@ -31,6 +31,7 @@
struct event_t; struct event_t;
#include "dobjgc.h" #include "dobjgc.h"
#include "name.h"
// The current state of the game: whether we are // The current state of the game: whether we are
// playing, gazing at the intermission screen, // playing, gazing at the intermission screen,

View file

@ -39,7 +39,7 @@
#include "resourcefile.h" #include "resourcefile.h"
#include "cmdlib.h" #include "cmdlib.h"
#include "doomtype.h" #include "doomtype.h"
#include "i_system.h" #include "findfile.h"

View file

@ -44,6 +44,7 @@
#include "vm.h" #include "vm.h"
#include "i_system.h" #include "i_system.h"
#include "v_video.h" #include "v_video.h"
#include "findfile.h"
// Save name length limit for old binary formats. // Save name length limit for old binary formats.
#define OLDSAVESTRINGSIZE 24 #define OLDSAVESTRINGSIZE 24

View file

@ -104,38 +104,6 @@ bool I_WriteIniFailed ();
class FTexture; class FTexture;
bool I_SetCursor(FTexture *); bool I_SetCursor(FTexture *);
// Directory searching routines
struct findstate_t
{
private:
FString path;
struct dirent **namelist;
int current;
int count;
friend void *I_FindFirst(const char *filespec, findstate_t *fileinfo);
friend int I_FindNext(void *handle, findstate_t *fileinfo);
friend const char *I_FindName(findstate_t *fileinfo);
friend int I_FindAttr(findstate_t *fileinfo);
friend int I_FindClose(void *handle);
};
void *I_FindFirst (const char *filespec, findstate_t *fileinfo);
int I_FindNext (void *handle, findstate_t *fileinfo);
int I_FindClose (void *handle);
int I_FindAttr (findstate_t *fileinfo);
inline const char *I_FindName(findstate_t *fileinfo)
{
return (fileinfo->namelist[fileinfo->current]->d_name);
}
#define FA_RDONLY 1
#define FA_HIDDEN 2
#define FA_SYSTEM 4
#define FA_DIREC 8
#define FA_ARCH 16
static inline char *strlwr(char *str) static inline char *strlwr(char *str)
{ {

View file

@ -62,93 +62,6 @@ bool I_WriteIniFailed()
return false; // return true to retry return false; // return true to retry
} }
static const char *pattern;
#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1080
static int matchfile(struct dirent *ent)
#else
static int matchfile(const struct dirent *ent)
#endif
{
return fnmatch(pattern, ent->d_name, FNM_NOESCAPE) == 0;
}
void *I_FindFirst(const char *const filespec, findstate_t *const fileinfo)
{
FString dir;
const char *const slash = strrchr(filespec, '/');
if (slash)
{
pattern = slash + 1;
dir = FString(filespec, slash - filespec + 1);
fileinfo->path = dir;
}
else
{
pattern = filespec;
dir = ".";
}
fileinfo->current = 0;
fileinfo->count = scandir(dir.GetChars(), &fileinfo->namelist, matchfile, alphasort);
if (fileinfo->count > 0)
{
return fileinfo;
}
return (void *)-1;
}
int I_FindNext(void *const handle, findstate_t *const fileinfo)
{
findstate_t *const state = static_cast<findstate_t *>(handle);
if (state->current < fileinfo->count)
{
return ++state->current < fileinfo->count ? 0 : -1;
}
return -1;
}
int I_FindClose(void *const handle)
{
findstate_t *const state = static_cast<findstate_t *>(handle);
if (handle != (void *)-1 && state->count > 0)
{
for (int i = 0; i < state->count; ++i)
{
free(state->namelist[i]);
}
free(state->namelist);
state->namelist = nullptr;
state->count = 0;
}
return 0;
}
int I_FindAttr(findstate_t *const fileinfo)
{
dirent *const ent = fileinfo->namelist[fileinfo->current];
const FString path = fileinfo->path + ent->d_name;
bool isdir;
if (DirEntryExists(path, &isdir))
{
return isdir ? FA_DIREC : 0;
}
return 0;
}
TArray<FString> I_GetGogPaths() TArray<FString> I_GetGogPaths()
{ {
// GOG's Doom games are Windows only at the moment // GOG's Doom games are Windows only at the moment

View file

@ -2,7 +2,7 @@
#include "hwrenderer/data/buffers.h" #include "hwrenderer/data/buffers.h"
#include "polyrenderer/drawers/poly_triangle.h" #include "polyrenderer/drawers/poly_triangle.h"
#include "utility/tarray.h" #include "tarray.h"
#include <vector> #include <vector>
#ifdef _MSC_VER #ifdef _MSC_VER

View file

@ -2,7 +2,7 @@
#include "hwrenderer/data/buffers.h" #include "hwrenderer/data/buffers.h"
#include "vk_objects.h" #include "vk_objects.h"
#include "utility/tarray.h" #include "tarray.h"
#ifdef _MSC_VER #ifdef _MSC_VER
// silence bogus warning C4250: 'VKVertexBuffer': inherits 'VKBuffer::VKBuffer::SetData' via dominance // silence bogus warning C4250: 'VKVertexBuffer': inherits 'VKBuffer::VKBuffer::SetData' via dominance

View file

@ -37,7 +37,7 @@
#include <vector> #include <vector>
#include "i_soundinternal.h" #include "i_soundinternal.h"
#include "utility/zstring.h" #include "zstring.h"
#include <zmusic.h> #include <zmusic.h>
class FileReader; class FileReader;

View file

@ -43,6 +43,7 @@
#include <zmusic.h> #include <zmusic.h>
#include "resourcefiles/resourcefile.h" #include "resourcefiles/resourcefile.h"
#include "version.h" #include "version.h"
#include "findfile.h"
//========================================================================== //==========================================================================
// //

View file

@ -952,64 +952,6 @@ bool I_WriteIniFailed()
return MessageBoxA(Window, errortext.GetChars(), GAMENAME " configuration not saved", MB_ICONEXCLAMATION | MB_RETRYCANCEL) == IDRETRY; return MessageBoxA(Window, errortext.GetChars(), GAMENAME " configuration not saved", MB_ICONEXCLAMATION | MB_RETRYCANCEL) == IDRETRY;
} }
//==========================================================================
//
// I_FindFirst
//
// Start a pattern matching sequence.
//
//==========================================================================
void *I_FindFirst(const char *filespec, findstate_t *fileinfo)
{
static_assert(sizeof(WIN32_FIND_DATAW) == sizeof(fileinfo->FindData), "Findata size mismatch");
auto widespec = WideString(filespec);
fileinfo->UTF8Name = "";
return FindFirstFileW(widespec.c_str(), (LPWIN32_FIND_DATAW)&fileinfo->FindData);
}
//==========================================================================
//
// I_FindNext
//
// Return the next file in a pattern matching sequence.
//
//==========================================================================
int I_FindNext(void *handle, findstate_t *fileinfo)
{
fileinfo->UTF8Name = "";
return !FindNextFileW((HANDLE)handle, (LPWIN32_FIND_DATAW)&fileinfo->FindData);
}
//==========================================================================
//
// I_FindClose
//
// Finish a pattern matching sequence.
//
//==========================================================================
int I_FindClose(void *handle)
{
return FindClose((HANDLE)handle);
}
//==========================================================================
//
// I_FindName
//
// Returns the name for an entry
//
//==========================================================================
const char *I_FindName(findstate_t *fileinfo)
{
if (fileinfo->UTF8Name.IsEmpty()) fileinfo->UTF8Name = fileinfo->FindData.Name;
return fileinfo->UTF8Name.GetChars();
}
//========================================================================== //==========================================================================
// //
// QueryPathKey // QueryPathKey

View file

@ -123,8 +123,6 @@ typedef long WLONG_PTR;
// Wrapper for GetLongPathName // Wrapper for GetLongPathName
FString I_GetLongPathName(const FString &shortpath); FString I_GetLongPathName(const FString &shortpath);
// Directory searching routines
// Mirror WIN32_FIND_DATAA in <winbase.h> // Mirror WIN32_FIND_DATAA in <winbase.h>
#ifndef MAX_PATH #ifndef MAX_PATH
#define MAX_PATH 260 #define MAX_PATH 260
@ -133,43 +131,6 @@ FString I_GetLongPathName(const FString &shortpath);
#define PATH_MAX 260 #define PATH_MAX 260
#endif #endif
struct findstate_t
{
private:
struct WinData
{
uint32_t Attribs;
uint32_t Times[3 * 2];
uint32_t Size[2];
uint32_t Reserved[2];
wchar_t Name[MAX_PATH];
wchar_t AltName[14];
};
WinData FindData;
FString UTF8Name;
friend void *I_FindFirst(const char *filespec, findstate_t *fileinfo);
friend int I_FindNext(void *handle, findstate_t *fileinfo);
friend const char *I_FindName(findstate_t *fileinfo);
friend int I_FindAttr(findstate_t *fileinfo);
};
void *I_FindFirst (const char *filespec, findstate_t *fileinfo);
int I_FindNext (void *handle, findstate_t *fileinfo);
int I_FindClose (void *handle);
const char *I_FindName(findstate_t *fileinfo);
inline int I_FindAttr(findstate_t *fileinfo)
{
return fileinfo->FindData.Attribs;
}
#define FA_RDONLY 0x00000001
#define FA_HIDDEN 0x00000002
#define FA_SYSTEM 0x00000004
#define FA_DIREC 0x00000010
#define FA_ARCH 0x00000020
int I_GetNumaNodeCount(); int I_GetNumaNodeCount();
int I_GetNumaNodeThreadCount(int numaNode); int I_GetNumaNodeThreadCount(int numaNode);
void I_SetThreadNumaNode(std::thread &thread, int numaNode); void I_SetThreadNumaNode(std::thread &thread, int numaNode);