simplify the raw json parsing
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com> Fixed Tests Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
4053229544
commit
01e98a6ce8
@ -179,142 +179,85 @@ bool processZIP(ResourcePack& pack, ProcessingLevel level)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TextFormatter {
|
QString buildStyle(const QJsonObject& obj)
|
||||||
// left is value, right is if the value was explicitly written
|
|
||||||
QPair<QString, bool> color = { "#000000", false };
|
|
||||||
QPair<bool, bool> bold = { false, false };
|
|
||||||
QPair<bool, bool> italic = { false, false };
|
|
||||||
QPair<bool, bool> underlined = { false, false };
|
|
||||||
QPair<bool, bool> strikethrough = { false, false };
|
|
||||||
QPair<bool, bool> is_linked = { false, false };
|
|
||||||
QPair<QString, bool> link_url = { "", false };
|
|
||||||
|
|
||||||
void setColor(const QString& new_color, bool written) { color = { new_color, written }; }
|
|
||||||
void setBold(bool new_bold, bool written) { bold = { new_bold, written }; }
|
|
||||||
void setItalic(bool new_italic, bool written) { italic = { new_italic, written }; }
|
|
||||||
void setUnderlined(bool new_underlined, bool written) { underlined = { new_underlined, written }; }
|
|
||||||
void setStrikethrough(bool new_strikethrough, bool written) { strikethrough = { new_strikethrough, written }; }
|
|
||||||
void setIsLinked(bool new_is_linked, bool written) { is_linked = { new_is_linked, written }; }
|
|
||||||
void setLinkURL(const QString& new_url, bool written) { link_url = { new_url, written }; }
|
|
||||||
|
|
||||||
void overrideFrom(const TextFormatter& child)
|
|
||||||
{
|
{
|
||||||
if (child.color.second)
|
QStringList styles;
|
||||||
color.first = child.color.first;
|
if (auto color = Json::ensureString(obj, "color"); !color.isEmpty()) {
|
||||||
if (child.bold.second)
|
styles << QString("color: %1;").arg(color);
|
||||||
bold.first = child.bold.first;
|
}
|
||||||
if (child.italic.second)
|
if (obj.contains("bold")) {
|
||||||
italic.first = child.italic.first;
|
QString weight = "normal";
|
||||||
if (child.underlined.second)
|
if (Json::ensureBoolean(obj, "bold", false)) {
|
||||||
underlined.first = child.underlined.first;
|
weight = "bold";
|
||||||
if (child.strikethrough.second)
|
}
|
||||||
strikethrough.first = child.strikethrough.first;
|
styles << QString("font-weight: %1;").arg(weight);
|
||||||
if (child.is_linked.second)
|
}
|
||||||
is_linked.first = child.is_linked.first;
|
if (obj.contains("italic")) {
|
||||||
if (child.link_url.second)
|
QString style = "normal";
|
||||||
link_url.first = child.link_url.first;
|
if (Json::ensureBoolean(obj, "italic", false)) {
|
||||||
|
style = "italic";
|
||||||
|
}
|
||||||
|
styles << QString("font-style: %1;").arg(style);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString format(QString text)
|
return styles.isEmpty() ? "" : QString("style=\"%1\"").arg(styles.join(" "));
|
||||||
|
}
|
||||||
|
|
||||||
|
QString processComponent(const QJsonArray& value, bool strikethrough, bool underline)
|
||||||
{
|
{
|
||||||
if (text.isEmpty())
|
|
||||||
return QString();
|
|
||||||
|
|
||||||
QString result;
|
QString result;
|
||||||
|
for (auto current : value)
|
||||||
if (color.first != "#000000")
|
result += processComponent(current, strikethrough, underline);
|
||||||
result.append("<font color=\"" + color.first + "\">");
|
|
||||||
if (bold.first)
|
|
||||||
result.append("<b>");
|
|
||||||
if (italic.first)
|
|
||||||
result.append("<i>");
|
|
||||||
if (underlined.first)
|
|
||||||
result.append("<u>");
|
|
||||||
if (strikethrough.first)
|
|
||||||
result.append("<s>");
|
|
||||||
if (is_linked.first)
|
|
||||||
result.append("<a href=\"" + link_url.first + "\">");
|
|
||||||
|
|
||||||
result.append(text);
|
|
||||||
|
|
||||||
if (is_linked.first)
|
|
||||||
result.append("</a>");
|
|
||||||
if (strikethrough.first)
|
|
||||||
result.append("</s>");
|
|
||||||
if (underlined.first)
|
|
||||||
result.append("</u>");
|
|
||||||
if (italic.first)
|
|
||||||
result.append("</i>");
|
|
||||||
if (bold.first)
|
|
||||||
result.append("</b>");
|
|
||||||
if (color.first != "#000000")
|
|
||||||
result.append("</font>");
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readFormat(const QJsonObject& obj)
|
QString processComponent(const QJsonObject& obj, bool strikethrough, bool underline)
|
||||||
{
|
{
|
||||||
setColor(Json::ensureString(obj, "color", "#000000"), obj.contains("color"));
|
underline = Json::ensureBoolean(obj, "underlined", underline);
|
||||||
setBold(Json::ensureBoolean(obj, "bold", false), obj.contains("bold"));
|
strikethrough = Json::ensureBoolean(obj, "strikethrough", strikethrough);
|
||||||
setItalic(Json::ensureBoolean(obj, "italic", false), obj.contains("italic"));
|
|
||||||
setUnderlined(Json::ensureBoolean(obj, "underlined", false), obj.contains("underlined"));
|
|
||||||
setStrikethrough(Json::ensureBoolean(obj, "strikethrough", false), obj.contains("strikethrough"));
|
|
||||||
|
|
||||||
|
QString result = Json::ensureString(obj, "text");
|
||||||
|
if (underline) {
|
||||||
|
result = QString("<u>%1</u>").arg(result);
|
||||||
|
}
|
||||||
|
if (strikethrough) {
|
||||||
|
result = QString("<s>%1</s>").arg(result);
|
||||||
|
}
|
||||||
|
// the extra needs to be a array
|
||||||
|
result += processComponent(Json::ensureArray(obj, "extra"), strikethrough, underline);
|
||||||
|
if (auto style = buildStyle(obj); !style.isEmpty()) {
|
||||||
|
result = QString("<span %1>%2</span>").arg(style, result);
|
||||||
|
}
|
||||||
|
if (obj.contains("clickEvent")) {
|
||||||
auto click_event = Json::ensureObject(obj, "clickEvent");
|
auto click_event = Json::ensureObject(obj, "clickEvent");
|
||||||
setIsLinked(Json::ensureBoolean(click_event, "open_url", false), click_event.contains("open_url"));
|
auto action = Json::ensureString(click_event, "action");
|
||||||
setLinkURL(Json::ensureString(click_event, "value"), click_event.contains("value"));
|
auto value = Json::ensureString(click_event, "value");
|
||||||
|
if (action == "open_url" && !value.isEmpty()) {
|
||||||
return true;
|
result = QString("<a href=\"%1\">%2</a>").arg(value, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
bool processComponent(const QJsonValue& value, QString& result, const TextFormatter* parentFormat)
|
QString processComponent(const QJsonValue& value, bool strikethrough, bool underline)
|
||||||
{
|
{
|
||||||
TextFormatter formatter;
|
|
||||||
if (parentFormat)
|
|
||||||
formatter = *parentFormat;
|
|
||||||
|
|
||||||
if (value.isString()) {
|
if (value.isString()) {
|
||||||
result.append(formatter.format(value.toString()));
|
return value.toString();
|
||||||
} else if (value.isBool()) {
|
|
||||||
result.append(formatter.format(value.toBool() ? "true" : "false"));
|
|
||||||
} else if (value.isDouble()) {
|
|
||||||
result.append(formatter.format(QString::number(value.toDouble())));
|
|
||||||
} else if (value.isObject()) {
|
|
||||||
auto obj = value.toObject();
|
|
||||||
|
|
||||||
if (not formatter.readFormat(obj))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// override the parent format with our new one
|
|
||||||
TextFormatter mixed;
|
|
||||||
if (parentFormat)
|
|
||||||
mixed = *parentFormat;
|
|
||||||
|
|
||||||
mixed.overrideFrom(formatter);
|
|
||||||
|
|
||||||
result.append(mixed.format(Json::ensureString(obj, "text")));
|
|
||||||
|
|
||||||
// process any 'extra' children with this format
|
|
||||||
auto extra = obj.value("extra");
|
|
||||||
if (not extra.isUndefined())
|
|
||||||
return processComponent(extra, result, &mixed);
|
|
||||||
|
|
||||||
} else if (value.isArray()) {
|
|
||||||
auto array = value.toArray();
|
|
||||||
|
|
||||||
for (const QJsonValue& current : array) {
|
|
||||||
if (not processComponent(current, result, parentFormat)) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
if (value.isBool()) {
|
||||||
|
return value.toBool() ? "true" : "false";
|
||||||
|
}
|
||||||
|
if (value.isDouble()) {
|
||||||
|
return QString::number(value.toDouble());
|
||||||
|
}
|
||||||
|
if (value.isArray()) {
|
||||||
|
return processComponent(value.toArray(), strikethrough, underline);
|
||||||
|
}
|
||||||
|
if (value.isObject()) {
|
||||||
|
return processComponent(value.toObject(), strikethrough, underline);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
qWarning() << "Invalid component type!";
|
qWarning() << "Invalid component type!";
|
||||||
return false;
|
return {};
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta
|
// https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta
|
||||||
@ -327,12 +270,7 @@ bool processMCMeta(ResourcePack& pack, QByteArray&& raw_data)
|
|||||||
|
|
||||||
pack.setPackFormat(Json::ensureInteger(pack_obj, "pack_format", 0));
|
pack.setPackFormat(Json::ensureInteger(pack_obj, "pack_format", 0));
|
||||||
|
|
||||||
auto desc_val = pack_obj.value("description");
|
pack.setDescription(processComponent(pack_obj.value("description")));
|
||||||
QString desc{};
|
|
||||||
if (not processComponent(desc_val, desc))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
pack.setDescription(desc);
|
|
||||||
|
|
||||||
} catch (Json::JsonException& e) {
|
} catch (Json::JsonException& e) {
|
||||||
qWarning() << "JsonException: " << e.what() << e.cause();
|
qWarning() << "JsonException: " << e.what() << e.cause();
|
||||||
|
@ -34,8 +34,7 @@ bool process(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
|||||||
bool processZIP(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
bool processZIP(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
bool processFolder(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
bool processFolder(ResourcePack& pack, ProcessingLevel level = ProcessingLevel::Full);
|
||||||
|
|
||||||
struct TextFormatter;
|
QString processComponent(const QJsonValue& value, bool strikethrough = false, bool underline = false);
|
||||||
bool processComponent(const QJsonValue& value, QString& result, const TextFormatter* parentFormat = nullptr);
|
|
||||||
bool processMCMeta(ResourcePack& pack, QByteArray&& raw_data);
|
bool processMCMeta(ResourcePack& pack, QByteArray&& raw_data);
|
||||||
bool processPackPNG(const ResourcePack& pack, QByteArray&& raw_data);
|
bool processPackPNG(const ResourcePack& pack, QByteArray&& raw_data);
|
||||||
|
|
||||||
|
@ -287,8 +287,6 @@ void InfoFrame::setDescription(QString text)
|
|||||||
ui->descriptionLabel->setTextFormat(Qt::TextFormat::RichText); // This allows injecting HTML here.
|
ui->descriptionLabel->setTextFormat(Qt::TextFormat::RichText); // This allows injecting HTML here.
|
||||||
m_description = text;
|
m_description = text;
|
||||||
|
|
||||||
const QString elidedPostfix = "<a href=\"#mod_desc\">...</a>";
|
|
||||||
|
|
||||||
// move the cursor to the character elide, doesn't see html
|
// move the cursor to the character elide, doesn't see html
|
||||||
QTextCursor cursor(&doc);
|
QTextCursor cursor(&doc);
|
||||||
cursor.movePosition(QTextCursor::End);
|
cursor.movePosition(QTextCursor::End);
|
||||||
@ -296,7 +294,7 @@ void InfoFrame::setDescription(QString text)
|
|||||||
cursor.removeSelectedText();
|
cursor.removeSelectedText();
|
||||||
|
|
||||||
// insert the post fix at the cursor
|
// insert the post fix at the cursor
|
||||||
cursor.insertHtml(elidedPostfix);
|
cursor.insertHtml("<a href=\"#mod_desc\">...</a>");
|
||||||
|
|
||||||
labeltext.append(doc.toHtml());
|
labeltext.append(doc.toHtml());
|
||||||
QObject::connect(ui->descriptionLabel, &QLabel::linkActivated, this, &InfoFrame::descriptionEllipsisHandler);
|
QObject::connect(ui->descriptionLabel, &QLabel::linkActivated, this, &InfoFrame::descriptionEllipsisHandler);
|
||||||
@ -335,7 +333,7 @@ void InfoFrame::setLicense(QString text)
|
|||||||
if (finaltext.length() > 290) {
|
if (finaltext.length() > 290) {
|
||||||
ui->licenseLabel->setOpenExternalLinks(false);
|
ui->licenseLabel->setOpenExternalLinks(false);
|
||||||
ui->licenseLabel->setTextFormat(Qt::TextFormat::RichText);
|
ui->licenseLabel->setTextFormat(Qt::TextFormat::RichText);
|
||||||
m_description = text;
|
m_license = text;
|
||||||
// This allows injecting HTML here.
|
// This allows injecting HTML here.
|
||||||
labeltext.append("<html><body>" + finaltext.left(287) + "<a href=\"#mod_desc\">...</a></body></html>");
|
labeltext.append("<html><body>" + finaltext.left(287) + "<a href=\"#mod_desc\">...</a></body></html>");
|
||||||
QObject::connect(ui->licenseLabel, &QLabel::linkActivated, this, &InfoFrame::licenseEllipsisHandler);
|
QObject::connect(ui->licenseLabel, &QLabel::linkActivated, this, &InfoFrame::licenseEllipsisHandler);
|
||||||
|
@ -64,16 +64,14 @@ class MetaComponentParseTest : public QObject {
|
|||||||
QJsonValue description_json = obj.value("description");
|
QJsonValue description_json = obj.value("description");
|
||||||
QJsonValue expected_json = obj.value("expected_output");
|
QJsonValue expected_json = obj.value("expected_output");
|
||||||
|
|
||||||
QVERIFY(description_json.isUndefined() == false);
|
QVERIFY(!description_json.isUndefined());
|
||||||
QVERIFY(expected_json.isString() == true);
|
QVERIFY(expected_json.isString());
|
||||||
|
|
||||||
QString expected = expected_json.toString();
|
QString expected = expected_json.toString();
|
||||||
|
|
||||||
QString processed;
|
QString processed = ResourcePackUtils::processComponent(description_json);
|
||||||
bool valid = ResourcePackUtils::processComponent(description_json, processed);
|
|
||||||
|
|
||||||
QVERIFY(processed == expected);
|
QCOMPARE(processed, expected);
|
||||||
QVERIFY(valid == true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
@ -7,12 +7,15 @@
|
|||||||
"italic": true,
|
"italic": true,
|
||||||
"extra": [
|
"extra": [
|
||||||
{
|
{
|
||||||
"extra": "Component!",
|
"extra": [
|
||||||
|
"Component!"
|
||||||
|
],
|
||||||
"bold": false,
|
"bold": false,
|
||||||
"italic": false
|
"italic": false
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"expected_output": "<font color=\"red\"><b><i>Hello, </i></b></font><font color=\"red\">Component!</font>"
|
"expected_output":
|
||||||
|
"<span style=\"color: red; font-weight: bold; font-style: italic;\">Hello, <span style=\"font-weight: normal; font-style: normal;\">Component!</span></span>"
|
||||||
}
|
}
|
@ -9,5 +9,5 @@
|
|||||||
"strikethrough": true
|
"strikethrough": true
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"expected_output": "<font color=\"blue\"><b><i><u><s>Hello, Component!</s></u></i></b></font>"
|
"expected_output": "<span style=\"color: blue; font-weight: bold; font-style: italic;\"><s><u>Hello, Component!</u></s></span>"
|
||||||
}
|
}
|
@ -3,7 +3,7 @@
|
|||||||
{
|
{
|
||||||
"text": "Hello, Component!",
|
"text": "Hello, Component!",
|
||||||
"clickEvent": {
|
"clickEvent": {
|
||||||
"open_url": true,
|
"action": "open_url",
|
||||||
"value": "https://google.com"
|
"value": "https://google.com"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,8 @@
|
|||||||
"color": "#873600",
|
"color": "#873600",
|
||||||
"bold": true,
|
"bold": true,
|
||||||
"underlined": true,
|
"underlined": true,
|
||||||
"extra": {
|
"extra": [
|
||||||
|
{
|
||||||
"text": "jumped over ",
|
"text": "jumped over ",
|
||||||
"color": "blue",
|
"color": "blue",
|
||||||
"bold": false,
|
"bold": false,
|
||||||
@ -18,6 +19,7 @@
|
|||||||
"italic": true,
|
"italic": true,
|
||||||
"strikethrough": true
|
"strikethrough": true
|
||||||
}
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "the lazy dog's back. ",
|
"text": "the lazy dog's back. ",
|
||||||
@ -31,11 +33,13 @@
|
|||||||
"text": "1234567890 ",
|
"text": "1234567890 ",
|
||||||
"color": "black",
|
"color": "black",
|
||||||
"strikethrough": false,
|
"strikethrough": false,
|
||||||
"extra": "How vexingly quick daft zebras jump!"
|
"extra": [
|
||||||
|
"How vexingly quick daft zebras jump!"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"expected_output":
|
"expected_output":
|
||||||
"<font color=\"blue\"><i>The quick </i></font><font color=\"#873600\"><b><u>brown fox </u></b></font><font color=\"blue\"><i><s>jumped over </s></i></font><font color=\"green\"><b><i><u><s>the lazy dog's back. </s></u></i></b></font><font color=\"black\"><b><i><u>1234567890 </u></i></b></font><font color=\"black\"><b><i><u>How vexingly quick daft zebras jump!</u></i></b></font>"
|
"<span style=\"color: blue; font-style: italic;\">The quick </span><span style=\"color: #873600; font-weight: bold;\"><u>brown fox </u><span style=\"color: blue; font-weight: normal; font-style: italic;\"><s>jumped over </s></span></span><span style=\"color: green; font-weight: bold; font-style: italic;\"><s><u>the lazy dog's back. </u></s><span style=\"color: black;\"><u>1234567890 </u>How vexingly quick daft zebras jump!</span></span>"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user