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
base_xml.cpp
Go to the documentation of this file.
1 #include "config.h"
2 #include <Python.h>
3 #include <vector>
4 #include <string>
5 #include <math.h>
6 #include "python/python_class.h"
7 #include "base.h"
8 #include "base_util.h"
9 #include "vsfilesystem.h"
10 #include <boost/version.hpp>
11 #if BOOST_VERSION != 102800
12 #include <boost/python/object.hpp>
13 #else
14 #include <boost/python/objects.hpp>
15 #endif
16 
17 static FILE * withAndWithout( std::string filename, std::string time_of_day_hint )
18 {
19  string with( filename+"_"+time_of_day_hint+BASE_EXTENSION );
20  FILE *fp = VSFileSystem::vs_open( with.c_str(), "r" );
21  if (!fp) {
22  string without( filename+BASE_EXTENSION );
23  fp = VSFileSystem::vs_open( without.c_str(), "r" );
24  }
25  return fp;
26 }
27 static FILE * getFullFile( std::string filename, std::string time_of_day_hint, std::string faction )
28 {
29  FILE *fp = withAndWithout( filename+"_"+faction, time_of_day_hint );
30  if (!fp)
31  fp = withAndWithout( filename, time_of_day_hint );
32  return fp;
33 }
34 void BaseInterface::Load( const char *filename, const char *time_of_day_hint, const char *faction )
35 {
36 #if 0
37  std::string full_filename = string( "bases/" )+filename;
38  std::string daynight_filename = full_filename+"_"+string( time_of_day_hint );
39  full_filename += BASE_EXTENSION;
40  daynight_filename += BASE_EXTENSION;
41  std::string newfile = daynight_filename;
42  cout<<"BaseInterface::LoadXML "<<full_filename<<endl;
43  FILE *inFile = VSFileSystem::vs_open( daynight_filename.c_str(), "r" );
44  if (!inFile) {
45  newfile = full_filename;
46  inFile = VSFileSystem::vs_open( full_filename.c_str(), "r" );
47  }
48  if (!inFile) {
49  Unit *baseun = this->baseun.GetUnit();
50  if (baseun) {
51  if (baseun->isUnit() == PLANETPTR) {
52  daynight_filename = string( "bases/planet_" )+time_of_day_hint+string( BASE_EXTENSION );
53  inFile = VSFileSystem::vs_open( daynight_filename.c_str(), "r" );
54  newfile = daynight_filename;
55  if (!inFile) {
56  newfile = "bases/planet" BASE_EXTENSION;
57  inFile = VSFileSystem::vs_open( newfile.c_str(), "r" );
58  }
59  } else {
60  daynight_filename = string( "bases/unit_" )+time_of_day_hint+string( BASE_EXTENSION );
61  inFile = VSFileSystem::vs_open( daynight_filename.c_str(), "r" );
62  newfile = daynight_filename;
63  if (!inFile) {
64  newfile = "bases/unit" BASE_EXTENSION;
65  inFile = VSFileSystem::vs_open( newfile.c_str(), "r" );
66  }
67  }
68  }
69  if (!inFile)
70  return;
71  }
72 #else
73  FILE *inFile = getFullFile( string( "bases/" )+filename, time_of_day_hint, faction );
74  if (!inFile) {
75  bool planet = false;
76  Unit *baseun = this->baseun.GetUnit();
77  if (baseun)
78  planet = (baseun->isUnit() == PLANETPTR);
79  string basestring( "bases/unit" );
80  if (planet)
81  basestring = "bases/planet";
82  inFile = getFullFile( basestring, time_of_day_hint, faction );
83  if (!inFile)
84  return;
85  }
86 #endif
87  //now that we have a FILE * named inFile and a std::string named newfile we can finally begin the python
88  string compilefile = string( filename )+time_of_day_hint+string( faction )+BASE_EXTENSION;
89  Python::reseterrors();
90  PyRun_SimpleFile( inFile, compilefile.c_str() );
91  Python::reseterrors();
92  VSFileSystem::vs_close( inFile );
93 }
94