AI now fetches scripts rather than asking ai_python::python_ai to do it.

This commit is contained in:
Greg Copeland 2008-07-09 20:29:50 +00:00
parent 7244cc5ec2
commit 7aa7c0c290
3 changed files with 39 additions and 34 deletions

View File

@ -46,6 +46,7 @@
#include "wml_exception.hpp"
#include <cassert>
#include <fstream>
#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<std::string> get_available_py_scripts()
{
int allow_unsafe = !preferences::run_safe_python() ;
std::vector<std::string> scripts;
const std::vector<std::string>& paths = get_binary_paths("data");
for(std::vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
std::vector<std::string> files;
get_files_in_dir(*i + "ais", &files, NULL, ENTIRE_FILE_PATH);
for(std::vector<std::string>::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<std::string> get_available_ais()
{
std::vector<std::string> ais;
@ -195,7 +231,7 @@ std::vector<std::string> get_available_ais()
//ais.push_back("idle_ai");
ais.push_back("dfool_ai");
#ifdef HAVE_PYTHON
std::vector<std::string> scripts = python_ai::get_available_scripts();
std::vector<std::string> 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";

View File

@ -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<std::string> python_ai::get_available_scripts()
{
int allow_unsafe = !preferences::run_safe_python() ;
std::vector<std::string> scripts;
const std::vector<std::string>& paths = get_binary_paths("data");
for(std::vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
std::vector<std::string> files;
get_files_in_dir(*i + "ais", &files, NULL, ENTIRE_FILE_PATH);
for(std::vector<std::string>::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

View File

@ -80,7 +80,6 @@ public:
static bool is_unit_valid(const unit* unit);
std::vector<team>& get_teams() { return get_info().teams; }
static std::vector<std::string> get_available_scripts();
static void initialize_python();
static void invoke(std::string name);