1
0
Fork 0

Codechange: Store GameStrings as shared_ptr. (#13905)

Uses shared_ptr/weak_ptr as LanguageScanner needs access without ownership.
pull/13909/head
Peter Nelson 2025-03-28 17:44:43 +00:00 committed by GitHub
parent d95422561b
commit a361841848
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 14 deletions

View File

@ -168,12 +168,12 @@ struct StringNameWriter : HeaderWriter {
*/
class LanguageScanner : protected FileScanner {
private:
GameStrings *gs;
std::weak_ptr<GameStrings> gs;
std::string exclude;
public:
/** Initialise */
LanguageScanner(GameStrings *gs, const std::string &exclude) : gs(gs), exclude(exclude) {}
LanguageScanner(std::weak_ptr<GameStrings> gs, const std::string &exclude) : gs(gs), exclude(exclude) {}
/**
* Scan.
@ -190,16 +190,20 @@ public:
auto ls = ReadRawLanguageStrings(filename);
if (!ls.IsValid()) return false;
gs->raw_strings.push_back(std::move(ls));
if (auto sp = this->gs.lock()) {
sp->raw_strings.push_back(std::move(ls));
return true;
}
return false;
}
};
/**
* Load all translations that we know of.
* @return Container with all (compiled) translations.
*/
GameStrings *LoadTranslations()
static std::shared_ptr<GameStrings> LoadTranslations()
{
const GameInfo *info = Game::GetInfo();
assert(info != nullptr);
@ -214,7 +218,7 @@ GameStrings *LoadTranslations()
auto ls = ReadRawLanguageStrings(filename);
if (!ls.IsValid()) return nullptr;
GameStrings *gs = new GameStrings();
auto gs = std::make_shared<GameStrings>();
try {
gs->raw_strings.push_back(std::move(ls));
@ -245,7 +249,6 @@ GameStrings *LoadTranslations()
gs->Compile();
return gs;
} catch (...) {
delete gs;
return nullptr;
}
}
@ -308,7 +311,7 @@ void GameStrings::Compile()
}
/** The currently loaded game strings. */
GameStrings *_current_data = nullptr;
std::shared_ptr<GameStrings> _current_data = nullptr;
/**
* Get the string pointer of a particular game string.
@ -355,7 +358,6 @@ const std::string &GetGameStringName(StringIndexInTab id)
*/
void RegisterGameTranslation(Squirrel *engine)
{
delete _current_data;
_current_data = LoadTranslations();
if (_current_data == nullptr) return;

View File

@ -113,7 +113,7 @@ struct GSDTChunkHandler : ChunkHandler {
}
};
extern GameStrings *_current_data;
extern std::shared_ptr<GameStrings> _current_data;
static std::string _game_saveload_string;
static uint32_t _game_saveload_strings;
@ -159,8 +159,7 @@ struct GSTRChunkHandler : ChunkHandler {
{
const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_language_desc, _game_language_sl_compat);
delete _current_data;
_current_data = new GameStrings();
_current_data = std::make_shared<GameStrings>();
while (SlIterateArray() != -1) {
LanguageStrings ls;
@ -170,8 +169,7 @@ struct GSTRChunkHandler : ChunkHandler {
/* If there were no strings in the savegame, set GameStrings to nullptr */
if (_current_data->raw_strings.empty()) {
delete _current_data;
_current_data = nullptr;
_current_data.reset();
return;
}