| 1178 | | if (!wxFileExists(PointFileName)) |
| 1179 | | { |
| 1180 | | wxMessageBox("Couldn't load the pointfile."); |
| 1181 | | return; |
| 1182 | | } |
| 1183 | | |
| 1184 | | TextParserT TP(PointFileName); |
| 1185 | | |
| 1186 | | while (!TP.IsAtEOF()) |
| 1187 | | { |
| 1188 | | const float x=TP.GetNextTokenAsFloat(); if (TP.IsAtEOF()) break; |
| 1189 | | const float y=TP.GetNextTokenAsFloat(); if (TP.IsAtEOF()) break; |
| 1190 | | const float z=TP.GetNextTokenAsFloat(); if (TP.IsAtEOF()) break; |
| 1191 | | |
| 1192 | | m_PointFilePoints.PushBack(Vector3fT(x, y, z)); |
| 1193 | | } |
| 1194 | | |
| 1195 | | UpdateAllObservers(UPDATE_POINTFILE); |
| | 1185 | |
| | 1186 | // Create a new Lua state. |
| | 1187 | lua_State* LuaState=lua_open(); |
| | 1188 | |
| | 1189 | try |
| | 1190 | { |
| | 1191 | if (LuaState==NULL) throw wxString("Couldn't open Lua state."); |
| | 1192 | if (!wxFileExists(PointFileName)) throw wxString("The file does not exist."); |
| | 1193 | |
| | 1194 | lua_pushcfunction(LuaState, luaopen_base); lua_pushstring(LuaState, ""); lua_call(LuaState, 1, 0); // Opens the basic library. |
| | 1195 | lua_pushcfunction(LuaState, luaopen_package); lua_pushstring(LuaState, LUA_LOADLIBNAME); lua_call(LuaState, 1, 0); // Opens the package library. |
| | 1196 | lua_pushcfunction(LuaState, luaopen_table); lua_pushstring(LuaState, LUA_TABLIBNAME); lua_call(LuaState, 1, 0); // Opens the table library. |
| | 1197 | lua_pushcfunction(LuaState, luaopen_io); lua_pushstring(LuaState, LUA_IOLIBNAME); lua_call(LuaState, 1, 0); // Opens the I/O library. |
| | 1198 | lua_pushcfunction(LuaState, luaopen_os); lua_pushstring(LuaState, LUA_OSLIBNAME); lua_call(LuaState, 1, 0); // Opens the OS library. |
| | 1199 | lua_pushcfunction(LuaState, luaopen_string); lua_pushstring(LuaState, LUA_STRLIBNAME); lua_call(LuaState, 1, 0); // Opens the string lib. |
| | 1200 | lua_pushcfunction(LuaState, luaopen_math); lua_pushstring(LuaState, LUA_MATHLIBNAME); lua_call(LuaState, 1, 0); // Opens the math lib. |
| | 1201 | |
| | 1202 | // Load and process the Lua script file with the entity class definitions. |
| | 1203 | if (luaL_loadfile(LuaState, PointFileName.c_str())!=0 || lua_pcall(LuaState, 0, 0, 0)!=0) |
| | 1204 | throw wxString("Couldn't load the file:\n")+lua_tostring(LuaState, -1); |
| | 1205 | |
| | 1206 | wxASSERT(lua_gettop(LuaState)==0); |
| | 1207 | m_PointFilePoints.Clear(); |
| | 1208 | lua_getglobal(LuaState, "Points"); |
| | 1209 | const size_t NumPoints=lua_objlen(LuaState, 1); |
| | 1210 | |
| | 1211 | for (size_t PointNr=1; PointNr<=NumPoints; PointNr++) |
| | 1212 | { |
| | 1213 | // Put that table of the current point onto the stack (at index 2). |
| | 1214 | lua_rawgeti(LuaState, 1, PointNr); |
| | 1215 | m_PointFilePoints.PushBackEmpty(); |
| | 1216 | |
| | 1217 | for (size_t i=2; i<=4; i++) |
| | 1218 | { |
| | 1219 | lua_rawgeti(LuaState, 2, i); |
| | 1220 | m_PointFilePoints[PointNr-1][i-2]=lua_tonumber(LuaState, 3); |
| | 1221 | lua_pop(LuaState, 1); |
| | 1222 | } |
| | 1223 | |
| | 1224 | // Remove the processed points table from the stack again. |
| | 1225 | lua_pop(LuaState, 1); |
| | 1226 | } |
| | 1227 | |
| | 1228 | wxASSERT(lua_gettop(LuaState)==1); |
| | 1229 | lua_pop(LuaState, 1); |
| | 1230 | |
| | 1231 | UpdateAllObservers(UPDATE_POINTFILE); |
| | 1232 | } |
| | 1233 | catch (const wxString& msg) |
| | 1234 | { |
| | 1235 | wxMessageBox(msg, "Error loading "+PointFileName, wxOK | wxICON_ERROR); |
| | 1236 | } |
| | 1237 | |
| | 1238 | // Close the Lua state. |
| | 1239 | if (LuaState) lua_close(LuaState); |