2023-04-09 05:35:35 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-03-24 13:23:42 +00:00
|
|
|
%nodefaultctor QmlBackend;
|
|
|
|
%nodefaultdtor QmlBackend;
|
|
|
|
class QmlBackend : public QObject {
|
|
|
|
public:
|
2024-04-19 12:53:19 +00:00
|
|
|
void notifyUI(const QString &command, const QVariant &data);
|
2022-04-30 07:27:56 +00:00
|
|
|
static void cd(const QString &path);
|
|
|
|
static QStringList ls(const QString &dir);
|
|
|
|
static QString pwd();
|
|
|
|
static bool exists(const QString &file);
|
|
|
|
static bool isDir(const QString &file);
|
2022-03-24 13:23:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern QmlBackend *Backend;
|
|
|
|
|
|
|
|
%nodefaultctor Client;
|
|
|
|
%nodefaultdtor Client;
|
|
|
|
class Client : public QObject {
|
|
|
|
public:
|
2022-04-30 07:27:56 +00:00
|
|
|
void replyToServer(const QString &command, const QString &json_data);
|
|
|
|
void notifyServer(const QString &command, const QString &json_data);
|
2022-03-24 13:23:42 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
LuaFunction callback;
|
2022-03-28 14:24:30 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientPlayer *addPlayer(int id, const QString &name, const QString &avatar);
|
|
|
|
void removePlayer(int id);
|
2023-04-27 06:15:08 +00:00
|
|
|
void changeSelf(int id);
|
2023-06-27 08:50:24 +00:00
|
|
|
|
|
|
|
void saveRecord(const QString &json, const QString &fname);
|
2022-03-24 13:23:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Client *ClientInstance;
|
|
|
|
|
|
|
|
%{
|
2023-06-27 08:50:24 +00:00
|
|
|
void Client::callLua(const QString& command, const QString& json_data, bool isRequest)
|
2022-03-24 13:23:42 +00:00
|
|
|
{
|
2022-04-30 07:27:56 +00:00
|
|
|
Q_ASSERT(callback);
|
2022-03-24 13:23:42 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
lua_getglobal(L, "debug");
|
|
|
|
lua_getfield(L, -1, "traceback");
|
|
|
|
lua_replace(L, -2);
|
2022-03-31 09:16:30 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, callback);
|
|
|
|
SWIG_NewPointerObj(L, this, SWIGTYPE_p_Client, 0);
|
|
|
|
lua_pushstring(L, command.toUtf8());
|
|
|
|
lua_pushstring(L, json_data.toUtf8());
|
2023-06-27 08:50:24 +00:00
|
|
|
lua_pushboolean(L, isRequest);
|
2022-03-24 13:23:42 +00:00
|
|
|
|
2023-06-27 08:50:24 +00:00
|
|
|
int error = lua_pcall(L, 4, 0, -6);
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if (error) {
|
|
|
|
const char *error_msg = lua_tostring(L, -1);
|
2022-12-18 04:52:52 +00:00
|
|
|
qCritical() << error_msg;
|
2022-04-30 07:27:56 +00:00
|
|
|
lua_pop(L, 2);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
2022-03-24 13:23:42 +00:00
|
|
|
}
|
|
|
|
%}
|