cyphesis/client/Python_ClientAPI.cpp

100 lines
2.8 KiB
C++
Raw Normal View History

// 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>
#include "Python_ClientAPI.h"
#include "rulesets/Python_Script_Utils.h"
#include "common/log.h"
2018-07-23 16:43:25 +02:00
#include "CyPy_CreatorClient.h"
#include "CyPy_ObserverClient.h"
#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>
int python_client_script(const std::string & package,
const std::string & func,
const std::map<std::string, std::string> & keywords)
{
2018-07-23 16:43:25 +02:00
auto module = Get_PyModule(package);
if (module.isNull()) {
return -1;
}
2018-07-23 16:43:25 +02:00
auto function = module.getAttr(func);
if (function.isNull()) {
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()) {
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));
}
2018-07-23 16:43:25 +02:00
auto ret = callable.apply(args, kwds);
2018-07-23 16:43:25 +02:00
if (ret.isNull()) {
2018-01-30 16:54:49 +01:00
if (PyErr_Occurred() == nullptr) {
std::cerr << "Could not call function" << std::endl << std::flush;
} else {
std::cerr << "Reporting python error" << std::endl << std::flush;
PyErr_Print();
}
}
return 0;
}
void extend_client_python_api()
{
2018-07-23 16:43:25 +02:00
Py::Module server("server");
if (server.isNull()) {
return;
}
2018-07-23 16:43:25 +02:00
CyPy_CreatorClient::init_type();
CyPy_ObserverClient::init_type();
2018-07-23 16:43:25 +02:00
server.setAttr("CreatorClient", CyPy_CreatorClient::type());
server.setAttr("ObserverClient", CyPy_ObserverClient::type());
}
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
}