Skip to content

Commit

Permalink
Common: separate scanning for old-style audio in an explicit function
Browse files Browse the repository at this point in the history
This action requires AssetManager and scanning for environment (game packages, files in dirs), which may not be always possible or wanted when updating a loaded game data.
  • Loading branch information
ivan-mogilko committed Nov 10, 2023
1 parent e9fe1ae commit a753e25
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
5 changes: 2 additions & 3 deletions Common/ac/gamesetupstruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ struct GameSetupStruct : public GameSetupStructBase
std::vector<ScriptAudioClip> audioClips;
std::vector<AudioClipType> audioClipTypes;
// A clip to play when player gains score in game
// TODO: find out why OPT_SCORESOUND option cannot be used to store this in >=3.2 games
// NOTE: this stores an internal audio clip index, which may or not correspond
// to the OPT_SCORESOUND, and also depends on whether the game was upgraded from <3.2 data.
int scoreClipID;
// number of accessible game audio channels (the ones under direct user control)
int numGameChannels = 0;
Expand Down Expand Up @@ -122,8 +123,6 @@ struct GameSetupStruct : public GameSetupStructBase
// being read between them;
// b) use a helper struct to pass some arguments
//
// I also had to move BuildAudioClipArray from the engine and make it
// GameSetupStruct member.

//--------------------------------------------------------------------
// Do not call these directly
Expand Down
46 changes: 27 additions & 19 deletions Common/game/main_game_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ void BuildAudioClipArray(const std::vector<String> &assets, std::vector<ScriptAu
int temp_number;
char temp_extension[10];

// FIXME: use audio type constants instead of obscure numeric literals
for (const String &asset : assets)
{
if (sscanf(asset.GetCStr(), "%5s%d.%3s", temp_name, &temp_number, temp_extension) != 3)
Expand Down Expand Up @@ -543,21 +544,10 @@ void UpgradeAudio(GameSetupStruct &game, LoadedGameEntities &ents, GameDataVersi
if (data_ver >= kGameVersion_320)
return;

// An explanation of building audio clips array for pre-3.2 games.
//
// When AGS version 3.2 was released, it contained new audio system.
// In the nutshell, prior to 3.2 audio files had to be manually put
// to game project directory and their IDs were taken out of filenames.
// Since 3.2 this information is stored inside the game data.
// To make the modern engine compatible with pre-3.2 games, we have
// to scan game data packages for audio files, and enumerate them
// ourselves, then add this information to game struct.

// Create soundClips and audioClipTypes structures.
// Create new-style audioClipTypes array.
std::vector<AudioClipType> audiocliptypes;
std::vector<ScriptAudioClip> audioclips;

// TODO: find out what is 4 (maybe music, sound, ambient sound, voice?)
// FIXME: use audio type constants instead of obscure numeric literals
// (maybe music, sound, ambient sound, voice???)
audiocliptypes.resize(4);
for (int i = 0; i < 4; i++)
{
Expand All @@ -567,22 +557,40 @@ void UpgradeAudio(GameSetupStruct &game, LoadedGameEntities &ents, GameDataVersi
}
audiocliptypes[3].reservedChannels = 0;

// Assign new types to the game
game.audioClipTypes = audiocliptypes;
}

void ScanOldStyleAudio(AssetManager *asset_mgr, GameSetupStruct &game, std::vector<ViewStruct> &views, GameDataVersion data_ver)
{
// An explanation of building audio clips array for pre-3.2 games.
//
// When AGS version 3.2 was released, it contained new audio system.
// In the nutshell, prior to 3.2 audio files had to be manually put
// to game project directory and their IDs were taken out of filenames.
// Since 3.2 this information is stored inside the game data.
// To make the modern engine compatible with pre-3.2 games, we have
// to scan game data packages for audio files, and enumerate them
// ourselves, then add this information to game struct.
if (data_ver >= kGameVersion_320)
return;

// Read audio clip names from registered libraries
std::vector<String> assets;
std::vector<String> filtered_assets;
AssetMgr->FindAssets(assets, "*.*", "audio");
asset_mgr->FindAssets(assets, "*.*", "audio");
for (const String &filename : assets)
{
if (filename.CompareLeftNoCase("music", 5) == 0 || filename.CompareLeftNoCase("sound", 5) == 0)
filtered_assets.push_back(filename);
}
BuildAudioClipArray(filtered_assets, audioclips);

// Copy gathered data over to game
game.audioClipTypes = audiocliptypes;
// Build new-style audio clips array and assign to the game
std::vector<ScriptAudioClip> audioclips;
BuildAudioClipArray(filtered_assets, audioclips);
game.audioClips = audioclips;

RemapLegacySoundNums(game, ents.Views, data_ver);
RemapLegacySoundNums(game, views, data_ver);
}

// Convert character data to the current version
Expand Down
3 changes: 3 additions & 0 deletions Common/game/main_game_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ void PreReadGameData(GameSetupStruct &game, Stream *in, GameDataVe
HGameFileError UpdateGameData(LoadedGameEntities &ents, GameDataVersion data_ver);
// Ensures that the game saves directory path is valid
void FixupSaveDirectory(GameSetupStruct &game);
// Scans the Asset libraries for the old-style music and sound files and generate
// new-style audio clip array for the game
void ScanOldStyleAudio(AssetManager *asset_mgr, GameSetupStruct &game, std::vector<ViewStruct> &views, GameDataVersion data_ver);
// Maps legacy sound numbers to real audio clips
void RemapLegacySoundNums(GameSetupStruct &game, std::vector<ViewStruct> &views, GameDataVersion data_ver);

Expand Down
3 changes: 3 additions & 0 deletions Engine/main/game_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ HError load_game_file()
err = (HError)UpdateGameData(ents, src.DataVersion);
if (!err)
return err;
// Search the asset locations for old-style audio files and recreate clips array;
// we do this separately after UpdateGameData, because this involves scanning enviroment.
ScanOldStyleAudio(AssetMgr.get(), ents.Game, ents.Views, src.DataVersion);
err = LoadGameScripts(ents);
if (!err)
return err;
Expand Down

0 comments on commit a753e25

Please sign in to comment.