Minor changes in file type handling

This commit is contained in:
Eduardo Bart 2013-01-27 23:23:53 -02:00
parent 4536c68f00
commit 773837da98
18 changed files with 43 additions and 50 deletions

View File

@ -1,22 +1,23 @@
function init() function init()
local styles = g_resources.listDirectoryFiles('/styles') local files
for _i,style in pairs(styles) do files = g_resources.listDirectoryFiles('/styles')
if string.ends(style, '.otui') then for _,file in pairs(files) do
g_ui.importStyle('/styles/' .. style) if g_resources.isFileType(file, 'otui') then
g_ui.importStyle('/styles/' .. file)
end end
end end
local fonts = g_resources.listDirectoryFiles('/fonts') files = g_resources.listDirectoryFiles('/fonts')
for _i,font in pairs(fonts) do for _,file in pairs(files) do
if string.ends(font, '.otfont') then if g_resources.isFileType(file, 'otfont') then
g_fonts.importFont('/fonts/' .. font) g_fonts.importFont('/fonts/' .. file)
end end
end end
local particles = g_resources.listDirectoryFiles('/particles') files = g_resources.listDirectoryFiles('/particles')
for _i,particle in pairs(particles) do for _,file in pairs(files) do
if string.ends(particle, '.otps') then if g_resources.isFileType(file, 'otps')then
g_particles.importParticle('/particles/' .. particle) g_particles.importParticle('/particles/' .. file)
end end
end end

View File

