2010-07-27 17:53:47 +01:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2010 Alistair Riddoch
|
|
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
|
|
|
2010-07-27 23:31:28 +01:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2010-07-27 18:01:13 +01:00
|
|
|
#include "Python_ClientAPI.h"
|
|
|
|
|
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/python/Python_Script_Utils.h"
|
2010-07-27 17:53:47 +01:00
|
|
|
|
|
|
|
|
#include "common/log.h"
|
2018-07-23 16:43:25 +02:00
|
|
|
#include "CyPy_CreatorClient.h"
|
|
|
|
|
#include "CyPy_ObserverClient.h"
|
2010-07-27 17:53:47 +01:00
|
|
|
|
|
|
|
|
#include <iostream>
|
2018-05-15 20:34:38 +02:00
|
|
|
#include <string>
|
2018-07-23 16:43:25 +02:00
|
|
|
#include <external/pycxx/CXX/Objects.hxx>
|
2010-07-27 17:53:47 +01:00
|
|
|
|
2010-07-27 22:32:24 +01:00
|
|
|
int python_client_script(const std::string & package,
|
|
|
|
|
const std::string & func,
|
|
|
|
|
const std::map<std::string, std::string> & keywords)
|
2010-07-27 17:53:47 +01:00
|
|
|
{
|
2018-07-23 16:43:25 +02:00
|
|
|
auto module = Get_PyModule(package);
|
|
|
|
|
if (module.isNull()) {
|
2010-07-27 17:53:47 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-23 16:43:25 +02:00
|
|
|
auto function = module.getAttr(func);
|
|
|
|
|
if (function.isNull()) {
|
2010-07-27 17:53:47 +01:00
|
|
|
std::cerr << "Could not find " << func << " function" << std::endl
|
|
|
|
|
<< std::flush;
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-23 16:43:25 +02:00
|
|
|
if (!function.isCallable()) {
|
2010-07-27 17:53:47 +01:00
|
|
|
std::cerr << "It does not seem to be a function at all" << std::endl
|
|
|
|
|
<< std::flush;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-23 16:43:25 +02:00
|
|
|
Py::Callable callable(function);
|
|
|
|
|
Py::Dict kwds;
|
|
|
|
|
Py::Tuple args;
|
|
|
|
|
for (auto& entry : keywords) {
|
|
|
|
|
kwds.setAttr(entry.first, Py::String(entry.second));
|
2010-07-27 17:53:47 +01:00
|
|
|
}
|
2018-07-24 21:16:50 +02:00
|
|
|
try {
|
|
|
|
|
auto ret = callable.apply(args, kwds);
|
|
|
|
|
} catch (...) {
|
2018-01-30 16:54:49 +01:00
|
|
|
if (PyErr_Occurred() == nullptr) {
|
2018-07-24 21:16:50 +02:00
|
|
|
log(ERROR, "Could not call function");
|
2010-07-27 17:53:47 +01:00
|
|
|
} else {
|
2018-07-24 21:16:50 +02:00
|
|
|
log(ERROR, "Reporting python error");
|
2010-07-27 17:53:47 +01:00
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
2018-07-24 21:16:50 +02:00
|
|
|
return -1;
|
2010-07-27 17:53:47 +01:00
|
|
|
}
|
2018-07-24 21:16:50 +02:00
|
|
|
|
2010-07-27 17:53:47 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void extend_client_python_api()
|
|
|
|
|
{
|
2018-07-23 16:43:25 +02:00
|
|
|
Py::Module server("server");
|
|
|
|
|
if (server.isNull()) {
|
2010-07-27 17:53:47 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2018-07-23 16:43:25 +02:00
|
|
|
CyPy_CreatorClient::init_type();
|
|
|
|
|
CyPy_ObserverClient::init_type();
|
2010-07-27 17:53:47 +01:00
|
|
|
|
2018-07-23 16:43:25 +02:00
|
|
|
server.setAttr("CreatorClient", CyPy_CreatorClient::type());
|
|
|
|
|
server.setAttr("ObserverClient", CyPy_ObserverClient::type());
|
2010-07-27 17:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void python_prompt()
|
|
|
|
|
{
|
|
|
|
|
|
2018-05-15 20:34:38 +02:00
|
|
|
std::wstring prgname = L"python";
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2018-05-15 20:34:38 +02:00
|
|
|
auto bytes = (prgname.size() * sizeof(wchar_t)) + 1;
|
|
|
|
|
auto mem = new wchar_t[bytes];
|
|
|
|
|
memcpy(mem, prgname.c_str(), bytes);
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2018-05-15 20:34:38 +02:00
|
|
|
Py_Main(1, &mem);
|
|
|
|
|
|
|
|
|
|
delete[] mem;
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2010-07-27 17:53:47 +01:00
|
|
|
}
|