Changeset - r26956:da97fd88fa9b
[Not reviewed]
master
0 3 0
glx22 - 21 months ago 2023-03-05 16:00:16
glx@openttd.org
Change: [Script] Improve ScriptText validation error messages
3 files changed with 23 insertions and 7 deletions:
0 comments (0 inline, 0 general)
src/game/game_text.cpp
Show inline comments
 
@@ -355,6 +355,20 @@ const StringParams &GetGameStringParams(
 
}
 

	
 
/**
 
 * Get the name of a particular game string.
 
 * @param id The ID of the game string.
 
 * @return The name of the string.
 
 */
 
const std::string &GetGameStringName(uint id)
 
{
 
	/* The name for STR_UNDEFINED. */
 
	static const std::string undefined = "STR_UNDEFINED";
 

	
 
	if (id >= _current_data->string_names.size()) return undefined;
 
	return _current_data->string_names[id];
 
}
 

	
 
/**
 
 * Register the current translation to the Squirrel engine.
 
 * @param engine The engine to update/
 
 */
src/game/game_text.hpp
Show inline comments
 
@@ -29,6 +29,7 @@ using StringParamsList = std::vector<Str
 

	
 
const char *GetGameStringPtr(uint id);
 
const StringParams &GetGameStringParams(uint id);
 
const std::string &GetGameStringName(uint id);
 
void RegisterGameTranslation(class Squirrel *engine);
 
void ReconsiderGameScriptLanguage();
 

	
src/script/api/script_text.cpp
Show inline comments
 
@@ -161,7 +161,7 @@ const std::string ScriptText::GetEncoded
 
	static char buf[1024];
 
	int param_count = 0;
 
	this->_GetEncodedText(buf, lastof(buf), param_count);
 
	if (param_count > SCRIPT_TEXT_MAX_PARAMETERS) throw Script_FatalError("A string had too many parameters");
 
	if (param_count > SCRIPT_TEXT_MAX_PARAMETERS) throw Script_FatalError(fmt::format("{}: Too many parameters", GetGameStringName(this->string)));
 
	return buf;
 
}
 

	
 
@@ -171,30 +171,31 @@ char *ScriptText::_GetEncodedText(char *
 
	p += seprintf(p, lastofp, "%X", this->string);
 

	
 
	const StringParams &params = GetGameStringParams(this->string);
 
	const std::string &name = GetGameStringName(this->string);
 
	int cur_idx = 0;
 

	
 
	for (const StringParam &cur_param : params) {
 
		if (cur_idx >= this->paramc) throw Script_FatalError("Not enough string parameters");
 
		if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
 

	
 
		switch (cur_param.type) {
 
			case StringParam::RAW_STRING:
 
				if (!std::holds_alternative<std::string>(this->param[cur_idx])) throw Script_FatalError("Wrong string parameter type");
 
				if (!std::holds_alternative<std::string>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a raw string", name, cur_idx));
 
				p += seprintf(p, lastofp, ":\"%s\"", std::get<std::string>(this->param[cur_idx++]).c_str());
 
				break;
 

	
 
			case StringParam::STRING: {
 
				if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError("Wrong string parameter type");
 
				if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a substring", name, cur_idx));
 
				int count = 1; // 1 because the string id is included in consumed parameters
 
				p += seprintf(p, lastofp, ":");
 
				p = std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(p, lastofp, count);
 
				if (count != cur_param.consumes) throw Script_FatalError("Substring doesn't consume the expected amount of parameters.");
 
				if (count != cur_param.consumes) throw Script_FatalError(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, cur_idx, count - 1, cur_param.consumes - 1));
 
				break;
 
			}
 

	
 
			default:
 
				if (cur_idx + cur_param.consumes > this->paramc) throw Script_FatalError("Not enough string parameters");
 
				if (cur_idx + cur_param.consumes > this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
 
				for (int i = 0; i < cur_param.consumes; i++) {
 
					if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError("Wrong string parameter type");
 
					if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, cur_idx));
 
					p += seprintf(p, lastofp,":" OTTD_PRINTFHEX64, std::get<SQInteger>(this->param[cur_idx++]));
 
				}
 
		}
0 comments (0 inline, 0 general)