Skip to content

Commit

Permalink
Refactor|libdoomsday|FS1: Removed F_FindFileForLumpNum()
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jun 18, 2014
1 parent 3af44a9 commit 55cd8e7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 76 deletions.
59 changes: 26 additions & 33 deletions doomsday/client/src/audio/s_cache.cpp
Expand Up @@ -580,24 +580,21 @@ static sfxsample_t *cacheSample(int id, sfxinfo_t const *info)
return 0;
}

size_t lumpLength = App_FileSystem().lump(info->lumpNum).size();
if(lumpLength <= 8) return 0;

int lumpIdx;
File1 *file = F_FindFileForLumpNum(info->lumpNum, &lumpIdx);
DENG2_ASSERT(file != 0);
File1 &lump = App_FileSystem().lump(info->lumpNum);
if(lump.size() <= 8) return 0;

char hdr[12];
F_ReadLumpSection(file, lumpIdx, (uint8_t *)hdr, 0, 12);
F_ReadLumpSection(&lump.container(), lump.info().lumpIdx,
(uint8_t *)hdr, 0, 12);

// Is this perhaps a WAV sound?
if(WAV_CheckFormat(hdr))
{
// Load as WAV, then.
uint8_t const *sp = F_CacheLump(file, lumpIdx);
uint8_t const *sp = F_CacheLump(&lump.container(), lump.info().lumpIdx);

data = WAV_MemoryLoad((byte const *) sp, lumpLength, &bytesPer, &rate, &numSamples);
F_UnlockLump(file, lumpIdx);
data = WAV_MemoryLoad((byte const *) sp, lump.size(), &bytesPer, &rate, &numSamples);
F_UnlockLump(&lump.container(), lump.info().lumpIdx);

if(!data)
{
Expand All @@ -623,35 +620,31 @@ static sfxsample_t *cacheSample(int id, sfxinfo_t const *info)
size_t lumpLength = 0;
if(info->lumpNum >= 0)
{
lumpLength = App_FileSystem().lump(info->lumpNum).size();
}

if(lumpLength > 8)
{
int lumpIdx;
File1 *file = F_FindFileForLumpNum(info->lumpNum, &lumpIdx);
DENG2_ASSERT(file != 0);
File1 &lump = App_FileSystem().lump(info->lumpNum);

uint8_t hdr[8];
F_ReadLumpSection(file, lumpIdx, hdr, 0, 8);
int head = SHORT(*(short const *) (hdr));
rate = SHORT(*(short const *) (hdr + 2));
numSamples = de::max(0, LONG(*(int const *) (hdr + 4)));
if(lump.size() > 8)
{
uint8_t hdr[8];
F_ReadLumpSection(&lump.container(), lump.info().lumpIdx, hdr, 0, 8);
int head = SHORT(*(short const *) (hdr));
rate = SHORT(*(short const *) (hdr + 2));
numSamples = de::max(0, LONG(*(int const *) (hdr + 4)));

bytesPer = 1; // 8-bit.
bytesPer = 1; // 8-bit.

if(head == 3 && numSamples > 0 && (unsigned) numSamples <= lumpLength - 8)
{
// The sample data can be used as-is - load directly from the lump cache.
uint8_t const *data = F_CacheLump(file, lumpIdx) + 8; // Skip the header.
if(head == 3 && numSamples > 0 && (unsigned) numSamples <= lumpLength - 8)
{
// The sample data can be used as-is - load directly from the lump cache.
uint8_t const *data = F_CacheLump(&lump.container(), lump.info().lumpIdx) + 8; // Skip the header.

// Insert a copy of this into the cache.
SfxCache *node = Sfx_CacheInsert(id, data, bytesPer * numSamples, numSamples,
bytesPer, rate, info->group);
// Insert a copy of this into the cache.
SfxCache *node = Sfx_CacheInsert(id, data, bytesPer * numSamples, numSamples,
bytesPer, rate, info->group);

F_UnlockLump(file, lumpIdx);
F_UnlockLump(&lump.container(), lump.info().lumpIdx);

return &node->sample;
return &node->sample;
}
}
}

Expand Down
29 changes: 16 additions & 13 deletions doomsday/client/src/audio/s_mus.cpp
Expand Up @@ -208,15 +208,19 @@ void Mus_Stop(void)
*/
dd_bool Mus_IsMUSLump(lumpnum_t lumpNum)
{
char buf[4];
int lumpIdx;
File1 *file = F_FindFileForLumpNum(lumpNum, &lumpIdx);
if(!file) return false;
try
{
File1 const &lump = App_FileSystem().lump(lumpNum);

F_ReadLumpSection(file, lumpIdx, (uint8_t *)buf, 0, 4);
char buf[4];
F_ReadLumpSection(&lump.container(), lump.info().lumpIdx, (uint8_t *)buf, 0, 4);

// ASCII "MUS" and CTRL-Z (hex 4d 55 53 1a)
return !strncmp(buf, "MUS\x01a", 4);
// ASCII "MUS" and CTRL-Z (hex 4d 55 53 1a)
return !strncmp(buf, "MUS\x01a", 4);
}
catch(LumpIndex::NotFoundError const&)
{} // Ignore error.
return false;
}

/**
Expand Down Expand Up @@ -309,13 +313,12 @@ int Mus_StartLump(lumpnum_t lumpNum, dd_bool looped, dd_bool canPlayMUS)
// any player which relies on the it for format recognition works as
// expected.

size_t const lumpLength = App_FileSystem().lump(lumpNum).size();
uint8_t *buf = (uint8_t *) M_Malloc(lumpLength);
int lumpIdx;
File1 *file = F_FindFileForLumpNum(lumpNum, &lumpIdx);
File1 &lump = App_FileSystem().lump(lumpNum);
uint8_t *buf = (uint8_t *) M_Malloc(lump.size());

F_ReadLumpSection(&lump.container(), lump.info().lumpIdx, buf, 0, lump.size());
M_Mus2Midi((void *)buf, lump.size(), Str_Text(srcFile));

F_ReadLumpSection(file, lumpIdx, buf, 0, lumpLength);
M_Mus2Midi((void *)buf, lumpLength, Str_Text(srcFile));
M_Free(buf);

return AudioDriver_Music_PlayNativeFile(Str_Text(srcFile), looped);
Expand Down
14 changes: 6 additions & 8 deletions doomsday/client/src/ui/finaleinterpreter.cpp
Expand Up @@ -2101,18 +2101,16 @@ DEFFC(TextFromLump)
lumpNum = F_LumpNumForName(OP_CSTRING(3));
if(lumpNum >= 0)
{
int lumpIdx;
size_t lumpSize = App_FileSystem().lump(lumpNum).size();
de::File1 *file = F_FindFileForLumpNum(lumpNum, &lumpIdx);
uint8_t const *lumpPtr = F_CacheLump(file, lumpIdx);
de::File1 &lump = App_FileSystem().lump(lumpNum);
uint8_t const *data = F_CacheLump(&lump.container(), lump.info().lumpIdx);

size_t bufSize = 2 * lumpSize + 1;
size_t bufSize = 2 * lump.size() + 1;
char *str = (char *) M_Calloc(bufSize);

char *out = str;
for(size_t i = 0; i < lumpSize; ++i)
for(size_t i = 0; i < lump.size(); ++i)
{
char ch = (char)(lumpPtr[i]);
char ch = (char)(data[i]);
if(ch == '\r') continue;
if(ch == '\n')
{
Expand All @@ -2124,7 +2122,7 @@ DEFFC(TextFromLump)
*out++ = ch;
}
}
F_UnlockLump(file, lumpIdx);
F_UnlockLump(&lump.container(), lump.info().lumpIdx);

FIData_TextCopy(obj, str);
free(str);
Expand Down
2 changes: 0 additions & 2 deletions doomsday/libdoomsday/include/doomsday/filesys/fs_main.h
Expand Up @@ -591,8 +591,6 @@ LIBDOOMSDAY_PUBLIC de::FileHandle *F_Open(char const *nativePath, char const *mo

LIBDOOMSDAY_PUBLIC lumpnum_t F_LumpNumForName(char const *name);

LIBDOOMSDAY_PUBLIC de::File1 *F_FindFileForLumpNum(lumpnum_t lumpNum, int *lumpIdx);

LIBDOOMSDAY_PUBLIC void F_Delete(de::FileHandle *hndl);

LIBDOOMSDAY_PUBLIC size_t F_ReadLumpSection(de::File1 *file, int lumpIdx, uint8_t *buffer, size_t startOffset, size_t length);
Expand Down
16 changes: 9 additions & 7 deletions doomsday/libdoomsday/src/defs/dedfile.cpp
Expand Up @@ -62,18 +62,20 @@ void Def_ReadProcessDED(ded_t *defs, char const* path)

int DED_ReadLump(ded_t *ded, lumpnum_t lumpNum)
{
int lumpIdx;
if(File1 *file = F_FindFileForLumpNum(lumpNum, &lumpIdx))
try
{
if(App_FileSystem().lump(lumpNum).size() != 0)
File1 const &lump = App_FileSystem().lump(lumpNum);
if(lump.size() > 0)
{
uint8_t const *lumpPtr = F_CacheLump(file, lumpIdx);
String sourcePath = file->composePath();
DED_ReadData(ded, (char const *)lumpPtr, sourcePath.toUtf8().constData());
F_UnlockLump(file, lumpIdx);
uint8_t const *data = F_CacheLump(&lump.container(), lump.info().lumpIdx);
String sourcePath = lump.container().composePath();
DED_ReadData(ded, (char const *)data, sourcePath.toUtf8().constData());
F_UnlockLump(&lump.container(), lump.info().lumpIdx);
}
return true;
}
catch(LumpIndex::NotFoundError const&)
{} // Ignore error.
DED_SetError("Bad lump number.");
return false;
}
Expand Down
13 changes: 0 additions & 13 deletions doomsday/libdoomsday/src/filesys/fs_main.cpp
Expand Up @@ -1368,16 +1368,3 @@ void F_UnlockLump(File1 *file, int lumpIdx)
}
file->unlock();
}

File1 *F_FindFileForLumpNum(lumpnum_t lumpNum, int *lumpIdx)
{
try
{
File1 const &lump = App_FileSystem().lump(lumpNum);
if(lumpIdx) *lumpIdx = lump.info().lumpIdx;
return &lump.container();
}
catch(LumpIndex::NotFoundError const&)
{} // Ignore error.
return 0;
}
1 change: 1 addition & 0 deletions doomsday/libdoomsday/src/filesys/zip.cpp
Expand Up @@ -356,6 +356,7 @@ DENG2_PIMPL(Zip)

Zip::Zip(FileHandle &hndl, String path, FileInfo const &info, File1 *container)
: File1(hndl, path, info, container)
, LumpIndex(true/*paths are unique*/)
, d(new Instance(this))
{
// Scan the end of the file for the central directory end record.
Expand Down

0 comments on commit 55cd8e7

Please sign in to comment.