Skip to content

Commit

Permalink
id_ca: Use filenames from EPISODE.CKx
Browse files Browse the repository at this point in the history
Instead of hardcoding the filenames for the various files needed by
ID_CA (i.e., audio, graphics, and level files), load them from variables
in EPISODE.CKx.

This gets rid of some AdjustExtension() calls, as well as making it
easier for mods to use the different filenames produced by different
tools (e.g. EDITMAPS, MAPTEMP, etc).

Note that this doesn't do anything fancy with the episode detection or
anything yet, nor does it cover other files, outside ID_CA.
  • Loading branch information
sulix committed Sep 26, 2024
1 parent 0f177e0 commit 2a1f60b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 21 deletions.
18 changes: 18 additions & 0 deletions data/keen4/EPISODE.CK4
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
#

# Episode variables

# Graphics files
%string ca_gfxInfoFile "GFXINFOE.CK4"
%string ca_graphDict "EGADICT.CK4"
%string ca_graphHead "EGAHEAD.CK4"
%string ca_graphFile "EGAGRAPH.CK4"

# Sound Files
%string ca_audioInfoFile "AUDINFOE.CK4"
%string ca_audioHead "AUDIOHHD.CK4"
%string ca_audioDict "AUDIODCT.CK4"
%string ca_audioFile "AUDIO.CK4"

# Level Files
%string ca_mapFile "GAMEMAPS.CK4"
%string ca_mapHead "MAPHEAD.CK4"
%string ca_tileInfo "TILEINFO.CK4"

%int CK_activeLimit 4
%int CK_highScoreLevel 19
%int CK_highScoreTopMargin 0x33
Expand Down
18 changes: 18 additions & 0 deletions data/keen5/EPISODE.CK5
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
#

# Episode variables

# Graphics files
%string ca_gfxInfoFile "GFXINFOE.CK5"
%string ca_graphDict "EGADICT.CK5"
%string ca_graphHead "EGAHEAD.CK5"
%string ca_graphFile "EGAGRAPH.CK5"

# Sound Files
%string ca_audioInfoFile "AUDINFOE.CK5"
%string ca_audioHead "AUDIOHHD.CK5"
%string ca_audioDict "AUDIODCT.CK5"
%string ca_audioFile "AUDIO.CK5"

# Level Files
%string ca_mapFile "GAMEMAPS.CK5"
%string ca_mapHead "MAPHEAD.CK5"
%string ca_tileInfo "TILEINFO.CK5"

%int CK_activeLimit 6
%int CK_highScoreLevel 15
%int CK_highScoreTopMargin 0x23
Expand Down
18 changes: 18 additions & 0 deletions data/keen6e14/EPISODE.CK6
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
#

# Episode variables

# Graphics files
%string ca_gfxInfoFile "GFXINFOE.CK6"
%string ca_graphDict "EGADICT.CK6"
%string ca_graphHead "EGAHEAD.CK6"
%string ca_graphFile "EGAGRAPH.CK6"

# Sound Files
%string ca_audioInfoFile "AUDINFOE.CK6"
%string ca_audioHead "AUDIOHHD.CK6"
%string ca_audioDict "AUDIODCT.CK6"
%string ca_audioFile "AUDIO.CK6"

# Level Files
%string ca_mapFile "GAMEMAPS.CK6"
%string ca_mapHead "MAPHEAD.CK6"
%string ca_tileInfo "TILEINFO.CK6"

%int CK_activeLimit 4
%int CK_highScoreLevel 18
%int CK_highScoreTopMargin 0x33
Expand Down
18 changes: 18 additions & 0 deletions data/keen6e15/EPISODE.CK6
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
#

# Episode variables

# Graphics files
%string ca_gfxInfoFile "GFXINFOE.CK6"
%string ca_graphDict "EGADICT.CK6"
%string ca_graphHead "EGAHEAD.CK6"
%string ca_graphFile "EGAGRAPH.CK6"

# Sound Files
%string ca_audioInfoFile "AUDINFOE.CK6"
%string ca_audioHead "AUDIOHHD.CK6"
%string ca_audioDict "AUDIODCT.CK6"
%string ca_audioFile "AUDIO.CK6"

# Level Files
%string ca_mapFile "GAMEMAPS.CK6"
%string ca_mapHead "MAPHEAD.CK6"
%string ca_tileInfo "TILEINFO.CK6"

%int CK_activeLimit 4
%int CK_highScoreLevel 18
%int CK_highScoreTopMargin 0x33
Expand Down
2 changes: 1 addition & 1 deletion src/ck_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,5 +660,5 @@ void CK_VAR_LoadVars(const char *filename)
while (CK_VAR_ParseVar(&parserstate))
numVarsParsed++;

