From 6fcee4c7eccb3a58314d7fd7e20c77ca46cec0e5 Mon Sep 17 00:00:00 2001 From: Curve Date: Sat, 14 Oct 2023 12:53:11 +0200 Subject: [PATCH] feat: expose actual props instead of mapping --- addon/addon.cpp | 21 +++++++++++++++------ include/vencord/patchbay.hpp | 10 ++++++++-- private/message.hpp | 2 +- server/main.cpp | 2 +- src/patchbay.cpp | 4 ++-- src/patchbay.impl.cpp | 29 +++++++++++++++++++---------- 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/addon/addon.cpp b/addon/addon.cpp index 09bacb3..8a38b5b 100644 --- a/addon/addon.cpp +++ b/addon/addon.cpp @@ -30,7 +30,14 @@ struct patchbay : public Napi::ObjectWrap auto convert = [&](const auto &item) { - return Napi::String::New(env, item); + auto rtn = Napi::Object::New(env); + + for (const auto &[key, value] : item) + { + rtn.Set(key, Napi::String::New(env, value)); + } + + return rtn; }; auto add = [&](const auto &item) { @@ -49,14 +56,15 @@ struct patchbay : public Napi::ObjectWrap { auto env = info.Env(); - if (info.Length() != 2 || !info[0].IsString() || !info[1].IsString()) + if (info.Length() != 3 || !info[0].IsString() || !info[1].IsString() || !info[2].IsString()) { - Napi::Error::New(env, "[venmic] expected two string arguments").ThrowAsJavaScriptException(); + Napi::Error::New(env, "[venmic] expected three string arguments").ThrowAsJavaScriptException(); return Napi::Boolean::New(env, false); } - auto target = static_cast(info[0].ToString()); - auto mode = static_cast(info[1].ToString()); + auto key = static_cast(info[0].ToString()); + auto value = static_cast(info[1].ToString()); + auto mode = static_cast(info[2].ToString()); if (mode != "include" && mode != "exclude") { @@ -67,7 +75,8 @@ struct patchbay : public Napi::ObjectWrap } vencord::patchbay::get().link({ - target, + key, + value, mode == "include" ? vencord::target_mode::include : vencord::target_mode::exclude, }); diff --git a/include/vencord/patchbay.hpp b/include/vencord/patchbay.hpp index d90e167..603ed87 100644 --- a/include/vencord/patchbay.hpp +++ b/include/vencord/patchbay.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -14,10 +15,15 @@ namespace vencord struct target { - std::string name; + std::string key; + std::string value; + + public: target_mode mode; }; + using node = std::map; + class patchbay { class impl; @@ -32,7 +38,7 @@ namespace vencord patchbay(); public: - std::set list(); + std::set list(); public: void link(target target); diff --git a/private/message.hpp b/private/message.hpp index a76ac56..fc5be51 100644 --- a/private/message.hpp +++ b/private/message.hpp @@ -30,5 +30,5 @@ namespace vencord struct target; using pw_recipe = pw::recipe; - using cr_recipe = cr::recipe, ready>; + using cr_recipe = cr::recipe, ready>; } // namespace vencord diff --git a/server/main.cpp b/server/main.cpp index 5bc6fb1..142099f 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -16,7 +16,7 @@ template <> struct glz::meta { using T = vencord::target; - static constexpr auto value = object("name", &T::name, "mode", &T::mode); + static constexpr auto value = object("key", &T::key, "value", &T::value, "mode", &T::mode); }; int main(int argc, char **args) diff --git a/src/patchbay.cpp b/src/patchbay.cpp index 6dfa36c..79fc6ba 100644 --- a/src/patchbay.cpp +++ b/src/patchbay.cpp @@ -6,10 +6,10 @@ namespace vencord patchbay::patchbay() : m_impl(std::make_unique()) {} - std::set patchbay::list() + std::set patchbay::list() { m_impl->sender->send(list_nodes{}); - return m_impl->receiver->recv_as>(); + return m_impl->receiver->recv_as>(); } void patchbay::link(target target) diff --git a/src/patchbay.impl.cpp b/src/patchbay.impl.cpp index b01c4af..b961c8a 100644 --- a/src/patchbay.impl.cpp +++ b/src/patchbay.impl.cpp @@ -249,7 +249,7 @@ namespace vencord auto &output = nodes[info.output.node]; // "Output" = the node that is emitting sound - if (output.info.props["node.name"] == target->name) + if (output.info.props[target->key] == target->value) { return; } @@ -271,7 +271,7 @@ namespace vencord return; } - if (nodes[parent].info.props["node.name"] != target->name) + if (nodes[parent].info.props[target->key] != target->value) { return; } @@ -289,23 +289,32 @@ namespace vencord template <> void patchbay::impl::receive(cr_recipe::sender sender, [[maybe_unused]] list_nodes) { - auto has_name = [&](auto &item) + static std::set desired_props{"application.process.binary", "application.process.id", "node.name"}; + + auto desireable = [&](auto &item) { - return item.second.info.props.contains("application.name") && item.second.info.props.contains("node.name"); + return ranges::all_of(desired_props, [&](const auto &key) { return item.second.info.props.contains(key); }); }; auto can_output = [](const auto &item) { return item.second.info.output.max > 0; }; - auto get_name = [](auto &item) + auto to_node = [](auto &item) { - return item.second.info.props["node.name"]; + node rtn; + + for (const auto &key : desired_props) + { + rtn[key] = item.second.info.props[key]; + } + + return rtn; }; - auto rtn = nodes // - | ranges::views::filter(has_name) // - | ranges::views::filter(can_output) // - | ranges::views::transform(get_name) // + auto rtn = nodes // + | ranges::views::filter(desireable) // + | ranges::views::filter(can_output) // + | ranges::views::transform(to_node) // | ranges::to; sender.send(rtn);