fix formatting and add more proper errors.
Signed-off-by: cullvox <cullvox@outlook.com>
This commit is contained in:
parent
58bd6d929f
commit
fbe4043651
@ -184,45 +184,60 @@ struct TextFormat {
|
|||||||
bool italic = false;
|
bool italic = false;
|
||||||
bool underlined = false;
|
bool underlined = false;
|
||||||
bool strikethrough = false;
|
bool strikethrough = false;
|
||||||
|
|
||||||
TextFormat() = default;
|
|
||||||
~TextFormat() = default;
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
color = "#00000";
|
|
||||||
bold = false;
|
|
||||||
italic = false;
|
|
||||||
underlined = false;
|
|
||||||
strikethrough = false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QString textColorToHexColor(const QString& color)
|
bool textColorToHexColor(const QString& text_color, QString& result)
|
||||||
{
|
{
|
||||||
static const std::unordered_map<QString, QString> str_to_hex = {
|
static const QHash<QString, QString> text_to_hex = {
|
||||||
{ "black", "#000000" },
|
{ "black", "#000000" }, { "dark_blue", "#0000AA" }, { "dark_green", "#00AA00" }, { "dark_aqua", "#00AAAA" },
|
||||||
{ "dark_blue", "#0000AA" },
|
{ "dark_red", "#AA0000" }, { "dark_purple", "#AA00AA" }, { "gold", "#FFAA00" }, { "gray", "#AAAAAA" },
|
||||||
{ "dark_green", "#00AA00" },
|
{ "dark_gray", "#555555" }, { "blue", "#5555FF" }, { "green", "#55FF55" }, { "aqua", "#55FFFF" },
|
||||||
{ "dark_aqua", "#00AAAA" },
|
{ "red", "#FF5555" }, { "light_purple", "#FF55FF" }, { "yellow", "#FFFF55" }, { "white", "#FFFFFF" },
|
||||||
{ "dark_red", "#AA0000" },
|
|
||||||
{ "dark_purple", "#AA00AA" },
|
|
||||||
{ "gold", "#FFAA00" },
|
|
||||||
{ "gray", "#AAAAAA" },
|
|
||||||
{ "dark_gray", "#555555" },
|
|
||||||
{ "blue", "#5555FF" },
|
|
||||||
{ "green", "#55FF55" },
|
|
||||||
{ "aqua", "#55FFFF" },
|
|
||||||
{ "red", "#FF5555" },
|
|
||||||
{ "light_purple", "#FF55FF" },
|
|
||||||
{ "yellow", "#FFFF55" },
|
|
||||||
{ "white", "#FFFFFF" },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto it = str_to_hex.find(color);
|
auto it = text_to_hex.find(text_color);
|
||||||
return (it != str_to_hex.end()) ? it->second : QString("#000000"); // return black if color not found
|
|
||||||
|
if (it == text_to_hex.end()) {
|
||||||
|
qWarning() << "Invalid color string in component!";
|
||||||
|
result = "#000000"; // return black if color not found
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = it.value();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readFormat(const QJsonObject& obj, TextFormat& format) {
|
bool getBoolOrFromText(const QJsonValue& val, bool& result)
|
||||||
|
{
|
||||||
|
if (val.isUndefined()) {
|
||||||
|
result = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (val.isBool()) {
|
||||||
|
result = val.toBool();
|
||||||
|
return true;
|
||||||
|
} else if (val.isString()) {
|
||||||
|
auto bool_str = val.toString();
|
||||||
|
|
||||||
|
if (bool_str == "true") {
|
||||||
|
result = true;
|
||||||
|
return true;
|
||||||
|
} else if (bool_str == "false") {
|
||||||
|
result = false;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
qWarning() << "Invalid bool value in component!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qWarning() << "Invalid type where bool expected!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool readFormat(const QJsonObject& obj, TextFormat& format)
|
||||||
|
{
|
||||||
auto text = obj.value("text");
|
auto text = obj.value("text");
|
||||||
auto color = obj.value("color");
|
auto color = obj.value("color");
|
||||||
auto bold = obj.value("bold");
|
auto bold = obj.value("bold");
|
||||||
@ -232,21 +247,30 @@ bool readFormat(const QJsonObject& obj, TextFormat& format) {
|
|||||||
auto extra = obj.value("extra");
|
auto extra = obj.value("extra");
|
||||||
|
|
||||||
if (color.isString()) {
|
if (color.isString()) {
|
||||||
format.color = textColorToHexColor(color.toString());
|
// colors can either be a hex code or one of a few text colors
|
||||||
}
|
auto col_str = color.toString();
|
||||||
if (bold.isBool())
|
|
||||||
format.bold = bold.toBool();
|
|
||||||
if (italic.isBool())
|
|
||||||
format.italic = italic.toBool();
|
|
||||||
if (underlined.isBool())
|
|
||||||
format.underlined = underlined.toBool();
|
|
||||||
if (strikethrough.isBool())
|
|
||||||
format.strikethrough = strikethrough.toBool();
|
|
||||||
|
|
||||||
return true;
|
const QRegularExpression hex_expression("^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$");
|
||||||
|
const auto hex_match = hex_expression.match(col_str);
|
||||||
|
|
||||||
|
if (hex_match.hasMatch()) {
|
||||||
|
format.color = color.toString();
|
||||||
|
} else {
|
||||||
|
if (!textColorToHexColor(color.toString(), format.color)) {
|
||||||
|
qWarning() << "Invalid color type in component!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getBoolOrFromText(bold, format.bold) &&
|
||||||
|
getBoolOrFromText(italic, format.italic) &&
|
||||||
|
getBoolOrFromText(underlined, format.underlined) &&
|
||||||
|
getBoolOrFromText(strikethrough, format.strikethrough);
|
||||||
}
|
}
|
||||||
|
|
||||||
void appendBeginFormat(TextFormat& format, QString& toAppend) {
|
void appendBeginFormat(TextFormat& format, QString& toAppend)
|
||||||
|
{
|
||||||
toAppend.append("<font color=\"" + format.color + "\">");
|
toAppend.append("<font color=\"" + format.color + "\">");
|
||||||
if (format.bold)
|
if (format.bold)
|
||||||
toAppend.append("<b>");
|
toAppend.append("<b>");
|
||||||
@ -275,7 +299,8 @@ bool processComponent(const QJsonValue& value, QString& result, TextFormat* pare
|
|||||||
bool processComponentList(const QJsonArray& arr, QString& result, TextFormat* parentFormat = nullptr) {
|
bool processComponentList(const QJsonArray& arr, QString& result, TextFormat* parentFormat = nullptr) {
|
||||||
|
|
||||||
for (const QJsonValue& val : arr) {
|
for (const QJsonValue& val : arr) {
|
||||||
processComponent(val, result, parentFormat);
|
if (!processComponent(val, result, parentFormat))
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -306,6 +331,9 @@ bool processComponent(const QJsonValue& value, QString& result, TextFormat* pare
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
appendEndFormat(format, result);
|
appendEndFormat(format, result);
|
||||||
|
} else {
|
||||||
|
qWarning() << "Invalid component type!";
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -324,12 +352,15 @@ bool processMCMeta(ResourcePack& pack, QByteArray&& raw_data) {
|
|||||||
// description could either be string, or array of dictionaries
|
// description could either be string, or array of dictionaries
|
||||||
auto desc_val = pack_obj.value("description");
|
auto desc_val = pack_obj.value("description");
|
||||||
|
|
||||||
if (desc_val.isString())
|
if (desc_val.isString()) {
|
||||||
pack.setDescription(desc_val.toString());
|
pack.setDescription(desc_val.toString());
|
||||||
else if (desc_val.isArray()) {
|
} else if (desc_val.isArray()) {
|
||||||
QString desc{};
|
QString desc;
|
||||||
processComponentList(desc_val.toArray(), desc);
|
if (!processComponentList(desc_val.toArray(), desc))
|
||||||
|
return false;
|
||||||
pack.setDescription(desc);
|
pack.setDescription(desc);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Json::JsonException& e) {
|
} catch (Json::JsonException& e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user