Add parseJson and clientWorld functions to LuaBindings

This commit is contained in:
ErodeesFleurs 2024-03-21 10:10:22 +08:00
parent 330fc938f3
commit 868216a992
4 changed files with 12 additions and 0 deletions

View File

@ -113,6 +113,7 @@ LuaCallbacks LuaBindings::makeUtilityCallbacks() {
callbacks.registerCallback("logWarn", UtilityCallbacks::logWarn); callbacks.registerCallback("logWarn", UtilityCallbacks::logWarn);
callbacks.registerCallback("logError", UtilityCallbacks::logError); callbacks.registerCallback("logError", UtilityCallbacks::logError);
callbacks.registerCallback("setLogMap", UtilityCallbacks::setLogMap); callbacks.registerCallback("setLogMap", UtilityCallbacks::setLogMap);
callbacks.registerCallback("parseJson", UtilityCallbacks::parseJson);
callbacks.registerCallback("printJson", UtilityCallbacks::printJson); callbacks.registerCallback("printJson", UtilityCallbacks::printJson);
callbacks.registerCallback("print", UtilityCallbacks::print); callbacks.registerCallback("print", UtilityCallbacks::print);
callbacks.registerCallback("interpolateSinEase", UtilityCallbacks::interpolateSinEase); callbacks.registerCallback("interpolateSinEase", UtilityCallbacks::interpolateSinEase);
@ -192,6 +193,10 @@ void LuaBindings::UtilityCallbacks::setLogMap(String const& key, String const& v
LogMap::set(key, formatLua(value, args)); LogMap::set(key, formatLua(value, args));
} }
Json LuaBindings::UtilityCallbacks::parseJson(String const& str) {
return Json::parse(str);
}
String LuaBindings::UtilityCallbacks::printJson(Json const& arg, Maybe<int> pretty) { String LuaBindings::UtilityCallbacks::printJson(Json const& arg, Maybe<int> pretty) {
return arg.repr(pretty.value()); return arg.repr(pretty.value());
} }

View File

@ -16,6 +16,7 @@ namespace LuaBindings {
void logWarn(String const& str, LuaVariadic<LuaValue> const& args); void logWarn(String const& str, LuaVariadic<LuaValue> const& args);
void logError(String const& str, LuaVariadic<LuaValue> const& args); void logError(String const& str, LuaVariadic<LuaValue> const& args);
void setLogMap(String const& key, String const& value, LuaVariadic<LuaValue> const& args); void setLogMap(String const& key, String const& value, LuaVariadic<LuaValue> const& args);
Json parseJson(String const& str);
String printJson(Json const& arg, Maybe<int> pretty); String printJson(Json const& arg, Maybe<int> pretty);
String print(LuaValue const& arg); String print(LuaValue const& arg);
LuaValue interpolateSinEase(LuaEngine& engine, double offset, LuaValue const& value1, LuaValue const& value2); LuaValue interpolateSinEase(LuaEngine& engine, double offset, LuaValue const& value1, LuaValue const& value2);

View File

@ -22,6 +22,7 @@ LuaCallbacks LuaBindings::makeUniverseServerCallbacks(UniverseServer* universe)
callbacks.registerCallbackWithSignature<StringList>("activeWorlds", bind(UniverseServerCallbacks::activeWorlds, universe)); callbacks.registerCallbackWithSignature<StringList>("activeWorlds", bind(UniverseServerCallbacks::activeWorlds, universe));
callbacks.registerCallbackWithSignature<RpcThreadPromise<Json>, String, String, LuaVariadic<Json>>("sendWorldMessage", bind(UniverseServerCallbacks::sendWorldMessage, universe, _1, _2, _3)); callbacks.registerCallbackWithSignature<RpcThreadPromise<Json>, String, String, LuaVariadic<Json>>("sendWorldMessage", bind(UniverseServerCallbacks::sendWorldMessage, universe, _1, _2, _3));
callbacks.registerCallbackWithSignature<void, ConnectionId, String, Json>("sendPacket", bind(UniverseServerCallbacks::sendPacket, universe, _1, _2, _3)); callbacks.registerCallbackWithSignature<void, ConnectionId, String, Json>("sendPacket", bind(UniverseServerCallbacks::sendPacket, universe, _1, _2, _3));
callbacks.registerCallbackWithSignature<String, ConnectionId>("clientWorld", bind(UniverseServerCallbacks::clientWorld, universe, _1));
return callbacks; return callbacks;
} }
@ -133,4 +134,8 @@ void LuaBindings::UniverseServerCallbacks::sendPacket(UniverseServer* universe,
universe->sendPacket(clientId, packet); universe->sendPacket(clientId, packet);
} }
String LuaBindings::UniverseServerCallbacks::clientWorld(UniverseServer* universe, ConnectionId clientId) {
return printWorldId(universe->clientWorld(clientId));
}
} }

View File

@ -26,6 +26,7 @@ namespace LuaBindings {
StringList activeWorlds(UniverseServer* universe); StringList activeWorlds(UniverseServer* universe);
RpcThreadPromise<Json> sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> args); RpcThreadPromise<Json> sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> args);
void sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args); void sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args);
String clientWorld(UniverseServer* universe, ConnectionId clientId);
} }
} }
} }