Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
python_compile.cpp File Reference
#include "cmd/unit_generic.h"
#include "python_compile.h"
#include <compile.h>
#include <eval.h>
#include "configxml.h"
#include "vs_globals.h"
#include "vsfilesystem.h"
#include "init.h"
#include "universe_util.h"
#include "in_kb_data.h"

Go to the source code of this file.

Functions

char * LoadString (const char *filename)
 
std::string getCompilingName (const std::string &name)
 
void InterpretPython (const std::string &name)
 
PyCodeObject * CompilePython (const std::string &name)
 
void CompileRunPython (const std::string &filename)
 
PyObject * CreateTuple (const std::vector< PythonBasicType > &values)
 
static void pySetScratchVector (const KBSTATE k)
 
void RunPythonPress (const KBData &s, KBSTATE k)
 
void RunPythonRelease (const KBData &s, KBSTATE k)
 
void RunPythonToggle (const KBData &s, KBSTATE k)
 
void RunPythonPhysicsFrame (const KBData &s, KBSTATE k)
 

Variables

Hashtable< string,
PyCodeObject, 1023 > 
compiled_python
 

Function Documentation

PyCodeObject* CompilePython ( const std::string &  name)

Definition at line 49 of file python_compile.cpp.

References fprintf, Hashtable< KEY, VALUE, SIZ >::Get(), getCompilingName(), LoadString(), and Hashtable< KEY, VALUE, SIZ >::Put().

Referenced by CompileRunPython().

50 {
51  Python::reseterrors();
52  PyCodeObject *retval = compiled_python.Get( name );
53  Python::reseterrors();
54  if (retval)
55  return retval;
56  char *str = LoadString( name.c_str() );
57  if (str) {
58  fprintf( stdout, "Compiling python module %s\n", name.c_str() );
59 
60  std::string compiling_name = getCompilingName( name ).c_str();
61  char *temp = strdup( compiling_name.c_str() );
62 
63  retval = (PyCodeObject*) Py_CompileString( str, temp, Py_file_input );
64  if (retval)
65  compiled_python.Put( name, retval );
66  free( temp );
67  free( str );
68  }
69  return retval;
70 }
void CompileRunPython ( const std::string &  filename)

Definition at line 72 of file python_compile.cpp.

References CompilePython(), d, VegaConfig::getVariable(), InterpretPython(), XMLSupport::parse_bool(), and vs_config.

Referenced by ChooseNavPoint(), DockedScript(), PythonAI< SuperClass >::Factory(), PythonClass< PythonMissionBaseClass >::Factory(), RunPython(), RunPythonPhysicsFrame(), RunPythonPress(), RunPythonRelease(), and RunPythonToggle().

73 {
74  static bool ndebug_libs = XMLSupport::parse_bool( vs_config->getVariable( "AI", "compile_python", "true" ) );
75  if (ndebug_libs) {
76  Python::reseterrors();
77  PyCodeObject *CompiledProgram = CompilePython( filename );
78  Python::reseterrors();
79  if (CompiledProgram) {
80  PyObject *m, *d;
81  static char main_str[16] = "__main__"; //by chuck_starchaser, to squash a warning
82  if ( ( m = PyImport_AddModule( main_str ) ) != NULL ) {
83  PyObject *localdict = PyDict_New();
84  if ( ( d = PyModule_GetDict( m ) ) != NULL ) {
85  PyObject *exe = PyEval_EvalCode( CompiledProgram, d, localdict );
86  Py_XDECREF( exe );
87  //unref exe?
88  }
89  Py_XDECREF( localdict );
90  }
91  }
92  } else {
93  Python::reseterrors();
94  InterpretPython( filename );
95  Python::reseterrors();
96  }
97 }
PyObject* CreateTuple ( const std::vector< PythonBasicType > &  values)

Definition at line 99 of file python_compile.cpp.

References i.

100 {
101  PyObject *retval = PyTuple_New( values.size() );
102  for (unsigned int i = 0; i < values.size(); i++) {
103  PyObject *val = values[i].NewObject();
104  if (val)
105  PyTuple_SET_ITEM( retval, i, val );
106  }
107  return retval;
108 }
std::string getCompilingName ( const std::string &  name)

Definition at line 31 of file python_compile.cpp.

References DELIMSTR, and VSFileSystem::homedir.

