Skip to content

Commit

Permalink
feat: expose actual props instead of mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Oct 14, 2023
1 parent 5a0875b commit 6fcee4c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
21 changes: 15 additions & 6 deletions addon/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ struct patchbay : public Napi::ObjectWrap<patchbay>

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)
{
Expand All @@ -49,14 +56,15 @@ struct patchbay : public Napi::ObjectWrap<patchbay>
{
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<std::string>(info[0].ToString());
auto mode = static_cast<std::string>(info[1].ToString());
auto key = static_cast<std::string>(info[0].ToString());
auto value = static_cast<std::string>(info[1].ToString());
auto mode = static_cast<std::string>(info[2].ToString());

if (mode != "include" && mode != "exclude")
{
Expand All @@ -67,7 +75,8 @@ struct patchbay : public Napi::ObjectWrap<patchbay>
}

vencord::patchbay::get().link({
target,
key,
value,
mode == "include" ? vencord::target_mode::include : vencord::target_mode::exclude,
});

Expand Down
10 changes: 8 additions & 2 deletions include/vencord/patchbay.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <set>
#include <map>
#include <memory>
#include <string>
#include <cstdint>
Expand All @@ -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<std::string, std::string>;

class patchbay
{
class impl;
Expand All @@ -32,7 +38,7 @@ namespace vencord
patchbay();

public:
std::set<std::string> list();
std::set<node> list();

public:
void link(target target);
Expand Down
2 changes: 1 addition & 1 deletion private/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ namespace vencord
struct target;

using pw_recipe = pw::recipe<list_nodes, target, unset_target, quit>;
using cr_recipe = cr::recipe<std::set<std::string>, ready>;
using cr_recipe = cr::recipe<std::set<node>, ready>;
} // namespace vencord
2 changes: 1 addition & 1 deletion server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template <>
struct glz::meta<vencord::target>
{
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)
Expand Down
4 changes: 2 additions & 2 deletions src/patchbay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace vencord

patchbay::patchbay() : m_impl(std::make_unique<impl>()) {}

std::set<std::string> patchbay::list()
std::set<node> patchbay::list()
{
m_impl->sender->send(list_nodes{});
return m_impl->receiver->recv_as<std::set<std::string>>();
return m_impl->receiver->recv_as<std::set<node>>();
}

void patchbay::link(target target)
Expand Down
29 changes: 19 additions & 10 deletions src/patchbay.impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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<std::string> 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<std::set>;

sender.send(rtn);
Expand Down

0 comments on commit 6fcee4c

Please sign in to comment.