2022-03-24 13:23:42 +00:00
|
|
|
%nodefaultctor QmlBackend;
|
|
|
|
%nodefaultdtor QmlBackend;
|
|
|
|
class QmlBackend : public QObject {
|
|
|
|
public:
|
2022-03-27 06:49:41 +00:00
|
|
|
void emitNotifyUI(const QString &command, const QString &json_data);
|
2022-03-28 14:24:30 +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:
|
|
|
|
void replyToServer(const QString &command, const QString &json_data);
|
|
|
|
void notifyServer(const QString &command, const QString &json_data);
|
|
|
|
|
|
|
|
LuaFunction callback;
|
2022-03-28 14:24:30 +00:00
|
|
|
|
|
|
|
ClientPlayer *addPlayer(int id, const QString &name, const QString &avatar);
|
|
|
|
void removePlayer(int id);
|
2022-03-24 13:23:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Client *ClientInstance;
|
|
|
|
|
|
|
|
%{
|
|
|
|
void Client::callLua(const QString& command, const QString& json_data)
|
|
|
|
{
|
|
|
|
Q_ASSERT(callback);
|
|
|
|
|
2022-03-31 09:16:30 +00:00
|
|
|
lua_getglobal(L, "debug");
|
|
|
|
lua_getfield(L, -1, "traceback");
|
|
|
|
lua_replace(L, -2);
|
|
|
|
|
2022-03-24 13:23:42 +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());
|
|
|
|
|
2022-03-31 09:16:30 +00:00
|
|
|
int error = lua_pcall(L, 3, 0, -5);
|
2022-03-24 13:23:42 +00:00
|
|
|
if (error) {
|
|
|
|
const char *error_msg = lua_tostring(L, -1);
|
|
|
|
qDebug() << error_msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
%}
|