This commit is contained in:
Eduardo Bart 2012-10-24 18:03:15 -02:00
parent c7890e7a49
commit 8bb115d6d4
9 changed files with 54 additions and 51 deletions

View File

@ -7,9 +7,6 @@ g_logger.setLogFile(g_resources.getWorkDir() .. g_app.getCompactName() .. ".log"
-- print first terminal message -- 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()) 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 -- add modules directory to the search path
if not g_resources.addSearchPath(g_resources.getWorkDir() .. "modules", true) then if not g_resources.addSearchPath(g_resources.getWorkDir() .. "modules", true) then
g_logger.fatal("Unable to add modules directory to the search path.") g_logger.fatal("Unable to add modules directory to the search path.")

View File

@ -98,7 +98,7 @@ void Logger::fireOldMessages()
void Logger::setLogFile(const std::string& file) 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()) { if(!m_outFile.is_open() || !m_outFile.good()) {
g_logger.error(stdext::format("Unable to save log to '%s'", file)); g_logger.error(stdext::format("Unable to save log to '%s'", file));
return; return;

View File

@ -34,6 +34,7 @@ ResourceManager g_resources;
void ResourceManager::init(const char *argv0) void ResourceManager::init(const char *argv0)
{ {
PHYSFS_init(argv0); PHYSFS_init(argv0);
PHYSFS_permitSymbolicLinks(1);
} }
void ResourceManager::terminate() void ResourceManager::terminate()
@ -44,21 +45,23 @@ void ResourceManager::terminate()
void ResourceManager::discoverWorkDir(const std::string& appName, const std::string& existentFile) void ResourceManager::discoverWorkDir(const std::string& appName, const std::string& existentFile)
{ {
// search for modules directory // search for modules directory
std::string sep = PHYSFS_getDirSeparator(); std::string possiblePaths[] = { g_resources.getCurrentDir(),
std::string possiblePaths[] = { boost::filesystem::current_path().generic_string() + sep, g_resources.getBaseDir() + "../",
g_resources.getBaseDir() + ".." + sep, g_resources.getBaseDir() + "../share/" + appName + "/",
g_resources.getBaseDir() + ".." + sep + "share" + sep + appName + sep, g_resources.getBaseDir() + appName + "/" };
g_resources.getBaseDir() + appName + sep };
bool found = false; bool found = false;
for(const std::string& dir : possiblePaths) { for(const std::string& dir : possiblePaths) {
// try to directory to modules path to see if it exists if(!PHYSFS_addToSearchPath(dir.c_str(), 0))
std::ifstream fin(dir + existentFile); continue;
if(fin) {
if(PHYSFS_exists(existentFile.c_str())) {
g_logger.debug(stdext::format("Found work dir at '%s'", dir.c_str())); g_logger.debug(stdext::format("Found work dir at '%s'", dir.c_str()));
m_workDir = dir; m_workDir = dir;
found = true; found = true;
break; break;
} }
PHYSFS_removeFromSearchPath(dir.c_str());
} }
if(!found) if(!found)
@ -75,13 +78,18 @@ bool ResourceManager::setupUserWriteDir(const std::string& appWriteDirName)
dirName = appWriteDirName; dirName = appWriteDirName;
#endif #endif
std::string writeDir = userDir + dirName; 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); return setWriteDir(writeDir);
} }
bool ResourceManager::setWriteDir(const std::string& writeDir, bool create) bool ResourceManager::setWriteDir(const std::string& writeDir, bool create)
{ {
boost::filesystem::create_directory(writeDir);
if(!PHYSFS_setWriteDir(writeDir.c_str())) { if(!PHYSFS_setWriteDir(writeDir.c_str())) {
g_logger.error(stdext::format("Unable to set write directory '%s': %s", writeDir, PHYSFS_getLastError())); g_logger.error(stdext::format("Unable to set write directory '%s': %s", writeDir, PHYSFS_getLastError()));
return false; return false;
@ -113,7 +121,7 @@ bool ResourceManager::addSearchPath(const std::string& path, bool pushFront)
} }
if(!found) { 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; return false;
} }
} }
@ -121,7 +129,6 @@ bool ResourceManager::addSearchPath(const std::string& path, bool pushFront)
m_searchPaths.push_front(savePath); m_searchPaths.push_front(savePath);
else else
m_searchPaths.push_back(savePath); m_searchPaths.push_back(savePath);
m_hasSearchPath = true;
return true; return true;
} }
@ -161,31 +168,21 @@ bool ResourceManager::directoryExists(const std::string& directoryName)
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out) void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
{ {
out.clear(std::ios::goodbit); out.clear(std::ios::goodbit);
if(m_hasSearchPath) { std::string fullPath = resolvePath(fileName);
std::string fullPath = resolvePath(fileName); PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); if(!file) {
if(!file) { out.clear(std::ios::failbit);
out.clear(std::ios::failbit); stdext::throw_exception(stdext::format("unable to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError()));
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<char> 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);
}
} else { } else {
std::ifstream fin(fileName); int fileSize = PHYSFS_fileLength(file);
if(!fin) { if(fileSize > 0) {
out.clear(std::ios::failbit); std::vector<char> buffer(fileSize);
stdext::throw_exception(stdext::format("unable to load file '%s': %s", fileName.c_str(), PHYSFS_getLastError())); PHYSFS_read(file, (void*)&buffer[0], 1, fileSize);
} else { out.write(&buffer[0], fileSize);
out << fin.rdbuf(); } 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; 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;
} }

View File

@ -64,6 +64,7 @@ public:
std::string resolvePath(const std::string& path); std::string resolvePath(const std::string& path);
std::string getRealDir(const std::string& path); std::string getRealDir(const std::string& path);
std::string getCurrentDir();
std::string getBaseDir(); std::string getBaseDir();
std::string getWriteDir() { return m_writeDir; } std::string getWriteDir() { return m_writeDir; }
std::string getWorkDir() { return m_workDir; } std::string getWorkDir() { return m_workDir; }
@ -72,7 +73,6 @@ public:
private: private:
std::string m_workDir; std::string m_workDir;
std::string m_writeDir; std::string m_writeDir;
stdext::boolean<false> m_hasSearchPath;
std::deque<std::string> m_searchPaths; std::deque<std::string> m_searchPaths;
}; };

View File

@ -946,7 +946,7 @@ std::string WIN32Window::getClipboardText()
if(hglb) { if(hglb) {
LPTSTR lptstr = (LPTSTR)GlobalLock(hglb); LPTSTR lptstr = (LPTSTR)GlobalLock(hglb);
if(lptstr) { if(lptstr) {
text = stdext::utf8_to_latin1((uchar*)lptstr); text = stdext::utf8_to_latin1(lptstr);
GlobalUnlock(hglb); GlobalUnlock(hglb);
} }
} }

View File

@ -1056,7 +1056,7 @@ std::string X11Window::getClipboardText()
&bytesLeft, &bytesLeft,
&data); &data);
if(len > 0) { if(len > 0) {
clipboardText = stdext::utf8_to_latin1(data); clipboardText = stdext::utf8_to_latin1((char*)data);
} }
} }

