- got rid of FileSystem::GetFileData.

Using FMemFile is better in all these places.
This commit is contained in:
Christoph Oelckers 2023-08-19 16:49:07 +02:00
parent 21d6eb99eb
commit 5398045f7d
14 changed files with 50 additions and 84 deletions

View file

@ -179,11 +179,10 @@ static void SetupGenMidi()
Printf("No GENMIDI lump found. OPL playback not available.\n"); Printf("No GENMIDI lump found. OPL playback not available.\n");
return; return;
} }
auto data = fileSystem.OpenFileReader(lump); auto genmidi = fileSystem.ReadFile(lump);
auto genmidi = data.Read(); if (genmidi.GetSize() < 8 + 175 * 36 || memcmp(genmidi.GetMem(), "#OPL_II#", 8)) return;
if (genmidi.size() < 8 + 175 * 36 || memcmp(genmidi.data(), "#OPL_II#", 8)) return; ZMusic_SetGenMidi((uint8_t*)genmidi.GetString().GetChars() + 8);
ZMusic_SetGenMidi(genmidi.data()+8);
} }
static void SetupWgOpn() static void SetupWgOpn()

View file

@ -136,7 +136,7 @@ void FScanner::Open (const char *name)
// //
// FScanner :: OpenFile // FScanner :: OpenFile
// //
// Loads a script from a file. Uses new/delete for memory allocation. // Loads a script from a file.
// //
//========================================================================== //==========================================================================

View file

