switch to qdcss for parsing
make it not horrible to look at Signed-off-by: kumquat-ir <66188216+kumquat-ir@users.noreply.github.com>
This commit is contained in:
parent
7896dd19c1
commit
c07fff7503
@ -448,6 +448,7 @@ if (NOT ghc_filesystem_FOUND)
|
|||||||
else()
|
else()
|
||||||
message(STATUS "Using system ghc_filesystem")
|
message(STATUS "Using system ghc_filesystem")
|
||||||
endif()
|
endif()
|
||||||
|
add_subdirectory(libraries/qdcss) # css parser
|
||||||
|
|
||||||
############################### Built Artifacts ###############################
|
############################### Built Artifacts ###############################
|
||||||
|
|
||||||
|
@ -1025,6 +1025,7 @@ target_link_libraries(Launcher_logic
|
|||||||
nbt++
|
nbt++
|
||||||
${ZLIB_LIBRARIES}
|
${ZLIB_LIBRARIES}
|
||||||
tomlplusplus::tomlplusplus
|
tomlplusplus::tomlplusplus
|
||||||
|
qdcss
|
||||||
BuildConfig
|
BuildConfig
|
||||||
Katabasis
|
Katabasis
|
||||||
Qt${QT_VERSION_MAJOR}::Widgets
|
Qt${QT_VERSION_MAJOR}::Widgets
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <quazip/quazip.h>
|
#include <quazip/quazip.h>
|
||||||
#include <quazip/quazipfile.h>
|
#include <quazip/quazipfile.h>
|
||||||
#include <toml++/toml.h>
|
#include <toml++/toml.h>
|
||||||
|
#include <qdcss.h>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
@ -290,37 +291,21 @@ ModDetails ReadNilModInfo(QByteArray contents, QString fname)
|
|||||||
{
|
{
|
||||||
ModDetails details;
|
ModDetails details;
|
||||||
|
|
||||||
// this is a css file (why) but we only care about a couple key/value pairs from it
|
QDCSS cssData = QDCSS(contents);
|
||||||
// hence this instead of a css parser lib
|
auto name = cssData.get("@nilmod.name");
|
||||||
// could be made a lot better but it works(tm)
|
auto desc = cssData.get("@nilmod.description");
|
||||||
// (does css require properties to be on their own lines? if so, the code can get less horrible looking)
|
auto authors = cssData.get("@nilmod.authors");
|
||||||
QString contentStr = QString(contents).trimmed();
|
|
||||||
int firstidx = contentStr.indexOf("@nilmod");
|
|
||||||
firstidx = contentStr.indexOf("{", firstidx);
|
|
||||||
int lastidx = contentStr.indexOf("}", firstidx);
|
|
||||||
int nameidx = contentStr.indexOf("name:", firstidx);
|
|
||||||
int descidx = contentStr.indexOf("description:", firstidx);
|
|
||||||
int authorsidx = contentStr.indexOf("authors:", firstidx);
|
|
||||||
int versionidx = contentStr.indexOf("version:", firstidx);
|
|
||||||
|
|
||||||
if (nameidx != -1 && nameidx < lastidx) {
|
if (name->has_value()) {
|
||||||
nameidx = contentStr.indexOf('"', nameidx);
|
details.name = name->value();
|
||||||
details.name = contentStr.mid(nameidx + 1, contentStr.indexOf('"', nameidx + 1) - nameidx - 1);
|
|
||||||
}
|
}
|
||||||
if (descidx != -1 && descidx < lastidx) {
|
if (desc->has_value()) {
|
||||||
descidx = contentStr.indexOf('"', descidx);
|
details.description = desc->value();
|
||||||
details.description = contentStr.mid(descidx + 1, contentStr.indexOf('"', descidx + 1) - descidx - 1);
|
|
||||||
}
|
}
|
||||||
if (authorsidx != -1 && authorsidx < lastidx) {
|
if (authors->has_value()) {
|
||||||
authorsidx = contentStr.indexOf('"', authorsidx);
|
details.authors.append(authors->value());
|
||||||
details.authors.append(contentStr.mid(authorsidx + 1, contentStr.indexOf('"', authorsidx + 1) - authorsidx - 1));
|
|
||||||
}
|
|
||||||
if (versionidx != -1 && versionidx < lastidx) {
|
|
||||||
versionidx = contentStr.indexOf('"', versionidx);
|
|
||||||
details.version = contentStr.mid(versionidx + 1, contentStr.indexOf('"', versionidx + 1) - versionidx - 1);
|
|
||||||
} else {
|
|
||||||
details.version = "?";
|
|
||||||
}
|
}
|
||||||
|
details.version = cssData.get("@nilmod.version")->value_or("?");
|
||||||
|
|
||||||
details.mod_id = fname.remove(".nilmod.css");
|
details.mod_id = fname.remove(".nilmod.css");
|
||||||
|
|
||||||
|
@ -140,3 +140,11 @@ A TOML language parser. Used by Forge 1.14+ to store mod metadata.
|
|||||||
See [github repo](https://github.com/marzer/tomlplusplus).
|
See [github repo](https://github.com/marzer/tomlplusplus).
|
||||||
|
|
||||||
Licenced under the MIT licence.
|
Licenced under the MIT licence.
|
||||||
|
|
||||||
|
## qdcss
|
||||||
|
|
||||||
|
A quick and dirty css parser, used by NilLoader to store mod metadata.
|
||||||
|
|
||||||
|
Translated (and heavily trimmed down) from [the original Java code](https://github.com/unascribed/NilLoader/blob/trunk/src/main/java/nilloader/api/lib/qdcss/QDCSS.java) from NilLoader
|
||||||
|
|
||||||
|
Licensed under LGPL version 3.
|
||||||
|
18
libraries/qdcss/CMakeLists.txt
Normal file
18
libraries/qdcss/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.9.4)
|
||||||
|
project(qdcss)
|
||||||
|
|
||||||
|
if(QT_VERSION_MAJOR EQUAL 5)
|
||||||
|
find_package(Qt5 COMPONENTS Core REQUIRED)
|
||||||
|
elseif(Launcher_QT_VERSION_MAJOR EQUAL 6)
|
||||||
|
find_package(Qt6 COMPONENTS Core Core5Compat REQUIRED)
|
||||||
|
list(APPEND qdcss_LIBS Qt${QT_VERSION_MAJOR}::Core5Compat)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(QDCSS_SOURCES
|
||||||
|
include/qdcss.h
|
||||||
|
src/qdcss.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(qdcss STATIC ${QDCSS_SOURCES})
|
||||||
|
target_include_directories(qdcss PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
target_link_libraries(qdcss Qt${QT_VERSION_MAJOR}::Core ${qdcss_LIBS})
|
21
libraries/qdcss/include/qdcss.h
Normal file
21
libraries/qdcss/include/qdcss.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef QDCSS_H
|
||||||
|
#define QDCSS_H
|
||||||
|
|
||||||
|
#include <QMap>
|
||||||
|
#include <QString>
|
||||||
|
#include <QStringList>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
class QDCSS {
|
||||||
|
// these are all we need to parse a couple string values out of a css string
|
||||||
|
// lots more in the original code, yet to be ported
|
||||||
|
// https://github.com/unascribed/NilLoader/blob/trunk/src/main/java/nilloader/api/lib/qdcss/QDCSS.java
|
||||||
|
public:
|
||||||
|
QDCSS(QString);
|
||||||
|
std::optional<QString>* get(QString);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QMap<QString, QStringList> m_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // QDCSS_H
|
49
libraries/qdcss/src/qdcss.cpp
Normal file
49
libraries/qdcss/src/qdcss.cpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#include "qdcss.h"
|
||||||
|
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QRegularExpressionMatch>
|
||||||
|
#include <QRegularExpressionMatchIterator>
|
||||||
|
|
||||||
|
QRegularExpression ruleset_re = QRegularExpression(R"([#.]?(@?\w+?)\s*\{(.*?)\})", QRegularExpression::DotMatchesEverythingOption);
|
||||||
|
QRegularExpression rule_re = QRegularExpression(R"((\S+?)\s*:\s*(?:\"(.*?)(?<!\\)\"|'(.*?)(?<!\\)'|(\S+?))\s*(?:;|$))");
|
||||||
|
|
||||||
|
QDCSS::QDCSS(QString s)
|
||||||
|
{
|
||||||
|
// not much error handling over here...
|
||||||
|
// the original java code used indeces returned by the matcher for them, but QRE does not expose those
|
||||||
|
QRegularExpressionMatchIterator ruleset_i = ruleset_re.globalMatch(s);
|
||||||
|
while (ruleset_i.hasNext()) {
|
||||||
|
QRegularExpressionMatch ruleset = ruleset_i.next();
|
||||||
|
QString selector = ruleset.captured(1);
|
||||||
|
QString rules = ruleset.captured(2);
|
||||||
|
QRegularExpressionMatchIterator rule_i = rule_re.globalMatch(rules);
|
||||||
|
while (rule_i.hasNext()) {
|
||||||
|
QRegularExpressionMatch rule = rule_i.next();
|
||||||
|
QString property = rule.captured(1);
|
||||||
|
QString value;
|
||||||
|
if (!rule.captured(2).isNull()) {
|
||||||
|
value = rule.captured(2);
|
||||||
|
} else if (!rule.captured(3).isNull()) {
|
||||||
|
value = rule.captured(3);
|
||||||
|
} else {
|
||||||
|
value = rule.captured(4);
|
||||||
|
}
|
||||||
|
QString key = selector + "." + property;
|
||||||
|
if (!m_data.contains(key)) {
|
||||||
|
m_data.insert(key, QStringList());
|
||||||
|
}
|
||||||
|
m_data.find(key)->append(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<QString>* QDCSS::get(QString key)
|
||||||
|
{
|
||||||
|
auto found = m_data.find(key);
|
||||||
|
|
||||||
|
if (found == m_data.end() || found->empty()) {
|
||||||
|
return new std::optional<QString>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new std::optional<QString>(found->back());
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user