From 5ad85c6b42ee22a72c8f9857b39f251c7bf72504 Mon Sep 17 00:00:00 2001 From: Walter Agazzi Date: Sun, 26 Feb 2023 18:47:42 +0100 Subject: [PATCH] AGS: Common: replaced GameSetupStruct::messages with an array of Strings This also fixes an allocation/deallocation mismatch found in the code. From upstream b47a086cbd589cd271c5fe1b3497f9b8e4ae4abf --- engines/ags/engine/ac/game.cpp | 8 ++--- engines/ags/engine/game/savegame_v321.cpp | 2 +- engines/ags/shared/ac/game_setup_struct.cpp | 32 +++++++++---------- engines/ags/shared/ac/game_setup_struct.h | 3 +- .../ags/shared/ac/game_setup_struct_base.cpp | 6 ++-- .../ags/shared/ac/game_setup_struct_base.h | 3 +- engines/ags/shared/game/main_game_file.cpp | 4 +-- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/engines/ags/engine/ac/game.cpp b/engines/ags/engine/ac/game.cpp index f57fedb0fbd..8d5c764469b 100644 --- a/engines/ags/engine/ac/game.cpp +++ b/engines/ags/engine/ac/game.cpp @@ -1299,9 +1299,9 @@ void replace_tokens(const char *srcmes, char *destm, int maxlen) { } const char *get_global_message(int msnum) { - if (_GP(game).messages[msnum - 500] == nullptr) + if (_GP(game).messages[msnum - 500].IsEmpty()) return ""; - return get_translation(_GP(game).messages[msnum - 500]); + return get_translation(_GP(game).messages[msnum - 500].GetCStr()); } void get_message_text(int msnum, char *buffer, char giveErr) { @@ -1311,14 +1311,14 @@ void get_message_text(int msnum, char *buffer, char giveErr) { if (msnum >= 500) { - if ((msnum >= MAXGLOBALMES + 500) || (_GP(game).messages[msnum - 500] == nullptr)) { + if ((msnum >= MAXGLOBALMES + 500) || (_GP(game).messages[msnum - 500].IsEmpty())) { if (giveErr) quit("!DisplayGlobalMessage: message does not exist"); buffer[0] = 0; return; } buffer[0] = 0; - replace_tokens(get_translation(_GP(game).messages[msnum - 500]), buffer, maxlen); + replace_tokens(get_translation(_GP(game).messages[msnum - 500].GetCStr()), buffer, maxlen); return; } else if (msnum < 0 || (size_t)msnum >= _GP(thisroom).MessageCount) { if (giveErr) diff --git a/engines/ags/engine/game/savegame_v321.cpp b/engines/ags/engine/game/savegame_v321.cpp index efbed45c027..eac27316cea 100644 --- a/engines/ags/engine/game/savegame_v321.cpp +++ b/engines/ags/engine/game/savegame_v321.cpp @@ -414,7 +414,7 @@ HSaveError restore_save_data_v321(Stream *in, const PreservedParams &pp, Restore ccScript *compsc = _GP(game).compiled_script; CharacterInfo *chwas = _GP(game).chars; WordsDictionary *olddict = _GP(game).dict; - char *mesbk[MAXGLOBALMES]; + std::vector mesbk(MAXGLOBALMES); int numchwas = _GP(game).numcharacters; for (size_t i = 0; i < MAXGLOBALMES; ++i) mesbk[i] = _GP(game).messages[i]; diff --git a/engines/ags/shared/ac/game_setup_struct.cpp b/engines/ags/shared/ac/game_setup_struct.cpp index 19b5968eecd..5d2abc2c536 100644 --- a/engines/ags/shared/ac/game_setup_struct.cpp +++ b/engines/ags/shared/ac/game_setup_struct.cpp @@ -220,12 +220,12 @@ void GameSetupStruct::read_lipsync(Shared::Stream *in, GameDataVersion data_ver) } void GameSetupStruct::read_messages(Shared::Stream *in, GameDataVersion data_ver) { - for (int ee = 0; ee < MAXGLOBALMES; ee++) { - if (!load_messages[ee]) continue; - messages[ee] = new char[GLOBALMESLENGTH]; - + char mbuf[GLOBALMESLENGTH]; + for (int i = 0; i < MAXGLOBALMES; ++i) { + if (!load_messages[i]) + continue; if (data_ver < kGameVersion_261) { // Global messages are not encrypted on < 2.61 - char *nextchar = messages[ee]; + char *nextchar = mbuf; // TODO: probably this is same as fgetstring while (1) { @@ -234,8 +234,10 @@ void GameSetupStruct::read_messages(Shared::Stream *in, GameDataVersion data_ver break; nextchar++; } - } else - read_string_decrypt(in, messages[ee], GLOBALMESLENGTH); + } else { + read_string_decrypt(in, mbuf, GLOBALMESLENGTH); + } + messages[i] = mbuf; } delete[] load_messages; load_messages = nullptr; @@ -340,17 +342,15 @@ void GameSetupStruct::ReadAudioClips_Aligned(Shared::Stream *in, size_t count) { } void GameSetupStruct::ReadFromSaveGame_v321(Stream *in, char *gswas, ccScript *compsc, CharacterInfo *chwas, - WordsDictionary *olddict, char **mesbk) { - int bb; - + WordsDictionary *olddict, std::vector &mesbk) { ReadInvInfo_Aligned(in); ReadMouseCursors_Aligned(in); if (_G(loaded_game_file_version) <= kGameVersion_272) { - for (bb = 0; bb < numinvitems; bb++) - intrInv[bb]->ReadTimesRunFromSave_v321(in); - for (bb = 0; bb < numcharacters; bb++) - intrChar[bb]->ReadTimesRunFromSave_v321(in); + for (int i = 0; i < numinvitems; ++i) + intrInv[i]->ReadTimesRunFromSave_v321(in); + for (int i = 0; i < numcharacters; ++i) + intrChar[i]->ReadTimesRunFromSave_v321(in); } // restore pointer members @@ -358,8 +358,8 @@ void GameSetupStruct::ReadFromSaveGame_v321(Stream *in, char *gswas, ccScript *c compiled_script = compsc; chars = chwas; dict = olddict; - for (int vv = 0; vv < MAXGLOBALMES; vv++) messages[vv] = mesbk[vv]; - + for (size_t i = 0; i < MAXGLOBALMES; ++i) + messages[i] = mesbk[i]; in->ReadArrayOfInt32(&options[0], OPT_HIGHESTOPTION_321 + 1); options[OPT_LIPSYNCTEXT] = in->ReadByte(); diff --git a/engines/ags/shared/ac/game_setup_struct.h b/engines/ags/shared/ac/game_setup_struct.h index b451ba22391..b8df68e0730 100644 --- a/engines/ags/shared/ac/game_setup_struct.h +++ b/engines/ags/shared/ac/game_setup_struct.h @@ -22,6 +22,7 @@ #ifndef AGS_SHARED_AC_GAME_SETUP_STRUCT_H #define AGS_SHARED_AC_GAME_SETUP_STRUCT_H +//#include "ags/lib/std/array.h" #include "ags/lib/std/vector.h" #include "ags/shared/ac/audio_clip_type.h" #include "ags/shared/ac/character_info.h" // TODO: constants to separate header @@ -159,7 +160,7 @@ struct GameSetupStruct : public GameSetupStructBase { // Functions for reading and writing appropriate data from/to save game void ReadFromSaveGame_v321(Shared::Stream *in, char *gswas, ccScript *compsc, CharacterInfo *chwas, - WordsDictionary *olddict, char **mesbk); + WordsDictionary *olddict, std::vector &mesbk); void ReadFromSavegame(Shared::Stream *in); void WriteForSavegame(Shared::Stream *out); diff --git a/engines/ags/shared/ac/game_setup_struct_base.cpp b/engines/ags/shared/ac/game_setup_struct_base.cpp index 8cd765516c9..4fd318a0d14 100644 --- a/engines/ags/shared/ac/game_setup_struct_base.cpp +++ b/engines/ags/shared/ac/game_setup_struct_base.cpp @@ -65,7 +65,6 @@ GameSetupStructBase::GameSetupStructBase() memset(paluses, 0, sizeof(paluses)); memset(defpal, 0, sizeof(defpal)); memset(reserved, 0, sizeof(reserved)); - memset(messages, 0, sizeof(messages)); } GameSetupStructBase::~GameSetupStructBase() { @@ -74,8 +73,7 @@ GameSetupStructBase::~GameSetupStructBase() { void GameSetupStructBase::Free() { for (int i = 0; i < MAXGLOBALMES; ++i) { - delete[] messages[i]; - messages[i] = nullptr; + messages[i].Free(); } delete[] load_messages; load_messages = nullptr; @@ -224,7 +222,7 @@ void GameSetupStructBase::WriteToFile(Stream *out) { out->WriteInt32(invhotdotsprite); out->WriteArrayOfInt32(reserved, 17); for (int i = 0; i < MAXGLOBALMES; ++i) { - out->WriteInt32(messages[i] ? 1 : 0); + out->WriteInt32(!messages[i].IsEmpty() ? 1 : 0); } out->WriteInt32(dict ? 1 : 0); out->WriteInt32(0); // globalscript diff --git a/engines/ags/shared/ac/game_setup_struct_base.h b/engines/ags/shared/ac/game_setup_struct_base.h index e251998847d..26a0160bbdb 100644 --- a/engines/ags/shared/ac/game_setup_struct_base.h +++ b/engines/ags/shared/ac/game_setup_struct_base.h @@ -70,12 +70,13 @@ struct GameSetupStructBase { int default_lipsync_frame; // used for unknown chars int invhotdotsprite; int32_t reserved[NUM_INTS_RESERVED]; - char *messages[MAXGLOBALMES]; + String messages[MAXGLOBALMES]; WordsDictionary *dict; char *globalscript; CharacterInfo *chars; ccScript *compiled_script; + // TODO: refactor to not have this as struct members int32_t *load_messages; bool load_dictionary; bool load_compiled_script; diff --git a/engines/ags/shared/game/main_game_file.cpp b/engines/ags/shared/game/main_game_file.cpp index 41b6255d783..187336ea71b 100644 --- a/engines/ags/shared/game/main_game_file.cpp +++ b/engines/ags/shared/game/main_game_file.cpp @@ -637,8 +637,8 @@ void SetDefaultGlmsg(GameSetupStruct &game, int msgnum, const char *val) { // TODO: find out why the index should be lowered by 500 // (or rather if we may pass correct index right away) msgnum -= 500; - if (_GP(game).messages[msgnum] == nullptr) - _GP(game).messages[msgnum] = ags_strdup(val); + if (_GP(game).messages[msgnum].IsEmpty()) + _GP(game).messages[msgnum] = val; } // Sets up default global messages (these are used mainly in older games)