diff --git a/init.lua b/init.lua index 63c9626a..a3864d0c 100644 --- a/init.lua +++ b/init.lua @@ -7,9 +7,6 @@ g_logger.setLogFile(g_resources.getWorkDir() .. g_app.getCompactName() .. ".log" -- print first terminal message g_logger.info(g_app.getName() .. ' ' .. g_app.getVersion() .. ' rev ' .. g_app.getBuildRevision() .. ' (' .. g_app.getBuildCommit() .. ') built on ' .. g_app.getBuildDate() .. ' for arch ' .. g_app.getBuildArch()) ---add base folder to search path -g_resources.addSearchPath(g_resources.getWorkDir()) - -- add modules directory to the search path if not g_resources.addSearchPath(g_resources.getWorkDir() .. "modules", true) then g_logger.fatal("Unable to add modules directory to the search path.") diff --git a/src/framework/core/logger.cpp b/src/framework/core/logger.cpp index 77f30422..8ba729e4 100644 --- a/src/framework/core/logger.cpp +++ b/src/framework/core/logger.cpp @@ -98,7 +98,7 @@ void Logger::fireOldMessages() void Logger::setLogFile(const std::string& file) { - m_outFile.open(file.c_str(), std::ios::out | std::ios::app); + m_outFile.open(stdext::utf8_to_latin1(file.c_str()).c_str(), std::ios::out | std::ios::app); if(!m_outFile.is_open() || !m_outFile.good()) { g_logger.error(stdext::format("Unable to save log to '%s'", file)); return; diff --git a/src/framework/core/resourcemanager.cpp b/src/framework/core/resourcemanager.cpp index 4754ce2e..02f68e45 100644 --- a/src/framework/core/resourcemanager.cpp +++ b/src/framework/core/resourcemanager.cpp @@ -34,6 +34,7 @@ ResourceManager g_resources; void ResourceManager::init(const char *argv0) { PHYSFS_init(argv0); + PHYSFS_permitSymbolicLinks(1); } void ResourceManager::terminate() @@ -44,21 +45,23 @@ void ResourceManager::terminate() void ResourceManager::discoverWorkDir(const std::string& appName, const std::string& existentFile) { // search for modules directory - std::string sep = PHYSFS_getDirSeparator(); - std::string possiblePaths[] = { boost::filesystem::current_path().generic_string() + sep, - g_resources.getBaseDir() + ".." + sep, - g_resources.getBaseDir() + ".." + sep + "share" + sep + appName + sep, - g_resources.getBaseDir() + appName + sep }; + std::string possiblePaths[] = { g_resources.getCurrentDir(), + g_resources.getBaseDir() + "../", + g_resources.getBaseDir() + "../share/" + appName + "/", + g_resources.getBaseDir() + appName + "/" }; + bool found = false; for(const std::string& dir : possiblePaths) { - // try to directory to modules path to see if it exists - std::ifstream fin(dir + existentFile); - if(fin) { + if(!PHYSFS_addToSearchPath(dir.c_str(), 0)) + continue; + + if(PHYSFS_exists(existentFile.c_str())) { g_logger.debug(stdext::format("Found work dir at '%s'", dir.c_str())); m_workDir = dir; found = true; break; } + PHYSFS_removeFromSearchPath(dir.c_str()); } if(!found) @@ -75,13 +78,18 @@ bool ResourceManager::setupUserWriteDir(const std::string& appWriteDirName) dirName = appWriteDirName; #endif std::string writeDir = userDir + dirName; + + if(!PHYSFS_setWriteDir(writeDir.c_str())) { + if(!PHYSFS_setWriteDir(userDir.c_str()) || !PHYSFS_mkdir(dirName.c_str())) { + g_logger.error(stdext::format("Unable to create write directory '%s': %s", writeDir, PHYSFS_getLastError())); + return false; + } + } return setWriteDir(writeDir); } bool ResourceManager::setWriteDir(const std::string& writeDir, bool create) { - boost::filesystem::create_directory(writeDir); - if(!PHYSFS_setWriteDir(writeDir.c_str())) { g_logger.error(stdext::format("Unable to set write directory '%s': %s", writeDir, PHYSFS_getLastError())); return false; @@ -113,7 +121,7 @@ bool ResourceManager::addSearchPath(const std::string& path, bool pushFront) } if(!found) { - g_logger.error(stdext::format("Could not add '%s' to directory search path. Reason %s", path, PHYSFS_getLastError())); + //g_logger.error(stdext::format("Could not add '%s' to directory search path. Reason %s", path, PHYSFS_getLastError())); return false; } } @@ -121,7 +129,6 @@ bool ResourceManager::addSearchPath(const std::string& path, bool pushFront) m_searchPaths.push_front(savePath); else m_searchPaths.push_back(savePath); - m_hasSearchPath = true; return true; } @@ -161,31 +168,21 @@ bool ResourceManager::directoryExists(const std::string& directoryName) void ResourceManager::loadFile(const std::string& fileName, std::iostream& out) { out.clear(std::ios::goodbit); - if(m_hasSearchPath) { - std::string fullPath = resolvePath(fileName); - PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); - if(!file) { - out.clear(std::ios::failbit); - stdext::throw_exception(stdext::format("unable to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError())); - } else { - int fileSize = PHYSFS_fileLength(file); - if(fileSize > 0) { - std::vector buffer(fileSize); - PHYSFS_read(file, (void*)&buffer[0], 1, fileSize); - out.write(&buffer[0], fileSize); - } else - out.clear(std::ios::eofbit); - PHYSFS_close(file); - out.seekg(0, std::ios::beg); - } + std::string fullPath = resolvePath(fileName); + PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); + if(!file) { + out.clear(std::ios::failbit); + stdext::throw_exception(stdext::format("unable to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError())); } else { - std::ifstream fin(fileName); - if(!fin) { - out.clear(std::ios::failbit); - stdext::throw_exception(stdext::format("unable to load file '%s': %s", fileName.c_str(), PHYSFS_getLastError())); - } else { - out << fin.rdbuf(); - } + int fileSize = PHYSFS_fileLength(file); + if(fileSize > 0) { + std::vector buffer(fileSize); + PHYSFS_read(file, (void*)&buffer[0], 1, fileSize); + out.write(&buffer[0], fileSize); + } else + out.clear(std::ios::eofbit); + PHYSFS_close(file); + out.seekg(0, std::ios::beg); } } @@ -300,8 +297,17 @@ std::string ResourceManager::getRealDir(const std::string& path) return dir; } -std::string ResourceManager::getBaseDir() +std::string ResourceManager::getCurrentDir() { - return PHYSFS_getBaseDir(); + char buffer[2048]; + PHYSFS_utf8FromLatin1((boost::filesystem::current_path().generic_string() + "/").c_str(), buffer, 2048); + return buffer; +} + +std::string ResourceManager::getBaseDir() +{ + char buffer[2048]; + PHYSFS_utf8FromLatin1(PHYSFS_getBaseDir(), buffer, 2048); + return buffer; } diff --git a/src/framework/core/resourcemanager.h b/src/framework/core/resourcemanager.h index 89a39f05..bfa6a931 100644 --- a/src/framework/core/resourcemanager.h +++ b/src/framework/core/resourcemanager.h @@ -64,6 +64,7 @@ public: std::string resolvePath(const std::string& path); std::string getRealDir(const std::string& path); + std::string getCurrentDir(); std::string getBaseDir(); std::string getWriteDir() { return m_writeDir; } std::string getWorkDir() { return m_workDir; } @@ -72,7 +73,6 @@ public: private: std::string m_workDir; std::string m_writeDir; - stdext::boolean m_hasSearchPath; std::deque m_searchPaths; }; diff --git a/src/framework/platform/win32window.cpp b/src/framework/platform/win32window.cpp index 6b21c2ce..01e530a5 100644 --- a/src/framework/platform/win32window.cpp +++ b/src/framework/platform/win32window.cpp @@ -946,7 +946,7 @@ std::string WIN32Window::getClipboardText() if(hglb) { LPTSTR lptstr = (LPTSTR)GlobalLock(hglb); if(lptstr) { - text = stdext::utf8_to_latin1((uchar*)lptstr); + text = stdext::utf8_to_latin1(lptstr); GlobalUnlock(hglb); } } diff --git a/src/framework/platform/x11window.cpp b/src/framework/platform/x11window.cpp index 6fc6882b..1c9794dc 100644 --- a/src/framework/platform/x11window.cpp +++ b/src/framework/platform/x11window.cpp @@ -1056,7 +1056,7 @@ std::string X11Window::getClipboardText() &bytesLeft, &data); if(len > 0) { - clipboardText = stdext::utf8_to_latin1(data); + clipboardText = stdext::utf8_to_latin1((char*)data); } } diff --git a/src/framework/stdext/string.cpp b/src/framework/stdext/string.cpp index 232f9ef5..c5373819 100644 --- a/src/framework/stdext/string.cpp +++ b/src/framework/stdext/string.cpp @@ -67,9 +67,9 @@ uint64_t hex_to_dec(const std::string& str) return num; } -std::string utf8_to_latin1(uchar *utf8) +std::string utf8_to_latin1(const std::string& src) { - auto utf8CharToLatin1 = [](uchar *utf8, int *read) -> char { + auto utf8CharToLatin1 = [](const uchar *utf8, int *read) -> char { char c = '?'; uchar opt1 = utf8[0]; *read = 1; @@ -89,10 +89,10 @@ std::string utf8_to_latin1(uchar *utf8) }; std::string out; - int len = strlen((char*)utf8); + int len = src.length(); for(int i=0; i