From 773837da98cce5456a809e1b94f840cfecf628d7 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sun, 27 Jan 2013 23:23:53 -0200 Subject: [PATCH] Minor changes in file type handling --- modules/client_styles/styles.lua | 25 +++++++++++----------- src/client/CMakeLists.txt | 2 +- src/client/shadermanager.cpp | 2 +- src/client/spritemanager.cpp | 2 +- src/client/thingtypemanager.cpp | 4 ++-- src/framework/cmake/FindGit2.cmake | 16 -------------- src/framework/core/modulemanager.cpp | 2 +- src/framework/core/resourcemanager.cpp | 11 ++++++++-- src/framework/core/resourcemanager.h | 3 ++- src/framework/graphics/fontmanager.cpp | 2 +- src/framework/graphics/image.cpp | 2 +- src/framework/graphics/particlemanager.cpp | 2 +- src/framework/graphics/texturemanager.cpp | 2 +- src/framework/input/mouse.cpp | 2 +- src/framework/luaengine/luainterface.cpp | 8 +++---- src/framework/luafunctions.cpp | 2 ++ src/framework/sound/soundmanager.cpp | 2 +- src/framework/ui/uimanager.cpp | 4 ++-- 18 files changed, 43 insertions(+), 50 deletions(-) delete mode 100644 src/framework/cmake/FindGit2.cmake diff --git a/modules/client_styles/styles.lua b/modules/client_styles/styles.lua index bd123597..f10cdab2 100644 --- a/modules/client_styles/styles.lua +++ b/modules/client_styles/styles.lua @@ -1,22 +1,23 @@ function init() - local styles = g_resources.listDirectoryFiles('/styles') - for _i,style in pairs(styles) do - if string.ends(style, '.otui') then - g_ui.importStyle('/styles/' .. style) + local files + files = g_resources.listDirectoryFiles('/styles') + for _,file in pairs(files) do + if g_resources.isFileType(file, 'otui') then + g_ui.importStyle('/styles/' .. file) end end - local fonts = g_resources.listDirectoryFiles('/fonts') - for _i,font in pairs(fonts) do - if string.ends(font, '.otfont') then - g_fonts.importFont('/fonts/' .. font) + files = g_resources.listDirectoryFiles('/fonts') + for _,file in pairs(files) do + if g_resources.isFileType(file, 'otfont') then + g_fonts.importFont('/fonts/' .. file) end end - local particles = g_resources.listDirectoryFiles('/particles') - for _i,particle in pairs(particles) do - if string.ends(particle, '.otps') then - g_particles.importParticle('/particles/' .. particle) + files = g_resources.listDirectoryFiles('/particles') + for _,file in pairs(files) do + if g_resources.isFileType(file, 'otps')then + g_particles.importParticle('/particles/' .. file) end end diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index d7e8ee2f..7ca86aa3 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -4,7 +4,7 @@ if(${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 6) endif(${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 6) # client options -add_definitions(-DOTCLIENT) +add_definitions(-DCLIENT) option(BOT_PROTECTION "Enable bot protection" ON) if(BOT_PROTECTION) add_definitions(-DBOT_PROTECTION) diff --git a/src/client/shadermanager.cpp b/src/client/shadermanager.cpp index 1b76c097..05d89706 100644 --- a/src/client/shadermanager.cpp +++ b/src/client/shadermanager.cpp @@ -66,7 +66,7 @@ PainterShaderProgramPtr ShaderManager::createFragmentShader(const std::string& n if(!shader) return nullptr; - file = g_resources.guessFileType(file, "frag"); + file = g_resources.guessFilePath(file, "frag"); shader->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader); if(!shader->addShaderFromSourceFile(Shader::Fragment, file)) { diff --git a/src/client/spritemanager.cpp b/src/client/spritemanager.cpp index b773054a..1b2ac395 100644 --- a/src/client/spritemanager.cpp +++ b/src/client/spritemanager.cpp @@ -45,7 +45,7 @@ bool SpriteManager::loadSpr(std::string file) m_signature = 0; m_loaded = false; try { - file = g_resources.guessFileType(file, "spr"); + file = g_resources.guessFilePath(file, "spr"); m_spritesFile = g_resources.openFile(file); // cache file buffer to avoid lags from hard drive diff --git a/src/client/thingtypemanager.cpp b/src/client/thingtypemanager.cpp index 6105b2e8..076ceb6e 100644 --- a/src/client/thingtypemanager.cpp +++ b/src/client/thingtypemanager.cpp @@ -66,7 +66,7 @@ bool ThingTypeManager::loadDat(std::string file) m_datLoaded = false; m_datSignature = 0; try { - file = g_resources.guessFileType(file, "dat"); + file = g_resources.guessFilePath(file, "dat"); FileStreamPtr fin = g_resources.openFile(file); @@ -100,7 +100,7 @@ bool ThingTypeManager::loadDat(std::string file) bool ThingTypeManager::loadOtml(std::string file) { try { - file = g_resources.guessFileType(file, "otml"); + file = g_resources.guessFilePath(file, "otml"); OTMLDocumentPtr doc = OTMLDocument::parse(file); for(const OTMLNodePtr& node : doc->children()) { diff --git a/src/framework/cmake/FindGit2.cmake b/src/framework/cmake/FindGit2.cmake deleted file mode 100644 index c640ad59..00000000 --- a/src/framework/cmake/FindGit2.cmake +++ /dev/null @@ -1,16 +0,0 @@ -# Try to find the libgit2 library -# GIT2_FOUND - system has libgit2 -# GIT2_INCLUDE_DIR - the libgit2 include directory -# GIT2_LIBRARY - the libgit2 library - -FIND_PATH(GIT2_INCLUDE_DIR NAMES git2.h) -SET(_GIT2_STATIC_LIBS libgit2.a) -SET(_GIT2_SHARED_LIBS libgit2.dll.a git2) -IF(USE_STATIC_LIBS) - FIND_LIBRARY(GIT2_LIBRARY NAMES ${_GIT2_STATIC_LIBS} ${_GIT2_SHARED_LIBS}) -ELSE() - FIND_LIBRARY(GIT2_LIBRARY NAMES ${_GIT2_SHARED_LIBS} ${_GIT2_STATIC_LIBS}) -ENDIF() -INCLUDE(FindPackageHandleStandardArgs) -FIND_PACKAGE_HANDLE_STANDARD_ARGS(GIT2 DEFAULT_MSG GIT2_LIBRARY GIT2_INCLUDE_DIR) -MARK_AS_ADVANCED(GIT2_LIBRARY GIT2_INCLUDE_DIR) diff --git a/src/framework/core/modulemanager.cpp b/src/framework/core/modulemanager.cpp index 745baa5e..45537e67 100644 --- a/src/framework/core/modulemanager.cpp +++ b/src/framework/core/modulemanager.cpp @@ -43,7 +43,7 @@ void ModuleManager::discoverModules() for(const std::string& moduleDir : moduleDirs) { auto moduleFiles = g_resources.listDirectoryFiles("/" + moduleDir); for(const std::string& moduleFile : moduleFiles) { - if(stdext::ends_with(moduleFile, ".otmod")) { + if(g_resources.isFileType(moduleFile, "otmod")) { ModulePtr module = discoverModule("/" + moduleDir + "/" + moduleFile); if(module && module->isAutoLoad()) m_autoLoadModules.insert(std::make_pair(module->getAutoLoadPriority(), module)); diff --git a/src/framework/core/resourcemanager.cpp b/src/framework/core/resourcemanager.cpp index 806d650b..c14ef3c1 100644 --- a/src/framework/core/resourcemanager.cpp +++ b/src/framework/core/resourcemanager.cpp @@ -311,9 +311,16 @@ std::string ResourceManager::getUserDir() return PHYSFS_getUserDir(); } -std::string ResourceManager::guessFileType(const std::string& filename, const std::string& type) +std::string ResourceManager::guessFilePath(const std::string& filename, const std::string& type) { - if(g_resources.fileExists(filename)) + if(isFileType(filename, type)) return filename; return filename + "." + type; } + +bool ResourceManager::isFileType(const std::string& filename, const std::string& type) +{ + if(stdext::ends_with(filename, std::string(".") + type)) + return true; + return false; +} diff --git a/src/framework/core/resourcemanager.h b/src/framework/core/resourcemanager.h index 40588b46..77a61a20 100644 --- a/src/framework/core/resourcemanager.h +++ b/src/framework/core/resourcemanager.h @@ -71,7 +71,8 @@ public: std::string getWorkDir() { return m_workDir; } std::deque getSearchPaths() { return m_searchPaths; } - std::string guessFileType(const std::string& filename, const std::string& type); + std::string guessFilePath(const std::string& filename, const std::string& type); + bool isFileType(const std::string& filename, const std::string& type); private: std::string m_workDir; diff --git a/src/framework/graphics/fontmanager.cpp b/src/framework/graphics/fontmanager.cpp index d72024f2..00c1ffbe 100644 --- a/src/framework/graphics/fontmanager.cpp +++ b/src/framework/graphics/fontmanager.cpp @@ -48,7 +48,7 @@ void FontManager::clearFonts() bool FontManager::importFont(std::string file) { try { - file = g_resources.guessFileType(file, "otfont"); + file = g_resources.guessFilePath(file, "otfont"); OTMLDocumentPtr doc = OTMLDocument::parse(file); OTMLNodePtr fontNode = doc->at("Font"); diff --git a/src/framework/graphics/image.cpp b/src/framework/graphics/image.cpp index cd5fa89e..94eb46cf 100644 --- a/src/framework/graphics/image.cpp +++ b/src/framework/graphics/image.cpp @@ -39,7 +39,7 @@ ImagePtr Image::load(std::string file) { ImagePtr image; try { - file = g_resources.guessFileType(file, "png"); + file = g_resources.guessFilePath(file, "png"); // load image file data image = loadPNG(file); diff --git a/src/framework/graphics/particlemanager.cpp b/src/framework/graphics/particlemanager.cpp index e4ed4287..d1008710 100644 --- a/src/framework/graphics/particlemanager.cpp +++ b/src/framework/graphics/particlemanager.cpp @@ -29,7 +29,7 @@ ParticleManager g_particles; bool ParticleManager::importParticle(std::string file) { try { - file = g_resources.guessFileType(file, "otps"); + file = g_resources.guessFilePath(file, "otps"); OTMLDocumentPtr doc = OTMLDocument::parse(file); for(const OTMLNodePtr& node : doc->children()) { diff --git a/src/framework/graphics/texturemanager.cpp b/src/framework/graphics/texturemanager.cpp index 88670ebd..cb4dd6e7 100644 --- a/src/framework/graphics/texturemanager.cpp +++ b/src/framework/graphics/texturemanager.cpp @@ -78,7 +78,7 @@ TexturePtr TextureManager::getTexture(const std::string& fileName) // texture not found, load it if(!texture) { try { - std::string filePathEx = g_resources.guessFileType(filePath, "png"); + std::string filePathEx = g_resources.guessFilePath(filePath, "png"); // load texture file data std::stringstream fin; diff --git a/src/framework/input/mouse.cpp b/src/framework/input/mouse.cpp index e2c41930..e1fd0f86 100644 --- a/src/framework/input/mouse.cpp +++ b/src/framework/input/mouse.cpp @@ -38,7 +38,7 @@ void Mouse::terminate() void Mouse::loadCursors(std::string filename) { - filename = g_resources.guessFileType(filename, "otml"); + filename = g_resources.guessFilePath(filename, "otml"); try { OTMLDocumentPtr doc = OTMLDocument::parse(filename); OTMLNodePtr cursorsNode = doc->at("Cursors"); diff --git a/src/framework/luaengine/luainterface.cpp b/src/framework/luaengine/luainterface.cpp index 598439bc..54ecb179 100644 --- a/src/framework/luaengine/luainterface.cpp +++ b/src/framework/luaengine/luainterface.cpp @@ -328,7 +328,7 @@ void LuaInterface::loadScript(const std::string& fileName) if(!stdext::starts_with(fileName, "/")) filePath = getCurrentSourcePath() + "/" + filePath; - filePath = g_resources.guessFileType(filePath, "lua"); + filePath = g_resources.guessFilePath(filePath, "lua"); std::string buffer = g_resources.readFileContents(filePath); std::string source = "@" + filePath; @@ -550,7 +550,7 @@ int LuaInterface::luaScriptLoader(lua_State* L) { // loads the script as a function std::string fileName = g_lua.popString(); - fileName += ".lua"; + try { g_lua.loadScript(fileName); return 1; @@ -580,7 +580,7 @@ int LuaInterface::lua_dofiles(lua_State* L) std::string directory = g_lua.popString(); for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) { - if(!stdext::ends_with(fileName, ".lua") && !stdext::ends_with(fileName, ".bc")) + if(!g_resources.isFileType(fileName, "lua")) continue; try { @@ -597,8 +597,6 @@ int LuaInterface::lua_dofiles(lua_State* L) int LuaInterface::lua_loadfile(lua_State* L) { std::string fileName = g_lua.popString(); - if(!stdext::ends_with(fileName, ".lua")) - fileName += ".lua"; try { g_lua.loadScript(fileName); diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index ea0c2fa4..ebd8d027 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -184,6 +184,8 @@ void Application::registerLuaFunctions() g_lua.bindSingletonFunction("g_resources", "getSearchPaths", &ResourceManager::getSearchPaths, &g_resources); g_lua.bindSingletonFunction("g_resources", "listDirectoryFiles", &ResourceManager::listDirectoryFiles, &g_resources); g_lua.bindSingletonFunction("g_resources", "readFileContents", &ResourceManager::readFileContents, &g_resources); + g_lua.bindSingletonFunction("g_resources", "guessFilePath", &ResourceManager::guessFilePath, &g_resources); + g_lua.bindSingletonFunction("g_resources", "isFileType", &ResourceManager::isFileType, &g_resources); // Module g_lua.registerClass(); diff --git a/src/framework/sound/soundmanager.cpp b/src/framework/sound/soundmanager.cpp index e8ee7d35..6c5bff25 100644 --- a/src/framework/sound/soundmanager.cpp +++ b/src/framework/sound/soundmanager.cpp @@ -252,7 +252,7 @@ SoundSourcePtr SoundManager::createSoundSource(const std::string& filename) std::string SoundManager::resolveSoundFile(std::string file) { - file = g_resources.guessFileType(file, "ogg"); + file = g_resources.guessFilePath(file, "ogg"); file = g_resources.resolvePath(file); return file; } diff --git a/src/framework/ui/uimanager.cpp b/src/framework/ui/uimanager.cpp index 43d3e110..0f88e6d9 100644 --- a/src/framework/ui/uimanager.cpp +++ b/src/framework/ui/uimanager.cpp @@ -311,7 +311,7 @@ void UIManager::clearStyles() bool UIManager::importStyle(std::string file) { try { - file = g_resources.guessFileType(file, "otui"); + file = g_resources.guessFilePath(file, "otui"); OTMLDocumentPtr doc = OTMLDocument::parse(file); @@ -394,7 +394,7 @@ std::string UIManager::getStyleClass(const std::string& styleName) UIWidgetPtr UIManager::loadUI(std::string file, const UIWidgetPtr& parent) { try { - file = g_resources.guessFileType(file, "otui"); + file = g_resources.guessFilePath(file, "otui"); OTMLDocumentPtr doc = OTMLDocument::parse(file); UIWidgetPtr widget;