From eb40ead9684fd22440c6deeaaf5c3408eead208a Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Wed, 28 Dec 2005 00:09:51 +0000 Subject: [PATCH] normalize paths for images so that you can use .. in them SVN-Revision: 2960 --- src/badguy/bomb.cpp | 3 +-- src/badguy/bouncing_snowball.cpp | 5 ++-- src/badguy/dispenser.cpp | 3 +-- src/badguy/fish.cpp | 4 +-- src/badguy/flame.cpp | 3 +-- src/badguy/flyingsnowball.cpp | 6 ++--- src/badguy/jumpy.cpp | 2 +- src/badguy/kugelblitz.cpp | 2 +- src/badguy/mrbomb.cpp | 5 ++-- src/file_system.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ src/file_system.hpp | 5 ++++ src/tile_manager.cpp | 3 +++ src/video/texture_manager.cpp | 4 ++- 13 files changed, 78 insertions(+), 20 deletions(-) diff --git a/src/badguy/bomb.cpp b/src/badguy/bomb.cpp index c611337e8..741d161d1 100644 --- a/src/badguy/bomb.cpp +++ b/src/badguy/bomb.cpp @@ -17,7 +17,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. - #include #include "bomb.hpp" @@ -30,7 +29,7 @@ Bomb::Bomb(const Vector& pos, Direction dir) start_position = pos; bbox.set_pos(pos); bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("bomb"); + sprite = sprite_manager->create("images/creatures/mr_bomb/bomb.sprite"); state = 0; timer.start(TICKINGTIME); this->dir = dir; diff --git a/src/badguy/bouncing_snowball.cpp b/src/badguy/bouncing_snowball.cpp index a704a04d9..80d9bb660 100644 --- a/src/badguy/bouncing_snowball.cpp +++ b/src/badguy/bouncing_snowball.cpp @@ -17,7 +17,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. - #include #include "bouncing_snowball.hpp" @@ -30,7 +29,7 @@ BouncingSnowball::BouncingSnowball(const lisp::Lisp& reader) reader.get("x", start_position.x); reader.get("y", start_position.y); bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("bouncingsnowball"); + sprite = sprite_manager->create("images/creatures/bouncing_snowball/bouncing_snowball.sprite"); set_direction = false; } @@ -39,7 +38,7 @@ BouncingSnowball::BouncingSnowball(float pos_x, float pos_y, Direction d) start_position.x = pos_x; start_position.y = pos_y; bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("bouncingsnowball"); + sprite = sprite_manager->create("images/creatures/bouncing_snowball/bouncing_snowball.sprite"); set_direction = true; initial_direction = d; } diff --git a/src/badguy/dispenser.cpp b/src/badguy/dispenser.cpp index b565bfe21..4aea90c1b 100644 --- a/src/badguy/dispenser.cpp +++ b/src/badguy/dispenser.cpp @@ -17,7 +17,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. - #include #include "dispenser.hpp" @@ -36,7 +35,7 @@ Dispenser::Dispenser(const lisp::Lisp& reader) reader.get("cycle", cycle); reader.get("badguy", badguy); bbox.set_size(32, 32); - sprite = sprite_manager->create("dispenser"); + sprite = sprite_manager->create("images/creatures/dispenser/dispenser.sprite"); if (badguy == "mrrocket") { sprite->set_action(dir == LEFT ? "working-left" : "working-right"); } diff --git a/src/badguy/fish.cpp b/src/badguy/fish.cpp index e5003a4e8..5fca83773 100644 --- a/src/badguy/fish.cpp +++ b/src/badguy/fish.cpp @@ -32,7 +32,7 @@ Fish::Fish(const lisp::Lisp& reader) reader.get("x", start_position.x); reader.get("y", start_position.y); bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("fish"); + sprite = sprite_manager->create("images/creatures/fish/fish.sprite"); physic.enable_gravity(true); } @@ -42,7 +42,7 @@ Fish::Fish(float pos_x, float pos_y) start_position.x = pos_x; start_position.y = pos_y; bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("fish"); + sprite = sprite_manager->create("images/creatures/fish/fish.sprite"); physic.enable_gravity(true); } diff --git a/src/badguy/flame.cpp b/src/badguy/flame.cpp index 1c03b3595..9697a3748 100644 --- a/src/badguy/flame.cpp +++ b/src/badguy/flame.cpp @@ -17,7 +17,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. - #include #include "flame.hpp" @@ -32,7 +31,7 @@ Flame::Flame(const lisp::Lisp& reader) bbox.set_pos(Vector(start_position.x + cos(angle) * radius, start_position.y + sin(angle) * radius)); bbox.set_size(32, 32); - sprite = sprite_manager->create("flame"); + sprite = sprite_manager->create("images/creatures/flame.sprite"); countMe = false; } diff --git a/src/badguy/flyingsnowball.cpp b/src/badguy/flyingsnowball.cpp index 3135c6a21..c25a8cbd2 100644 --- a/src/badguy/flyingsnowball.cpp +++ b/src/badguy/flyingsnowball.cpp @@ -17,8 +17,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. - #include + #include #include "flyingsnowball.hpp" @@ -31,7 +31,7 @@ FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader) reader.get("x", start_position.x); reader.get("y", start_position.y); bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("flyingsnowball"); + sprite = sprite_manager->create("images/creatures/flying_snowball/flying_snowball.sprite"); physic.enable_gravity(false); } @@ -40,7 +40,7 @@ FlyingSnowBall::FlyingSnowBall(float pos_x, float pos_y) start_position.x = pos_x; start_position.y = pos_y; bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("flyingsnowball"); + sprite = sprite_manager->create("images/creatures/flying_snowball/flying_snowball.sprite"); physic.enable_gravity(false); } diff --git a/src/badguy/jumpy.cpp b/src/badguy/jumpy.cpp index 9b2a6b770..2b450b23f 100644 --- a/src/badguy/jumpy.cpp +++ b/src/badguy/jumpy.cpp @@ -31,7 +31,7 @@ Jumpy::Jumpy(const lisp::Lisp& reader) reader.get("x", start_position.x); reader.get("y", start_position.y); bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("jumpy"); + sprite = sprite_manager->create("images/creatures/jumpy/jumpy.sprite"); } void diff --git a/src/badguy/kugelblitz.cpp b/src/badguy/kugelblitz.cpp index 4c7f5eb32..eb4fc2ebc 100644 --- a/src/badguy/kugelblitz.cpp +++ b/src/badguy/kugelblitz.cpp @@ -38,7 +38,7 @@ Kugelblitz::Kugelblitz(const lisp::Lisp& reader) reader.get("x", start_position.x); start_position.y = 0; //place above visible area bbox.set_size(63.8, 63.8); - sprite = sprite_manager->create("kugelblitz"); + sprite = sprite_manager->create("images/creatures/kugelblitz/kugelblitz.sprite"); sprite->set_action("falling"); physic.enable_gravity(false); } diff --git a/src/badguy/mrbomb.cpp b/src/badguy/mrbomb.cpp index 596899e60..282961687 100644 --- a/src/badguy/mrbomb.cpp +++ b/src/badguy/mrbomb.cpp @@ -17,7 +17,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. - #include #include "mrbomb.hpp" @@ -30,7 +29,7 @@ MrBomb::MrBomb(const lisp::Lisp& reader) reader.get("x", start_position.x); reader.get("y", start_position.y); bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("mrbomb"); + sprite = sprite_manager->create("images/creatures/mr_bomb/mr_bomb.sprite"); set_direction = false; } @@ -39,7 +38,7 @@ MrBomb::MrBomb(float pos_x, float pos_y, Direction d) start_position.x = pos_x; start_position.y = pos_y; bbox.set_size(31.8, 31.8); - sprite = sprite_manager->create("mrbomb"); + sprite = sprite_manager->create("images/creatures/mr_bomb/mr_bomb.sprite"); set_direction = true; initial_direction = d; } diff --git a/src/file_system.cpp b/src/file_system.cpp index 4989540c4..826d4b753 100644 --- a/src/file_system.cpp +++ b/src/file_system.cpp @@ -3,6 +3,9 @@ #include "file_system.hpp" #include +#include +#include +#include namespace FileSystem { @@ -25,5 +28,55 @@ std::string basename(const std::string& filename) return filename.substr(p, filename.size()-p); } +std::string normalize(const std::string& filename) +{ + std::vector path_stack; + + const char* p = filename.c_str(); + + while(true) { + while(*p == '/') { + p++; + continue; + } + + const char* pstart = p; + while(*p != '/' && *p != 0) { + ++p; + } + + size_t len = p - pstart; + if(len == 0) + break; + + std::string pathelem(pstart, p-pstart); + if(pathelem == ".") + continue; + + if(pathelem == "..") { + if(path_stack.empty()) { + std::cout << "Invalid '..' in path '" << filename << "'.\n"; + // push it into the result path so that the users sees his error... + path_stack.push_back(pathelem); + } else { + path_stack.pop_back(); + } + } else { + path_stack.push_back(pathelem); + } + } + + // construct path + std::ostringstream result; + for(std::vector::iterator i = path_stack.begin(); + i != path_stack.end(); ++i) { + result << '/' << *i; + } + if(path_stack.empty()) + result << '/'; + + return result.str(); +} + } diff --git a/src/file_system.hpp b/src/file_system.hpp index 47f86f4a3..224190485 100644 --- a/src/file_system.hpp +++ b/src/file_system.hpp @@ -8,6 +8,11 @@ namespace FileSystem { std::string dirname(const std::string& filename); std::string basename(const std::string& filename); + /** + * normalize filename so that "blup/bla/blo/../../bar" will become + * "blup/bar" + */ + std::string normalize(const std::string& filename); } #endif diff --git a/src/tile_manager.cpp b/src/tile_manager.cpp index e6a3a155b..25c4997a9 100644 --- a/src/tile_manager.cpp +++ b/src/tile_manager.cpp @@ -72,6 +72,9 @@ void TileManager::load_tileset(std::string filename) while(tile->id >= tiles.size()) { tiles.push_back(0); } + if(tiles[tile->id] != 0) { + std::cout << "Warning: Tile with ID " << tile->id << " redefined\n"; + } tiles[tile->id] = tile; } else if(iter.item() == "tilegroup") { TileGroup tilegroup; diff --git a/src/video/texture_manager.cpp b/src/video/texture_manager.cpp index ab5788eaa..73c71fd26 100644 --- a/src/video/texture_manager.cpp +++ b/src/video/texture_manager.cpp @@ -12,6 +12,7 @@ #include "physfs/physfs_sdl.hpp" #include "image_texture.hpp" #include "glutil.hpp" +#include "file_system.hpp" TextureManager* texture_manager = NULL; @@ -33,8 +34,9 @@ TextureManager::~TextureManager() } ImageTexture* -TextureManager::get(const std::string& filename) +TextureManager::get(const std::string& _filename) { + std::string filename = FileSystem::normalize(_filename); ImageTextures::iterator i = image_textures.find(filename); ImageTexture* texture = NULL; -- 2.11.0