Initial integration, lots of broken stuff
[supertux.git] / src / world.cpp
index 8b68b08..d8beb4a 100644 (file)
@@ -20,7 +20,8 @@
 #include <config.h>
 
 #include <stddef.h>
-#include <physfs.h>
+//#include <physfs.h>
+#include <unison/vfs/FileSystem.hpp>
 #include <stdexcept>
 
 #include "world.hpp"
@@ -61,19 +62,21 @@ World::~World()
 void
 World::set_savegame_filename(const std::string& filename)
 {
+  Unison::VFS::FileSystem &fs = Unison::VFS::FileSystem::get();
   this->savegame_filename = filename;
   // make sure the savegame directory exists
   std::string dirname = FileSystem::dirname(filename);
-  if(!PHYSFS_exists(dirname.c_str())) {
-      if(PHYSFS_mkdir(dirname.c_str())) {
+  if(!fs.exists(dirname)) {
+      fs.mkdir(dirname);
+      /*if(PHYSFS_mkdir(dirname.c_str())) {
           std::ostringstream msg;
           msg << "Couldn't create directory for savegames '"
               << dirname << "': " <<PHYSFS_getLastError();
           throw std::runtime_error(msg.str());
-      }
+      }*/
   }
-  if(!PHYSFS_isDirectory(dirname.c_str())) {
+
+  if(!fs.is_dir(dirname)) {
       std::ostringstream msg;
       msg << "Savegame path '" << dirname << "' is not a directory";
       throw std::runtime_error(msg.str());
@@ -84,9 +87,9 @@ void
 World::load(const std::string& filename)
 {
   basedir = FileSystem::dirname(filename);
-  
+
   lisp::Parser parser;
-  std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
+  const lisp::Lisp* root = parser.parse(filename);
 
   const lisp::Lisp* info = root->get_lisp("supertux-world");
   if(info == NULL)
@@ -105,9 +108,16 @@ World::load(const std::string& filename)
 
   // Level info file doesn't define any levels, so read the
   // directory to see what we can find
-      
-  std::string path = basedir + "/";
-  char** files = PHYSFS_enumerateFiles(path.c_str());
+
+  std::string path = basedir;
+  std::vector<std::string> files = Unison::VFS::FileSystem::get().ls(path);
+  for(std::vector<std::string>::iterator iter = files.begin();iter != files.end();++iter)
+  {
+    if(has_suffix(iter->c_str(), ".stl")) {
+      levels.push_back(path + *iter);
+    }
+  }
+  /*char** files = PHYSFS_enumerateFiles(path.c_str());
   if(!files) {
     log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
     return;
@@ -118,7 +128,7 @@ World::load(const std::string& filename)
       levels.push_back(path + *filename);
     }
   }
-  PHYSFS_freeList(files);
+  PHYSFS_freeList(files);*/
 }
 
 void
@@ -127,7 +137,7 @@ World::run()
   using namespace Scripting;
 
   current_ = this;
-  
+
   // create new squirrel table for persisten game state
   HSQUIRRELVM vm = Scripting::global_vm;
 
@@ -139,7 +149,7 @@ World::run()
   sq_pop(vm, 1);
 
   load_state();
-  
+
   std::string filename = basedir + "/world.nut";
   try {
     IFileStream in(filename);
@@ -147,7 +157,7 @@ World::run()
     sq_release(global_vm, &world_thread);
     world_thread = create_thread(global_vm);
     compile_and_run(object_to_vm(world_thread), in, filename);
-  } catch(std::exception& e) {
+  } catch(std::exception& ) {
     // fallback: try to load worldmap worldmap.stwm
     using namespace WorldMapNS;
     main_loop->push_screen(new WorldMap(basedir + "worldmap.stwm"));
@@ -163,12 +173,12 @@ World::save_state()
 
   writer.start_list("supertux-savegame");
   writer.write_int("version", 1);
-  
+
   using namespace WorldMapNS;
   if(WorldMap::current() != NULL) {
     std::ostringstream title;
     title << WorldMap::current()->get_title();
-    title << " (" << WorldMap::current()->solved_level_count() 
+    title << " (" << WorldMap::current()->solved_level_count()
           << "/" << WorldMap::current()->level_count() << ")";
     writer.write_string("title", title.str());
   }
@@ -178,7 +188,7 @@ World::save_state()
   writer.end_list("tux");
 
   writer.start_list("state");
-  
+
   sq_pushroottable(global_vm);
   sq_pushstring(global_vm, "state", -1);
   if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
@@ -187,7 +197,7 @@ World::save_state()
   }
   sq_pop(global_vm, 1);
   writer.end_list("state");
-  
+
   writer.end_list("supertux-savegame");
 }
 
@@ -198,7 +208,7 @@ World::load_state()
 
   try {
     lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (parser.parse(savegame_filename));
+    const lisp::Lisp* root = parser.parse(savegame_filename);
 
     const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
     if(lisp == NULL)
@@ -217,18 +227,18 @@ World::load_state()
     const lisp::Lisp* state = lisp->get_lisp("state");
     if(state == NULL)
       throw std::runtime_error("No state section in savegame");
-    
+
     sq_pushroottable(global_vm);
     sq_pushstring(global_vm, "state", -1);
     if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
       sq_pop(global_vm, 1);
-    
+
     sq_pushstring(global_vm, "state", -1);
     sq_newtable(global_vm);
     load_squirrel_table(global_vm, -1, state);
     if(SQ_FAILED(sq_createslot(global_vm, -3)))
       throw std::runtime_error("Couldn't create state table");
-    sq_pop(global_vm, 1); 
+    sq_pop(global_vm, 1);
   } catch(std::exception& e) {
     log_debug << "Couldn't load savegame: " << e.what() << std::endl;
   }