@ -60,10 +60,10 @@ void FStringTable::LoadStrings (const char *language)
lastlump = 0; lastlump = 0;
while ((lump = fileSystem.FindLump ("LANGUAGE", &lastlump)) != -1) while ((lump = fileSystem.FindLump ("LANGUAGE", &lastlump)) != -1)
{ {
auto lumpdata = fileSystem.GetFileData(lump); auto lumpdata = fileSystem.ReadFile(lump);
if (!ParseLanguageCSV(lump, lumpdata)) if (!ParseLanguageCSV(lump, lumpdata.GetString(), lumpdata.GetSize()))
LoadLanguage (lump, lumpdata); LoadLanguage (lump, lumpdata.GetString(), lumpdata.GetSize());
} }
UpdateLanguage(language); UpdateLanguage(language);
allMacros.Clear(); allMacros.Clear();
@ -77,9 +77,9 @@ void FStringTable::LoadStrings (const char *language)
//========================================================================== //==========================================================================
TArray<TArray<FString>> FStringTable::parseCSV(const TArray<uint8_t> &buffer) TArray<TArray<FString>> FStringTable::parseCSV(const char* buffer, size_t size)
{ {
const size_t bufLength = buffer.Size(); const size_t bufLength = size;
TArray<TArray<FString>> data; TArray<TArray<FString>> data;
TArray<FString> row; TArray<FString> row;
TArray<char> cell; TArray<char> cell;
@ -158,8 +158,8 @@ TArray<TArray<FString>> FStringTable::parseCSV(const TArray<uint8_t> &buffer)
bool FStringTable::readMacros(int lumpnum) bool FStringTable::readMacros(int lumpnum)
{ {
auto lumpdata = fileSystem.GetFileData(lumpnum); auto lumpdata = fileSystem.ReadFile(lumpnum);
auto data = parseCSV(lumpdata); auto data = parseCSV(lumpdata.GetString(), lumpdata.GetSize());
for (unsigned i = 1; i < data.Size(); i++) for (unsigned i = 1; i < data.Size(); i++)
{ {
@ -186,11 +186,11 @@ bool FStringTable::readMacros(int lumpnum)
// //
//========================================================================== //==========================================================================
bool FStringTable::ParseLanguageCSV(int lumpnum, const TArray<uint8_t> &buffer) bool FStringTable::ParseLanguageCSV(int lumpnum, const char* buffer, size_t size)
{ {
if (buffer.Size() < 11) return false; if (size < 11) return false;
if (strnicmp((const char*)buffer.Data(), "default,", 8) && strnicmp((const char*)buffer.Data(), "identifier,", 11 )) return false; if (strnicmp(buffer, "default,", 8) && strnicmp(buffer, "identifier,", 11 )) return false;
auto data = parseCSV(buffer); auto data = parseCSV(buffer, size);
int labelcol = -1; int labelcol = -1;
int filtercol = -1; int filtercol = -1;
@ -282,14 +282,14 @@ bool FStringTable::ParseLanguageCSV(int lumpnum, const TArray<uint8_t> &buffer)
// //
//========================================================================== //==========================================================================
void FStringTable::LoadLanguage (int lumpnum, const TArray<uint8_t> &buffer) void FStringTable::LoadLanguage (int lumpnum, const char* buffer, size_t size)
{ {
bool errordone = false; bool errordone = false;
TArray<uint32_t> activeMaps; TArray<uint32_t> activeMaps;
FScanner sc; FScanner sc;
bool hasDefaultEntry = false; bool hasDefaultEntry = false;
sc.OpenMem("LANGUAGE", buffer); sc.OpenMem("LANGUAGE", buffer, (int)size);
sc.SetCMode (true); sc.SetCMode (true);
while (sc.GetString ()) while (sc.GetString ())
{ {

View file

@ -44,6 +44,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <vector>
#include "basics.h" #include "basics.h"
#include "zstring.h" #include "zstring.h"
#include "tarray.h" #include "tarray.h"
@ -111,11 +112,10 @@ private:
LangMap allStrings; LangMap allStrings;
TArray<std::pair<uint32_t, StringMap*>> currentLanguageSet; TArray<std::pair<uint32_t, StringMap*>> currentLanguageSet;
void LoadLanguage (int lumpnum, const TArray<uint8_t> &buffer); void LoadLanguage (int lumpnum, const char* buffer, size_t size);
TArray<TArray<FString>> parseCSV(const TArray<uint8_t> &buffer); TArray<TArray<FString>> parseCSV(const char* buffer, size_t size);
bool ParseLanguageCSV(int lumpnum, const TArray<uint8_t> &buffer); bool ParseLanguageCSV(int lumpnum, const char* buffer, size_t size);
bool LoadLanguageFromSpreadsheet(int lumpnum, const TArray<uint8_t> &buffer);
bool readMacros(int lumpnum); bool readMacros(int lumpnum);
void DeleteString(int langid, FName label); void DeleteString(int langid, FName label);
void DeleteForLabel(int lumpnum, FName label); void DeleteForLabel(int lumpnum, FName label);

View file

@ -1294,32 +1294,6 @@ unsigned FileSystem::GetFilesInFolder(const char *inpath, TArray<FolderEntry> &r
return result.Size(); return result.Size();
} }
//==========================================================================
//
// GetFileData
//
// Loads the lump into a TArray and returns it.
//
//==========================================================================
TArray<uint8_t> FileSystem::GetFileData(int lump, int pad)
{
if ((size_t)lump >= FileInfo.Size())
return TArray<uint8_t>();
auto lumpr = OpenFileReader(lump);
auto size = lumpr.GetLength();
TArray<uint8_t> data(size + pad, true);
auto numread = lumpr.Read(data.Data(), size);
if (numread != size)
{
throw FileSystemException("GetFileData: only read %ld of %ld on lump %i\n",
numread, size, lump);
}
if (pad > 0) memset(&data[size], 0, pad);
return data;
}
//========================================================================== //==========================================================================
// //
// W_ReadFile // W_ReadFile
@ -1352,7 +1326,7 @@ void FileSystem::ReadFile (int lump, void *dest)
FileData FileSystem::ReadFile (int lump) FileData FileSystem::ReadFile (int lump)
{ {
return FileData(FString(ELumpNum(lump))); return FileData(FString(*this, ELumpNum(lump)));
} }
//========================================================================== //==========================================================================
@ -1578,7 +1552,7 @@ FileData::~FileData ()
{ {
} }
FString::FString (ELumpNum lumpnum) FString::FString (FileSystem& fileSystem, ELumpNum lumpnum)
{ {
auto lumpr = fileSystem.OpenFileReader ((int)lumpnum); auto lumpr = fileSystem.OpenFileReader ((int)lumpnum);
auto size = lumpr.GetLength (); auto size = lumpr.GetLength ();

View file

@ -125,16 +125,10 @@ public:
inline int GetNumForFullName (const FString &name) { return GetNumForFullName(name.GetChars()); } inline int GetNumForFullName (const FString &name) { return GetNumForFullName(name.GetChars()); }
void ReadFile (int lump, void *dest); void ReadFile (int lump, void *dest);
TArray<uint8_t> GetFileData(int lump, int pad = 0); // reads lump into a writable buffer and optionally adds some padding at the end. (FileData isn't writable!) // These should only be used if the file data really needs padding.
FileData ReadFile (int lump); FileData ReadFile (int lump);
FileData ReadFile (const char *name) { return ReadFile (GetNumForName (name)); } FileData ReadFile (const char *name) { return ReadFile (GetNumForName (name)); }
FileData ReadFileFullName(const char* name) { return ReadFile(GetNumForFullName(name)); }
inline TArray<uint8_t> LoadFile(const char* name, int padding = 0)
{
auto lump = FindFile(name);
if (lump < 0) return TArray<uint8_t>();
return GetFileData(lump, padding);
}
FileReader OpenFileReader(int lump); // opens a reader that redirects to the containing file's one. FileReader OpenFileReader(int lump); // opens a reader that redirects to the containing file's one.
FileReader ReopenFileReader(int lump, bool alwayscache = false); // opens an independent reader. FileReader ReopenFileReader(int lump, bool alwayscache = false); // opens an independent reader.

View file

@ -140,15 +140,15 @@ int FQOITexture::CopyPixels(FBitmap *bmp, int conversion)
constexpr auto QOI_COLOR_HASH = [](PalEntry C) { return (C.r * 3 + C.g * 5 + C.b * 7 + C.a * 11); }; constexpr auto QOI_COLOR_HASH = [](PalEntry C) { return (C.r * 3 + C.g * 5 + C.b * 7 + C.a * 11); };
auto lump = fileSystem.OpenFileReader(SourceLump); auto lump = fileSystem.ReadFile(SourceLump);
auto bytes = lump.Read(); if (lump.GetSize() < 22) return 0; // error
if (bytes.size() < 22) return 0; // error
PalEntry index[64] = {}; PalEntry index[64] = {};
PalEntry pe = 0xff000000; PalEntry pe = 0xff000000;
size_t p = 14, run = 0; size_t p = 14, run = 0;
size_t chunks_len = bytes.size() - 8; size_t chunks_len = lump.GetSize() - 8;
auto bytes = (const uint8_t*)lump.GetMem();
for (int h = 0; h < Height; h++) for (int h = 0; h < Height; h++)
{ {

View file

@ -117,6 +117,8 @@ enum ELumpNum
{ {
}; };
class FileSystem;
class FString class FString
{ {
public: public:
@ -146,7 +148,7 @@ public:
FString (char head, const FString &tail); FString (char head, const FString &tail);
// Other constructors // Other constructors
FString (ELumpNum); // Create from a lump FString (FileSystem&, ELumpNum); // Create from a lump
~FString (); ~FString ();

View file

@ -314,8 +314,8 @@ FIWadManager::FIWadManager(const char *firstfn, const char *optfn)
int num = check.CheckNumForName("IWADINFO"); int num = check.CheckNumForName("IWADINFO");
if (num >= 0) if (num >= 0)
{ {
auto data = check.GetFileData(num); auto data = check.ReadFile(num);
ParseIWadInfo("IWADINFO", (const char*)data.Data(), data.Size()); ParseIWadInfo("IWADINFO", data.GetString(), (int)data.GetSize());
} }
} }
} }
@ -399,8 +399,8 @@ int FIWadManager::CheckIWADInfo(const char* fn)
{ {
FIWADInfo result; FIWADInfo result;
auto data = check.GetFileData(num); auto data = check.ReadFile(num);
ParseIWadInfo(fn, (const char*)data.Data(), data.Size(), &result); ParseIWadInfo(fn, data.GetString(), (int)data.GetSize(), &result);
for (unsigned i = 0, count = mIWadInfos.Size(); i < count; ++i) for (unsigned i = 0, count = mIWadInfos.Size(); i < count; ++i)
{ {

View file

@ -1934,9 +1934,9 @@ static FString CheckGameInfo(TArray<FString> & pwads)
if (num >= 0) if (num >= 0)
{ {
// Found one! // Found one!
auto data = check.GetFileData(num); auto data = check.ReadFile(num);
auto wadname = check.GetResourceFileName(check.GetFileContainer(num)); auto wadname = check.GetResourceFileName(check.GetFileContainer(num));
return ParseGameInfo(pwads, wadname, (const char*)data.Data(), data.Size()); return ParseGameInfo(pwads, wadname, data.GetString(), (int)data.GetSize());
} }
} }
return ""; return "";

View file

@ -78,7 +78,7 @@ bool FParseContext::FindSym (char *sym, FParseSymbol **val)
// //
// //
//========================================================================== //==========================================================================
int FParseContext::GetToken (char *&sourcep, FParseToken *yylval) int FParseContext::GetToken (const char *&sourcep, FParseToken *yylval)
{ {
char token[80]; char token[80];
int toksize; int toksize;
@ -103,7 +103,7 @@ loop:
c = *sourcep++; c = *sourcep++;
if (c == 'x' || c == 'X') if (c == 'x' || c == 'X')
{ {
yylval->val = (int)strtoll(sourcep, &sourcep, 16); yylval->val = (int)strtoll(sourcep, (char**)&sourcep, 16);
return TokenTrans[NUM]; return TokenTrans[NUM];
} }
else else
@ -118,7 +118,7 @@ loop:
if (*endp == '.') if (*endp == '.')
{ {
// It's a float // It's a float
yylval->fval = strtod(sourcep, &sourcep); yylval->fval = strtod(sourcep, (char**)& sourcep);
return TokenTrans[FLOATVAL]; return TokenTrans[FLOATVAL];
} }
else else
@ -323,12 +323,12 @@ void FParseContext::ParseLump(const char *lumpname)
} }
// Read the lump into a buffer and add a 0-terminator // Read the lump into a buffer and add a 0-terminator
auto lumpdata = fileSystem.GetFileData(lumpno, 1); auto lumpdata = fileSystem.ReadFile(lumpno);
SourceLine = 0; SourceLine = 0;
SourceFile = lumpname; SourceFile = lumpname;
char *sourcep = (char*)lumpdata.Data(); const char *sourcep = lumpdata.GetString();
while ( (tokentype = GetToken(sourcep, &token)) ) while ( (tokentype = GetToken(sourcep, &token)) )
{ {
// It is much easier to handle include statements outside the main parser. // It is much easier to handle include statements outside the main parser.

View file

@ -146,7 +146,7 @@ struct FParseContext
void AddSym (char *sym, int val); void AddSym (char *sym, int val);
bool FindSym (char *sym, FParseSymbol **val); bool FindSym (char *sym, FParseSymbol **val);
virtual bool FindToken (char *tok, int *type) = 0; virtual bool FindToken (char *tok, int *type) = 0;
int GetToken (char *&sourcep, FParseToken *yylval); int GetToken (const char *&sourcep, FParseToken *yylval);
int PrintError (const char *s); int PrintError (const char *s);
void ParseLump(const char *lumpname); void ParseLump(const char *lumpname);
}; };

View file

@ -410,7 +410,6 @@ void DFsScript::Preprocess(FLevelLocals *Level)
void DFsScript::ParseInclude(FLevelLocals *Level, char *lumpname) void DFsScript::ParseInclude(FLevelLocals *Level, char *lumpname)
{ {
int lumpnum; int lumpnum;
char *lump;
if((lumpnum = fileSystem.CheckNumForName(lumpname)) == -1) if((lumpnum = fileSystem.CheckNumForName(lumpname)) == -1)
{ {
@ -419,21 +418,18 @@ void DFsScript::ParseInclude(FLevelLocals *Level, char *lumpname)
} }
int lumplen=fileSystem.FileLength(lumpnum); int lumplen=fileSystem.FileLength(lumpnum);
lump=new char[lumplen+10]; TArray<char> lump(lumplen + 10);
fileSystem.ReadFile(lumpnum,lump); fileSystem.ReadFile(lumpnum,lump.Data());
lump[lumplen]=0; lump[lumplen]=0;
// preprocess the include // preprocess the include
// we assume that it does not include sections or labels or // we assume that it does not include sections or labels or
// other nasty things // other nasty things
ProcessFindChar(lump, 0); ProcessFindChar(lump.Data(), 0);
// now parse the lump // now parse the lump
FParser parse(Level, this); FParser parse(Level, this);
parse.Run(lump, lump, lump+lumplen); parse.Run(lump.Data(), lump.Data(), lump.Data() + lumplen);
// free the lump
delete[] lump;
} }

View file

@ -237,7 +237,8 @@ void S_Init()
TArray<uint8_t> curve; TArray<uint8_t> curve;
if (curvelump >= 0) if (curvelump >= 0)
{ {
curve = fileSystem.GetFileData(curvelump); curve.Resize(fileSystem.FileLength(curvelump));
fileSystem.ReadFile(curvelump, curve.Data());
} }
soundEngine->Init(curve); soundEngine->Init(curve);
} }