CK_Cross_LogMessage(CK_LOG_MSG_NORMAL, "Parsed %d vars from \"%s\" over %d lines (%d actions created).\n", numVarsParsed, FS_AdjustExtension(filename), parserstate.linecount, ck_actionsUsed);
CK_Cross_LogMessage(CK_LOG_MSG_NORMAL, "Parsed %d vars from \"%s\" over %d lines (%d actions created).\n", numVarsParsed, filename, parserstate.linecount, ck_actionsUsed);
}
2 changes: 2 additions & 0 deletions src/ck_act.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ typedef int16_t chunk_id_t;
#define CK_INTELEMENT(name, i) (CK_INTARRAY(name)[i])
#define CK_STRINGARRAY(name) STRARRAY_ ## name
#define CK_STRINGELEMENT(name, i) (CK_STRINGARRAY(name)[i])
#define CK_FILENAME(name, def) STRINGS_ ## name
#else
#define CK_INT(name, default) CK_VAR_GetInt(#name, default)
#define CK_CHUNKNUM(name) CK_VAR_GetInt(#name, 0)
Expand All @@ -80,6 +81,7 @@ typedef const char *chunk_id_t;
#define CK_INTELEMENT(name, i) CK_VAR_GetIntArrayElement(#name, i)
#define CK_STRINGARRAY(name) CK_VAR_GetStringArray(#name)
#define CK_STRINGELEMENT(name, i) CK_VAR_GetStringArrayElement(#name, i)
#define CK_FILENAME(name, def) CK_VAR_GetString(#name, FS_AdjustExtension(def))
#endif


Expand Down
4 changes: 2 additions & 2 deletions src/ck_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void CK_InitGame()


CK_VAR_Startup();
CK_VAR_LoadVars("EPISODE.EXT");
CK_VAR_LoadVars(FS_AdjustExtension("EPISODE.EXT"));

// Load the core datafiles
CA_Startup();
Expand Down Expand Up @@ -615,7 +615,7 @@ int main(int argc, char *argv[])

if (!ck_currentEpisode->isPresent())
{
Quit("Couldn't find game data files!");
// Quit("Couldn't find game data files!");
}

#ifdef CK_ENABLE_PLAYLOOP_DUMPER
Expand Down
37 changes: 20 additions & 17 deletions src/id_ca.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void (*ca_finishCacheBox)(void);

