- refactored all file access in SW frontend to use the FileReader variant and added a few utilities to avoid opening handles.

Because use of the handle API needs to be eliminated before a functioning resource management can be plugged in here.
This commit is contained in:
Christoph Oelckers 2019-10-20 20:08:17 +02:00
parent 1b96861615
commit 7cacb7203f
8 changed files with 60 additions and 68 deletions

View File

@ -136,6 +136,7 @@ public:
}; };
// Wrappers for the handle based API to get rid of the direct calls without any actual changes to the implementation.
inline FileReader kopenFileReader(const char* name, int where) inline FileReader kopenFileReader(const char* name, int where)
{ {
int handle = where == 0 ? kopen4loadfrommod(name, 0) : kopen4load(name, where); int handle = where == 0 ? kopen4loadfrommod(name, 0) : kopen4load(name, where);
@ -143,5 +144,30 @@ inline FileReader kopenFileReader(const char* name, int where)
return FileReader(fri); return FileReader(fri);
} }
inline bool testkopen(const char* name, int where)
{
int handle = where == 0 ? kopen4loadfrommod(name, 0) : kopen4load(name, where);
if (handle != buildvfs_kfd_invalid) kclose(handle);
return handle != buildvfs_kfd_invalid;
}
inline TArray<uint8_t> kloadfile(const char* name, int where)
{
auto fr = kopenFileReader(name, where);
return fr.isOpen() ? fr.Read() : TArray <uint8_t>();
}
inline int32_t kfilesize(const char* name, int where)
{
int handle = where == 0 ? kopen4loadfrommod(name, 0) : kopen4load(name, where);
if (handle != buildvfs_kfd_invalid)
{
auto fs = kfilelength(handle);
kclose(handle);
return fs;
}
return -1;
}
#endif // cache1d_h_ #endif // cache1d_h_

View File

@ -80,7 +80,6 @@ int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens);
int32_t G_CheckCmdSwitch(int32_t argc, char const * const * argv, const char *str); int32_t G_CheckCmdSwitch(int32_t argc, char const * const * argv, const char *str);
int32_t testkopen(const char *filename, char searchfirst); // full-blown kopen4load
int32_t check_file_exist(const char *fn); // findfrompath with pathsearchmode=1 / search in zips int32_t check_file_exist(const char *fn); // findfrompath with pathsearchmode=1 / search in zips
void fnlist_clearnames(fnlist_t *fnl); void fnlist_clearnames(fnlist_t *fnl);

View File

