00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "framework/package.hpp"
00035 #include "framework/application.hpp"
00036
00037 #include "platform/lowlevel.hpp"
00038
00039 namespace gsgl
00040 {
00041
00042 using namespace data;
00043 using namespace io;
00044
00045 namespace framework
00046 {
00047
00048 pkg_scenery::pkg_scenery(const string & fname)
00049 : framework_object(), fname(fname)
00050 {
00051 config_record scenery_config(fname);
00052
00053 if (scenery_config.get_name() != L"scenery")
00054 throw runtime_exception(L"Invalid scenery specification %ls", fname.w_string());
00055
00056 scenery_name = scenery_config[L"name"];
00057 description = scenery_config.get_child(L"description").get_text();
00058 }
00059
00060
00061 pkg_scenery::~pkg_scenery()
00062 {
00063 }
00064
00065
00066
00067
00068 pkg_vehicle::pkg_vehicle(const string & fname)
00069 : framework_object(), fname(fname)
00070 {
00071 config_record vehicle_config(fname);
00072
00073 if (vehicle_config.get_name() != L"vehicle")
00074 throw runtime_exception(L"Invalid vehicle specification %ls", fname.w_string());
00075
00076 vehicle_name = vehicle_config[L"name"];
00077 class_name = vehicle_config[L"class"];
00078 description = vehicle_config.get_child(L"description").get_text();
00079 }
00080
00081
00082 pkg_vehicle::~pkg_vehicle()
00083 {
00084 }
00085
00086
00087
00088
00089
00090 #ifdef WIN32
00091 static const string LIB_SFX = L".dll";
00092 #else
00093 static const string LIB_SFX = L".so";
00094 #endif
00095
00096 pkg_library::pkg_library(const string & fname)
00097 : framework_object(), fname(fname)
00098 {
00099 void *lib = 0;
00100 #ifdef WIN32
00101 if (!(lib = LoadLibraryA(fname.c_string())))
00102 throw runtime_exception(L"Unable to load library %ls.", fname.w_string());
00103 #else
00104 if (!(lib = dlopen(fname.c_string(), DLOPEN_FLAG)))
00105 throw runtime_exception(L"Unable to load library %ls: %hs", fname.w_string(), dlerror());
00106 #endif
00107 };
00108
00109
00110 pkg_library::~pkg_library()
00111 {
00112 }
00113
00114
00115
00116 package::package(const string & fname)
00117 : framework_object(), fname(fname)
00118 {
00119
00120 config_record app_config(fname);
00121
00122 if (app_config.get_name() != L"package")
00123 throw runtime_exception(L"Invalid package specification %ls", fname.w_string());
00124
00125 package_name = app_config[L"name"];
00126 description = app_config.get_child(L"description").get_text();
00127
00128
00129 config_record & scenery_config = app_config.get_child(L"sceneries");
00130 for (list<config_record>::iterator i = scenery_config.get_children().iter(); i.is_valid(); ++i)
00131 {
00132 if (i->get_name() == L"scenery")
00133 {
00134 const string & fname = i->get_attribute(L"filename");
00135
00136 if (!fname.is_empty())
00137 loaded_sceneries.append(new pkg_scenery(app_config.get_directory().get_full_path() + fname));
00138 }
00139 }
00140
00141 config_record & vehicle_config = app_config.get_child(L"vehicles");
00142 for (list<config_record>::iterator i = vehicle_config.get_children().iter(); i.is_valid(); ++i)
00143 {
00144 if (i->get_name() == L"vehicle")
00145 {
00146 const string & fname = i->get_attribute(L"filename");
00147
00148 if (!fname.is_empty())
00149 loaded_vehicles.append(new pkg_vehicle(app_config.get_directory().get_full_path() + fname));
00150 }
00151 }
00152
00153 config_record & library_config = app_config.get_child(L"libraries");
00154 for (list<config_record>::iterator i = library_config.get_children().iter(); i.is_valid(); ++i)
00155 {
00156 if (i->get_name() == L"library")
00157 {
00158 string fname = i->get_attribute(L"basename");
00159
00160 if (!fname.is_empty())
00161 {
00162 fname = app_config.get_directory().get_full_path() + fname + LIB_SFX;
00163 loaded_libraries.append(new pkg_library(fname));
00164 }
00165 }
00166 else if (i->get_name() == L"default_library")
00167 {
00168 string fname = i->get_attribute(L"basename");
00169
00170 if (!fname.is_empty())
00171 {
00172 fname = application::PROGRAM_PATH + fname + LIB_SFX;
00173 const wchar_t *str = fname.w_string();
00174 loaded_libraries.append(new pkg_library(fname));
00175 }
00176 }
00177 }
00178 }
00179
00180
00181 package::~package()
00182 {
00183 for (list<pkg_library *>::iterator i = loaded_libraries.iter(); i.is_valid(); ++i)
00184 {
00185 delete *i;
00186 }
00187 }
00188
00189
00190 }
00191
00192 }