From a534176eebc2ff79ae09c83b47d3694d484e4f16 Mon Sep 17 00:00:00 2001 From: Elias Pschernig Date: Sun, 21 May 2006 14:56:54 +0000 Subject: [PATCH] Made the python AI look in /data/ais for python AIs, instead of the current directory. --- src/ai_python.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ai_python.cpp b/src/ai_python.cpp index bc113acd761..6f8253719b3 100644 --- a/src/ai_python.cpp +++ b/src/ai_python.cpp @@ -40,6 +40,7 @@ Additionally, useful utility functions such as those found in pathutils.hpp shou #include "ai_python.hpp" #include "wassert.hpp" #include "gamestatus.hpp" +#include "filesystem.hpp" static python_ai* running_instance; bool python_ai::init_ = false; @@ -1359,15 +1360,30 @@ python_ai::~python_ai() void python_ai::play_turn() { - std::string script = current_team().ai_parameters()["python_script"]; + std::string script_name = current_team().ai_parameters()["python_script"]; + std::string script = get_binary_file_location("data", "ais/" + script_name); PyObject* file = PyFile_FromString((char*)script.c_str(),"rt"); PyObject* dict = PyDict_New(); PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins()); + + // Always execute an import statement including all the current binary + // pathes, so the python script can import any other python modules. + // e.g. sys.path.extend(['~/.wesnoth/data/ais', '/usr/share/wesnoth/data/ais', ]) + std::string import_modules("import sys; sys.path.extend(["); + const std::vector& paths = get_binary_paths("data"); + for(std::vector::const_iterator i = paths.begin(); i != paths.end(); ++i) { + import_modules += "'" + *i + "ais', "; + } + import_modules += "])\n"; + PyObject* pre = PyRun_String(import_modules.c_str(), Py_file_input, dict, dict); + + // Now execute the actual python AI. PyObject* ret = PyRun_File(PyFile_AsFile(file), script.c_str(), Py_file_input, dict, dict); if (PyErr_Occurred()) { PyErr_Print(); } + Py_XDECREF(pre); Py_XDECREF(ret); Py_DECREF(dict); Py_DECREF(file);