@ -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) endif(${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 6)
# client options # client options
add_definitions(-DOTCLIENT) add_definitions(-DCLIENT)
option(BOT_PROTECTION "Enable bot protection" ON) option(BOT_PROTECTION "Enable bot protection" ON)
if(BOT_PROTECTION) if(BOT_PROTECTION)
add_definitions(-DBOT_PROTECTION) add_definitions(-DBOT_PROTECTION)

View File

@ -66,7 +66,7 @@ PainterShaderProgramPtr ShaderManager::createFragmentShader(const std::string& n
if(!shader) if(!shader)
return nullptr; return nullptr;
file = g_resources.guessFileType(file, "frag"); file = g_resources.guessFilePath(file, "frag");
shader->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader); shader->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
if(!shader->addShaderFromSourceFile(Shader::Fragment, file)) { if(!shader->addShaderFromSourceFile(Shader::Fragment, file)) {

View File

@ -45,7 +45,7 @@ bool SpriteManager::loadSpr(std::string file)
m_signature = 0; m_signature = 0;
m_loaded = false; m_loaded = false;
try { try {
file = g_resources.guessFileType(file, "spr"); file = g_resources.guessFilePath(file, "spr");
m_spritesFile = g_resources.openFile(file); m_spritesFile = g_resources.openFile(file);
// cache file buffer to avoid lags from hard drive // cache file buffer to avoid lags from hard drive

View File

@ -66,7 +66,7 @@ bool ThingTypeManager::loadDat(std::string file)
m_datLoaded = false; m_datLoaded = false;
m_datSignature = 0; m_datSignature = 0;
try { try {
file = g_resources.guessFileType(file, "dat"); file = g_resources.guessFilePath(file, "dat");
FileStreamPtr fin = g_resources.openFile(file); FileStreamPtr fin = g_resources.openFile(file);
@ -100,7 +100,7 @@ bool ThingTypeManager::loadDat(std::string file)
bool ThingTypeManager::loadOtml(std::string file) bool ThingTypeManager::loadOtml(std::string file)
{ {
try { try {
file = g_resources.guessFileType(file, "otml"); file = g_resources.guessFilePath(file, "otml");
OTMLDocumentPtr doc = OTMLDocument::parse(file); OTMLDocumentPtr doc = OTMLDocument::parse(file);
for(const OTMLNodePtr& node : doc->children()) { for(const OTMLNodePtr& node : doc->children()) {

View File

@ -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)

View File

@ -43,7 +43,7 @@ void ModuleManager::discoverModules()
for(const std::string& moduleDir : moduleDirs) { for(const std::string& moduleDir : moduleDirs) {
auto moduleFiles = g_resources.listDirectoryFiles("/" + moduleDir); auto moduleFiles = g_resources.listDirectoryFiles("/" + moduleDir);
for(const std::string& moduleFile : moduleFiles) { for(const std::string& moduleFile : moduleFiles) {
if(stdext::ends_with(moduleFile, ".otmod")) { if(g_resources.isFileType(moduleFile, "otmod")) {
ModulePtr module = discoverModule("/" + moduleDir + "/" + moduleFile); ModulePtr module = discoverModule("/" + moduleDir + "/" + moduleFile);
if(module && module->isAutoLoad()) if(module && module->isAutoLoad())
m_autoLoadModules.insert(std::make_pair(module->getAutoLoadPriority(), module)); m_autoLoadModules.insert(std::make_pair(module->getAutoLoadPriority(), module));

View File

@ -311,9 +311,16 @@ std::string ResourceManager::getUserDir()
return PHYSFS_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;
return filename + "." + type; 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;
}

View File

@ -71,7 +71,8 @@ public:
std::string getWorkDir() { return m_workDir; } std::string getWorkDir() { return m_workDir; }
std::deque<std::string> getSearchPaths() { return m_searchPaths; } std::deque<std::string> 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: private:
std::string m_workDir; std::string m_workDir;

View File

@ -48,7 +48,7 @@ void FontManager::clearFonts()
bool FontManager::importFont(std::string file) bool FontManager::importFont(std::string file)
{ {
try { try {
file = g_resources.guessFileType(file, "otfont"); file = g_resources.guessFilePath(file, "otfont");
OTMLDocumentPtr doc = OTMLDocument::parse(file); OTMLDocumentPtr doc = OTMLDocument::parse(file);
OTMLNodePtr fontNode = doc->at("Font"); OTMLNodePtr fontNode = doc->at("Font");

View File

@ -39,7 +39,7 @@ ImagePtr Image::load(std::string file)
{ {
ImagePtr image; ImagePtr image;
try { try {
file = g_resources.guessFileType(file, "png"); file = g_resources.guessFilePath(file, "png");
// load image file data // load image file data
image = loadPNG(file); image = loadPNG(file);

View File

@ -29,7 +29,7 @@ ParticleManager g_particles;
bool ParticleManager::importParticle(std::string file) bool ParticleManager::importParticle(std::string file)
{ {
try { try {
file = g_resources.guessFileType(file, "otps"); file = g_resources.guessFilePath(file, "otps");
OTMLDocumentPtr doc = OTMLDocument::parse(file); OTMLDocumentPtr doc = OTMLDocument::parse(file);
for(const OTMLNodePtr& node : doc->children()) { for(const OTMLNodePtr& node : doc->children()) {

View File

@ -78,7 +78,7 @@ TexturePtr TextureManager::getTexture(const std::string& fileName)
// texture not found, load it // texture not found, load it
if(!texture) { if(!texture) {
try { try {
std::string filePathEx = g_resources.guessFileType(filePath, "png"); std::string filePathEx = g_resources.guessFilePath(filePath, "png");
// load texture file data // load texture file data
std::stringstream fin; std::stringstream fin;

View File

@ -38,7 +38,7 @@ void Mouse::terminate()
void Mouse::loadCursors(std::string filename) void Mouse::loadCursors(std::string filename)
{ {
filename = g_resources.guessFileType(filename, "otml"); filename = g_resources.guessFilePath(filename, "otml");
try { try {
OTMLDocumentPtr doc = OTMLDocument::parse(filename); OTMLDocumentPtr doc = OTMLDocument::parse(filename);
OTMLNodePtr cursorsNode = doc->at("Cursors"); OTMLNodePtr cursorsNode = doc->at("Cursors");

View File

@ -328,7 +328,7 @@ void LuaInterface::loadScript(const std::string& fileName)
if(!stdext::starts_with(fileName, "/")) if(!stdext::starts_with(fileName, "/"))
filePath = getCurrentSourcePath() + "/" + filePath; filePath = getCurrentSourcePath() + "/" + filePath;
filePath = g_resources.guessFileType(filePath, "lua"); filePath = g_resources.guessFilePath(filePath, "lua");
std::string buffer = g_resources.readFileContents(filePath); std::string buffer = g_resources.readFileContents(filePath);
std::string source = "@" + filePath; std::string source = "@" + filePath;
@ -550,7 +550,7 @@ int LuaInterface::luaScriptLoader(lua_State* L)
{ {
// loads the script as a function // loads the script as a function
std::string fileName = g_lua.popString(); std::string fileName = g_lua.popString();
fileName += ".lua";
try { try {
g_lua.loadScript(fileName); g_lua.loadScript(fileName);
return 1; return 1;
@ -580,7 +580,7 @@ int LuaInterface::lua_dofiles(lua_State* L)
std::string directory = g_lua.popString(); std::string directory = g_lua.popString();
for(const std::string& fileName : g_resources.listDirectoryFiles(directory)) { 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; continue;
try { try {
@ -597,8 +597,6 @@ int LuaInterface::lua_dofiles(lua_State* L)
int LuaInterface::lua_loadfile(lua_State* L) int LuaInterface::lua_loadfile(lua_State* L)
{ {
std::string fileName = g_lua.popString(); std::string fileName = g_lua.popString();
if(!stdext::ends_with(fileName, ".lua"))
fileName += ".lua";
try { try {
g_lua.loadScript(fileName); g_lua.loadScript(fileName);

View File

@ -184,6 +184,8 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_resources", "getSearchPaths", &ResourceManager::getSearchPaths, &g_resources); 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", "listDirectoryFiles", &ResourceManager::listDirectoryFiles, &g_resources);
g_lua.bindSingletonFunction("g_resources", "readFileContents", &ResourceManager::readFileContents, &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 // Module
g_lua.registerClass<Module>(); g_lua.registerClass<Module>();

View File

@ -252,7 +252,7 @@ SoundSourcePtr SoundManager::createSoundSource(const std::string& filename)
std::string SoundManager::resolveSoundFile(std::string file) std::string SoundManager::resolveSoundFile(std::string file)
{ {
file = g_resources.guessFileType(file, "ogg"); file = g_resources.guessFilePath(file, "ogg");
file = g_resources.resolvePath(file); file = g_resources.resolvePath(file);
return file; return file;
} }

View File

@ -311,7 +311,7 @@ void UIManager::clearStyles()
bool UIManager::importStyle(std::string file) bool UIManager::importStyle(std::string file)
{ {
try { try {
file = g_resources.guessFileType(file, "otui"); file = g_resources.guessFilePath(file, "otui");
OTMLDocumentPtr doc = OTMLDocument::parse(file); 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) UIWidgetPtr UIManager::loadUI(std::string file, const UIWidgetPtr& parent)
{ {
try { try {
file = g_resources.guessFileType(file, "otui"); file = g_resources.guessFilePath(file, "otui");
OTMLDocumentPtr doc = OTMLDocument::parse(file); OTMLDocumentPtr doc = OTMLDocument::parse(file);
UIWidgetPtr widget; UIWidgetPtr widget;