AGS: Common: replaced GameSetupStruct::messages with an array of Strings

This also fixes an allocation/deallocation mismatch found in the code.
From upstream b47a086cbd589cd271c5fe1b3497f9b8e4ae4abf
This commit is contained in:
Walter Agazzi 2023-02-26 18:47:42 +01:00 committed by Thierry Crozat
parent 0263572d1c
commit 5ad85c6b42
7 changed files with 29 additions and 29 deletions

View File

@ -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)

View File

@ -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<String> mesbk(MAXGLOBALMES);
int numchwas = _GP(game).numcharacters;
for (size_t i = 0; i < MAXGLOBALMES; ++i)
mesbk[i] = _GP(game).messages[i];

View File

@ -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<String> &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();

View File

@ -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<String> &mesbk);
void ReadFromSavegame(Shared::Stream *in);
void WriteForSavegame(Shared::Stream *out);

View File

@ -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

View File

@ -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;

View File

@ -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)