diff --git a/src/ai.cpp b/src/ai.cpp index 4c5e37ddc40..2f38c7e58c8 100644 --- a/src/ai.cpp +++ b/src/ai.cpp @@ -46,6 +46,7 @@ #include "wml_exception.hpp" #include +#include #define LOG_AI LOG_STREAM(info, ai) #define WRN_AI LOG_STREAM(warn, ai) @@ -187,6 +188,41 @@ protected: } }; + +#ifdef HAVE_PYTHON +// Finds all python AI scripts available in the current binary path. +// They have to end with .py, and have #!WPY as first line. +// If preferences allow for unsafe python AIs, then also look for +// the #!UNSAFE_WPY tag. +std::vector get_available_py_scripts() +{ + int allow_unsafe = !preferences::run_safe_python() ; + std::vector scripts; + const std::vector& paths = get_binary_paths("data"); + for(std::vector::const_iterator i = paths.begin(); i != paths.end(); ++i) { + std::vector files; + get_files_in_dir(*i + "ais", &files, NULL, ENTIRE_FILE_PATH); + for(std::vector::const_iterator j = files.begin(); j != files.end(); ++j) { + // file ends with .py + if (j->substr(j->length() - 3) == ".py") { + std::string name(j->substr(j->rfind("/") + 1)); // extract name + // read first line + std::ifstream s(j->c_str()); std::string mark; s >> mark; s.close(); + if (mark == "#!WPY" && + std::find(scripts.begin(), scripts.end(), name) == scripts.end()) + scripts.push_back(name); + else if (allow_unsafe && mark == "#!UNSAFE_WPY" && + std::find(scripts.begin(), scripts.end(), name) == scripts.end()) + scripts.push_back(name); + } + } + } + return scripts; +} +#endif + + + std::vector get_available_ais() { std::vector ais; @@ -195,7 +231,7 @@ std::vector get_available_ais() //ais.push_back("idle_ai"); ais.push_back("dfool_ai"); #ifdef HAVE_PYTHON - std::vector scripts = python_ai::get_available_scripts(); + std::vector scripts = get_available_py_scripts(); ais.insert(ais.end(), scripts.begin(), scripts.end()); #endif return ais; @@ -223,8 +259,8 @@ ai_interface* create_ai(const std::string& name, ai_interface::info& info) else if(name == "python_ai") #ifdef HAVE_PYTHON return new python_ai(info); -// else if(name == "newpy_ai") -// return new pythonai::PythonAI( info ) ; +// else if(name == "newpy_ai") +// return new pyai::PythonAI( info ) ; #else { LOG_STREAM(err, ai) << "No Python AI support available in this Wesnoth build!\n"; diff --git a/src/ai_python.cpp b/src/ai_python.cpp index b6901e5ba46..6e0acfe4072 100644 --- a/src/ai_python.cpp +++ b/src/ai_python.cpp @@ -2196,34 +2196,4 @@ void python_ai::play_turn() Py_DECREF(globals); } -// Finds all python AI scripts available in the current binary path. -// They have to end with .py, and have #!WPY as first line. -// If preferences allow for unsafe python AIs, then also look for -// the #!UNSAFE_WPY tag. -std::vector python_ai::get_available_scripts() -{ - int allow_unsafe = !preferences::run_safe_python() ; - std::vector scripts; - const std::vector& paths = get_binary_paths("data"); - for(std::vector::const_iterator i = paths.begin(); i != paths.end(); ++i) { - std::vector files; - get_files_in_dir(*i + "ais", &files, NULL, ENTIRE_FILE_PATH); - for(std::vector::const_iterator j = files.begin(); j != files.end(); ++j) { - // file ends with .py - if (j->substr(j->length() - 3) == ".py") { - std::string name(j->substr(j->rfind("/") + 1)); // extract name - // read first line - std::ifstream s(j->c_str()); std::string mark; s >> mark; s.close(); - if (mark == "#!WPY" && - std::find(scripts.begin(), scripts.end(), name) == scripts.end()) - scripts.push_back(name); - else if (allow_unsafe && mark == "#!UNSAFE_WPY" && - std::find(scripts.begin(), scripts.end(), name) == scripts.end()) - scripts.push_back(name); - } - } - } - return scripts; -} - #endif // HAVE_PYTHON diff --git a/src/ai_python.hpp b/src/ai_python.hpp index f147ae57245..082fe4e00c4 100644 --- a/src/ai_python.hpp +++ b/src/ai_python.hpp @@ -80,7 +80,6 @@ public: static bool is_unit_valid(const unit* unit); std::vector& get_teams() { return get_info().teams; } - static std::vector get_available_scripts(); static void initialize_python(); static void invoke(std::string name);