Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/LegacyRemoteCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
#include "ll/api/mod/RegisterHelper.h"

namespace RemoteCall {
extern void removeAllFunc();
}
void removeAllFunc();
} // namespace RemoteCall
namespace legacy_remote_call_api {

LegacyRemoteCallAPI& LegacyRemoteCallAPI::getInstance() {
static LegacyRemoteCallAPI instance;
return instance;
}

bool LegacyRemoteCallAPI::load() { return true; }
bool LegacyRemoteCallAPI::load() /*NOLINT*/ { return true; }

bool LegacyRemoteCallAPI::enable() { return true; }
bool LegacyRemoteCallAPI::enable() /*NOLINT*/ { return true; }

bool LegacyRemoteCallAPI::disable() {
bool LegacyRemoteCallAPI::disable() /*NOLINT*/ {
RemoteCall::removeAllFunc();
return true;
}

} // namespace legacy_remotecallapi
} // namespace legacy_remote_call_api

LL_REGISTER_MOD(legacy_remote_call_api::LegacyRemoteCallAPI, legacy_remote_call_api::LegacyRemoteCallAPI::getInstance());
LL_REGISTER_MOD(
legacy_remote_call_api::LegacyRemoteCallAPI,
legacy_remote_call_api::LegacyRemoteCallAPI::getInstance()
);
112 changes: 26 additions & 86 deletions src/RemoteCallAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include "RemoteCallAPI.h"

#include "LegacyRemoteCall.h"
#include "ll/api/io/Logger.h"
#include "ll/api/utils/StringUtils.h"
#include "RemoteCallAPI.h"