@ -129,15 +129,6 @@ int32_t G_CheckCmdSwitch(int32_t argc, char const * const * argv, const char *st
return 0; return 0;
} }
// returns: 1 if file could be opened, 0 else
int32_t testkopen(const char *filename, char searchfirst)
{
buildvfs_kfd fd = kopen4load(filename, searchfirst);
if (fd != buildvfs_kfd_invalid)
kclose(fd);
return (fd != buildvfs_kfd_invalid);
}
// checks from path and in ZIPs, returns 1 if NOT found // checks from path and in ZIPs, returns 1 if NOT found
int32_t check_file_exist(const char *fn) int32_t check_file_exist(const char *fn)
{ {

View File

@ -226,7 +226,6 @@ void AnimZilla(int frame, int numframes)
unsigned char *LoadAnm(short anim_num) unsigned char *LoadAnm(short anim_num)
{ {
int handle;
int length; int length;
unsigned char *animbuf, *palptr; unsigned char *animbuf, *palptr;
int i,j,k; int i,j,k;
@ -242,17 +241,16 @@ unsigned char *LoadAnm(short anim_num)
if (anm_ptr[anim_num] == 0) if (anm_ptr[anim_num] == 0)
{ {
handle = kopen4load(ANIMname[ANIMnum], 0); auto handle = kopenFileReader(ANIMname[ANIMnum], 0);
if (handle == -1) if (!handle.isOpen())
return NULL; return NULL;
length = kfilelength(handle); length = handle.GetLength();
buffer.Resize(length + sizeof(anim_t)); buffer.Resize(length + sizeof(anim_t));
anm_ptr[anim_num] = (anim_t*)buffer.Data(); anm_ptr[anim_num] = (anim_t*)buffer.Data();
animbuf = (unsigned char *)((intptr_t)anm_ptr[anim_num] + sizeof(anim_t)); animbuf = (unsigned char *)((intptr_t)anm_ptr[anim_num] + sizeof(anim_t));
kread(handle, animbuf, length); handle.Read(animbuf, length);
kclose(handle);
} }
else else
{ {
@ -289,10 +287,8 @@ playanm(short anim_num)
return; return;
// [JM] Temporary, needed to get the file's length for ANIM_LoadAnim. !CHECKME! // [JM] Temporary, needed to get the file's length for ANIM_LoadAnim. !CHECKME!
handle = kopen4load(ANIMname[ANIMnum], 0); length = kfilesize(ANIMname[ANIMnum], 0);
if (handle == -1) return; if (length == -1) return;
length = kfilelength(handle);
kclose(handle);
DSPRINTF(ds,"PlayAnm - Palette Stuff"); DSPRINTF(ds,"PlayAnm - Palette Stuff");
MONO_PRINT(ds); MONO_PRINT(ds);

View File

@ -46,7 +46,7 @@ Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
BEGIN_SW_NS BEGIN_SW_NS
DFILE DemoFileIn = DF_ERR; DFILE DemoFileIn;
FILE *DemoFileOut; FILE *DemoFileOut;
SWBOOL DemoPlaying = FALSE; SWBOOL DemoPlaying = FALSE;
SWBOOL DemoRecording = FALSE; SWBOOL DemoRecording = FALSE;
@ -228,7 +228,7 @@ DemoReadHeader(void)
DemoFileIn = DOPEN_READ(DemoFileName); DemoFileIn = DOPEN_READ(DemoFileName);
} }
if (DemoFileIn == DF_ERR) if (DF_ERR(DemoFileIn))
{ {
TerminateGame(); TerminateGame();
printf("File %s is not a valid demo file.",DemoFileName); printf("File %s is not a valid demo file.",DemoFileName);
@ -360,11 +360,10 @@ DemoTerm(void)
if (DemoPlaying) if (DemoPlaying)
{ {
if (DemoFileIn == DF_ERR) if (DF_ERR(DemoFileIn))
return; return;
DCLOSE(DemoFileIn); DCLOSE(DemoFileIn);
DemoFileIn = DF_ERR;
} }
if (DemoSyncTest||DemoSyncRecord) if (DemoSyncTest||DemoSyncRecord)

View File

@ -53,11 +53,11 @@ extern int DemoRecCnt; // Can only record 1-player game
// Demo File - reading from group // Demo File - reading from group
#if DEMO_FILE_TYPE == DEMO_FILE_GROUP #if DEMO_FILE_TYPE == DEMO_FILE_GROUP
typedef long DFILE; typedef FileReader DFILE;
#define DREAD(ptr, size, num, handle) kread((handle),(ptr),(size)*(num)) #define DREAD(ptr, size, num, handle) (handle).Read((ptr),(size)*(num))
#define DOPEN_READ(name) kopen4load(name,0) #define DOPEN_READ(name) kopenFileReader(name,0)
#define DCLOSE(handle) kclose(handle) #define DCLOSE(handle) ((handle).Close())
#define DF_ERR -1 #define DF_ERR(f) (!(f).isOpen())
#else #else
typedef FILE *DFILE; typedef FILE *DFILE;
#define DREAD(ptr, size, num,handle) fread((ptr),(size),(num),(handle)) #define DREAD(ptr, size, num,handle) fread((ptr),(size),(num),(handle))
@ -65,7 +65,7 @@ typedef FILE *DFILE;
#define DOPEN_WRITE(name) fopen(name,"wb") #define DOPEN_WRITE(name) fopen(name,"wb")
#define DOPEN_READ(name) fopen(name,"rb") #define DOPEN_READ(name) fopen(name,"rb")
#define DCLOSE(handle) fclose(handle) #define DCLOSE(handle) fclose(handle)
#define DF_ERR NULL #define DF_ERR(f) ((f) == NULL)
#endif #endif
void DemoTerm(void); void DemoTerm(void);

View File

@ -1721,7 +1721,6 @@ LogoLevel(void)
char called; char called;
int fin; int fin;
unsigned char backup_pal[256*3]; unsigned char backup_pal[256*3];
unsigned char pal[PAL_SIZE];
char tempbuf[256]; char tempbuf[256];
char *palook_bak = palookup[0]; char *palook_bak = palookup[0];
UserInput uinfo = { FALSE, FALSE, dir_None }; UserInput uinfo = { FALSE, FALSE, dir_None };
@ -1754,12 +1753,10 @@ LogoLevel(void)
// PreCache Anim // PreCache Anim
LoadAnm(0); LoadAnm(0);
if ((fin = kopen4load("3drealms.pal", 0)) != -1) auto pal = kloadfile("3drealms.pal", 0);
if (pal.Size() >= 768)
{ {
kread(fin, pal, PAL_SIZE); paletteSetColorTable(1, pal.Data());
kclose(fin);
paletteSetColorTable(1, pal);
videoSetPalette(gs.Brightness, 1, 2); videoSetPalette(gs.Brightness, 1, 2);
} }
DSPRINTF(ds,"Just read in 3drealms.pal..."); DSPRINTF(ds,"Just read in 3drealms.pal...");
@ -1951,7 +1948,7 @@ TenScreen(void)
GetPaletteFromVESA(pal); GetPaletteFromVESA(pal);
memcpy(backup_pal, pal, PAL_SIZE); memcpy(backup_pal, pal, PAL_SIZE);
if ((fin = kopen4load("ten.pal", 0)) != -1) if ((fin = k open4load("ten.pal", 0)) != -1)
{ {
kread(fin, pal, PAL_SIZE); kread(fin, pal, PAL_SIZE);
kclose(fin); kclose(fin);
@ -2012,7 +2009,7 @@ TitleLevel(void)
videoClearViewableArea(0L); videoClearViewableArea(0L);
videoNextPage(); videoNextPage();
// if ((fin = kopen4load("title.pal", 0)) != -1) // if ((fin = k open4load("title.pal", 0)) != -1)
// { // {
// kread(fin, pal, PAL_SIZE); // kread(fin, pal, PAL_SIZE);
// kclose(fin); // kclose(fin);
@ -3216,11 +3213,10 @@ void DosScreen(void)
#define DOS_SCREEN_SIZE (4000-(80*2)) #define DOS_SCREEN_SIZE (4000-(80*2))
#define DOS_SCREEN_PTR ((void *)(0xB8000)) #define DOS_SCREEN_PTR ((void *)(0xB8000))
int fin;
int i; int i;
char buffer[DOS_SCREEN_SIZE]; char buffer[DOS_SCREEN_SIZE];
fin = kopen4load(DOS_SCREEN_NAME,0); fin = k open4load(DOS_SCREEN_NAME,0);
if (fin == -1) if (fin == -1)
return; return;
@ -3370,19 +3366,15 @@ int DetectShareware(void)
int h; int h;
h = kopen4load(DOS_SCREEN_NAME_SW,1); if (testkopen(DOS_SCREEN_NAME_SW, 1))
if (h >= 0)
{ {
isShareware = TRUE; isShareware = TRUE;
kclose(h);
return 0; return 0;
} }
h = kopen4load(DOS_SCREEN_NAME_REG,1); if (testkopen(DOS_SCREEN_NAME_REG, 1))
if (h >= 0)
{ {
isShareware = FALSE; isShareware = FALSE;
kclose(h);
return 0; return 0;
} }
@ -3469,15 +3461,6 @@ int32_t app_main(int32_t argc, char const * const * argv)
} }
} }
#ifdef RENDERTYPEWIN
if (win_checkinstance())
{
if (!wm_ynbox("Shadow Warrior","Another Build game is currently running. "
"Do you wish to continue starting this copy?"))
return 0;
}
#endif
#if defined(PREFIX) #if defined(PREFIX)
{ {
const char *prefixdir = PREFIX; const char *prefixdir = PREFIX;
@ -4057,7 +4040,7 @@ int32_t app_main(int32_t argc, char const * const * argv)
if (strchr(UserMapName, '.') == 0) if (strchr(UserMapName, '.') == 0)
strcat(UserMapName, ".map"); strcat(UserMapName, ".map");
if ((fil = kopen4load(UserMapName,0)) == -1) if (!testkopen(UserMapName,0))
{ {
#ifdef RENDERTYPEWIN #ifdef RENDERTYPEWIN
char msg[256]; char msg[256];
@ -4066,11 +4049,8 @@ int32_t app_main(int32_t argc, char const * const * argv)
#else #else
printf("ERROR: Could not find user map %s!\n\n",UserMapName); printf("ERROR: Could not find user map %s!\n\n",UserMapName);
#endif #endif
kclose(fil);
swexit(0); swexit(0);
} }
else
kclose(fil);
} }
else if (Bstrncasecmp(arg, "g", 1) == 0 && !SW_SHAREWARE) else if (Bstrncasecmp(arg, "g", 1) == 0 && !SW_SHAREWARE)

View File

@ -29,15 +29,16 @@ Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
BEGIN_SW_NS BEGIN_SW_NS
typedef BFILE* MFILE_WRITE; typedef FILE* MFILE_WRITE;
typedef int32_t MFILE_READ; typedef FILE* MFILE_READ;
#define MREAD(ptr, size, num,handle) kread((handle),(ptr), (size) * (num)) // This needs some real fixing...
#define MREAD(ptr, size, num,handle) fread((ptr),(size),(num),(handle))
#define MWRITE(ptr, size, num,handle) fwrite((ptr),(size),(num),(handle)) #define MWRITE(ptr, size, num,handle) fwrite((ptr),(size),(num),(handle))
#define MOPEN_WRITE(name) fopen(name,"wb") #define MOPEN_WRITE(name) fopen(name,"wb")
#define MOPEN_READ(name) kopen4load(name,0) #define MOPEN_READ(name) fopen(name,"rb")
#define MCLOSE_WRITE(handle) Bfclose(handle) #define MCLOSE_WRITE(handle) fclose(handle)
#define MCLOSE_READ(handle) kclose(handle) #define MCLOSE_READ(handle) fclose(handle)
#define MOPEN_WRITE_ERR 0 #define MOPEN_WRITE_ERR nullptr
#define MOPEN_READ_ERR -1 #define MOPEN_READ_ERR nullptr
END_SW_NS END_SW_NS