mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Store GameStrings as shared_ptr. (#13905)
Uses shared_ptr/weak_ptr as LanguageScanner needs access without ownership.pull/13909/head
parent
d95422561b
commit
a361841848
|
@ -168,12 +168,12 @@ struct StringNameWriter : HeaderWriter {
|
||||||
*/
|
*/
|
||||||
class LanguageScanner : protected FileScanner {
|
class LanguageScanner : protected FileScanner {
|
||||||
private:
|
private:
|
||||||
GameStrings *gs;
|
std::weak_ptr<GameStrings> gs;
|
||||||
std::string exclude;
|
std::string exclude;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** Initialise */
|
/** 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.
|
* Scan.
|
||||||
|
@ -190,16 +190,20 @@ public:
|
||||||
auto ls = ReadRawLanguageStrings(filename);
|
auto ls = ReadRawLanguageStrings(filename);
|
||||||
if (!ls.IsValid()) return false;
|
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 true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load all translations that we know of.
|
* Load all translations that we know of.
|
||||||
* @return Container with all (compiled) translations.
|
* @return Container with all (compiled) translations.
|
||||||
*/
|
*/
|
||||||
GameStrings *LoadTranslations()
|
static std::shared_ptr<GameStrings> LoadTranslations()
|
||||||
{
|
{
|
||||||
const GameInfo *info = Game::GetInfo();
|
const GameInfo *info = Game::GetInfo();
|
||||||
assert(info != nullptr);
|
assert(info != nullptr);
|
||||||
|
@ -214,7 +218,7 @@ GameStrings *LoadTranslations()
|
||||||
auto ls = ReadRawLanguageStrings(filename);
|
auto ls = ReadRawLanguageStrings(filename);
|
||||||
if (!ls.IsValid()) return nullptr;
|
if (!ls.IsValid()) return nullptr;
|
||||||
|
|
||||||
GameStrings *gs = new GameStrings();
|
auto gs = std::make_shared<GameStrings>();
|
||||||
try {
|
try {
|
||||||
gs->raw_strings.push_back(std::move(ls));
|
gs->raw_strings.push_back(std::move(ls));
|
||||||
|
|
||||||
|
@ -245,7 +249,6 @@ GameStrings *LoadTranslations()
|
||||||
gs->Compile();
|
gs->Compile();
|
||||||
return gs;
|
return gs;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
delete gs;
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -308,7 +311,7 @@ void GameStrings::Compile()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The currently loaded game strings. */
|
/** 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.
|
* Get the string pointer of a particular game string.
|
||||||
|
@ -355,7 +358,6 @@ const std::string &GetGameStringName(StringIndexInTab id)
|
||||||
*/
|
*/
|
||||||
void RegisterGameTranslation(Squirrel *engine)
|
void RegisterGameTranslation(Squirrel *engine)
|
||||||
{
|
{
|
||||||
delete _current_data;
|
|
||||||
_current_data = LoadTranslations();
|
_current_data = LoadTranslations();
|
||||||
if (_current_data == nullptr) return;
|
if (_current_data == nullptr) return;
|
||||||
|
|
||||||
|
|
|
@ -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 std::string _game_saveload_string;
|
||||||
static uint32_t _game_saveload_strings;
|
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);
|
const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_language_desc, _game_language_sl_compat);
|
||||||
|
|
||||||
delete _current_data;
|
_current_data = std::make_shared<GameStrings>();
|
||||||
_current_data = new GameStrings();
|
|
||||||
|
|
||||||
while (SlIterateArray() != -1) {
|
while (SlIterateArray() != -1) {
|
||||||
LanguageStrings ls;
|
LanguageStrings ls;
|
||||||
|
@ -170,8 +169,7 @@ struct GSTRChunkHandler : ChunkHandler {
|
||||||
|
|
||||||
/* If there were no strings in the savegame, set GameStrings to nullptr */
|
/* If there were no strings in the savegame, set GameStrings to nullptr */
|
||||||
if (_current_data->raw_strings.empty()) {
|
if (_current_data->raw_strings.empty()) {
|
||||||
delete _current_data;
|
_current_data.reset();
|
||||||
_current_data = nullptr;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue