cyphesis/rulesets/PythonMindScript.cpp
Al Riddoch dcc3dfa85a * server/server.cpp, server/CommClient.h, client/ClientConnection.h,
tools/cycmd.cpp: Switched to using skstream library for socket
	  comms instead of hand rolled fstream based version.

	* ALL FILES: Fixed use of std namespace throughout all code.

This server is now ISO C++ compliand and builds with gcc 3.0.

Al Riddoch  <alriddoch@zepler.org>
2001-08-14 17:52:50 +00:00

104 lines
3.6 KiB
C++

// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2000,2001 Alistair Riddoch
#include "PythonMindScript.h"
#include "Python_API.h"
#include <common/debug.h>
#include "Entity.h"
#include "BaseMind.h"
#include "MemMap_methods.h"
using Atlas::Objects::Operation::RootOperation;
PythonMindScript::PythonMindScript(PyObject * o, BaseMind & m) :
PythonScript(o, m), mind(m)
{
}
PythonMindScript::~PythonMindScript()
{
}
bool PythonMindScript::Operation(const std::string & op_type,
const RootOperation & op,
oplist & ret_list, RootOperation * sub_op)
{
debug( std::cout << "Got script object for " << std::endl << std::flush;);
std::string op_name = op_type+"_operation";
// Construct apropriate python object thingies from op
if (!PyObject_HasAttrString(scriptObject, (char *)(op_name.c_str()))) {
debug( std::cout << "No method found for " << op_name << std::endl
<< std::flush;);
return false;
}
RootOperationObject * py_op = newAtlasRootOperation(NULL);
py_op->operation = new RootOperation(op);
py_op->own = 0;
py_op->from = mind.map.getAdd(op.GetFrom());
py_op->to = mind.map.getAdd(op.GetTo());
PyObject * ret;
if (sub_op == NULL) {
ret = PyObject_CallMethod(scriptObject, (char *)(op_name.c_str()),
"(O)", py_op);
} else {
RootOperationObject * py_sub_op = newAtlasRootOperation(NULL);
py_sub_op->operation = sub_op;
py_sub_op->own = 0;
py_sub_op->from = mind.map.getAdd(sub_op->GetFrom());
py_sub_op->to = mind.map.getAdd(sub_op->GetTo());
ret = PyObject_CallMethod(scriptObject, (char *)(op_name.c_str()),
"(OO)", py_op, py_sub_op);
Py_DECREF(py_sub_op);
}
delete py_op->operation;
Py_DECREF(py_op);
if (ret != NULL) {
debug( std::cout << "Called python method " << op_name << std::endl
<< std::flush;);
if (PyOperation_Check(ret)) {
RootOperationObject * op = (RootOperationObject*)ret;
if (op->operation != NULL) {
ret_list.push_back(op->operation);
op->own = 0;
} else {
debug( std::cout << "Method returned invalid op" << std::endl
<< std::flush;);
}
} else if (PyOplist_Check(ret)) {
OplistObject * op = (OplistObject*)ret;
if (op->ops != NULL) {
ret_list = *op->ops;
} else {
debug(std::cout << "Method returned invalid oplist" << std::endl
<< std::flush;);
}
} else {
debug( std::cout << "Method returned invalid object" << std::endl << std::flush;);
}
Py_DECREF(ret);
return true;
} else {
if (PyErr_Occurred() == NULL) {
debug( std::cout << "No method to be found for " << std::endl
<< std::flush;);
} else {
std::cerr << "Reporting python error for " << std::endl
<< std::flush;
PyErr_Print();
}
}
return false;
}
void PythonMindScript::hook(const std::string & method, Entity * object)
{
ThingObject * obj = newThingObject(NULL);
obj->m_thing = object;
PyObject_CallMethod(scriptObject, (char *)(method.c_str()), "(O)", obj);
Py_DECREF(obj);
}