From f1e3f6791d18f82e662a54ea407b5e5495e18a30 Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Wed, 6 Mar 2024 12:40:38 -0500 Subject: [PATCH 1/6] Multiple patch files --- source/base/StarAssets.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 2c3d04b..422ea22 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -86,10 +86,21 @@ Assets::Assets(Settings settings, StringList assetSources) { m_assetSourcePaths.add(sourcePath, source); for (auto const& filename : source->assetPaths()) { - if (filename.endsWith(AssetsPatchSuffix, String::CaseInsensitive)) { - auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix)); - if (auto p = m_files.ptr(targetPatchFile)) - p->patchSources.append({filename, source}); + if (filename.contains(AssetsPatchSuffix, String::CaseInsensitive)) { + if (filename.endsWith(AssetsPatchSuffix, String::CaseInsensitive)) { + auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix)); + if (auto p = m_files.ptr(targetPatchFile)) + p->patchSources.append({filename, source}); + } else { + for (int i = 0; i < 10; i++) { + if (filename.endsWith(AssetsPatchSuffix + toString(i), String::CaseInsensitive)) { + auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix) + 1); + if (auto p = m_files.ptr(targetPatchFile)) + p->patchSources.append({filename, source}); + break; + } + } + } } auto& descriptor = m_files[filename]; descriptor.sourceName = filename; From 14ec64ace75c83238ee9f81fe4ea4c3c9eed09fb Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 09:43:36 -0500 Subject: [PATCH 2/6] Added `jsonCompare` function --- source/core/StarJson.cpp | 30 ++++++++++++++++++++++++++++++ source/core/StarJson.hpp | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/source/core/StarJson.cpp b/source/core/StarJson.cpp index 3feaf37..ed0e48f 100644 --- a/source/core/StarJson.cpp +++ b/source/core/StarJson.cpp @@ -1035,4 +1035,34 @@ Json jsonMerge(Json const& base, Json const& merger) { } } +bool jsonCompare(Json const& base, Json const& compare) { + if (base == compare) { + return true; + } else { + if (base.type() == Json::Type::Object && compare.type() == Json::Type::Object) { + for (auto const& c : compare.toObject()) { + if (!base.contains(c.first) || !jsonCompare(base.get(c.first), c.second)) + return false; + } + return true; + } + if (base.type() == Json::Type::Array && compare.type() == Json::Type::Array) { + for (auto const& c : compare.toArray()) { + bool similar = false; + for (auto const& b : base.toArray()) { + if (jsonCompare(c, b)) { + similar = true; + break; + } + } + if (!similar) + return false; + } + return true; + } + + return false; + } +} + } \ No newline at end of file diff --git a/source/core/StarJson.hpp b/source/core/StarJson.hpp index 4aa6f7b..51e0be7 100644 --- a/source/core/StarJson.hpp +++ b/source/core/StarJson.hpp @@ -355,6 +355,22 @@ Json jsonMergeQueryDef(String const& key, Json def, Json const& first, T const&. return def; } +// Compares the two given json values and returns a boolean, by the following +// rules (applied in order): If both values are identical, return true. If both +// values are not equal, check if they are objects. If they are objects, +// iterate through every pair in the comparing object and check if the key is +// in the base object. If the key is in the base object, then jsonCompare is +// called recursively on both values. If the base object does not contain the +// key, or the recursion fails, return false. Otherwise, return true. If they +// are not objects, check if they are arrays. If they are arrays, iterate +// through every value in the comparing object and then recursively call +// jsonCompare on every value in the base object until a match is found. If a +// match is found, break and move on to the next value in the comparing array. +// If a match is found for every value in the comparing array, return true. +// Otherwise, return false. If both values are not identical, and are not +// objects or arrays, return false. +bool jsonCompare(Json const& base, Json const& compare); + } template <> struct fmt::formatter : ostream_formatter {}; From c808d207c91e577d12907ea9723d500849e0d61f Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 11:01:29 -0500 Subject: [PATCH 3/6] Added `search` operator to the `remove` operation --- source/core/StarJsonPatch.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp index 34c6a82..9ef11ff 100644 --- a/source/core/StarJsonPatch.cpp +++ b/source/core/StarJsonPatch.cpp @@ -66,7 +66,30 @@ namespace JsonPatching { } Json applyRemoveOperation(Json const& base, Json const& op) { - return JsonPath::Pointer(op.getString("path")).remove(base); + if (op.contains("search")) { + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(path); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + searchArray = searchArray.eraseIndex(index); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + return JsonPath::Pointer(op.getString("path")).remove(base); + } } Json applyAddOperation(Json const& base, Json const& op) { From 9b4ca69e0e75e08c4b0587dad931654c9ac9ee2e Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 12:31:48 -0500 Subject: [PATCH 4/6] Added the `search` operand to the `find`, `replace`, and `remove` operations --- source/core/StarJsonPatch.cpp | 99 ++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 14 deletions(-) diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp index 9ef11ff..6ed425e 100644 --- a/source/core/StarJsonPatch.cpp +++ b/source/core/StarJsonPatch.cpp @@ -40,24 +40,49 @@ namespace JsonPatching { Json applyTestOperation(Json const& base, Json const& op) { auto path = op.getString("path"); - auto value = op.opt("value"); auto inverseTest = op.getBool("inverse", false); auto pointer = JsonPath::Pointer(path); try { - auto testValue = pointer.get(base); - if (!value) { - if (inverseTest) - throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", op.getString("path"))); - return base; - } + if (op.contains("search")) { + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + bool found = false; + for(auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + } + if (found) { + if (inverseTest) + throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", searchValue)); + return base; + } else { + if (!inverseTest) + throw JsonPatchTestFail(strf("Test operation failure, could not find {}.", searchValue)); + return base; + } + } else { + throw JsonPatchException(strf("Search operation failure, value at '{}' is not an array.", path)); + } + } else { + auto value = op.opt("value"); + auto testValue = pointer.get(base); + if (!value) { + if (inverseTest) + throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", op.getString("path"))); + return base; + } - if ((value && (testValue == *value)) ^ inverseTest) { - return base; - } + if ((value && (testValue == *value)) ^ inverseTest) { + return base; + } - throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue)); + throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue)); + } } catch (JsonPath::TraversalException& e) { if (inverseTest) return base; @@ -93,12 +118,58 @@ namespace JsonPatching { } Json applyAddOperation(Json const& base, Json const& op) { - return JsonPath::Pointer(op.getString("path")).add(base, op.get("value")); + if (op.contains("search")) { + Json value = op.get("value"); + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(path); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + bool found = false; + for (auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + } + if (found) + searchArray = searchArray.append(value); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + return JsonPath::Pointer(op.getString("path")).add(base, op.get("value")); + } } Json applyReplaceOperation(Json const& base, Json const& op) { - auto pointer = JsonPath::Pointer(op.getString("path")); - return pointer.add(pointer.remove(base), op.get("value")); + if (op.contains("search")) { + Json value = op.get("value"); + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(path); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + searchArray = searchArray.set(index, value); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + auto pointer = JsonPath::Pointer(op.getString("path")); + return pointer.add(pointer.remove(base), op.get("value")); + } } Json applyMoveOperation(Json const& base, Json const& op) { From 15a12c06a66adca4c953be8af6fc434cb6d2b156 Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 17:28:34 -0500 Subject: [PATCH 5/6] Added the `search` operand to the `move` and `copy` operations, and added the new `merge` operation --- source/core/StarJsonPatch.cpp | 112 +++++++++++++++++++++++++++++----- source/core/StarJsonPatch.hpp | 5 +- 2 files changed, 100 insertions(+), 17 deletions(-) diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp index 6ed425e..f5e7877 100644 --- a/source/core/StarJsonPatch.cpp +++ b/source/core/StarJsonPatch.cpp @@ -25,6 +25,7 @@ namespace JsonPatching { {"replace", std::bind(applyReplaceOperation, _1, _2)}, {"move", std::bind(applyMoveOperation, _1, _2)}, {"copy", std::bind(applyCopyOperation, _1, _2)}, + {"merge", std::bind(applyMergeOperation, _1, _2)}, }; Json applyOperation(Json const& base, Json const& op) { @@ -50,8 +51,8 @@ namespace JsonPatching { Json searchValue = op.get("search"); if (searchArray.type() == Json::Type::Array) { bool found = false; - for(auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for(auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -99,8 +100,8 @@ namespace JsonPatching { if (searchArray.type() == Json::Type::Array) { size_t index = 0; bool found = false; - for (auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -126,8 +127,8 @@ namespace JsonPatching { Json searchValue = op.get("search"); if (searchArray.type() == Json::Type::Array) { bool found = false; - for (auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -144,17 +145,18 @@ namespace JsonPatching { } Json applyReplaceOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(op.getString("path")); + if (op.contains("search")) { Json value = op.get("value"); - String path = op.getString("path"); - auto pointer = JsonPath::Pointer(path); Json searchArray = pointer.get(base); Json searchValue = op.get("search"); if (searchArray.type() == Json::Type::Array) { size_t index = 0; bool found = false; - for (auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -167,24 +169,102 @@ namespace JsonPatching { throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); } } else { - auto pointer = JsonPath::Pointer(op.getString("path")); return pointer.add(pointer.remove(base), op.get("value")); } } Json applyMoveOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto toPointer = JsonPath::Pointer(path); auto fromPointer = JsonPath::Pointer(op.getString("from")); - auto toPointer = JsonPath::Pointer(op.getString("path")); - Json value = fromPointer.get(base); - return toPointer.add(fromPointer.remove(base), value); + if (op.contains("search")) { + Json value = op.get("value"); + Json searchArray = fromPointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { + found = true; + break; + } + index++; + } + if (found) { + toPointer.add(toPointer.remove(base), searchArray.get(index)); + searchArray = searchArray.eraseIndex(index); + fromPointer.add(fromPointer.remove(base), searchArray); + } + return toPointer.get(base); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + Json value = fromPointer.get(base); + return toPointer.add(fromPointer.remove(base), value); + } } Json applyCopyOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto toPointer = JsonPath::Pointer(path); auto fromPointer = JsonPath::Pointer(op.getString("from")); - auto toPointer = JsonPath::Pointer(op.getString("path")); - return toPointer.add(base, fromPointer.get(base)); + if (op.contains("search")) { + Json value = op.get("value"); + Json searchArray = fromPointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + toPointer.add(base, searchArray.get(index)); + return toPointer.get(base); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + Json value = fromPointer.get(base); + return toPointer.add(base, fromPointer.get(base)); + } + } + + Json applyMergeOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(op.getString("path")); + + if (op.contains("search")) { + Json value = op.get("value"); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + searchArray = searchArray.set(index, jsonMerge(searchArray.get(index), op.get("value"))); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + return pointer.add(pointer.remove(base), jsonMerge(pointer.get(base), op.get("value"))); + } } } diff --git a/source/core/StarJsonPatch.hpp b/source/core/StarJsonPatch.hpp index bd331c4..44bf482 100644 --- a/source/core/StarJsonPatch.hpp +++ b/source/core/StarJsonPatch.hpp @@ -33,6 +33,9 @@ namespace JsonPatching { // Copies "from" to "path" Json applyCopyOperation(Json const& base, Json const& op); -} + + // Merges "value" at "path" + Json applyMergeOperation(Json const& base, Json const& op); + } } From 53c102b0c72d47d88c0f73e9fc50e5c1099b5f41 Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Fri, 8 Mar 2024 11:39:39 -0500 Subject: [PATCH 6/6] Added searchbar to songbook --- .../windowconfig/songbook.config.patch | 23 +++++++++ source/frontend/StarSongbookInterface.cpp | 49 +++++++++++++++---- source/frontend/StarSongbookInterface.hpp | 4 ++ 3 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 assets/opensb/interface/windowconfig/songbook.config.patch diff --git a/assets/opensb/interface/windowconfig/songbook.config.patch b/assets/opensb/interface/windowconfig/songbook.config.patch new file mode 100644 index 0000000..36991a5 --- /dev/null +++ b/assets/opensb/interface/windowconfig/songbook.config.patch @@ -0,0 +1,23 @@ +{ + "paneLayout" : { + "group" : { + "position" : [8, 71] + }, + "search" : { + "type" : "textbox", + "position" : [86, 71], + "hint" : "Search", + "maxWidth" : 50 + }, + + "lblBandInput" : { + "position" : [3, 68] + }, + "lblSearchInput" : { + "type" : "image", + "file" : "/interface/songbook/band.png", + "position" : [81, 68], + "zlevel" : -3 + } + } +} \ No newline at end of file diff --git a/source/frontend/StarSongbookInterface.cpp b/source/frontend/StarSongbookInterface.cpp index 8ccb437..cc6a17d 100644 --- a/source/frontend/StarSongbookInterface.cpp +++ b/source/frontend/StarSongbookInterface.cpp @@ -23,21 +23,52 @@ SongbookInterface::SongbookInterface(PlayerPtr player) { dismiss(); }); reader.registerCallback("group", [=](Widget*) {}); + reader.registerCallback("search", [=](Widget*) {}); reader.construct(assets->json("/interface/windowconfig/songbook.config:paneLayout"), this); auto songList = fetchChild("songs.list"); + auto search = fetchChild("search")->getText(); - StringList files = assets->scan(".abc"); - sort(files, [](String const& a, String const& b) -> bool { return b.compare(a, String::CaseInsensitive) > 0; }); - for (auto s : files) { + if (m_searchValue != search) + m_searchValue = search; + + m_files = assets->scan(".abc"); + sort(m_files, [](String const& a, String const& b) -> bool { return b.compare(a, String::CaseInsensitive) > 0; }); + for (auto s : m_files) { auto song = s.substr(7, s.length() - (7 + 4)); - auto widget = songList->addItem(); - widget->setData(s); - auto songName = widget->fetchChild("songName"); - songName->setText(song); + if (song.contains(m_searchValue, String::CaseInsensitive)) { + auto widget = songList->addItem(); + widget->setData(s); + auto songName = widget->fetchChild("songName"); + songName->setText(song); - widget->show(); + widget->show(); + } + } +} + +void SongbookInterface::update(float dt) { + Pane::update(dt); + + auto search = fetchChild("search")->getText(); + if (m_searchValue != search) { + m_searchValue = search; + + auto songList = fetchChild("songs.list"); + songList->clear(); + + for (auto s : m_files) { + auto song = s.substr(7, s.length() - (7 + 4)); + if (song.contains(m_searchValue, String::CaseInsensitive)) { + auto widget = songList->addItem(); + widget->setData(s); + auto songName = widget->fetchChild("songName"); + songName->setText(song); + + widget->show(); + } + } } } @@ -58,4 +89,4 @@ bool SongbookInterface::play() { return true; } -} +} \ No newline at end of file diff --git a/source/frontend/StarSongbookInterface.hpp b/source/frontend/StarSongbookInterface.hpp index 47622a4..f8a6cb2 100644 --- a/source/frontend/StarSongbookInterface.hpp +++ b/source/frontend/StarSongbookInterface.hpp @@ -13,8 +13,12 @@ class SongbookInterface : public Pane { public: SongbookInterface(PlayerPtr player); + void update(float dt) override; + private: PlayerPtr m_player; + StringList m_files; + String m_searchValue; bool play(); };