Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/base/StarAssets.cpp5
-rw-r--r--source/core/StarAssetPath.cpp4
-rw-r--r--source/core/StarDirectives.cpp65
-rw-r--r--source/core/StarDirectives.hpp4
-rw-r--r--source/core/StarImageProcessing.cpp14
-rw-r--r--source/core/StarImageProcessing.hpp4
-rw-r--r--source/rendering/StarFontTextureGroup.cpp13
7 files changed, 50 insertions, 59 deletions
diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp
index f2582b1..18435e9 100644
--- a/source/base/StarAssets.cpp
+++ b/source/base/StarAssets.cpp
@@ -1160,7 +1160,10 @@ shared_ptr<Assets::AssetData> Assets::loadImage(AssetPath const& path) const {
Image newImage = *source->image;
path.directives.forEach([&](auto const& entry, Directives const&) {
if (auto error = entry.operation.template ptr<ErrorImageOperation>())
- std::rethrow_exception(error->exception);
+ if (auto string = error->cause.ptr<std::string>())
+ throw DirectivesException::format("ImageOperation parse error: {}", *string);
+ else
+ std::rethrow_exception(error->cause.get<std::exception_ptr>());
else
processImageOperation(entry.operation, newImage, [&](String const& ref) { return references.get(ref).get(); });
});
diff --git a/source/core/StarAssetPath.cpp b/source/core/StarAssetPath.cpp
index 23743d9..ddc1782 100644
--- a/source/core/StarAssetPath.cpp
+++ b/source/core/StarAssetPath.cpp
@@ -160,10 +160,10 @@ std::ostream& operator<<(std::ostream& os, AssetPath const& rhs) {
os << *rhs.subPath;
}
- rhs.directives.forEach([&](auto const& entry, Directives const& directives) {
+ rhs.directives.forEach([&](Directives::Entry const& entry, Directives const& directives) {
os << "?";
os << entry.string(*directives);
- });
+ });
return os;
}
diff --git a/source/core/StarDirectives.cpp b/source/core/StarDirectives.cpp
index b995b37..ea18bc9 100644
--- a/source/core/StarDirectives.cpp
+++ b/source/core/StarDirectives.cpp
@@ -26,8 +26,10 @@ Directives::Entry::Entry(Entry const& other) {
ImageOperation const& Directives::Entry::loadOperation(Shared const& parent) const {
if (operation.is<NullImageOperation>()) {
- try { operation = imageOperationFromString(string(parent)); }
- catch (StarException const& e) { operation = ErrorImageOperation{ std::current_exception() }; }
+ try
+ { operation = imageOperationFromString(string(parent)); }
+ catch (StarException const& e)
+ { operation = ErrorImageOperation{std::exception_ptr()}; }
}
return operation;
}
@@ -44,10 +46,9 @@ bool Directives::Shared::empty() const {
Directives::Shared::Shared() {}
-Directives::Shared::Shared(List<Entry>&& givenEntries, String&& givenString, StringView givenPrefix) {
+Directives::Shared::Shared(List<Entry>&& givenEntries, String&& givenString) {
entries = std::move(givenEntries);
string = std::move(givenString);
- prefix = givenPrefix;
hash = string.empty() ? 0 : XXH3_64bits(string.utf8Ptr(), string.utf8Size());
}
@@ -130,32 +131,25 @@ void Directives::parse(String&& directives) {
List<Entry> entries;
StringView view(directives);
- StringView prefix = "";
view.forEachSplitView("?", [&](StringView split, size_t beg, size_t end) {
if (!split.empty()) {
+ ImageOperation operation = NullImageOperation();
if (beg == 0) {
- try {
- ImageOperation operation = imageOperationFromString(split);
- entries.emplace_back(std::move(operation), beg, end);
- }
- catch (StarException const& e) {
- prefix = split;
- entries.emplace_back(ImageOperation(ErrorImageOperation{std::current_exception()}), beg, end);
- }
- }
- else {
- ImageOperation operation = NullImageOperation();
- entries.emplace_back(std::move(operation), beg, end);
+ try
+ { operation = imageOperationFromString(split); }
+ catch (StarException const& e)
+ { operation = ErrorImageOperation{std::exception_ptr()}; }
}
+ entries.emplace_back(std::move(operation), beg, end);
}
- });
+ });
- if (entries.empty() && !prefix.empty()) {
+ if (entries.empty()) {
m_shared.reset();
return;
}
- m_shared = std::make_shared<Shared const>(std::move(entries), std::move(directives), prefix);
+ m_shared = std::make_shared<Shared const>(std::move(entries), std::move(directives));
if (view.utf8().size() < 1000) { // Pre-load short enough directives
for (auto& entry : m_shared->entries)
entry.loadOperation(*m_shared);
@@ -169,13 +163,6 @@ String Directives::string() const {
return m_shared->string;
}
-StringView Directives::prefix() const {
- if (!m_shared)
- return "";
- else
- return m_shared->prefix;
-}
-
String const* Directives::stringPtr() const {
if (!m_shared)
return nullptr;
@@ -186,10 +173,10 @@ String const* Directives::stringPtr() const {
String Directives::buildString() const {
if (m_shared) {
- String built = m_shared->prefix;
-
+ String built;
for (auto& entry : m_shared->entries) {
- built += "?";
+ if (entry.begin > 0)
+ built += "?";
built += entry.string(*m_shared);
}
@@ -332,14 +319,11 @@ String DirectivesGroup::toString() const {
}
void DirectivesGroup::addToString(String& string) const {
- for (auto& directives : m_directives) {
- if (directives) {
- auto& dirString = directives->string;
- if (!dirString.empty()) {
- if (dirString.utf8().front() != '?')
- string += "?";
- string += dirString;
- }
+ for (auto& entry : m_directives) {
+ if (entry && !entry->string.empty()) {
+ if (!string.empty() && string.utf8().back() != '?' && entry->string.utf8()[0] != '?')
+ string.append('?');
+ string += entry->string;
}
}
}
@@ -376,7 +360,10 @@ void DirectivesGroup::applyExistingImage(Image& image) const {
forEach([&](auto const& entry, Directives const& directives) {
ImageOperation const& operation = entry.loadOperation(*directives);
if (auto error = operation.ptr<ErrorImageOperation>())
- std::rethrow_exception(error->exception);
+ if (auto string = error->cause.ptr<std::string>())
+ throw DirectivesException::format("ImageOperation parse error: {}", *string);
+ else
+ std::rethrow_exception(error->cause.get<std::exception_ptr>());
else
processImageOperation(operation, image);
});
diff --git a/source/core/StarDirectives.hpp b/source/core/StarDirectives.hpp
index fa10a33..e6b0a6e 100644
--- a/source/core/StarDirectives.hpp
+++ b/source/core/StarDirectives.hpp
@@ -31,13 +31,12 @@ public:
struct Shared {
List<Entry> entries;
String string;
- StringView prefix;
size_t hash = 0;
mutable Mutex mutex;
bool empty() const;
Shared();
- Shared(List<Entry>&& givenEntries, String&& givenString, StringView givenPrefix);
+ Shared(List<Entry>&& givenEntries, String&& givenString);
};
Directives();
@@ -57,7 +56,6 @@ public:
void loadOperations() const;
void parse(String&& directives);
String string() const;
- StringView prefix() const;
String const* stringPtr() const;
String buildString() const;
String& addToString(String& out) const;
diff --git a/source/core/StarImageProcessing.cpp b/source/core/StarImageProcessing.cpp
index 51cee0f..5ed8690 100644
--- a/source/core/StarImageProcessing.cpp
+++ b/source/core/StarImageProcessing.cpp
@@ -199,13 +199,11 @@ ImageOperation imageOperationFromString(StringView string) {
hexDecode(hexPtr, 8, c, 4);
}
else if (!which || (ptr != end && ++ptr != end))
- throw ImageOperationException(strf("Improper size for hex string '{}' in imageOperationFromString", StringView(hexPtr, hexLen)), false);
- else // we're in A of A=B. In vanilla only A=B pairs are evaluated, so only throw an exception if B is also there.
+ return ErrorImageOperation{strf("Improper size for hex string '{}'", StringView(hexPtr, hexLen))};
+ else // we're in A of A=B. In vanilla only A=B pairs are evaluated, so only throw an error if B is also there.
return operation;
-
- which = !which;
- if (which)
+ if (which = !which)
operation.colorReplaceMap[*(Vec4B*)&a] = *(Vec4B*)&b;
hexLen = 0;
@@ -341,12 +339,12 @@ ImageOperation imageOperationFromString(StringView string) {
return FlipImageOperation{FlipImageOperation::FlipXY};
} else {
- throw ImageOperationException(strf("Could not recognize ImageOperation type {}", type), false);
+ return NullImageOperation();
}
} catch (OutOfRangeException const& e) {
- throw ImageOperationException("Error reading ImageOperation", e);
+ return ErrorImageOperation{std::exception_ptr()};
} catch (BadLexicalCast const& e) {
- throw ImageOperationException("Error reading ImageOperation", e);
+ return ErrorImageOperation{std::exception_ptr()};
}
}
diff --git a/source/core/StarImageProcessing.hpp b/source/core/StarImageProcessing.hpp
index 71ce6fc..8316d18 100644
--- a/source/core/StarImageProcessing.hpp
+++ b/source/core/StarImageProcessing.hpp
@@ -18,11 +18,11 @@ StringList colorDirectivesFromConfig(JsonArray const& directives);
String paletteSwapDirectivesFromConfig(Json const& swaps);
struct NullImageOperation {
-
+ bool unloaded = false;
};
struct ErrorImageOperation {
- std::exception_ptr exception;
+ Variant<std::string, std::exception_ptr> cause;
};
struct HueShiftImageOperation {
diff --git a/source/rendering/StarFontTextureGroup.cpp b/source/rendering/StarFontTextureGroup.cpp
index cdc667a..60f66f6 100644
--- a/source/rendering/StarFontTextureGroup.cpp
+++ b/source/rendering/StarFontTextureGroup.cpp
@@ -64,12 +64,17 @@ const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Cha
for (auto& entry : directives->entries) {
if (auto error = entry.operation.ptr<ErrorImageOperation>()) {
- if (error->exception) {
+ if (auto string = error->cause.ptr<std::string>()) {
+ if (!string->empty()) {
+ Logger::error("Error parsing font directives: {}", *string);
+ string->clear();
+ }
+ } else if (auto& exception = error->cause.get<std::exception_ptr>()) {
try
- { std::rethrow_exception(error->exception); }
+ { std::rethrow_exception(error->cause.get<std::exception_ptr>()); }
catch (std::exception const& e)
- { Logger::error("Exception parsing font directives: {}", e.what()); }
- error->exception = {};
+ { Logger::error("Exception parsing font directives: {}", e.what()); };
+ exception = {};
}
image.forEachPixel([](unsigned x, unsigned y, Vec4B& pixel) {
pixel = ((x + y) % 2 == 0) ? Vec4B(255, 0, 255, pixel[3]) : Vec4B(0, 0, 0, pixel[3]);