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
Go to the documentation of this file.
1 #include "cmd/unit_generic.h"
2 #include "python_compile.h"
3 #include <compile.h>
4 #include <eval.h>
5 #include "configxml.h"
6 #include "vs_globals.h"
7 #include "vsfilesystem.h"
8 #include "init.h"
9 #include "universe_util.h"
10 #include "in_kb_data.h"
12 
13 char * LoadString( const char *filename )
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 }
30 
31 std::string getCompilingName( const std::string &name )
32 {
33  std::string compiling_name = VSFileSystem::homedir+DELIMSTR+name;
34  return compiling_name;
35 }
36 
37 void InterpretPython( const std::string &name )
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 }
48 
49 PyCodeObject * CompilePython( const std::string &name )
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 }
71 
72 void CompileRunPython( const std::string &filename )
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 }
98 
99 PyObject * CreateTuple( const std::vector< PythonBasicType > &values )
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 }
109 
110 static void pySetScratchVector( const KBSTATE k )
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 }
130 
131 void RunPythonPress( const KBData &s, KBSTATE k )
132 {
133  if ( k == PRESS && s.data.length() ) {
134  pySetScratchVector( k );
135  CompileRunPython( s.data );
137  }
138 }
139 
140 void RunPythonRelease( const KBData &s, KBSTATE k )
141 {
142  if ( k == RELEASE && s.data.length() ) {
143  pySetScratchVector( k );
144  CompileRunPython( s.data );
146  }
147 }
148 
149 void RunPythonToggle( const KBData &s, KBSTATE k )
150 {
151  if ( (k == RELEASE || k == PRESS) && s.data.length() ) {
152  pySetScratchVector( k );
153  CompileRunPython( s.data );
155  }
156 }
157 
159 {
160  if ( (k == DOWN || k == UP) && s.data.length() ) {
161  pySetScratchVector( k );
162  CompileRunPython( s.data );
164  }
165 }
166