- use TArray and FString in resource file management.

This commit is contained in:
Christoph Oelckers 2018-11-10 14:19:55 +01:00
parent 4d06c17a44
commit 191f2d9d76
13 changed files with 55 additions and 73 deletions

View file

@ -257,7 +257,7 @@ FIWadManager::FIWadManager(const char *fn)
if (lmp->Namespace == ns_global && !stricmp(lmp->Name, "IWADINFO")) if (lmp->Namespace == ns_global && !stricmp(lmp->Name, "IWADINFO"))
{ {
// Found one! // Found one!
ParseIWadInfo(resfile->Filename, (const char*)lmp->CacheLump(), lmp->LumpSize); ParseIWadInfo(resfile->FileName, (const char*)lmp->CacheLump(), lmp->LumpSize);
break; break;
} }
} }
@ -349,7 +349,7 @@ int FIWadManager::CheckIWADInfo(const char *fn)
try try
{ {
FIWADInfo result; FIWADInfo result;
ParseIWadInfo(resfile->Filename, (const char*)lmp->CacheLump(), lmp->LumpSize, &result); ParseIWadInfo(resfile->FileName, (const char*)lmp->CacheLump(), lmp->LumpSize, &result);
delete resfile; delete resfile;
for (unsigned i = 0, count = mIWadInfos.Size(); i < count; ++i) for (unsigned i = 0, count = mIWadInfos.Size(); i < count; ++i)

View file

@ -1905,7 +1905,7 @@ static FString CheckGameInfo(TArray<FString> & pwads)
if (lmp->Namespace == ns_global && !stricmp(lmp->Name, "GAMEINFO")) if (lmp->Namespace == ns_global && !stricmp(lmp->Name, "GAMEINFO"))
{ {
// Found one! // Found one!
FString iwad = ParseGameInfo(pwads, resfile->Filename, (const char*)lmp->CacheLump(), lmp->LumpSize); FString iwad = ParseGameInfo(pwads, resfile->FileName, (const char*)lmp->CacheLump(), lmp->LumpSize);
delete resfile; delete resfile;
return iwad; return iwad;
} }

View file

@ -230,7 +230,7 @@ bool F7ZFile::Open(bool quiet)
Archive = NULL; Archive = NULL;
if (!quiet) if (!quiet)
{ {
Printf("\n" TEXTCOLOR_RED "%s: ", Filename); Printf("\n" TEXTCOLOR_RED "%s: ", FileName.GetChars());
if (res == SZ_ERROR_UNSUPPORTED) if (res == SZ_ERROR_UNSUPPORTED)
{ {
Printf("Decoder does not support this archive\n"); Printf("Decoder does not support this archive\n");
@ -309,7 +309,7 @@ bool F7ZFile::Open(bool quiet)
if (SZ_OK != Archive->Extract(Lumps[0].Position, &temp[0])) if (SZ_OK != Archive->Extract(Lumps[0].Position, &temp[0]))
{ {
if (!quiet) Printf("\n%s: unsupported 7z/LZMA file!\n", Filename); if (!quiet) Printf("\n%s: unsupported 7z/LZMA file!\n", FileName.GetChars());
return false; return false;
} }
} }

View file

@ -102,9 +102,9 @@ FDirectory::FDirectory(const char * directory)
#ifdef _WIN32 #ifdef _WIN32
free((void *)directory); free((void *)directory);
#endif #endif
dirname.ReplaceChars('\\', '/'); FixPathSeperator(dirname);
if (dirname[dirname.Len()-1] != '/') dirname += '/'; if (dirname[dirname.Len()-1] != '/') dirname += '/';
Filename = copystring(dirname); FileName = dirname;
} }
@ -248,7 +248,7 @@ int FDirectory::AddDirectory(const char *dirpath)
bool FDirectory::Open(bool quiet) bool FDirectory::Open(bool quiet)
{ {
NumLumps = AddDirectory(Filename); NumLumps = AddDirectory(FileName);
if (!quiet) Printf(", %d lumps\n", NumLumps); if (!quiet) Printf(", %d lumps\n", NumLumps);
PostProcessArchive(&Lumps[0], sizeof(FDirectoryLump)); PostProcessArchive(&Lumps[0], sizeof(FDirectoryLump));
return true; return true;
@ -268,7 +268,7 @@ void FDirectory::AddEntry(const char *fullpath, int size)
lump_p->mFullPath = fullpath; lump_p->mFullPath = fullpath;
// [mxd] Convert name to lowercase // [mxd] Convert name to lowercase
FString name = fullpath + strlen(Filename); FString name = fullpath + strlen(FileName);
name.ToLower(); name.ToLower();
// The lump's name is only the part relative to the main directory // The lump's name is only the part relative to the main directory

View file

@ -85,7 +85,6 @@ public:
FGrpFile::FGrpFile(const char *filename, FileReader &file) FGrpFile::FGrpFile(const char *filename, FileReader &file)
: FUncompressedFile(filename, file) : FUncompressedFile(filename, file)
{ {
Lumps = NULL;
} }
//========================================================================== //==========================================================================
@ -104,7 +103,7 @@ bool FGrpFile::Open(bool quiet)
GrpLump *fileinfo = new GrpLump[NumLumps]; GrpLump *fileinfo = new GrpLump[NumLumps];
Reader.Read (fileinfo, NumLumps * sizeof(GrpLump)); Reader.Read (fileinfo, NumLumps * sizeof(GrpLump));
Lumps = new FUncompressedLump[NumLumps]; Lumps.Resize(NumLumps);
int Position = sizeof(GrpInfo) + NumLumps * sizeof(GrpLump); int Position = sizeof(GrpInfo) + NumLumps * sizeof(GrpLump);

View file

@ -69,17 +69,17 @@ FLumpFile::FLumpFile(const char *filename, FileReader &file)
bool FLumpFile::Open(bool quiet) bool FLumpFile::Open(bool quiet)
{ {
FString name(ExtractFileBase (Filename)); FString name(ExtractFileBase (FileName));
Lumps = new FUncompressedLump[1]; // must use array allocator Lumps.Resize(1);
uppercopy(Lumps->Name, name); uppercopy(Lumps[0].Name, name);
Lumps->Name[8] = 0; Lumps[0].Name[8] = 0;
Lumps->Owner = this; Lumps[0].Owner = this;
Lumps->Position = 0; Lumps[0].Position = 0;
Lumps->LumpSize = (int)Reader.GetLength(); Lumps[0].LumpSize = (int)Reader.GetLength();
Lumps->Namespace = ns_global; Lumps[0].Namespace = ns_global;
Lumps->Flags = 0; Lumps[0].Flags = 0;
Lumps->FullName = NULL; Lumps[0].FullName = NULL;
NumLumps = 1; NumLumps = 1;
if (!quiet) if (!quiet)
{ {

View file

@ -80,7 +80,6 @@ public:
FPakFile::FPakFile(const char *filename, FileReader &file) FPakFile::FPakFile(const char *filename, FileReader &file)
: FUncompressedFile(filename, file) : FUncompressedFile(filename, file)
{ {
Lumps = NULL;
} }
//========================================================================== //==========================================================================
@ -101,7 +100,7 @@ bool FPakFile::Open(bool quiet)
Reader.Seek (header.dirofs, FileReader::SeekSet); Reader.Seek (header.dirofs, FileReader::SeekSet);
Reader.Read (fileinfo, NumLumps * sizeof(dpackfile_t)); Reader.Read (fileinfo, NumLumps * sizeof(dpackfile_t));
Lumps = new FUncompressedLump[NumLumps]; Lumps.Resize(NumLumps);
if (!quiet && !batchrun) Printf(", %d lumps\n", NumLumps); if (!quiet && !batchrun) Printf(", %d lumps\n", NumLumps);

View file

@ -165,7 +165,7 @@ bool FWadFile::Open(bool quiet)
// Check again to detect broken wads // Check again to detect broken wads
if (InfoTableOfs + NumLumps*sizeof(wadlump_t) > (unsigned)wadSize) if (InfoTableOfs + NumLumps*sizeof(wadlump_t) > (unsigned)wadSize)
{ {
I_Error("Cannot load broken WAD file %s\n", Filename); I_Error("Cannot load broken WAD file %s\n", FileName.GetChars());
} }
} }
@ -194,7 +194,7 @@ bool FWadFile::Open(bool quiet)
{ {
if (Lumps[i].LumpSize != 0) if (Lumps[i].LumpSize != 0)
{ {
Printf(PRINT_HIGH, "%s: Lump %s contains invalid positioning info and will be ignored\n", Filename, Lumps[i].Name); Printf(PRINT_HIGH, "%s: Lump %s contains invalid positioning info and will be ignored\n", FileName.GetChars(), Lumps[i].Name);
Lumps[i].Name[0] = 0; Lumps[i].Name[0] = 0;
} }
Lumps[i].LumpSize = Lumps[i].Position = 0; Lumps[i].LumpSize = Lumps[i].Position = 0;
@ -439,7 +439,7 @@ void FWadFile::SkinHack ()
"The maps in %s will not be loaded because it has a skin.\n" "The maps in %s will not be loaded because it has a skin.\n"
TEXTCOLOR_BLUE TEXTCOLOR_BLUE
"You should remove the skin from the wad to play these maps.\n", "You should remove the skin from the wad to play these maps.\n",
Filename); FileName.GetChars());
} }
} }

View file

@ -181,7 +181,7 @@ bool FZipFile::Open(bool quiet)
if (centraldir == 0) if (centraldir == 0)
{ {
if (!quiet) Printf(TEXTCOLOR_RED "\n%s: ZIP file corrupt!\n", Filename); if (!quiet) Printf(TEXTCOLOR_RED "\n%s: ZIP file corrupt!\n", FileName.GetChars());
return false; return false;
} }
@ -193,7 +193,7 @@ bool FZipFile::Open(bool quiet)
if (info.NumEntries != info.NumEntriesOnAllDisks || if (info.NumEntries != info.NumEntriesOnAllDisks ||
info.FirstDisk != 0 || info.DiskNumber != 0) info.FirstDisk != 0 || info.DiskNumber != 0)
{ {
if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Multipart Zip files are not supported.\n", Filename); if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Multipart Zip files are not supported.\n", FileName.GetChars());
return false; return false;
} }
@ -229,7 +229,7 @@ bool FZipFile::Open(bool quiet)
if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file. if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file.
{ {
free(directory); free(directory);
if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", Filename); if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", FileName.GetChars());
return false; return false;
} }
@ -300,7 +300,7 @@ bool FZipFile::Open(bool quiet)
if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file. if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file.
{ {
free(directory); free(directory);
if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", Filename); if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", FileName.GetChars());
return false; return false;
} }
@ -320,7 +320,7 @@ bool FZipFile::Open(bool quiet)
zip_fh->Method != METHOD_IMPLODE && zip_fh->Method != METHOD_IMPLODE &&
zip_fh->Method != METHOD_SHRINK) zip_fh->Method != METHOD_SHRINK)
{ {
if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' uses an unsupported compression algorithm (#%d).\n", Filename, name.GetChars(), zip_fh->Method); if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' uses an unsupported compression algorithm (#%d).\n", FileName.GetChars(), name.GetChars(), zip_fh->Method);
skipped++; skipped++;
continue; continue;
} }
@ -328,7 +328,7 @@ bool FZipFile::Open(bool quiet)
zip_fh->Flags = LittleShort(zip_fh->Flags); zip_fh->Flags = LittleShort(zip_fh->Flags);
if (zip_fh->Flags & ZF_ENCRYPTED) if (zip_fh->Flags & ZF_ENCRYPTED)
{ {
if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' is encrypted. Encryption is not supported.\n", Filename, name.GetChars()); if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' is encrypted. Encryption is not supported.\n", FileName.GetChars(), name.GetChars());
skipped++; skipped++;
continue; continue;
} }

View file

@ -156,7 +156,7 @@ static bool IsWadInFolder(const FResourceFile* const archive, const char* const
return false; return false;
} }
const FString dirName = ExtractFileBase(archive->Filename); const FString dirName = ExtractFileBase(archive->FileName);
const FString fileName = ExtractFileBase(resPath, true); const FString fileName = ExtractFileBase(resPath, true);
const FString filePath = dirName + '/' + fileName; const FString filePath = dirName + '/' + fileName;
@ -323,9 +323,8 @@ FResourceFile *FResourceFile::OpenDirectory(const char *filename, bool quiet)
//========================================================================== //==========================================================================
FResourceFile::FResourceFile(const char *filename) FResourceFile::FResourceFile(const char *filename)
: FileName(filename)
{ {
if (filename != NULL) Filename = copystring(filename);
else Filename = NULL;
} }
FResourceFile::FResourceFile(const char *filename, FileReader &r) FResourceFile::FResourceFile(const char *filename, FileReader &r)
@ -336,7 +335,6 @@ FResourceFile::FResourceFile(const char *filename, FileReader &r)
FResourceFile::~FResourceFile() FResourceFile::~FResourceFile()
{ {
if (Filename != NULL) delete [] Filename;
} }
int lumpcmp(const void * a, const void * b) int lumpcmp(const void * a, const void * b)
@ -653,12 +651,6 @@ FUncompressedFile::FUncompressedFile(const char *filename, FileReader &r)
: FResourceFile(filename, r) : FResourceFile(filename, r)
{} {}
FUncompressedFile::~FUncompressedFile()
{
if (Lumps != NULL) delete [] Lumps;
}
//========================================================================== //==========================================================================
// //
@ -667,9 +659,8 @@ FUncompressedFile::~FUncompressedFile()
//========================================================================== //==========================================================================
FExternalLump::FExternalLump(const char *_filename, int filesize) FExternalLump::FExternalLump(const char *_filename, int filesize)
: Filename(_filename)
{ {
filename = _filename? copystring(_filename) : NULL;
if (filesize == -1) if (filesize == -1)
{ {
FileReader f; FileReader f;
@ -690,11 +681,6 @@ FExternalLump::FExternalLump(const char *_filename, int filesize)
} }
FExternalLump::~FExternalLump()
{
if (filename != NULL) delete [] filename;
}
//========================================================================== //==========================================================================
// //
// Caches a lump's content and increases the reference counter // Caches a lump's content and increases the reference counter
@ -707,7 +693,7 @@ int FExternalLump::FillCache()
Cache = new char[LumpSize]; Cache = new char[LumpSize];
FileReader f; FileReader f;
if (f.OpenFile(filename)) if (f.OpenFile(Filename))
{ {
f.Read(Cache, LumpSize); f.Read(Cache, LumpSize);
} }
@ -722,19 +708,19 @@ int FExternalLump::FillCache()
bool FMemoryFile::Open(bool quiet) bool FMemoryFile::Open(bool quiet)
{ {
FString name(ExtractFileBase(Filename)); FString name(ExtractFileBase(FileName));
FString fname(ExtractFileBase(Filename, true)); FString fname(ExtractFileBase(FileName, true));
Lumps = new FUncompressedLump[1]; // must use array allocator Lumps.Resize(1);
uppercopy(Lumps->Name, name); uppercopy(Lumps[0].Name, name);
Lumps->Name[8] = 0; Lumps[0].Name[8] = 0;
Lumps->FullName = fname; Lumps[0].FullName = fname;
Lumps->Owner = this; Lumps[0].Owner = this;
Lumps->Position = 0; Lumps[0].Position = 0;
Lumps->LumpSize = (int)Reader.GetLength(); Lumps[0].LumpSize = (int)Reader.GetLength();
Lumps->Namespace = ns_global; Lumps[0].Namespace = ns_global;
Lumps->Flags = 0; Lumps[0].Flags = 0;
Lumps->FullName = NULL; Lumps[0].FullName = nullptr;
NumLumps = 1; NumLumps = 1;
return true; return true;
} }

View file

@ -82,7 +82,7 @@ class FResourceFile
{ {
public: public:
FileReader Reader; FileReader Reader;
const char *Filename; FString FileName;
protected: protected:
uint32_t NumLumps; uint32_t NumLumps;
@ -133,21 +133,19 @@ struct FUncompressedLump : public FResourceLump
class FUncompressedFile : public FResourceFile class FUncompressedFile : public FResourceFile
{ {
protected: protected:
FUncompressedLump * Lumps = nullptr; TArray<FUncompressedLump> Lumps;
FUncompressedFile(const char *filename); FUncompressedFile(const char *filename);
FUncompressedFile(const char *filename, FileReader &r); FUncompressedFile(const char *filename, FileReader &r);
virtual ~FUncompressedFile();
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; } virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
}; };
struct FExternalLump : public FResourceLump struct FExternalLump : public FResourceLump
{ {
const char *filename; // the actual file name. This is not necessarily the same as the lump name! FString Filename;
FExternalLump(const char *_filename, int filesize = -1); FExternalLump(const char *_filename, int filesize = -1);
~FExternalLump();
virtual int FillCache(); virtual int FillCache();
}; };

View file

@ -431,11 +431,11 @@ public:
} }
// Reserves amount entries at the end of the array, but does nothing // Reserves amount entries at the end of the array, but does nothing
// with them. // with them.
unsigned int Reserve (unsigned int amount) unsigned int Reserve (size_t amount)
{ {
Grow (amount); Grow ((unsigned)amount);
unsigned int place = Count; unsigned int place = Count;
Count += amount; Count += (unsigned)amount;
if (Count > 0) ConstructEmpty(place, Count - 1); if (Count > 0) ConstructEmpty(place, Count - 1);
return place; return place;
} }

View file

@ -1413,7 +1413,7 @@ const char *FWadCollection::GetWadName (int wadnum) const
return NULL; return NULL;
} }
name = Files[wadnum]->Filename; name = Files[wadnum]->FileName;
slash = strrchr (name, '/'); slash = strrchr (name, '/');
return slash != NULL ? slash+1 : name; return slash != NULL ? slash+1 : name;
} }
@ -1476,10 +1476,10 @@ const char *FWadCollection::GetWadFullName (int wadnum) const
{ {
if ((unsigned int)wadnum >= Files.Size()) if ((unsigned int)wadnum >= Files.Size())
{ {
return NULL; return nullptr;
} }
return Files[wadnum]->Filename; return Files[wadnum]->FileName;
} }