mirror of
https://github.com/edubart/otclient.git
synced 2025-12-14 12:49:47 +01:00
Refactoring and flexibility changes
* Split game module into game and game_interface * Move core_lib to corelib * Move miniwindow to corelib * Introduce init.lua script for initializing the client, giving much more flexibility * OTClient is no longer Application derived and is much simpler
This commit is contained in:
@@ -45,7 +45,7 @@ bool ConfigManager::load(const std::string& file)
|
||||
m_confsDoc = confsDoc;
|
||||
return true;
|
||||
} catch(stdext::exception& e) {
|
||||
g_logger.error(stdext::format("Unable to load configuration file: %s", e.what()));
|
||||
g_logger.error(stdext::format("Unable to parse configuration file '%s'", e.what()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
Event(const std::function<void()>& callback) : m_callback(callback), m_canceled(false), m_executed(false) { }
|
||||
virtual ~Event() {
|
||||
// assure that we lost callback refs
|
||||
assert(m_callback == nullptr);
|
||||
//assert(m_callback == nullptr);
|
||||
}
|
||||
|
||||
virtual void execute() {
|
||||
|
||||
@@ -28,6 +28,11 @@ Logger g_logger;
|
||||
|
||||
void Logger::log(Fw::LogLevel level, const std::string& message)
|
||||
{
|
||||
#ifdef NDEBUG
|
||||
if(level == Fw::LogDebug)
|
||||
return;
|
||||
#endif
|
||||
|
||||
static bool ignoreLogs = false;
|
||||
if(ignoreLogs)
|
||||
return;
|
||||
|
||||
@@ -53,7 +53,7 @@ bool Module::load()
|
||||
m_loadCallback();
|
||||
|
||||
m_loaded = true;
|
||||
//g_logger.info(stdext::format("Loaded module '%s'", m_name));
|
||||
g_logger.debug(stdext::format("Loaded module '%s'", m_name));
|
||||
g_modules.updateModuleLoadOrder(asModule());
|
||||
|
||||
for(const std::string& modName : m_loadLaterModules) {
|
||||
|
||||
@@ -64,44 +64,6 @@ void ModuleManager::autoLoadModules(int maxPriority)
|
||||
}
|
||||
}
|
||||
|
||||
void ModuleManager::discoverModulesPath()
|
||||
{
|
||||
// search for modules directory
|
||||
std::string possibleModulesDirs[] = { "modules",
|
||||
g_resources.getBaseDir() + "modules",
|
||||
g_resources.getBaseDir() + "../modules",
|
||||
g_resources.getBaseDir() + "../share/" + g_app->getName() + "/modules",
|
||||
"" };
|
||||
bool found = false;
|
||||
for(const std::string& dir : possibleModulesDirs) {
|
||||
// try to add module directory
|
||||
if(g_resources.addToSearchPath(dir, false)) {
|
||||
//g_logger.info(stdext::format("Using modules directory '%s'", dir.c_str()));
|
||||
m_modulesPath = dir;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found)
|
||||
g_logger.fatal("Could not find modules directory");
|
||||
|
||||
// search for addons directory
|
||||
std::string possibleAddonsDirs[] = { "addons",
|
||||
g_resources.getBaseDir() + "addons",
|
||||
g_resources.getBaseDir() + "../addons",
|
||||
g_resources.getBaseDir() + "../share/" + g_app->getName() + "/addons",
|
||||
"" };
|
||||
for(const std::string& dir : possibleAddonsDirs) {
|
||||
// try to add module directory
|
||||
if(g_resources.addToSearchPath(dir, true)) {
|
||||
//g_logger.info(stdext::format("Using addons directory '%s'", dir.c_str()));
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ModulePtr ModuleManager::discoverModule(const std::string& moduleFile)
|
||||
{
|
||||
ModulePtr module;
|
||||
|
||||
@@ -30,14 +30,12 @@ class ModuleManager
|
||||
public:
|
||||
void clear();
|
||||
|
||||
void discoverModulesPath();
|
||||
void discoverModules();
|
||||
void autoLoadModules(int maxPriority);
|
||||
ModulePtr discoverModule(const std::string& moduleFile);
|
||||
void ensureModuleLoaded(const std::string& moduleName);
|
||||
void unloadModules();
|
||||
void reloadModules();
|
||||
std::string getModulesPath() { return m_modulesPath; }
|
||||
|
||||
ModulePtr getModule(const std::string& moduleName);
|
||||
std::deque<ModulePtr> getModules() { return m_modules; }
|
||||
@@ -48,7 +46,6 @@ protected:
|
||||
friend class Module;
|
||||
|
||||
private:
|
||||
std::string m_modulesPath;
|
||||
std::deque<ModulePtr> m_modules;
|
||||
std::multimap<int, ModulePtr> m_autoLoadModules;
|
||||
};
|
||||
|
||||
@@ -40,22 +40,58 @@ void ResourceManager::terminate()
|
||||
PHYSFS_deinit();
|
||||
}
|
||||
|
||||
void ResourceManager::discoverWorkDir(const std::string& appName, const std::string& existentFile)
|
||||
{
|
||||
// search for modules directory
|
||||
std::string sep = PHYSFS_getDirSeparator();
|
||||
std::string possiblePaths[] = { "",
|
||||
g_resources.getBaseDir(),
|
||||
g_resources.getBaseDir() + ".." + sep,
|
||||
g_resources.getBaseDir() + ".." + sep + "share" + sep + appName + sep,
|
||||
g_resources.getBaseDir() + appName + sep };
|
||||
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) {
|
||||
g_logger.debug(stdext::format("Found work dir at '%s'", dir.c_str()));
|
||||
m_workDir = dir;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found)
|
||||
g_logger.fatal("Unable to find application work directory.");
|
||||
}
|
||||
|
||||
bool ResourceManager::setupWriteDir(const std::string& appWriteDirName)
|
||||
{
|
||||
std::string userDir = PHYSFS_getUserDir();
|
||||
std::string dirName = stdext::format(".%s", appWriteDirName);
|
||||
std::string dirName;
|
||||
#ifndef WIN32
|
||||
dirName = stdext::format(".%s", appWriteDirName);
|
||||
#else
|
||||
dirName = appWriteDirName;
|
||||
#endif
|
||||
std::string writeDir = userDir + dirName;
|
||||
|
||||
if(!PHYSFS_setWriteDir(writeDir.c_str())) {
|
||||
if(!PHYSFS_setWriteDir(userDir.c_str()))
|
||||
return false;
|
||||
if(!PHYSFS_mkdir(dirName.c_str())) {
|
||||
PHYSFS_setWriteDir(NULL);
|
||||
if(!PHYSFS_setWriteDir(userDir.c_str())) {
|
||||
g_logger.error("User directory not found.");
|
||||
return false;
|
||||
}
|
||||
if(!PHYSFS_setWriteDir(writeDir.c_str()))
|
||||
if(!PHYSFS_mkdir(dirName.c_str())) {
|
||||
g_logger.error("Cannot create directory for saving configurations.");
|
||||
return false;
|
||||
}
|
||||
if(!PHYSFS_setWriteDir(writeDir.c_str())) {
|
||||
g_logger.error("Unable to set write directory.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
addToSearchPath(writeDir);
|
||||
addToSearchPath(writeDir, true);
|
||||
//g_logger.debug(stdext::format("Setup write dir %s", writeDir));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -63,6 +99,8 @@ bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFron
|
||||
{
|
||||
if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1))
|
||||
return false;
|
||||
//g_logger.debug(stdext::format("Add search path %s", path));
|
||||
m_hasSearchPath = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -70,6 +108,7 @@ bool ResourceManager::removeFromSearchPath(const std::string& path)
|
||||
{
|
||||
if(!PHYSFS_removeFromSearchPath(path.c_str()))
|
||||
return false;
|
||||
//g_logger.debug(stdext::format("Remove search path %s", path));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -94,22 +133,32 @@ bool ResourceManager::directoryExists(const std::string& directoryName)
|
||||
|
||||
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
|
||||
{
|
||||
std::string fullPath = resolvePath(fileName);
|
||||
out.clear(std::ios::goodbit);
|
||||
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
|
||||
if(!file) {
|
||||
out.clear(std::ios::failbit);
|
||||
stdext::throw_exception(stdext::format("failed to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError()));
|
||||
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("failed 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 {
|
||||
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::ifstream fin(fileName);
|
||||
if(!fin) {
|
||||
out.clear(std::ios::failbit);
|
||||
stdext::throw_exception(stdext::format("failed to load file '%s': %s", fileName.c_str(), PHYSFS_getLastError()));
|
||||
} else {
|
||||
out << fin.rdbuf();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +272,15 @@ std::string ResourceManager::resolvePath(const std::string& path)
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
std::string ResourceManager::getRealDir(const std::string& path)
|
||||
{
|
||||
std::string dir;
|
||||
const char *cdir = PHYSFS_getRealDir(path.c_str());
|
||||
if(cdir)
|
||||
dir = cdir;
|
||||
return dir;
|
||||
}
|
||||
|
||||
std::string ResourceManager::getBaseDir()
|
||||
{
|
||||
return PHYSFS_getBaseDir();
|
||||
|
||||
@@ -31,6 +31,7 @@ public:
|
||||
void init(const char *argv0);
|
||||
void terminate();
|
||||
|
||||
void discoverWorkDir(const std::string& appName, const std::string& existentFile);
|
||||
bool setupWriteDir(const std::string& appWriteDirName);
|
||||
|
||||
bool addToSearchPath(const std::string& path, bool insertInFront = true);
|
||||
@@ -55,7 +56,13 @@ public:
|
||||
std::list<std::string> listDirectoryFiles(const std::string& directoryPath = "");
|
||||
|
||||
std::string resolvePath(const std::string& path);
|
||||
std::string getRealDir(const std::string& path);
|
||||
std::string getBaseDir();
|
||||
std::string getWorkDir() { return m_workDir; }
|
||||
|
||||
private:
|
||||
std::string m_workDir;
|
||||
Boolean<false> m_hasSearchPath;
|
||||
};
|
||||
|
||||
extern ResourceManager g_resources;
|
||||
|
||||
Reference in New Issue
Block a user