master
Eduardo Bart 12 years ago
parent c7890e7a49
commit 8bb115d6d4

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

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

@ -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<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);
}
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<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);
}
}
@ -300,8 +297,17 @@ std::string ResourceManager::getRealDir(const std::string& path)
return dir;
}
std::string ResourceManager::getCurrentDir()
{
char buffer[2048];
PHYSFS_utf8FromLatin1((boost::filesystem::current_path().generic_string() + "/").c_str(), buffer, 2048);
return buffer;
}
std::string ResourceManager::getBaseDir()
{
return PHYSFS_getBaseDir();
char buffer[2048];
PHYSFS_utf8FromLatin1(PHYSFS_getBaseDir(), buffer, 2048);
return buffer;
}

@ -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<false> m_hasSearchPath;
std::deque<std::string> m_searchPaths;
};

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

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

@ -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<len;) {
int read = 0;
uchar *utf8char = &utf8[i];
uchar *utf8char = (uchar*)&src[i];
out += utf8CharToLatin1(utf8char, &read);
i += read;
}

@ -42,7 +42,7 @@ std::string date_time_string();
std::string dec_to_hex(uint64_t num);
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 toupper(std::string& str);
void trim(std::string& str);

@ -40,7 +40,7 @@ int main(int argc, const char* argv[])
// find script init.lua and run it
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!");
// the run application main loop

Loading…
Cancel
Save