Move mod loading/unloading stuff to a new class + fix some derps

pyro-refactor
DavivaD 2019-07-27 03:36:03 +02:00
parent e823c209fa
commit cd140f1384
4 changed files with 65 additions and 45 deletions

View File

@ -72,9 +72,9 @@ void CPathManager::AddModAutoloadDir(const std::string &modAutoloadDirPath)
void CPathManager::AddMod(const std::string &modPath) void CPathManager::AddMod(const std::string &modPath)
{ {
std::string::size_type ON; std::string::size_type enabled;
ON = modPath.find('~'); enabled = modPath.find('~');
if (ON == std::string::npos) if (enabled == std::string::npos)
{ {
GetLogger()->Info("Loading mod: '%s'\n", modPath.c_str()); GetLogger()->Info("Loading mod: '%s'\n", modPath.c_str());
CResourceManager::AddLocation(modPath, true); CResourceManager::AddLocation(modPath, true);
@ -87,9 +87,9 @@ void CPathManager::AddMod(const std::string &modPath)
void CPathManager::RemoveMod(const std::string &modPath) void CPathManager::RemoveMod(const std::string &modPath)
{ {
std::string::size_type ON; std::string::size_type enabled;
ON = modPath.find('~'); enabled = modPath.find('~');
if (ON == std::string::npos) if (enabled == std::string::npos)
{ {
GetLogger()->Info("Unloading mod: '%s'\n", modPath.c_str()); GetLogger()->Info("Unloading mod: '%s'\n", modPath.c_str());
CResourceManager::RemoveLocation(modPath); CResourceManager::RemoveLocation(modPath);
@ -171,9 +171,9 @@ void CPathManager::InitPaths()
GetLogger()->Trace("Searching for mods in '%s'...\n", modAutoloadDir.c_str()); GetLogger()->Trace("Searching for mods in '%s'...\n", modAutoloadDir.c_str());
for (const std::string& modPath : FindModsInDir(modAutoloadDir)) for (const std::string& modPath : FindModsInDir(modAutoloadDir))
{ {
std::string::size_type ON; std::string::size_type enabled;
ON = modPath.find('~'); enabled = modPath.find('~');
if (ON == std::string::npos) if (enabled == std::string::npos)
{ {
GetLogger()->Info("Autoloading mod: '%s'\n", modPath.c_str()); GetLogger()->Info("Autoloading mod: '%s'\n", modPath.c_str());
CResourceManager::AddLocation(modPath); CResourceManager::AddLocation(modPath);
@ -187,9 +187,9 @@ void CPathManager::InitPaths()
for (const std::string& modPath : m_mods) for (const std::string& modPath : m_mods)
{ {
std::string::size_type ON; std::string::size_type enabled;
ON = modPath.find('~'); enabled = modPath.find('~');
if (ON == std::string::npos) if (enabled == std::string::npos)
{ {
GetLogger()->Info("Loading mod: '%s'\n", modPath.c_str()); GetLogger()->Info("Loading mod: '%s'\n", modPath.c_str());
CResourceManager::AddLocation(modPath); CResourceManager::AddLocation(modPath);

View File

@ -240,7 +240,7 @@ bool CScreenSetup::EventProcess(const Event &event)
return false; return false;
case EVENT_INTERFACE_SETUPm: case EVENT_INTERFACE_SETUPm:
return false; assert(false); // should never get here
default: default:
break; break;

View File

@ -1,6 +1,6 @@
/* /*
* This file is part of the Colobot: Gold Edition source code * This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2018, Daniel Roux, EPSITEC SA & TerranovaTeam * Copyright (C) 2001-2019, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot * http://epsitec.ch; http://colobot.info; http://github.com/colobot
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -127,7 +127,7 @@ bool CScreenSetupMods::EventProcess(const Event &event)
CButton* pb; CButton* pb;
CList* pl; CList* pl;
int result; int result;
std::string modName, modPath, modPathRaw, OFF = "~"; std::string modName, modPath;
if (!CScreenSetup::EventProcess(event)) return false; if (!CScreenSetup::EventProcess(event)) return false;
@ -139,11 +139,8 @@ bool CScreenSetupMods::EventProcess(const Event &event)
pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_MODS_UNLOADED)); pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_MODS_UNLOADED));
if ( pl == nullptr ) return false; if ( pl == nullptr ) return false;
modName = pl->GetItemName(pl->GetSelect()); modName = pl->GetItemName(pl->GetSelect());
LoadMod(modName);
modPathRaw = CResourceManager::GetSaveLocation() + "/" + "mods" + "/";
modPath = modPathRaw.c_str();
boost::filesystem::rename(modPath+OFF+modName, modPath+modName);
m_pathManager->AddMod(modPath+modName);
m_app->Reload(); m_app->Reload();
m_main->ChangePhase(PHASE_SETUPm); m_main->ChangePhase(PHASE_SETUPm);
} }
@ -152,23 +149,21 @@ bool CScreenSetupMods::EventProcess(const Event &event)
pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_MODS_LOADED)); pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_MODS_LOADED));
if ( pl == nullptr ) return false; if ( pl == nullptr ) return false;
modName = pl->GetItemName(pl->GetSelect()); modName = pl->GetItemName(pl->GetSelect());
UnloadMod(modName);
modPathRaw = CResourceManager::GetSaveLocation() + "/" + "mods" + "/";
modPath = modPathRaw.c_str();
m_pathManager->RemoveMod(modPath+modName);
boost::filesystem::rename(modPath+modName, modPath+OFF+modName);
m_app->Reload(); m_app->Reload();
m_main->ChangePhase(PHASE_SETUPm); m_main->ChangePhase(PHASE_SETUPm);
} }
if (event.type == EVENT_INTERFACE_MODS_DIR) if (event.type == EVENT_INTERFACE_MODS_DIR)
{ {
modPathRaw = CResourceManager::GetSaveLocation() + "/" + "mods"; modPath = CResourceManager::GetSaveLocation() + "/" + "mods";
#if defined(PLATFORM_WINDOWS) #if defined(PLATFORM_WINDOWS)
result = system(("start \""+modPathRaw+"\"").c_str()); std::replace(modPath.begin(), modPath.end(), '/', '\\');
result = system(("explorer \""+modPath+"\"").c_str());
#elif defined(PLATFORM_LINUX) #elif defined(PLATFORM_LINUX)
result = system(("xdg-open \""+modPathRaw+"\"").c_str()); result = system(("xdg-open \""+modPath+"\"").c_str());
#elif defined(PLATFORM_MACOSX) #elif defined(PLATFORM_MACOSX)
result = system(("open \""+modPathRaw+"\"").c_str()); result = system(("open \""+modPath+"\"").c_str()); //TODO: Test on macOS
#endif #endif
if (result == -1) if (result == -1)
{ {
@ -207,11 +202,11 @@ bool CScreenSetupMods::EventProcess(const Event &event)
case EVENT_INTERFACE_WORKSHOP: case EVENT_INTERFACE_WORKSHOP:
#if defined(PLATFORM_WINDOWS) #if defined(PLATFORM_WINDOWS)
result = system("start \"https://colobot.info/forum/forumdisplay.php?fid=60\""); result = system("rundll32 url.dll,FileProtocolHandler \"https://www.moddb.com/games/colobot-gold-edition\"");
#elif defined(PLATFORM_LINUX) #elif defined(PLATFORM_LINUX)
result = system("xdg-open \"https://colobot.info/forum/forumdisplay.php?fid=60\""); result = system("xdg-open \"https://www.moddb.com/games/colobot-gold-edition\"");
#elif defined(PLATFORM_MACOSX) #elif defined(PLATFORM_MACOSX)
result = system("open \"https://colobot.info/forum/forumdisplay.php?fid=60\""); result = system("open \"https://www.moddb.com/games/colobot-gold-edition\""); //TODO: Test on macOS
#endif #endif
if (result == -1) if (result == -1)
{ {
@ -223,6 +218,29 @@ bool CScreenSetupMods::EventProcess(const Event &event)
} }
return false; return false;
} }
void CScreenSetupMods::UnloadMod(std::string modName)
{
std::string modPath, modPathRaw, disabled = "~";
modPathRaw = CResourceManager::GetSaveLocation() + "/" + "mods" + "/";
modPath = modPathRaw.c_str();
m_pathManager->RemoveMod(modPath+modName);
boost::filesystem::rename(modPath+modName, modPath+disabled+modName);
}
void CScreenSetupMods::LoadMod(std::string modName)
{
std::string modPath, modPathRaw, disabled = "~";
modPathRaw = CResourceManager::GetSaveLocation() + "/" + "mods" + "/";
modPath = modPathRaw.c_str();
boost::filesystem::rename(modPath+disabled+modName, modPath+modName);
m_pathManager->AddMod(modPath+modName);
}
void CScreenSetupMods::UpdateUnloadedModList() void CScreenSetupMods::UpdateUnloadedModList()
{ {
CWindow* pw; CWindow* pw;
@ -243,9 +261,9 @@ void CScreenSetupMods::UpdateUnloadedModList()
for(auto const& modNameRaw : modsDir) for(auto const& modNameRaw : modsDir)
{ {
modName = modNameRaw; modName = modNameRaw;
std::string::size_type ON; std::string::size_type enabled;
ON = modName.find('~'); enabled = modName.find('~');
if (ON != std::string::npos) if (enabled != std::string::npos)
{ {
modName.erase(0,1); modName.erase(0,1);
pl->SetItemName(i++, modName); pl->SetItemName(i++, modName);
@ -271,9 +289,9 @@ void CScreenSetupMods::UpdateLoadedModList()
for(auto const &modName : modsDir) for(auto const &modName : modsDir)
{ {
std::string::size_type ON; std::string::size_type enabled;
ON = modName.find('~'); enabled = modName.find('~');
if (ON == std::string::npos) if (enabled == std::string::npos)
pl->SetItemName(i++, modName); pl->SetItemName(i++, modName);
} }
pl->ShowSelect(false); // shows the selected columns pl->ShowSelect(false); // shows the selected columns

View File

@ -1,6 +1,6 @@
/* /*
* This file is part of the Colobot: Gold Edition source code * This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2018, Daniel Roux, EPSITEC SA & TerranovaTeam * Copyright (C) 2001-2019, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot * http://epsitec.ch; http://colobot.info; http://github.com/colobot
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -38,6 +38,8 @@ public:
bool EventProcess(const Event &event) override; bool EventProcess(const Event &event) override;
protected: protected:
void UnloadMod(std::string ModName);
void LoadMod(std::string ModName);
void UpdateUnloadedModList(); void UpdateUnloadedModList();
void UpdateLoadedModList(); void UpdateLoadedModList();