Referenced by CompilePython(), and InterpretPython().

32 {
33  std::string compiling_name = VSFileSystem::homedir+DELIMSTR+name;
34  return compiling_name;
35 }
void InterpretPython ( const std::string &  name)

Definition at line 37 of file python_compile.cpp.

References getCompilingName(), VSFileSystem::vs_close(), and VSFileSystem::vs_open().

Referenced by CompileRunPython().

38 {
39  char *temp = strdup( getCompilingName( name ).c_str() );
40  FILE *fp = VSFileSystem::vs_open( name.c_str(), "r" );
41  if (fp) {
42  PyRun_SimpleFile( fp, temp );
43  Python::reseterrors();
45  }
46  free( temp );
47 }
char* LoadString ( const char *  filename)

Definition at line 13 of file python_compile.cpp.

References VSFileSystem::vs_close(), VSFileSystem::vs_fseek(), VSFileSystem::vs_ftell(), VSFileSystem::vs_open(), and VSFileSystem::vs_read().

Referenced by CompilePython().

14 {
15  FILE *fp = VSFileSystem::vs_open( filename, "r" );
16  if (!fp)
17  return NULL;
18  VSFileSystem::vs_fseek( fp, 0, SEEK_END );
19  long len = VSFileSystem::vs_ftell( fp );
20  char *retval = NULL;
21  VSFileSystem::vs_fseek( fp, 0, SEEK_SET );
22  if (len) {
23  retval = (char*) malloc( (len+2)*sizeof (char) );
24  len = VSFileSystem::vs_read( retval, 1, len, fp );
25  retval[len] = '\0';
26  }
28  return retval;
29 }
static void pySetScratchVector ( const KBSTATE  k)
static

Definition at line 110 of file python_compile.cpp.

References DOWN, PRESS, RELEASE, UniverseUtil::setScratchVector(), UP, and Vector.

Referenced by RunPythonPhysicsFrame(), RunPythonPress(), RunPythonRelease(), and RunPythonToggle().

111 {
112  switch (k)
113  {
114  case PRESS:
116  break;
117  case RELEASE:
119  break;
120  case UP:
122  break;
123  case DOWN:
125  break;
126  default:
127  break;
128  }
129 }
void RunPythonPhysicsFrame ( const KBData s,
KBSTATE  k 
)

Definition at line 158 of file python_compile.cpp.

References CompileRunPython(), KBData::data, DOWN, pySetScratchVector(), UniverseUtil::setScratchVector(), UP, and Vector.

Referenced by initGlobalCommandMap().

159 {
160  if ( (k == DOWN || k == UP) && s.data.length() ) {
161  pySetScratchVector( k );
162  CompileRunPython( s.data );
164  }
165 }
void RunPythonPress ( const KBData s,
KBSTATE  k 
)

Definition at line 131 of file python_compile.cpp.

References CompileRunPython(), KBData::data, PRESS, pySetScratchVector(), UniverseUtil::setScratchVector(), and Vector.

Referenced by initGlobalCommandMap().

132 {
133  if ( k == PRESS && s.data.length() ) {
134  pySetScratchVector( k );
135  CompileRunPython( s.data );
137  }
138 }
void RunPythonRelease ( const KBData s,
KBSTATE  k 
)

Definition at line 140 of file python_compile.cpp.

References CompileRunPython(), KBData::data, pySetScratchVector(), RELEASE, UniverseUtil::setScratchVector(), and Vector.

Referenced by initGlobalCommandMap().

141 {
142  if ( k == RELEASE && s.data.length() ) {
143  pySetScratchVector( k );
144  CompileRunPython( s.data );
146  }
147 }
void RunPythonToggle ( const KBData s,
KBSTATE  k 
)

Definition at line 149 of file python_compile.cpp.

References CompileRunPython(), KBData::data, PRESS, pySetScratchVector(), RELEASE, UniverseUtil::setScratchVector(), and Vector.

Referenced by initGlobalCommandMap().

150 {
151  if ( (k == RELEASE || k == PRESS) && s.data.length() ) {
152  pySetScratchVector( k );
153  CompileRunPython( s.data );
155  }
156 }

Variable Documentation

Hashtable< string, PyCodeObject, 1023 > compiled_python

Definition at line 11 of file python_compile.cpp.