- refactored most of the remaining calls to kopen4load

This commit is contained in:
Christoph Oelckers 2019-10-21 17:16:08 +02:00
parent 1269a1715f
commit 8c7590e161
11 changed files with 64 additions and 96 deletions

View file

@ -170,22 +170,20 @@ int RFS::Open(const char *fileName)
{ {
strcpy(_fileName, fileName); strcpy(_fileName, fileName);
buildvfs_fd hFile = kopen4loadfrommod(fileName, 0); auto hFile = kopenFileReader(fileName, 0);
if (hFile == buildvfs_fd_invalid) { if (!hFile.isOpen()) {
initprintf("BARF: Error opening file %s", _fileName); initprintf("BARF: Error opening file %s", _fileName);
return 1; return 1;
} }
int fileSize = kfilelength(hFile); int fileSize = hFile.GetLength();
_ptr = (char*)Resource::Alloc(fileSize); _ptr = (char*)Resource::Alloc(fileSize);
if (_ptr == NULL) { if (_ptr == NULL) {
initprintf("BARF: Not enough memory to read %s", _fileName); initprintf("BARF: Not enough memory to read %s", _fileName);
kclose(hFile);
return 1; return 1;
} }
kread(hFile, _ptr, fileSize); hFile.Read(_ptr, fileSize);
kclose(hFile);
_curLine = 0; _curLine = 0;
_pUnknown2 = _ptr; _pUnknown2 = _ptr;

View file

@ -151,7 +151,7 @@ void credReset(void)
DoUnFade(1); DoUnFade(1);
} }
int credKOpen4Load(char *&pzFile) FileReader credKOpen4Load(char *&pzFile)
{ {
int nLen = strlen(pzFile); int nLen = strlen(pzFile);
for (int i = 0; i < nLen; i++) for (int i = 0; i < nLen; i++)
@ -159,14 +159,14 @@ int credKOpen4Load(char *&pzFile)
if (pzFile[i] == '\\') if (pzFile[i] == '\\')
pzFile[i] = '/'; pzFile[i] = '/';
} }
int nHandle = kopen4loadfrommod(pzFile, 0); auto nHandle = kopenFileReader(pzFile, 0);
if (nHandle == -1) if (!nHandle.isOpen())
{ {
// Hack // Hack
if (nLen >= 3 && isalpha(pzFile[0]) && pzFile[1] == ':' && pzFile[2] == '/') if (nLen >= 3 && isalpha(pzFile[0]) && pzFile[1] == ':' && pzFile[2] == '/')
{ {
pzFile += 3; pzFile += 3;
nHandle = kopen4loadfrommod(pzFile, 0); nHandle = kopenFileReader(pzFile, 0);
} }
} }
return nHandle; return nHandle;
@ -197,14 +197,13 @@ void credPlaySmk(const char *_pzSMK, const char *_pzWAV, int nWav)
char *pzWAV = Xstrdup(_pzWAV); char *pzWAV = Xstrdup(_pzWAV);
char *pzSMK_ = pzSMK; char *pzSMK_ = pzSMK;
char *pzWAV_ = pzWAV; char *pzWAV_ = pzWAV;
int nHandleSMK = credKOpen4Load(pzSMK); auto nHandleSMK = credKOpen4Load(pzSMK);
if (nHandleSMK == -1) if (!nHandleSMK.isOpen())
{ {
Bfree(pzSMK_); Bfree(pzSMK_);
Bfree(pzWAV_); Bfree(pzWAV_);
return; return;
} }
kclose(nHandleSMK);
SmackerHandle hSMK = Smacker_Open(pzSMK); SmackerHandle hSMK = Smacker_Open(pzSMK);
if (!hSMK.isValid) if (!hSMK.isValid)
{ {
@ -242,10 +241,9 @@ void credPlaySmk(const char *_pzSMK, const char *_pzWAV, int nWav)
sndStartWavID(nWav, FXVolume); sndStartWavID(nWav, FXVolume);
else else
{ {
int nHandleWAV = credKOpen4Load(pzWAV); auto nHandleWAV = credKOpen4Load(pzWAV);
if (nHandleWAV != -1) if (nHandleWAV.isOpen())
{ {
kclose(nHandleWAV);
sndStartWavDisk(pzWAV, FXVolume); sndStartWavDisk(pzWAV, FXVolume);
} }
} }

View file

@ -26,6 +26,6 @@ BEGIN_BLD_NS
void credLogosDos(void); void credLogosDos(void);
void credReset(void); void credReset(void);
void credPlaySmk(const char *pzSMK, const char *pzWAV, int nWAV); FileReader credPlaySmk(const char *pzSMK, const char *pzWAV, int nWAV);
END_BLD_NS END_BLD_NS

View file

@ -219,19 +219,19 @@ bool CDemo::SetupPlayback(const char *pzFile)
at1 = 0; at1 = 0;
if (pzFile) if (pzFile)
{ {
hPFile = kopen4loadfrommod(pzFile, 0); hPFile = fopenFileReader(pzFile, 0);
if (hPFile == -1) if (!hPFile.isOpen())
return false; return false;
} }
else else
{ {
if (!pCurrentDemo) if (!pCurrentDemo)
return false; return false;
hPFile = kopen4loadfrommod(pCurrentDemo->zName, 0); hPFile = fopenFileReader(pCurrentDemo->zName, 0);
if (hPFile == -1) if (hPFile.isOpen())
return false; return false;
} }
kread(hPFile, &atf, sizeof(DEMOHEADER)); hPFile.Read(&atf, sizeof(DEMOHEADER));
#if B_BIG_ENDIAN == 1 #if B_BIG_ENDIAN == 1
atf.signature = B_LITTLE32(atf.signature); atf.signature = B_LITTLE32(atf.signature);
atf.nVersion = B_LITTLE16(atf.nVersion); atf.nVersion = B_LITTLE16(atf.nVersion);
@ -253,14 +253,14 @@ bool CDemo::SetupPlayback(const char *pzFile)
GAMEOPTIONSLEGACY gameOptions; GAMEOPTIONSLEGACY gameOptions;
if (BloodVersion != atf.nVersion) if (BloodVersion != atf.nVersion)
return 0; return 0;
kread(hPFile, &gameOptions, sizeof(GAMEOPTIONSLEGACY)); hPFile.Read(&gameOptions, sizeof(GAMEOPTIONSLEGACY));
ReadGameOptionsLegacy(m_gameOptions, gameOptions); ReadGameOptionsLegacy(m_gameOptions, gameOptions);
} }
else else
{ {
if (BYTEVERSION != atf.nVersion) if (BYTEVERSION != atf.nVersion)
return 0; return 0;
kread(hPFile, &m_gameOptions, sizeof(GAMEOPTIONS)); hPFile.Read(&m_gameOptions, sizeof(GAMEOPTIONS));
} }
#if B_BIG_ENDIAN == 1 #if B_BIG_ENDIAN == 1
m_gameOptions.nEpisode = B_LITTLE32(m_gameOptions.nEpisode); m_gameOptions.nEpisode = B_LITTLE32(m_gameOptions.nEpisode);
@ -401,7 +401,7 @@ _DEMOPLAYBACK:
else else
{ {
int const nOffset = sizeof(DEMOHEADER)+(m_bLegacy ? sizeof(GAMEOPTIONSLEGACY) : sizeof(GAMEOPTIONS)); int const nOffset = sizeof(DEMOHEADER)+(m_bLegacy ? sizeof(GAMEOPTIONSLEGACY) : sizeof(GAMEOPTIONS));
klseek(hPFile, nOffset, SEEK_SET); hPFile.Seek(nOffset, FileReader::SeekSet);
v4 = 0; v4 = 0;
} }
} }
@ -444,11 +444,10 @@ void CDemo::LoadDemoInfo(void)
auto pIterator = pList; auto pIterator = pList;
while (pIterator != NULL) while (pIterator != NULL)
{ {
int hFile = kopen4loadfrommod(pIterator->name, 0); auto hFile = fopenFileReader(pIterator->name, 0);
if (hFile == -1) if (!hFile.isOpen())
ThrowError("Error loading demo file header."); ThrowError("Error loading demo file header.");
kread(hFile, &atf, sizeof(atf)); hFile.Read(&atf, sizeof(atf));
kclose(hFile);
#if B_BIG_ENDIAN == 1 #if B_BIG_ENDIAN == 1
atf.signature = B_LITTLE32(atf.signature); atf.signature = B_LITTLE32(atf.signature);
atf.nVersion = B_LITTLE16(atf.nVersion); atf.nVersion = B_LITTLE16(atf.nVersion);
@ -531,7 +530,7 @@ void CDemo::ReadInput(int nCount)
if (m_bLegacy) if (m_bLegacy)
{ {
char pBuffer[nInputSizeLegacy*kInputBufferSize]; char pBuffer[nInputSizeLegacy*kInputBufferSize];
kread(hPFile, pBuffer, nInputSizeLegacy*nCount); hPFile.Read(pBuffer, nInputSizeLegacy*nCount);
BitReader bitReader(pBuffer, sizeof(pBuffer)); BitReader bitReader(pBuffer, sizeof(pBuffer));
memset(at1aa, 0, nCount * sizeof(GINPUT)); memset(at1aa, 0, nCount * sizeof(GINPUT));
for (int i = 0; i < nCount; i++) for (int i = 0; i < nCount; i++)
@ -583,7 +582,7 @@ void CDemo::ReadInput(int nCount)
else else
{ {
char pBuffer[nInputSize*kInputBufferSize]; char pBuffer[nInputSize*kInputBufferSize];
kread(hPFile, pBuffer, nInputSize*nCount); hPFile.Read(pBuffer, nInputSize*nCount);
BitReader bitReader(pBuffer, sizeof(pBuffer)); BitReader bitReader(pBuffer, sizeof(pBuffer));
memset(at1aa, 0, nCount * sizeof(GINPUT)); memset(at1aa, 0, nCount * sizeof(GINPUT));
for (int i = 0; i < nCount; i++) for (int i = 0; i < nCount; i++)

View file

@ -97,7 +97,7 @@ public:
bool m_bLegacy; bool m_bLegacy;
char at2; char at2;
int at3; int at3;
int hPFile; FileReader hPFile;
FILE *hRFile; FILE *hRFile;
int atb; int atb;
DEMOHEADER atf; DEMOHEADER atf;

View file

@ -130,14 +130,12 @@ void IniFile::Load()
curNode = &head; curNode = &head;
int fp = kopen4loadfrommod(fileName, 0); auto fp = kopenFileReader(fileName, 0);
if (fp >= 0) if (fp.isOpen())
{ {
int nSize = kfilelength(fp); int nSize = fp.GetLength();
void *pBuffer = Xcalloc(1, nSize + 1); auto pBuffer = fp.Read();
kread(fp, pBuffer, nSize); LoadRes(pBuffer.Data());
LoadRes(pBuffer);
Bfree(pBuffer);
} }
else else
curNode->next = &head; curNode->next = &head;

View file

@ -69,10 +69,8 @@ IniFile *BloodINI;
void levelInitINI(const char *pzIni) void levelInitINI(const char *pzIni)
{ {
int fp = kopen4loadfrommod(pzIni, 0); if (!testkopen(pzIni, 0))
if (fp < 0)
ThrowError("Initialization: %s does not exist", pzIni); ThrowError("Initialization: %s does not exist", pzIni);
kclose(fp);
BloodINI = new IniFile(pzIni); BloodINI = new IniFile(pzIni);
Bstrncpy(BloodIniFile, pzIni, BMAX_PATH); Bstrncpy(BloodIniFile, pzIni, BMAX_PATH);
Bstrncpy(BloodIniPre, pzIni, BMAX_PATH); Bstrncpy(BloodIniPre, pzIni, BMAX_PATH);

View file

@ -59,7 +59,7 @@ void *dword_27AA44 = NULL;
LoadSave LoadSave::head(123); LoadSave LoadSave::head(123);
FILE *LoadSave::hSFile = NULL; FILE *LoadSave::hSFile = NULL;
int LoadSave::hLFile = -1; FileReader LoadSave::hLFile;
short word_27AA54 = 0; short word_27AA54 = 0;
@ -82,8 +82,8 @@ void LoadSave::Load(void)
void LoadSave::Read(void *pData, int nSize) void LoadSave::Read(void *pData, int nSize)
{ {
dword_27AA38 += nSize; dword_27AA38 += nSize;
dassert(hLFile != -1); dassert(hLFile.isOpen());
if (kread(hLFile, pData, nSize) != nSize) if (hLFile.Read(pData, nSize) != nSize)
ThrowError("Error reading save file."); ThrowError("Error reading save file.");
} }
@ -112,8 +112,8 @@ void LoadSave::LoadGame(char *pzFile)
memset(sprite, 0, sizeof(spritetype)*kMaxSprites); memset(sprite, 0, sizeof(spritetype)*kMaxSprites);
automapping = 1; automapping = 1;
} }
hLFile = kopen4load(pzFile, 0); hLFile = fopenFileReader(pzFile, 0);
if (hLFile == -1) if (!hLFile.isOpen())
ThrowError("Error loading save file."); ThrowError("Error loading save file.");
LoadSave *rover = head.next; LoadSave *rover = head.next;
while (rover != &head) while (rover != &head)
@ -121,8 +121,7 @@ void LoadSave::LoadGame(char *pzFile)
rover->Load(); rover->Load();
rover = rover->next; rover = rover->next;
} }
kclose(hLFile); hLFile.Close();
hLFile = -1;
if (!gGameStarted) if (!gGameStarted)
scrLoadPLUs(); scrLoadPLUs();
InitSectorFX(); InitSectorFX();
@ -429,33 +428,29 @@ void LoadSavedInfo(void)
int nCount = 0; int nCount = 0;
for (auto pIterator = pList; pIterator != NULL && nCount < 10; pIterator = pIterator->next, nCount++) for (auto pIterator = pList; pIterator != NULL && nCount < 10; pIterator = pIterator->next, nCount++)
{ {
int hFile = kopen4loadfrommod(pIterator->name, 0); auto hFile = kopenFileReader(pIterator->name, 0);
if (hFile == -1) if (!hFile.isOpen())
ThrowError("Error loading save file header."); ThrowError("Error loading save file header.");
int vc; int vc;
short v4; short v4;
vc = 0; vc = 0;
v4 = word_27AA54; v4 = word_27AA54;
if ((uint32_t)kread(hFile, &vc, sizeof(vc)) != sizeof(vc)) if ((uint32_t)hFile.Read(&vc, sizeof(vc)) != sizeof(vc))
{ {
kclose(hFile);
continue; continue;
} }
if (vc != 0x5653424e/*'VSBN'*/) if (vc != 0x5653424e/*'VSBN'*/)
{ {
kclose(hFile);
continue; continue;
} }
kread(hFile, &v4, sizeof(v4)); hFile.Read(&v4, sizeof(v4));
if (v4 != BYTEVERSION) if (v4 != BYTEVERSION)
{ {
kclose(hFile);
continue; continue;
} }
if ((uint32_t)kread(hFile, &gSaveGameOptions[nCount], sizeof(gSaveGameOptions[0])) != sizeof(gSaveGameOptions[0])) if ((uint32_t)hFile.Read(&gSaveGameOptions[nCount], sizeof(gSaveGameOptions[0])) != sizeof(gSaveGameOptions[0]))
ThrowError("Error reading save file."); ThrowError("Error reading save file.");
strcpy(strRestoreGameStrings[gSaveGameOptions[nCount].nSaveGameSlot], gSaveGameOptions[nCount].szUserGameName); strcpy(strRestoreGameStrings[gSaveGameOptions[nCount].nSaveGameSlot], gSaveGameOptions[nCount].szUserGameName);
kclose(hFile);
} }
klistfree(pList); klistfree(pList);
} }

View file

@ -30,7 +30,7 @@ class LoadSave {
public: public:
static LoadSave head; static LoadSave head;
static FILE *hSFile; static FILE *hSFile;
static int hLFile; static FileReader hLFile;
LoadSave *prev; LoadSave *prev;
LoadSave *next; LoadSave *next;
LoadSave() { LoadSave() {

View file

@ -2601,29 +2601,26 @@ static int32_t defsparser(scriptfile *script)
break; break;
} }
buildvfs_kfd const fil = kopen4load(fn, 0); FileReader fil = kopenFileReader(fn, 0);
if (EDUKE32_PREDICT_FALSE(fil == buildvfs_kfd_invalid)) if (!fil.isOpen())
{ {
initprintf("Error: basepalette: Failed opening \"%s\" on line %s:%d\n", fn, initprintf("Error: basepalette: Failed opening \"%s\" on line %s:%d\n", fn,
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
break; break;
} }
if (klseek_and_test(fil, offset, BSEEK_SET)) if (fil.Seek(offset, FileReader::SeekSet) < 0)
{ {
initprintf("Error: basepalette: Seek failed on line %s:%d\n", initprintf("Error: basepalette: Seek failed on line %s:%d\n",
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
kclose(fil);
break; break;
} }
uint8_t * const palbuf = (uint8_t *)Xmalloc(768); auto palbuf = fil.Read();
if (kread_and_test(fil,palbuf,768)) if (palbuf.Size() < 768)
{ {
initprintf("Error: basepalette: Read failed on line %s:%d\n", initprintf("Error: basepalette: Read failed on line %s:%d\n",
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
Xfree(palbuf);
kclose(fil);
break; break;
} }
@ -2633,11 +2630,8 @@ static int32_t defsparser(scriptfile *script)
palbuf[k] <<= shiftleft; palbuf[k] <<= shiftleft;
} }
paletteSetColorTable(id, palbuf); paletteSetColorTable(id, palbuf.Data());
didLoadPal = 1; didLoadPal = 1;
Xfree(palbuf);
kclose(fil);
break; break;
} }
case T_COPY: case T_COPY:
@ -2781,38 +2775,34 @@ static int32_t defsparser(scriptfile *script)
break; break;
} }
buildvfs_kfd const fil = kopen4load(fn, 0); FileReader fil = kopenFileReader(fn, 0);
if (EDUKE32_PREDICT_FALSE(fil == buildvfs_kfd_invalid)) if (!fil.isOpen())
{ {
initprintf("Error: palookup: Failed opening \"%s\" on line %s:%d\n", fn, initprintf("Error: palookup: Failed opening \"%s\" on line %s:%d\n", fn,
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
break; break;
} }
if (klseek_and_test(fil, offset, BSEEK_SET)) if (fil.Seek(offset, FileReader::SeekSet) < 0)
{ {
initprintf("Error: palookup: Seek failed on line %s:%d\n", initprintf("Error: palookup: Seek failed on line %s:%d\n",
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
kclose(fil);
break; break;
} }
char * const palookupbuf = (char *)Xmalloc(length); auto palookupbuf = fil.Read();
int32_t bytesread = kread(fil, palookupbuf, length); if (palookupbuf.Size() < 256)
if (bytesread < 256)
{ {
initprintf("Error: palookup: Read failed on line %s:%d\n", initprintf("Error: palookup: Read failed on line %s:%d\n",
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
Xfree(palookupbuf);
kclose(fil);
break; break;
} }
if (bytesread == 256*32) if (palookupbuf >= 256*32)
{ {
didLoadShade = 1; didLoadShade = 1;
numshades = 32; numshades = 32;
paletteSetLookupTable(id, (uint8_t *)palookupbuf); paletteSetLookupTable(id, palookupbuf.Data());
} }
else else
{ {
@ -2825,9 +2815,6 @@ static int32_t defsparser(scriptfile *script)
paletteMakeLookupTable(id, palookupbuf, 0,0,0, g_noFloorPal[id]); paletteMakeLookupTable(id, palookupbuf, 0,0,0, g_noFloorPal[id]);
} }
Xfree(palookupbuf);
kclose(fil);
break; break;
} }
case T_COPY: case T_COPY:
@ -3081,37 +3068,31 @@ static int32_t defsparser(scriptfile *script)
break; break;
} }
buildvfs_kfd const fil = kopen4load(fn, 0); FileReader fil = kopenFileReader(fn, 0);
if (EDUKE32_PREDICT_FALSE(fil == buildvfs_kfd_invalid)) if (!fil.isOpen())
{ {
initprintf("Error: blendtable: Failed opening \"%s\" on line %s:%d\n", fn, initprintf("Error: blendtable: Failed opening \"%s\" on line %s:%d\n", fn,
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
break; break;
} }
if (klseek_and_test(fil, offset, BSEEK_SET)) if (fil.Seek(offset, FileReader::SeekSet) < 0)
{ {
initprintf("Error: blendtable: Seek failed on line %s:%d\n", initprintf("Error: blendtable: Seek failed on line %s:%d\n",
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
kclose(fil);
break; break;
} }
char * const blendbuf = (char *)Xmalloc(256*256); auto blendbuf = fil.Read();
if (kread_and_test(fil,blendbuf,256*256)) if (blendbuf.Size() < 256*256)
{ {
initprintf("Error: blendtable: Read failed on line %s:%d\n", initprintf("Error: blendtable: Read failed on line %s:%d\n",
script->filename, scriptfile_getlinum(script,cmdtokptr)); script->filename, scriptfile_getlinum(script,cmdtokptr));
Xfree(blendbuf);
kclose(fil);
break; break;
} }
paletteSetBlendTable(id, blendbuf); paletteSetBlendTable(id, blendbuf);
didLoadTransluc = 1; didLoadTransluc = 1;
Xfree(blendbuf);
kclose(fil);
break; break;
} }
case T_COPY: case T_COPY:

View file

@ -547,6 +547,7 @@ void paletteSetBlendTable(int32_t blend, const char *tab)
blendtable[blend] = (char *) Xmalloc(256*256); blendtable[blend] = (char *) Xmalloc(256*256);
Bmemcpy(blendtable[blend], tab, 256*256); Bmemcpy(blendtable[blend], tab, 256*256);
// Todo: Calculate an alpha factor from the loaded data so that the hardware renderer uses the proper amount of tranlucency.
} }
void paletteFreeBlendTable(int32_t const blend) void paletteFreeBlendTable(int32_t const blend)
{ {