namespace RemoteCall {
CallbackFn const EMPTY_FUNC{};
std::unordered_map<std::string, RemoteCall::ExportedFuncData> exportedFuncs;

ll::io::Logger& getLogger() { return legacy_remote_call_api::LegacyRemoteCallAPI::getInstance().getSelf().getLogger();}

namespace RemoteCall {
ll::io::Logger& getLogger() { return legacy_remote_call_api::LegacyRemoteCallAPI::getInstance().getSelf().getLogger(); }
struct TransparentHasher {
using is_transparent = void;
auto operator()(const std::string& key) const { return std::hash<std::string>{}(key); }
auto operator()(std::string_view key) const { return std::hash<std::string_view>{}(key); }
auto operator()(const char* key) const { return std::hash<std::string_view>{}(key); }
};
CallbackFn const EMPTY_FUNC{};
std::unordered_map<std::string, RemoteCall::ExportedFuncData, TransparentHasher, std::equal_to<>> exportedFuncs;
bool exportFunc(std::string const& nameSpace, std::string const& funcName, CallbackFn&& callback, void* handle) {
if (nameSpace.find("::") != std::string::npos) {
getLogger().error("Namespace can't includes \"::\"");
Expand All @@ -29,18 +35,18 @@ bool hasFunc(std::string const& nameSpace, std::string const& funcName) {
return exportedFuncs.find(nameSpace + "::" + funcName) != exportedFuncs.end();
}

bool removeFunc(std::string&& key) { return exportedFuncs.erase(key); }
bool removeFunc(std::string_view key) {
if (auto iter = exportedFuncs.find(key); iter != exportedFuncs.end()) {
exportedFuncs.erase(iter);
return true;
}
return false;
}

bool removeFunc(std::string const& nameSpace, std::string const& funcName) {
return removeFunc(nameSpace + "::" + funcName);
}

void _onCallError(std::string const& msg, void* handle) {
getLogger().error(msg);
auto plugin = ll::mod::NativeMod::getByHandle(handle);
if (plugin) getLogger().error("In plugin <{}>", plugin->getManifest().name);
}

int removeNameSpace(std::string const& nameSpace) {
int count = 0;
for (auto iter = exportedFuncs.begin(); iter != exportedFuncs.end();) {
Expand All @@ -55,13 +61,18 @@ int removeNameSpace(std::string const& nameSpace) {
int removeFuncs(std::vector<std::pair<std::string, std::string>>& funcs) {
int count = 0;
for (auto& [ns, name] : funcs) {
if (removeFunc(ns + "::" + name)) count++;
if (removeFunc((ns + "::").append(name))) count++;
}
return count;
}

void removeAllFunc() { exportedFuncs.clear(); }

void _onCallError(std::string const& msg, void* handle) {
getLogger().error(msg);
auto plugin = ll::mod::NativeMod::getByHandle(handle);
if (plugin) getLogger().error("In plugin <{}>", plugin->getManifest().name);
}
} // namespace RemoteCall

static_assert(RemoteCall::is_supported_type_v<void>);
Expand All @@ -78,75 +89,4 @@ static_assert(RemoteCall::is_supported_type_v<Vec3>);
static_assert(RemoteCall::is_supported_type_v<Vec3&>);
static_assert(RemoteCall::is_supported_type_v<Vec3 const&>);
// static_assert(RemoteCall::is_supported_type_v<Vec3 const*>);
static_assert(RemoteCall::is_supported_type_v<CompoundTag*>);

#ifdef DEBUG
#include "llapi/ScheduleAPI.h"
#include "llapi/mc/Player.hpp"
inline bool testExtra = ([]() {
std::vector<std::string> input{"aa", "abcd", "test"};
auto output = RemoteCall::extract<decltype(input)>(RemoteCall::pack(input));
assert(output == input);
std::unordered_map<std::string, std::string> input2{
{"aa", "bb" },
{"ab", "ba" },
{"abc", "cba"},
};
auto output2 = RemoteCall::extract<decltype(input2)>(RemoteCall::pack(input2));
assert(output2 == input2);
std::vector<decltype(input2)> input3{input2, input2};
auto output3 = RemoteCall::extract<decltype(input3)>(RemoteCall::pack(input3));
assert(output3 == input3);
std::unordered_map<std::string, decltype(input3)> input4{
{"aa", input3},
{"ab", input3},
{"abc", input3},
};
auto output4 = RemoteCall::extract<decltype(input4)>(RemoteCall::pack(input4));
assert(output4 == input4);

std::vector<decltype(input4)> input5{input4, input4, input4};
auto output5 = RemoteCall::extract<decltype(input5)>(RemoteCall::pack(input5));
assert(output5 == input5);
#if false
__debugbreak();
output5.erase(output5.begin());
assert(output5 == input5);
__debugbreak();
#endif // false
return true;
})();
int TestExport(std::string a0, int a1, int a2) { return static_cast<int>(a0.size()) + a1; }
std::unique_ptr<CompoundTag> TestSimulatedPlayerLL(Player* player) { return player->getNbt(); }

void exportTestSimulatedPlayerLL() {
RemoteCall::exportAs("TestRemoteCall", "TestSimulatedPlayerLL", TestSimulatedPlayerLL);
}
#include "llapi/EventAPI.h"
auto TestRemoteCall = ([]() -> bool {
std::thread([]() {
SetCurrentThreadDescription(L"LL_Test_RemoteCall_Thread");
Sleep(5000);
Schedule::nextTick([]() {
RemoteCall::exportAs("TestNameSpace", "StrSize", [](std::string arg) -> size_t { return arg.size(); });

exportTestSimulatedPlayerLL();
RemoteCall::exportAs("Test", "test2", TestExport);
if (!RemoteCall::hasFunc("TestRemoteCall", "TestSimulatedPlayerJs")) return;
auto func = RemoteCall::importAs<decltype(TestExport)>("Test", "test2");
auto func2 = RemoteCall::importAs<std::function<decltype(TestExport)>>("Test", "test2");
auto size = func("TestParam", 5, 10);
static auto TestSimulatedPlayerJs =
RemoteCall::importAs<bool(Player*)>("TestRemoteCall", "TestSimulatedPlayerJs");
Event::PlayerJoinEvent::subscribe_ref([](Event::PlayerJoinEvent& ev) {
logger.warn("TestSimulatedPlayer");
auto res = TestSimulatedPlayerJs(ev.mPlayer);
logger.warn("Result: {}", res);
return true;
});
});
}).detach();
return true;
})();

#endif // DEBUG
static_assert(RemoteCall::is_supported_type_v<CompoundTag*>);
Loading