Changeset - r27859:6bcc242489a3
[Not reviewed]
master
0 1 0
Peter Nelson - 9 months ago 2023-09-03 19:33:24
peter1138@openttd.org
Change: Replace fixed length _grf_text array with vector.

Additionally reshuffle GRFTextEntry for better alignment.

This removes a mostly-unused static 20MB allocation.
1 file changed with 22 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/newgrf_text.cpp
Show inline comments
 
@@ -61,21 +61,20 @@ enum GRFExtendedLanguages {
 
/**
 
 * Holder of the above structure.
 
 * Putting both grfid and stringid together allows us to avoid duplicates,
 
 * since it is NOT SUPPOSED to happen.
 
 */
 
struct GRFTextEntry {
 
	GRFTextList textholder;
 
	StringID def_string;
 
	uint32_t grfid;
 
	uint16_t stringid;
 
	StringID def_string;
 
	GRFTextList textholder;
 
};
 

	
 

	
 
static uint _num_grf_texts = 0;
 
static GRFTextEntry _grf_text[TAB_SIZE_NEWGRF];
 
static std::vector<GRFTextEntry> _grf_text;
 
static byte _currentLangID = GRFLX_ENGLISH;  ///< by default, english is used.
 

	
 
/**
 
 * Get the mapping from the NewGRF supplied ID to OpenTTD's internal ID.
 
 * @param newgrf_id The NewGRF ID to map.
 
 * @param gender    Whether to map genders or cases.
 
@@ -558,48 +557,42 @@ StringID AddGRFString(uint32_t grfid, ui
 
			if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, allow_newlines, text_to_add, def_string);
 
			if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, allow_newlines, text_to_add, def_string);
 
			return ret;
 
		}
 
	}
 

	
 
	uint id;
 
	for (id = 0; id < _num_grf_texts; id++) {
 
		if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
 
			break;
 
		}
 
	auto it = std::find_if(std::begin(_grf_text), std::end(_grf_text), [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
 
	if (it == std::end(_grf_text)) {
 
		/* Too many strings allocated, return empty. */
 
		if (_grf_text.size() == TAB_SIZE_NEWGRF) return STR_EMPTY;
 

	
 
		/* We didn't find our stringid and grfid in the list, allocate a new id. */
 
		it = _grf_text.emplace(std::end(_grf_text));
 
		it->grfid      = grfid;
 
		it->stringid   = stringid;
 
		it->def_string = def_string;
 
	}
 

	
 
	/* Too many strings allocated, return empty */
 
	if (id == lengthof(_grf_text)) return STR_EMPTY;
 
	uint id = static_cast<uint>(it - std::begin(_grf_text));
 

	
 
	std::string newtext = TranslateTTDPatchCodes(grfid, langid_to_add, allow_newlines, text_to_add);
 

	
 
	/* If we didn't find our stringid and grfid in the list, allocate a new id */
 
	if (id == _num_grf_texts) _num_grf_texts++;
 

	
 
	if (_grf_text[id].textholder.empty()) {
 
		_grf_text[id].grfid      = grfid;
 
		_grf_text[id].stringid   = stringid;
 
		_grf_text[id].def_string = def_string;
 
	}
 
	AddGRFTextToList(_grf_text[id].textholder, langid_to_add, newtext);
 
	AddGRFTextToList(it->textholder, langid_to_add, newtext);
 

	
 
	GrfMsg(3, "Added 0x{:X} grfid {:08X} string 0x{:X} lang 0x{:X} string '{}' ({:X})", id, grfid, stringid, langid_to_add, newtext, MakeStringID(TEXT_TAB_NEWGRF_START, id));
 

	
 
	return MakeStringID(TEXT_TAB_NEWGRF_START, id);
 
}
 

	
 
/**
 
 * Returns the index for this stringid associated with its grfID
 
 */
 
StringID GetGRFStringID(uint32_t grfid, StringID stringid)
 
{
 
	for (uint id = 0; id < _num_grf_texts; id++) {
 
		if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
 
			return MakeStringID(TEXT_TAB_NEWGRF_START, id);
 
		}
 
	auto it = std::find_if(std::begin(_grf_text), std::end(_grf_text), [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
 
	if (it != std::end(_grf_text)) {
 
		uint id = static_cast<uint>(it - std::begin(_grf_text));
 
		return MakeStringID(TEXT_TAB_NEWGRF_START, id);
 
	}
 

	
 
	return STR_UNDEFINED;
 
}
 

	
 

	
 
@@ -642,12 +635,13 @@ const char *GetGRFStringFromGRFText(cons
 

	
 
/**
 
 * Get a C-string from a stringid set by a newgrf.
 
 */
 
const char *GetGRFStringPtr(uint16_t stringid)
 
{
 
	assert(stringid < _grf_text.size());
 
	assert(_grf_text[stringid].grfid != 0);
 

	
 
	const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
 
	if (str != nullptr) return str;
 

	
 
	/* Use the default string ID if the fallback string isn't available */
 
@@ -680,25 +674,17 @@ bool CheckGrfLangID(byte lang_id, byte g
 

	
 
	return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
 
}
 

	
 
/**
 
 * House cleaning.
 
 * Remove all strings and reset the text counter.
 
 * Remove all strings.
 
 */
 
void CleanUpStrings()
 
{
 
	uint id;
 

	
 
	for (id = 0; id < _num_grf_texts; id++) {
 
		_grf_text[id].grfid      = 0;
 
		_grf_text[id].stringid   = 0;
 
		_grf_text[id].textholder.clear();
 
	}
 

	
 
	_num_grf_texts = 0;
 
	_grf_text.clear();
 
}
 

	
 
struct TextRefStack {
 
	std::array<byte, 0x30> stack;
 
	byte position;
 
	const GRFFile *grffile;
0 comments (0 inline, 0 general)