bool CA_LoadFile(const char *filename, mm_ptr_t *ptr, int *memsize)
{
FS_File f = FS_OpenOmniFile(FS_AdjustExtension(filename));
FS_File f = FS_OpenOmniFile(filename);

if (!FS_IsFileValid(f))
return false;
Expand Down Expand Up @@ -578,7 +578,7 @@ void CAL_SetupGrFile()
CK_Cross_LogMessage(CK_LOG_MSG_NORMAL, "CAL_SetupGrFile: Loading graphics headers...\n");

//Load the ?GADICT
CA_LoadFile("EGADICT.EXT", (void **)(&ca_gr_huffdict), 0);
CA_LoadFile(CK_FILENAME(ca_graphDict, "EGADICT.EXT"), (void **)(&ca_gr_huffdict), 0);

#ifdef CK_CROSS_IS_BIGENDIAN
for (int i = 0; i < 256; ++i)
Expand All @@ -592,10 +592,10 @@ void CAL_SetupGrFile()
//CAL_OptimizeNodes(ca_gr_huffdict);

//Load the ?GAHEAD
CA_LoadFile("EGAHEAD.EXT", &ca_graphStarts, &ca_graphHeadSize);
CA_LoadFile(CK_FILENAME(ca_graphHead, "EGAHEAD.EXT"), &ca_graphStarts, &ca_graphHeadSize);

// Read chunk type info from GFEINFO?
FS_File gfxinfoe = FS_OpenOmniFile(FS_AdjustExtension("GFXINFOE.EXT"));
// Read chunk type info from GFXINFOE?
FS_File gfxinfoe = FS_OpenOmniFile(CK_FILENAME(ca_gfxInfoFile, "GFXINFOE.EXT"));
if (!FS_IsFileValid(gfxinfoe))
Quit("Couldn't load GFXINFOE!");
size_t gfxinfoeLen = FS_Read(&ca_gfxInfoE, sizeof(ca_gfxinfo), 1, gfxinfoe);
Expand All @@ -609,7 +609,7 @@ void CAL_SetupGrFile()
#endif

//Load the graphics --- we will keep the file open for the duration of the game.
ca_graphHandle = FS_OpenKeenFile(FS_AdjustExtension("EGAGRAPH.EXT"));
ca_graphHandle = FS_OpenKeenFile(CK_FILENAME(ca_graphFile, "EGAGRAPH.EXT"));

// Find the size of the file. Some mods do not have the last entry in the ?GAHEAD,
// so we compute it like so.
Expand Down Expand Up @@ -951,21 +951,24 @@ void CAL_SetupMapFile(void)
CK_Cross_LogMessage(CK_LOG_MSG_NORMAL, "CAL_SetupMapFile: Loading map headers...\n");

int mapHeadFileSize = 0;
CA_LoadFile("MAPHEAD.EXT", (void **)(&ca_MapHead), &mapHeadFileSize);
CA_LoadFile(CK_FILENAME(ca_mapHead, "MAPHEAD.EXT"), (void **)(&ca_MapHead), &mapHeadFileSize);
#ifdef CK_CROSS_IS_BIGENDIAN
ca_MapHead->rleTag = CK_Cross_SwapLE16(ca_MapHead->rleTag);
for (int i = 0; i < CA_NUMMAPS; ++i)
{
ca_MapHead->headerOffsets[i] = CK_Cross_SwapLE32(ca_MapHead->headerOffsets[i]);
}
#endif
ca_GameMaps = FS_OpenKeenFile(FS_AdjustExtension("GAMEMAPS.EXT"));
ca_GameMaps = FS_OpenKeenFile(CK_FILENAME(ca_mapFile, "GAMEMAPS.EXT"));
// Try reading TILEINFO.EXT first, otherwise use data from MAPHEAD.EXT
ti_tileInfo = NULL;
if (!CA_LoadFile("TILEINFO.EXT", (void **)(&ti_tileInfo), 0))
const char *tileInfoName = CK_FILENAME(ca_tileInfo, "TILEINFO.EXT");
if (tileInfoName)
CA_LoadFile(tileInfoName, (void **)(&ti_tileInfo), 0);
if (!ti_tileInfo)
{
if (ti_tileInfo) // CA_LoadFile may leave a memory leak
MM_FreePtr((void **)&ti_tileInfo);
// CA_LoadFile may leave a memory leak
MM_FreePtr((void **)&ti_tileInfo);

if (mapHeadFileSize <= sizeof(*ca_MapHead))
Quit("Can't open TILEINFO file, and MAPHEAD file lacks tileinfo data!");
Expand All @@ -984,7 +987,7 @@ void CAL_SetupAudioFile(void)
CK_Cross_LogMessage(CK_LOG_MSG_NORMAL, "CAL_SetupAudioFile: Loading audio headers...\n");

// Read audio chunk type info from AUDINFOE
FS_File audinfoe = FS_OpenOmniFile(FS_AdjustExtension("AUDINFOE.EXT"));
FS_File audinfoe = FS_OpenOmniFile(CK_FILENAME(ca_audioInfoFile, "AUDINFOE.EXT"));
size_t audinfoeLen = FS_Read(&ca_audInfoE, sizeof(ca_audinfo), 1, audinfoe);
FS_CloseFile(audinfoe);
if (audinfoeLen != 1)
Expand All @@ -997,7 +1000,7 @@ void CAL_SetupAudioFile(void)

#ifndef CA_AUDIOUNCOMPRESSED
//Load the AUDIODCT
CA_LoadFile("AUDIODCT.EXT", (void **)(&ca_audiohuffman), 0);
CA_LoadFile(CK_FILENAME(ca_audioDict, "AUDIODCT.EXT"), (void **)(&ca_audiohuffman), 0);

#ifdef CK_CROSS_IS_BIGENDIAN
for (int i = 0; i < 256; ++i)
Expand All @@ -1011,20 +1014,20 @@ void CAL_SetupAudioFile(void)
//CAL_OptimizeNodes(ca_audiohuffman);

//Load the AUDIOHHD
CA_LoadFile("AUDIOHHD.EXT", (void **)(&ca_audiostarts), 0);
CA_LoadFile(CK_FILENAME(ca_audioHead, "AUDIOHHD.EXT"), (void **)(&ca_audiostarts), 0);

//Load the sound data --- we will keep the file open for the duration of the game.
ca_audiohandle = FS_OpenKeenFile(FS_AdjustExtension("AUDIO.EXT"));
ca_audiohandle = FS_OpenKeenFile(CK_FILENAME(ca_audioFile, "AUDIO.EXT"));
if (!ca_audiohandle)
{
Quit("Can't open AUDIO.CKx!");
}
#else
//Load the AUDIOHHD
CA_LoadFile("AUDIOHED.EXT", (void **)(&ca_audiostarts), 0);
CA_LoadFile(CK_FILENAME(ca_audioHead, "AUDIOHED.EXT"), (void **)(&ca_audiostarts), 0);

//Load the sound data --- we will keep the file open for the duration of the game.
ca_audiohandle = FS_OpenKeenFile(FS_AdjustExtension("AUDIOT.EXT"));
ca_audiohandle = FS_OpenKeenFile(CK_FILENAME(ca_audioFile, "AUDIOT.EXT"));
if (!ca_audiohandle)
{
Quit("Can't open AUDIOT.CKx!");
Expand Down
1 change: 0 additions & 1 deletion src/id_ca.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void CA_LoadAllSounds(void);
// -- File IO --

size_t CA_GetFileSize(char *filename);
char *CAL_AdjustExtension(const char *filename);
bool CA_FarWrite(int handle, uint8_t *source, int length);
bool CA_ReadFile(const char *filename, void *offset);
bool CA_SafeReadFile(const char *filename, void *offset, int bufLength);
Expand Down

0 comments on commit 2a1f60b

Please sign in to comment.