Merge branch 'master' into jgrpp
# Conflicts: # src/core/bitmath_func.cpp # src/core/bitmath_func.hpp # src/core/geometry_type.hpp # src/game/game_text.hpp # src/graph_gui.cpp # src/pathfinder/npf/npf.cpp # src/script/api/script_text.cpp # src/spritecache.cpp # src/track_func.h
This commit is contained in:
@@ -125,7 +125,7 @@
|
||||
const std::string &text = question->GetEncodedText();
|
||||
EnforcePreconditionEncodedText(false, text);
|
||||
uint min_buttons = (type == QT_QUESTION ? 1 : 0);
|
||||
EnforcePrecondition(false, CountBits(buttons) >= min_buttons && CountBits(buttons) <= 3);
|
||||
EnforcePrecondition(false, CountBits<uint64_t>(buttons) >= min_buttons && CountBits<uint64_t>(buttons) <= 3);
|
||||
EnforcePrecondition(false, buttons >= 0 && buttons < (1 << ::GOAL_QUESTION_BUTTON_COUNT));
|
||||
EnforcePrecondition(false, (int)type < ::GQT_END);
|
||||
EnforcePrecondition(false, uniqueid >= 0 && uniqueid <= UINT16_MAX);
|
||||
|
@@ -168,7 +168,13 @@ std::string ScriptText::GetEncodedText()
|
||||
seen_ids.clear();
|
||||
std::string result;
|
||||
auto output = std::back_inserter(result);
|
||||
this->_GetEncodedText(output, param_count, seen_ids);
|
||||
if (this->GetActiveInstance()->IsTextParamMismatchAllowed()) {
|
||||
this->_GetEncodedTextTraditional(output, param_count, seen_ids);
|
||||
} else {
|
||||
ParamList params;
|
||||
this->_FillParamList(params);
|
||||
this->_GetEncodedText(output, param_count, seen_ids, params);
|
||||
}
|
||||
if (param_count > SCRIPT_TEXT_MAX_PARAMETERS) throw Script_FatalError(fmt::format("{}: Too many parameters", GetGameStringName(this->string)));
|
||||
return result;
|
||||
}
|
||||
@@ -182,13 +188,21 @@ void ScriptText::_TextParamError(std::string msg)
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptText::_GetEncodedTextParamsTraditional(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids, const std::string &name)
|
||||
void ScriptText::_GetEncodedTextTraditional(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids)
|
||||
{
|
||||
const std::string &name = GetGameStringName(this->string);
|
||||
|
||||
if (std::find(seen_ids.begin(), seen_ids.end(), this->string) != seen_ids.end()) throw Script_FatalError(fmt::format("{}: Circular reference detected", name));
|
||||
seen_ids.push_back(this->string);
|
||||
|
||||
Utf8Encode(output, SCC_ENCODED);
|
||||
fmt::format_to(output, "{:X}", this->string);
|
||||
|
||||
auto write_param_fallback = [&](int idx) {
|
||||
if (std::holds_alternative<ScriptTextRef>(this->param[idx])) {
|
||||
int count = 1; // 1 because the string id is included in consumed parameters
|
||||
fmt::format_to(output, ":");
|
||||
std::get<ScriptTextRef>(this->param[idx])->_GetEncodedText(output, count, seen_ids);
|
||||
std::get<ScriptTextRef>(this->param[idx])->_GetEncodedTextTraditional(output, count, seen_ids);
|
||||
param_count += count;
|
||||
} else if (std::holds_alternative<SQInteger>(this->param[idx])) {
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[idx]));
|
||||
@@ -228,7 +242,7 @@ void ScriptText::_GetEncodedTextParamsTraditional(std::back_insert_iterator<std:
|
||||
}
|
||||
int count = 1; // 1 because the string id is included in consumed parameters
|
||||
fmt::format_to(output, ":");
|
||||
std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(output, count, seen_ids);
|
||||
std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedTextTraditional(output, count, seen_ids);
|
||||
if (count != cur_param.consumes) {
|
||||
this->_TextParamError(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, cur_idx, count - 1, cur_param.consumes - 1));
|
||||
}
|
||||
@@ -256,9 +270,21 @@ void ScriptText::_GetEncodedTextParamsTraditional(std::back_insert_iterator<std:
|
||||
for (int i = cur_idx; i < this->paramc; i++) {
|
||||
write_param_fallback(i);
|
||||
}
|
||||
|
||||
seen_ids.pop_back();
|
||||
}
|
||||
|
||||
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids)
|
||||
void ScriptText::_FillParamList(ParamList ¶ms)
|
||||
{
|
||||
for (int i = 0; i < this->paramc; i++) {
|
||||
Param *p = &this->param[i];
|
||||
params.emplace_back(this->string, i, p);
|
||||
if (!std::holds_alternative<ScriptTextRef>(*p)) continue;
|
||||
std::get<ScriptTextRef>(*p)->_FillParamList(params);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids, ParamSpan args)
|
||||
{
|
||||
const std::string &name = GetGameStringName(this->string);
|
||||
|
||||
@@ -268,70 +294,52 @@ void ScriptText::_GetEncodedText(std::back_insert_iterator<std::string> &output,
|
||||
Utf8Encode(output, SCC_ENCODED);
|
||||
fmt::format_to(output, "{:X}", this->string);
|
||||
|
||||
if (this->GetActiveInstance()->IsTextParamMismatchAllowed()) {
|
||||
this->_GetEncodedTextParamsTraditional(output, param_count, seen_ids, name);
|
||||
seen_ids.pop_back();
|
||||
return;
|
||||
}
|
||||
|
||||
const StringParams ¶ms = GetGameStringParams(this->string);
|
||||
int cur_idx = 0;
|
||||
int prev_string = -1;
|
||||
int prev_idx = -1;
|
||||
int prev_count = -1;
|
||||
|
||||
size_t idx = 0;
|
||||
auto get_next_arg = [&]() {
|
||||
if (idx >= args.size()) throw Script_FatalError(fmt::format("{}({}): Not enough parameters", name, param_count + 1));
|
||||
ParamCheck &pc = args[idx++];
|
||||
if (pc.owner != this->string) ScriptLog::Warning(fmt::format("{}({}): Consumes {}({})", name, param_count + 1, GetGameStringName(pc.owner), pc.idx + 1));
|
||||
return pc.param;
|
||||
};
|
||||
auto skip_args = [&](size_t nb) { idx += nb; };
|
||||
|
||||
for (const StringParam &cur_param : params) {
|
||||
if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
||||
switch (cur_param.type) {
|
||||
case StringParam::UNUSED:
|
||||
skip_args(cur_param.consumes);
|
||||
break;
|
||||
|
||||
if (prev_string != -1) {
|
||||
/* The previous substring added more parameters than expected, means we will consume them but can't properly validate them. */
|
||||
for (int i = 0; i < cur_param.consumes; i++) {
|
||||
if (prev_idx < prev_count) {
|
||||
ScriptLog::Warning(fmt::format("{}: Parameter {} uses parameter {} from substring {} and cannot be validated", name, param_count + i, prev_idx++, prev_string));
|
||||
} else {
|
||||
/* No more extra parameters, assume SQInteger are expected. */
|
||||
if (cur_idx >= this->paramc) throw Script_FatalError(fmt::format("{}: Not enough parameters", name));
|
||||
if (!std::holds_alternative<SQInteger>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[cur_idx++]));
|
||||
case StringParam::RAW_STRING: {
|
||||
Param *p = get_next_arg();
|
||||
if (!std::holds_alternative<std::string>(*p)) throw Script_FatalError(fmt::format("{}({}): {{{}}} expects a raw string", name, param_count + 1, cur_param.cmd));
|
||||
fmt::format_to(output, ":\"{}\"", std::get<std::string>(*p));
|
||||
break;
|
||||
}
|
||||
|
||||
case StringParam::STRING: {
|
||||
Param *p = get_next_arg();
|
||||
if (!std::holds_alternative<ScriptTextRef>(*p)) throw Script_FatalError(fmt::format("{}({}): {{{}}} expects a GSText", name, param_count + 1, cur_param.cmd));
|
||||
int count = 0;
|
||||
fmt::format_to(output, ":");
|
||||
ScriptTextRef &ref = std::get<ScriptTextRef>(*p);
|
||||
ref->_GetEncodedText(output, count, seen_ids, args.subspan(idx));
|
||||
if (++count != cur_param.consumes) {
|
||||
ScriptLog::Error(fmt::format("{}({}): {{{}}} expects {} to be consumed, but {} consumes {}", name, param_count + 1, cur_param.cmd, cur_param.consumes - 1, GetGameStringName(ref->string), count - 1));
|
||||
/* Fill missing params if needed. */
|
||||
for (int i = count; i < cur_param.consumes; i++) fmt::format_to(output, ":0");
|
||||
}
|
||||
skip_args(cur_param.consumes - 1);
|
||||
break;
|
||||
}
|
||||
if (prev_idx == prev_count) {
|
||||
/* Re-enable validation. */
|
||||
prev_string = -1;
|
||||
}
|
||||
} else {
|
||||
switch (cur_param.type) {
|
||||
case StringParam::RAW_STRING:
|
||||
if (!std::holds_alternative<std::string>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a raw string", name, param_count));
|
||||
fmt::format_to(output, ":\"{}\"", std::get<std::string>(this->param[cur_idx++]));
|
||||
break;
|
||||
|
||||
case StringParam::STRING: {
|
||||
if (!std::holds_alternative<ScriptTextRef>(this->param[cur_idx])) throw Script_FatalError(fmt::format("{}: Parameter {} expects a substring", name, param_count));
|
||||
int count = 0;
|
||||
fmt::format_to(output, ":");
|
||||
std::get<ScriptTextRef>(this->param[cur_idx++])->_GetEncodedText(output, count, seen_ids);
|
||||
if (++count != cur_param.consumes) {
|
||||
ScriptLog::Error(fmt::format("{}: Parameter {} substring consumes {}, but expected {} to be consumed", name, param_count, count - 1, cur_param.consumes - 1));
|
||||
/* Fill missing params if needed. */
|
||||
for (int i = count; i < cur_param.consumes; i++) fmt::format_to(output, ":0");
|
||||
/* Disable validation for the extra params if any. */
|
||||
if (count > cur_param.consumes) {
|
||||
prev_string = param_count;
|
||||
prev_idx = cur_param.consumes - 1;
|
||||
prev_count = count - 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
for (int i = 0; i < cur_param.consumes; i++) {
|
||||
Param *p = get_next_arg();
|
||||
if (!std::holds_alternative<SQInteger>(*p)) throw Script_FatalError(fmt::format("{}({}): {{{}}} expects an integer", name, param_count + i + 1, cur_param.cmd));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(*p));
|
||||
}
|
||||
|
||||
default:
|
||||
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(fmt::format("{}: Parameter {} expects an integer", name, param_count + i));
|
||||
fmt::format_to(output, ":{:X}", std::get<SQInteger>(this->param[cur_idx++]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
param_count += cur_param.consumes;
|
||||
|
@@ -130,13 +130,32 @@ public:
|
||||
private:
|
||||
using ScriptTextRef = ScriptObjectRef<ScriptText>;
|
||||
using StringIDList = std::vector<StringID>;
|
||||
using Param = std::variant<SQInteger, std::string, ScriptTextRef>;
|
||||
|
||||
struct ParamCheck {
|
||||
StringID owner;
|
||||
int idx;
|
||||
Param *param;
|
||||
|
||||
ParamCheck(StringID owner, int idx, Param *param) : owner(owner), idx(idx), param(param) {}
|
||||
};
|
||||
|
||||
using ParamList = std::vector<ParamCheck>;
|
||||
using ParamSpan = std::span<ParamCheck>;
|
||||
|
||||
StringID string;
|
||||
std::variant<SQInteger, std::string, ScriptTextRef> param[SCRIPT_TEXT_MAX_PARAMETERS];
|
||||
Param param[SCRIPT_TEXT_MAX_PARAMETERS];
|
||||
int paramc;
|
||||
|
||||
void _TextParamError(std::string msg);
|
||||
void _GetEncodedTextParamsTraditional(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids, const std::string &name);
|
||||
|
||||
/**
|
||||
* Internal function to recursively fill a list of parameters.
|
||||
* The parameters are added as _GetEncodedText used to encode them
|
||||
* before the addition of parameter validation.
|
||||
* @param params The list of parameters to fill.
|
||||
*/
|
||||
void _FillParamList(ParamList ¶ms);
|
||||
|
||||
/**
|
||||
* Internal function for recursive calling this function over multiple
|
||||
@@ -145,7 +164,9 @@ private:
|
||||
* @param param_count The number of parameters that are in the string.
|
||||
* @param seen_ids The list of seen StringID.
|
||||
*/
|
||||
void _GetEncodedText(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids);
|
||||
void _GetEncodedText(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids, ParamSpan args);
|
||||
|
||||
void _GetEncodedTextTraditional(std::back_insert_iterator<std::string> &output, int ¶m_count, StringIDList &seen_ids);
|
||||
|
||||
/**
|
||||
* Set a parameter, where the value is the first item on the stack.
|
||||
|
Reference in New Issue
Block a user