diff --git a/source/blood/src/barf.cpp b/source/blood/src/barf.cpp index 92f3f4850..789f047e2 100644 --- a/source/blood/src/barf.cpp +++ b/source/blood/src/barf.cpp @@ -170,22 +170,20 @@ int RFS::Open(const char *fileName) { strcpy(_fileName, fileName); - buildvfs_fd hFile = kopen4loadfrommod(fileName, 0); - if (hFile == buildvfs_fd_invalid) { + auto hFile = kopenFileReader(fileName, 0); + if (!hFile.isOpen()) { initprintf("BARF: Error opening file %s", _fileName); return 1; } - int fileSize = kfilelength(hFile); + int fileSize = hFile.GetLength(); _ptr = (char*)Resource::Alloc(fileSize); if (_ptr == NULL) { initprintf("BARF: Not enough memory to read %s", _fileName); - kclose(hFile); return 1; } - kread(hFile, _ptr, fileSize); - kclose(hFile); + hFile.Read(_ptr, fileSize); _curLine = 0; _pUnknown2 = _ptr; diff --git a/source/blood/src/credits.cpp b/source/blood/src/credits.cpp index 5d5b297b2..51850b3d7 100644 --- a/source/blood/src/credits.cpp +++ b/source/blood/src/credits.cpp @@ -151,7 +151,7 @@ void credReset(void) DoUnFade(1); } -int credKOpen4Load(char *&pzFile) +FileReader credKOpen4Load(char *&pzFile) { int nLen = strlen(pzFile); for (int i = 0; i < nLen; i++) @@ -159,14 +159,14 @@ int credKOpen4Load(char *&pzFile) if (pzFile[i] == '\\') pzFile[i] = '/'; } - int nHandle = kopen4loadfrommod(pzFile, 0); - if (nHandle == -1) + auto nHandle = kopenFileReader(pzFile, 0); + if (!nHandle.isOpen()) { // Hack if (nLen >= 3 && isalpha(pzFile[0]) && pzFile[1] == ':' && pzFile[2] == '/') { pzFile += 3; - nHandle = kopen4loadfrommod(pzFile, 0); + nHandle = kopenFileReader(pzFile, 0); } } return nHandle; @@ -197,14 +197,13 @@ void credPlaySmk(const char *_pzSMK, const char *_pzWAV, int nWav) char *pzWAV = Xstrdup(_pzWAV); char *pzSMK_ = pzSMK; char *pzWAV_ = pzWAV; - int nHandleSMK = credKOpen4Load(pzSMK); - if (nHandleSMK == -1) + auto nHandleSMK = credKOpen4Load(pzSMK); + if (!nHandleSMK.isOpen()) { Bfree(pzSMK_); Bfree(pzWAV_); return; } - kclose(nHandleSMK); SmackerHandle hSMK = Smacker_Open(pzSMK); if (!hSMK.isValid) { @@ -242,10 +241,9 @@ void credPlaySmk(const char *_pzSMK, const char *_pzWAV, int nWav) sndStartWavID(nWav, FXVolume); else { - int nHandleWAV = credKOpen4Load(pzWAV); - if (nHandleWAV != -1) + auto nHandleWAV = credKOpen4Load(pzWAV); + if (nHandleWAV.isOpen()) { - kclose(nHandleWAV); sndStartWavDisk(pzWAV, FXVolume); } } diff --git a/source/blood/src/credits.h b/source/blood/src/credits.h index 03cbb05ae..f74367c70 100644 --- a/source/blood/src/credits.h +++ b/source/blood/src/credits.h @@ -26,6 +26,6 @@ BEGIN_BLD_NS void credLogosDos(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 diff --git a/source/blood/src/demo.cpp b/source/blood/src/demo.cpp index c294dbb2e..725d5ca17 100644 --- a/source/blood/src/demo.cpp +++ b/source/blood/src/demo.cpp @@ -219,19 +219,19 @@ bool CDemo::SetupPlayback(const char *pzFile) at1 = 0; if (pzFile) { - hPFile = kopen4loadfrommod(pzFile, 0); - if (hPFile == -1) + hPFile = fopenFileReader(pzFile, 0); + if (!hPFile.isOpen()) return false; } else { if (!pCurrentDemo) return false; - hPFile = kopen4loadfrommod(pCurrentDemo->zName, 0); - if (hPFile == -1) + hPFile = fopenFileReader(pCurrentDemo->zName, 0); + if (hPFile.isOpen()) return false; } - kread(hPFile, &atf, sizeof(DEMOHEADER)); + hPFile.Read(&atf, sizeof(DEMOHEADER)); #if B_BIG_ENDIAN == 1 atf.signature = B_LITTLE32(atf.signature); atf.nVersion = B_LITTLE16(atf.nVersion); @@ -253,14 +253,14 @@ bool CDemo::SetupPlayback(const char *pzFile) GAMEOPTIONSLEGACY gameOptions; if (BloodVersion != atf.nVersion) return 0; - kread(hPFile, &gameOptions, sizeof(GAMEOPTIONSLEGACY)); + hPFile.Read(&gameOptions, sizeof(GAMEOPTIONSLEGACY)); ReadGameOptionsLegacy(m_gameOptions, gameOptions); } else { if (BYTEVERSION != atf.nVersion) return 0; - kread(hPFile, &m_gameOptions, sizeof(GAMEOPTIONS)); + hPFile.Read(&m_gameOptions, sizeof(GAMEOPTIONS)); } #if B_BIG_ENDIAN == 1 m_gameOptions.nEpisode = B_LITTLE32(m_gameOptions.nEpisode); @@ -401,7 +401,7 @@ _DEMOPLAYBACK: else { int const nOffset = sizeof(DEMOHEADER)+(m_bLegacy ? sizeof(GAMEOPTIONSLEGACY) : sizeof(GAMEOPTIONS)); - klseek(hPFile, nOffset, SEEK_SET); + hPFile.Seek(nOffset, FileReader::SeekSet); v4 = 0; } } @@ -444,11 +444,10 @@ void CDemo::LoadDemoInfo(void) auto pIterator = pList; while (pIterator != NULL) { - int hFile = kopen4loadfrommod(pIterator->name, 0); - if (hFile == -1) + auto hFile = fopenFileReader(pIterator->name, 0); + if (!hFile.isOpen()) ThrowError("Error loading demo file header."); - kread(hFile, &atf, sizeof(atf)); - kclose(hFile); + hFile.Read(&atf, sizeof(atf)); #if B_BIG_ENDIAN == 1 atf.signature = B_LITTLE32(atf.signature); atf.nVersion = B_LITTLE16(atf.nVersion); @@ -531,7 +530,7 @@ void CDemo::ReadInput(int nCount) if (m_bLegacy) { char pBuffer[nInputSizeLegacy*kInputBufferSize]; - kread(hPFile, pBuffer, nInputSizeLegacy*nCount); + hPFile.Read(pBuffer, nInputSizeLegacy*nCount); BitReader bitReader(pBuffer, sizeof(pBuffer)); memset(at1aa, 0, nCount * sizeof(GINPUT)); for (int i = 0; i < nCount; i++) @@ -583,7 +582,7 @@ void CDemo::ReadInput(int nCount) else { char pBuffer[nInputSize*kInputBufferSize]; - kread(hPFile, pBuffer, nInputSize*nCount); + hPFile.Read(pBuffer, nInputSize*nCount); BitReader bitReader(pBuffer, sizeof(pBuffer)); memset(at1aa, 0, nCount * sizeof(GINPUT)); for (int i = 0; i < nCount; i++) diff --git a/source/blood/src/demo.h b/source/blood/src/demo.h index dc1a6b0db..bede89f3e 100644 --- a/source/blood/src/demo.h +++ b/source/blood/src/demo.h @@ -97,7 +97,7 @@ public: bool m_bLegacy; char at2; int at3; - int hPFile; + FileReader hPFile; FILE *hRFile; int atb; DEMOHEADER atf; diff --git a/source/blood/src/inifile.cpp b/source/blood/src/inifile.cpp index bc7543a12..b9263efe6 100644 --- a/source/blood/src/inifile.cpp +++ b/source/blood/src/inifile.cpp @@ -130,14 +130,12 @@ void IniFile::Load() curNode = &head; - int fp = kopen4loadfrommod(fileName, 0); - if (fp >= 0) + auto fp = kopenFileReader(fileName, 0); + if (fp.isOpen()) { - int nSize = kfilelength(fp); - void *pBuffer = Xcalloc(1, nSize + 1); - kread(fp, pBuffer, nSize); - LoadRes(pBuffer); - Bfree(pBuffer); + int nSize = fp.GetLength(); + auto pBuffer = fp.Read(); + LoadRes(pBuffer.Data()); } else curNode->next = &head; diff --git a/source/blood/src/levels.cpp b/source/blood/src/levels.cpp index cd609417f..0f4611b9a 100644 --- a/source/blood/src/levels.cpp +++ b/source/blood/src/levels.cpp @@ -69,10 +69,8 @@ IniFile *BloodINI; void levelInitINI(const char *pzIni) { - int fp = kopen4loadfrommod(pzIni, 0); - if (fp < 0) + if (!testkopen(pzIni, 0)) ThrowError("Initialization: %s does not exist", pzIni); - kclose(fp); BloodINI = new IniFile(pzIni); Bstrncpy(BloodIniFile, pzIni, BMAX_PATH); Bstrncpy(BloodIniPre, pzIni, BMAX_PATH); diff --git a/source/blood/src/loadsave.cpp b/source/blood/src/loadsave.cpp index 165be9c9f..f45f99ca0 100644 --- a/source/blood/src/loadsave.cpp +++ b/source/blood/src/loadsave.cpp @@ -59,7 +59,7 @@ void *dword_27AA44 = NULL; LoadSave LoadSave::head(123); FILE *LoadSave::hSFile = NULL; -int LoadSave::hLFile = -1; +FileReader LoadSave::hLFile; short word_27AA54 = 0; @@ -82,8 +82,8 @@ void LoadSave::Load(void) void LoadSave::Read(void *pData, int nSize) { dword_27AA38 += nSize; - dassert(hLFile != -1); - if (kread(hLFile, pData, nSize) != nSize) + dassert(hLFile.isOpen()); + if (hLFile.Read(pData, nSize) != nSize) ThrowError("Error reading save file."); } @@ -112,8 +112,8 @@ void LoadSave::LoadGame(char *pzFile) memset(sprite, 0, sizeof(spritetype)*kMaxSprites); automapping = 1; } - hLFile = kopen4load(pzFile, 0); - if (hLFile == -1) + hLFile = fopenFileReader(pzFile, 0); + if (!hLFile.isOpen()) ThrowError("Error loading save file."); LoadSave *rover = head.next; while (rover != &head) @@ -121,8 +121,7 @@ void LoadSave::LoadGame(char *pzFile) rover->Load(); rover = rover->next; } - kclose(hLFile); - hLFile = -1; + hLFile.Close(); if (!gGameStarted) scrLoadPLUs(); InitSectorFX(); @@ -429,33 +428,29 @@ void LoadSavedInfo(void) int nCount = 0; for (auto pIterator = pList; pIterator != NULL && nCount < 10; pIterator = pIterator->next, nCount++) { - int hFile = kopen4loadfrommod(pIterator->name, 0); - if (hFile == -1) + auto hFile = kopenFileReader(pIterator->name, 0); + if (!hFile.isOpen()) ThrowError("Error loading save file header."); int vc; short v4; vc = 0; 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; } if (vc != 0x5653424e/*'VSBN'*/) { - kclose(hFile); continue; } - kread(hFile, &v4, sizeof(v4)); + hFile.Read(&v4, sizeof(v4)); if (v4 != BYTEVERSION) { - kclose(hFile); 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."); strcpy(strRestoreGameStrings[gSaveGameOptions[nCount].nSaveGameSlot], gSaveGameOptions[nCount].szUserGameName); - kclose(hFile); } klistfree(pList); } diff --git a/source/blood/src/loadsave.h b/source/blood/src/loadsave.h index 03f6ffd87..760fda235 100644 --- a/source/blood/src/loadsave.h +++ b/source/blood/src/loadsave.h @@ -30,7 +30,7 @@ class LoadSave { public: static LoadSave head; static FILE *hSFile; - static int hLFile; + static FileReader hLFile; LoadSave *prev; LoadSave *next; LoadSave() { diff --git a/source/build/src/defs.cpp b/source/build/src/defs.cpp index cf637dd10..d5c6dbdf0 100644 --- a/source/build/src/defs.cpp +++ b/source/build/src/defs.cpp @@ -2601,29 +2601,26 @@ static int32_t defsparser(scriptfile *script) break; } - buildvfs_kfd const fil = kopen4load(fn, 0); - if (EDUKE32_PREDICT_FALSE(fil == buildvfs_kfd_invalid)) + FileReader fil = kopenFileReader(fn, 0); + if (!fil.isOpen()) { initprintf("Error: basepalette: Failed opening \"%s\" on line %s:%d\n", fn, script->filename, scriptfile_getlinum(script,cmdtokptr)); 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", script->filename, scriptfile_getlinum(script,cmdtokptr)); - kclose(fil); break; } - uint8_t * const palbuf = (uint8_t *)Xmalloc(768); - if (kread_and_test(fil,palbuf,768)) + auto palbuf = fil.Read(); + if (palbuf.Size() < 768) { initprintf("Error: basepalette: Read failed on line %s:%d\n", script->filename, scriptfile_getlinum(script,cmdtokptr)); - Xfree(palbuf); - kclose(fil); break; } @@ -2633,11 +2630,8 @@ static int32_t defsparser(scriptfile *script) palbuf[k] <<= shiftleft; } - paletteSetColorTable(id, palbuf); + paletteSetColorTable(id, palbuf.Data()); didLoadPal = 1; - - Xfree(palbuf); - kclose(fil); break; } case T_COPY: @@ -2781,38 +2775,34 @@ static int32_t defsparser(scriptfile *script) break; } - buildvfs_kfd const fil = kopen4load(fn, 0); - if (EDUKE32_PREDICT_FALSE(fil == buildvfs_kfd_invalid)) + FileReader fil = kopenFileReader(fn, 0); + if (!fil.isOpen()) { initprintf("Error: palookup: Failed opening \"%s\" on line %s:%d\n", fn, script->filename, scriptfile_getlinum(script,cmdtokptr)); 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", script->filename, scriptfile_getlinum(script,cmdtokptr)); - kclose(fil); break; } - char * const palookupbuf = (char *)Xmalloc(length); - int32_t bytesread = kread(fil, palookupbuf, length); - if (bytesread < 256) + auto palookupbuf = fil.Read(); + if (palookupbuf.Size() < 256) { initprintf("Error: palookup: Read failed on line %s:%d\n", script->filename, scriptfile_getlinum(script,cmdtokptr)); - Xfree(palookupbuf); - kclose(fil); break; } - if (bytesread == 256*32) + if (palookupbuf >= 256*32) { didLoadShade = 1; numshades = 32; - paletteSetLookupTable(id, (uint8_t *)palookupbuf); + paletteSetLookupTable(id, palookupbuf.Data()); } else { @@ -2825,9 +2815,6 @@ static int32_t defsparser(scriptfile *script) paletteMakeLookupTable(id, palookupbuf, 0,0,0, g_noFloorPal[id]); } - - Xfree(palookupbuf); - kclose(fil); break; } case T_COPY: @@ -3081,37 +3068,31 @@ static int32_t defsparser(scriptfile *script) break; } - buildvfs_kfd const fil = kopen4load(fn, 0); - if (EDUKE32_PREDICT_FALSE(fil == buildvfs_kfd_invalid)) + FileReader fil = kopenFileReader(fn, 0); + if (!fil.isOpen()) { initprintf("Error: blendtable: Failed opening \"%s\" on line %s:%d\n", fn, script->filename, scriptfile_getlinum(script,cmdtokptr)); 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", script->filename, scriptfile_getlinum(script,cmdtokptr)); - kclose(fil); break; } - char * const blendbuf = (char *)Xmalloc(256*256); - if (kread_and_test(fil,blendbuf,256*256)) + auto blendbuf = fil.Read(); + if (blendbuf.Size() < 256*256) { initprintf("Error: blendtable: Read failed on line %s:%d\n", script->filename, scriptfile_getlinum(script,cmdtokptr)); - Xfree(blendbuf); - kclose(fil); break; } paletteSetBlendTable(id, blendbuf); didLoadTransluc = 1; - - Xfree(blendbuf); - kclose(fil); break; } case T_COPY: diff --git a/source/build/src/palette.cpp b/source/build/src/palette.cpp index 2db4f3b37..b0d5dc21b 100644 --- a/source/build/src/palette.cpp +++ b/source/build/src/palette.cpp @@ -547,6 +547,7 @@ void paletteSetBlendTable(int32_t blend, const char *tab) blendtable[blend] = (char *) Xmalloc(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) {