- 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)
{
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);
}
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_

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 testkopen(const char *filename, char searchfirst); // full-blown kopen4load
int32_t check_file_exist(const char *fn); // findfrompath with pathsearchmode=1 / search in zips
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;
}
// 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
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)
{
int handle;
int length;
unsigned char *animbuf, *palptr;
int i,j,k;
@ -242,17 +241,16 @@ unsigned char *LoadAnm(short anim_num)
if (anm_ptr[anim_num] == 0)
{
handle = kopen4load(ANIMname[ANIMnum], 0);
if (handle == -1)
auto handle = kopenFileReader(ANIMname[ANIMnum], 0);
if (!handle.isOpen())
return NULL;
length = kfilelength(handle);
length = handle.GetLength();
buffer.Resize(length + sizeof(anim_t));
anm_ptr[anim_num] = (anim_t*)buffer.Data();
animbuf = (unsigned char *)((intptr_t)anm_ptr[anim_num] + sizeof(anim_t));
kread(handle, animbuf, length);
kclose(handle);
handle.Read(animbuf, length);
}
else
{
@ -289,10 +287,8 @@ playanm(short anim_num)
return;
// [JM] Temporary, needed to get the file's length for ANIM_LoadAnim. !CHECKME!
handle = kopen4load(ANIMname[ANIMnum], 0);
if (handle == -1) return;
length = kfilelength(handle);
kclose(handle);
length = kfilesize(ANIMname[ANIMnum], 0);
if (length == -1) return;
DSPRINTF(ds,"PlayAnm - Palette Stuff");
MONO_PRINT(ds);

View file

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

View file

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

View file

@ -1721,7 +1721,6 @@ LogoLevel(void)
char called;
int fin;
unsigned char backup_pal[256*3];
unsigned char pal[PAL_SIZE];
char tempbuf[256];
char *palook_bak = palookup[0];
UserInput uinfo = { FALSE, FALSE, dir_None };
@ -1754,12 +1753,10 @@ LogoLevel(void)
// PreCache Anim
LoadAnm(0);
if ((fin = kopen4load("3drealms.pal", 0)) != -1)
auto pal = kloadfile("3drealms.pal", 0);
if (pal.Size() >= 768)
{
kread(fin, pal, PAL_SIZE);
kclose(fin);
paletteSetColorTable(1, pal);
paletteSetColorTable(1, pal.Data());
videoSetPalette(gs.Brightness, 1, 2);
}
DSPRINTF(ds,"Just read in 3drealms.pal...");
@ -3216,7 +3213,6 @@ void DosScreen(void)
#define DOS_SCREEN_SIZE (4000-(80*2))
#define DOS_SCREEN_PTR ((void *)(0xB8000))
int fin;
int i;
char buffer[DOS_SCREEN_SIZE];
@ -3370,19 +3366,15 @@ int DetectShareware(void)
int h;
h = kopen4load(DOS_SCREEN_NAME_SW,1);
if (h >= 0)
if (testkopen(DOS_SCREEN_NAME_SW, 1))
{
isShareware = TRUE;
kclose(h);
return 0;
}
h = kopen4load(DOS_SCREEN_NAME_REG,1);
if (h >= 0)
if (testkopen(DOS_SCREEN_NAME_REG, 1))
{
isShareware = FALSE;
kclose(h);
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)
{
const char *prefixdir = PREFIX;
@ -4057,7 +4040,7 @@ int32_t app_main(int32_t argc, char const * const * argv)
if (strchr(UserMapName, '.') == 0)
strcat(UserMapName, ".map");
if ((fil = kopen4load(UserMapName,0)) == -1)
if (!testkopen(UserMapName,0))
{
#ifdef RENDERTYPEWIN
char msg[256];
@ -4066,11 +4049,8 @@ int32_t app_main(int32_t argc, char const * const * argv)
#else
printf("ERROR: Could not find user map %s!\n\n",UserMapName);
#endif
kclose(fil);
swexit(0);
}
else
kclose(fil);
}
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
typedef BFILE* MFILE_WRITE;
typedef int32_t MFILE_READ;
typedef FILE* MFILE_WRITE;
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 MOPEN_WRITE(name) fopen(name,"wb")
#define MOPEN_READ(name) kopen4load(name,0)
#define MCLOSE_WRITE(handle) Bfclose(handle)
#define MCLOSE_READ(handle) kclose(handle)
#define MOPEN_WRITE_ERR 0
#define MOPEN_READ_ERR -1
#define MOPEN_READ(name) fopen(name,"rb")
#define MCLOSE_WRITE(handle) fclose(handle)
#define MCLOSE_READ(handle) fclose(handle)
#define MOPEN_WRITE_ERR nullptr
#define MOPEN_READ_ERR nullptr
END_SW_NS