View File

@ -67,9 +67,9 @@ uint64_t hex_to_dec(const std::string& str)
return num; 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 = '?'; char c = '?';
uchar opt1 = utf8[0]; uchar opt1 = utf8[0];
*read = 1; *read = 1;
@ -89,10 +89,10 @@ std::string utf8_to_latin1(uchar *utf8)
}; };
std::string out; std::string out;
int len = strlen((char*)utf8); int len = src.length();
for(int i=0; i<len;) { for(int i=0; i<len;) {
int read = 0; int read = 0;
uchar *utf8char = &utf8[i]; uchar *utf8char = (uchar*)&src[i];
out += utf8CharToLatin1(utf8char, &read); out += utf8CharToLatin1(utf8char, &read);
i += read; i += read;
} }

View File

@ -42,7 +42,7 @@ std::string date_time_string();
std::string dec_to_hex(uint64_t num); std::string dec_to_hex(uint64_t num);
uint64_t hex_to_dec(const std::string& str); uint64_t hex_to_dec(const std::string& str);
std::string utf8_to_latin1(uchar *utf8); std::string utf8_to_latin1(const std::string& src);
void tolower(std::string& str); void tolower(std::string& str);
void toupper(std::string& str); void toupper(std::string& str);
void trim(std::string& str); void trim(std::string& str);

View File

@ -40,7 +40,7 @@ int main(int argc, const char* argv[])
// find script init.lua and run it // find script init.lua and run it
g_resources.discoverWorkDir(g_app.getCompactName(), "init.lua"); g_resources.discoverWorkDir(g_app.getCompactName(), "init.lua");
if(!g_lua.safeRunScript(g_resources.getWorkDir() + "init.lua")) if(!g_lua.safeRunScript("init.lua"))
g_logger.fatal("Unable to run script init.lua!"); g_logger.fatal("Unable to run script init.lua!");
// the run application main loop // the run application main loop