2009-05-25 16:35:25 +01:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2009 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
|
|
|
|
|
|
|
|
|
|
|
2011-02-15 09:30:46 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
2011-02-16 09:00:40 +00:00
|
|
|
#else
|
|
|
|
|
#define CYPHESIS_DEBUG
|
2011-02-15 09:30:46 +00:00
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-05-25 16:35:25 +01:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
#include "python_testers.h"
|
|
|
|
|
|
2009-05-25 16:35:25 +01:00
|
|
|
#include "rulesets/Python_API.h"
|
2009-11-12 21:07:41 +00:00
|
|
|
#include "rulesets/Py_Task.h"
|
|
|
|
|
#include "rulesets/Py_Thing.h"
|
|
|
|
|
#include "rulesets/Character.h"
|
2011-11-03 22:57:46 +00:00
|
|
|
#include "rulesets/Task.h"
|
2009-05-25 16:35:25 +01:00
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
2009-11-12 21:07:41 +00:00
|
|
|
static PyObject * null_wrapper(PyObject * self, PyTask * o)
|
|
|
|
|
{
|
|
|
|
|
if (PyTask_Check(o)) {
|
2011-02-16 09:00:40 +00:00
|
|
|
#ifdef CYPHESIS_DEBUG
|
2018-01-30 16:54:49 +01:00
|
|
|
o->m_task = nullptr;
|
2009-11-12 21:07:41 +00:00
|
|
|
#endif // NDEBUG
|
2011-12-14 02:02:20 +00:00
|
|
|
} else if (PyLocatedEntity_Check(o)) {
|
2011-02-16 09:00:40 +00:00
|
|
|
#ifdef CYPHESIS_DEBUG
|
2011-12-14 02:02:20 +00:00
|
|
|
((PyEntity*)o)->m_entity.l = 0;
|
2009-11-12 21:07:41 +00:00
|
|
|
#endif // NDEBUG
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Unknown Object type");
|
2018-01-30 16:54:49 +01:00
|
|
|
return nullptr;
|
2009-11-12 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyMethodDef sabotage_methods[] = {
|
|
|
|
|
{"null", (PyCFunction)null_wrapper, METH_O},
|
2018-01-30 16:54:49 +01:00
|
|
|
{nullptr, nullptr} /* Sentinel */
|
2009-11-12 21:07:41 +00:00
|
|
|
};
|
|
|
|
|
|
2018-05-04 19:17:55 +02:00
|
|
|
static PyObject* init_sabotage() {
|
|
|
|
|
static struct PyModuleDef def = {
|
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
|
"sabotage",
|
|
|
|
|
nullptr,
|
|
|
|
|
0,
|
|
|
|
|
sabotage_methods,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return PyModule_Create(&def);
|
2009-11-12 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
2009-05-25 16:35:25 +01:00
|
|
|
int main()
|
|
|
|
|
{
|
2018-05-04 19:17:55 +02:00
|
|
|
PyImport_AppendInittab("sabotage", &init_sabotage);
|
2012-01-19 19:34:20 +00:00
|
|
|
init_python_api("6715c02a-cc63-497b-988d-453579eae35d");
|
2009-05-25 16:35:25 +01:00
|
|
|
|
2018-05-04 19:17:55 +02:00
|
|
|
PyTask * task = (PyTask *)PyType_GenericAlloc(&PyTask_Type, 0);
|
2009-11-12 21:07:41 +00:00
|
|
|
assert(task != 0);
|
|
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("from server import Task");
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("Task()", PyExc_TypeError);
|
|
|
|
|
expect_python_error("Task(1)", PyExc_TypeError);
|
|
|
|
|
expect_python_error("Task('1')", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("from server import Character");
|
|
|
|
|
run_python_string("c=Character('1')");
|
|
|
|
|
run_python_string("t=Task(c)");
|
|
|
|
|
run_python_string("Task(t)");
|
|
|
|
|
run_python_string("t==Task(c)");
|
2011-09-03 09:49:30 +01:00
|
|
|
run_python_string("assert t.character == c");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(t.progress)");
|
|
|
|
|
run_python_string("print(t.rate)");
|
|
|
|
|
expect_python_error("print(t.foo)", PyExc_AttributeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("t.progress = 0");
|
|
|
|
|
run_python_string("t.progress = 0.5");
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("t.progress = '1'", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("t.rate = 0");
|
|
|
|
|
run_python_string("t.rate = 0.5");
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("t.rate = '1'", PyExc_TypeError);
|
2011-12-27 07:14:32 +00:00
|
|
|
// The raw wrapper object has no dict for arbitrary attributes
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("t.foo = 1", PyExc_AttributeError);
|
|
|
|
|
expect_python_error("t.foo = 1.1", PyExc_AttributeError);
|
|
|
|
|
expect_python_error("t.foo = 'foois1'", PyExc_AttributeError);
|
|
|
|
|
expect_python_error("assert t.foo == 'foois1'", PyExc_AttributeError);
|
2011-12-27 07:14:32 +00:00
|
|
|
|
|
|
|
|
run_python_string("class TaskSubclass(Task): pass");
|
|
|
|
|
run_python_string("t2=TaskSubclass(c)");
|
|
|
|
|
// The subclass should have a dict offset
|
|
|
|
|
run_python_string("t2.foo = 1");
|
|
|
|
|
run_python_string("t2.foo = 1.1");
|
|
|
|
|
run_python_string("t2.foo = 'foois1'");
|
|
|
|
|
run_python_string("assert t2.foo == 'foois1'");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("assert t!=Task(c)");
|
|
|
|
|
|
2011-12-13 19:02:58 +00:00
|
|
|
|
|
|
|
|
// Tasks do not permit wrappers of core server objects
|
|
|
|
|
// to be stored directly.
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("t.foo = Character('2')", PyExc_TypeError);
|
2011-12-13 19:02:58 +00:00
|
|
|
run_python_string("import server");
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("t.foo = server.LocatedEntity('2')", PyExc_TypeError);
|
|
|
|
|
expect_python_error("t.foo = server.Thing('2')", PyExc_TypeError);
|
2011-12-13 19:02:58 +00:00
|
|
|
|
2011-09-03 09:49:30 +01:00
|
|
|
run_python_string("assert not t.obsolete()");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(t.count())");
|
|
|
|
|
run_python_string("print(t.new_tick())");
|
|
|
|
|
run_python_string("print(t.next_tick(1))");
|
|
|
|
|
run_python_string("print(t.next_tick(1.1))");
|
|
|
|
|
expect_python_error("print(t.next_tick('1'))", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("t.irrelevant()");
|
2011-09-03 09:49:30 +01:00
|
|
|
run_python_string("assert t.obsolete()");
|
2009-05-25 16:35:25 +01:00
|
|
|
|
2011-02-16 09:00:40 +00:00
|
|
|
#ifdef CYPHESIS_DEBUG
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("import sabotage");
|
2009-11-12 21:07:41 +00:00
|
|
|
// Hit the assert checks.
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("irrelevant_methd=t.irrelevant");
|
|
|
|
|
run_python_string("obsolete_methd=t.obsolete");
|
|
|
|
|
run_python_string("count_methd=t.count");
|
|
|
|
|
run_python_string("new_tick_methd=t.new_tick");
|
|
|
|
|
run_python_string("next_tick_methd=t.next_tick");
|
|
|
|
|
|
|
|
|
|
run_python_string("sabotage.null(t)");
|
|
|
|
|
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("irrelevant_methd()", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("obsolete_methd()", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("count_methd()", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("new_tick_methd()", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("next_tick_methd(1.1)", PyExc_AssertionError);
|
2009-11-20 20:18:53 +00:00
|
|
|
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("t.progress", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("t.progress = 0", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("Task(t)", PyExc_AssertionError);
|
2009-11-20 20:18:53 +00:00
|
|
|
|
|
|
|
|
run_python_string("c2=Character('2')");
|
|
|
|
|
run_python_string("sabotage.null(c2)");
|
2012-01-04 16:27:29 +00:00
|
|
|
expect_python_error("t=Task(c2)", PyExc_AssertionError);
|
2009-11-12 21:07:41 +00:00
|
|
|
#endif // NDEBUG
|
|
|
|
|
|
2009-05-25 16:35:25 +01:00
|
|
|
shutdown_python_api();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|