2020-07-18 12:30:50 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2023-08-06 21:15:48 +00:00
|
|
|
* Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2020-07-18 12:30:50 +00:00
|
|
|
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see http://gnu.org/licenses
|
|
|
|
*/
|
|
|
|
|
2020-07-18 13:01:36 +00:00
|
|
|
#include "app/modman.h"
|
2020-07-18 12:30:50 +00:00
|
|
|
|
|
|
|
#include "common/config.h"
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/pathman.h"
|
|
|
|
|
2020-07-19 13:02:35 +00:00
|
|
|
#include "common/config_file.h"
|
2020-07-18 12:30:50 +00:00
|
|
|
#include "common/logger.h"
|
2023-08-09 17:32:59 +00:00
|
|
|
#include "common/stringutils.h"
|
2020-07-18 12:30:50 +00:00
|
|
|
|
|
|
|
#include "common/resources/resourcemanager.h"
|
|
|
|
|
2020-07-22 19:40:13 +00:00
|
|
|
#include "level/parser/parser.h"
|
|
|
|
|
2020-07-18 12:30:50 +00:00
|
|
|
#include <algorithm>
|
2023-08-09 15:04:09 +00:00
|
|
|
#include <filesystem>
|
2020-07-20 20:12:51 +00:00
|
|
|
#include <map>
|
2020-07-18 12:30:50 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
CModManager::CModManager(CApplication* app, CPathManager* pathManager)
|
|
|
|
: m_app{app},
|
|
|
|
m_pathManager{pathManager}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:59:33 +00:00
|
|
|
void CModManager::FindMods()
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-27 13:59:33 +00:00
|
|
|
m_mods.clear();
|
|
|
|
m_userChanges = false;
|
|
|
|
|
2020-07-19 13:02:35 +00:00
|
|
|
// Load names from the config file
|
|
|
|
std::vector<std::string> savedModNames;
|
|
|
|
GetConfigFile().GetArrayProperty("Mods", "Names", savedModNames);
|
|
|
|
std::vector<bool> savedEnabled;
|
|
|
|
GetConfigFile().GetArrayProperty("Mods", "Enabled", savedEnabled);
|
|
|
|
|
|
|
|
// Transform the data into Mod structures
|
|
|
|
m_mods.reserve(savedModNames.size());
|
2020-07-19 13:16:39 +00:00
|
|
|
for (size_t i = 0; i < savedModNames.size(); ++i)
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-19 13:02:35 +00:00
|
|
|
Mod mod{};
|
|
|
|
mod.name = savedModNames[i];
|
|
|
|
if (i < savedEnabled.size())
|
|
|
|
{
|
|
|
|
mod.enabled = savedEnabled[i];
|
|
|
|
}
|
|
|
|
mod.path = ""; // Find the path later
|
|
|
|
m_mods.push_back(mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search the folders for mods
|
2020-07-23 15:44:38 +00:00
|
|
|
auto rawPaths = m_pathManager->FindMods();
|
2020-07-19 13:02:35 +00:00
|
|
|
std::map<std::string, std::string> modPaths;
|
|
|
|
for (const auto& path : rawPaths)
|
|
|
|
{
|
2023-08-09 15:04:09 +00:00
|
|
|
auto modName = std::filesystem::path(path).stem().string();
|
2020-07-19 13:02:35 +00:00
|
|
|
modPaths.insert(std::make_pair(modName, path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find paths for already saved mods
|
|
|
|
auto it = m_mods.begin();
|
|
|
|
while (it != m_mods.end())
|
|
|
|
{
|
|
|
|
auto& mod = *it;
|
|
|
|
const auto pathsIt = modPaths.find(mod.name);
|
|
|
|
if (pathsIt != modPaths.end())
|
|
|
|
{
|
|
|
|
mod.path = (*pathsIt).second;
|
|
|
|
modPaths.erase(pathsIt);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetLogger()->Warn("Could not find mod %s, removing it from the list\n", mod.name.c_str());
|
|
|
|
it = m_mods.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the remaining found mods to the end of the list
|
|
|
|
for (const auto& newMod : modPaths)
|
|
|
|
{
|
|
|
|
Mod mod{};
|
|
|
|
mod.name = newMod.first;
|
|
|
|
mod.path = newMod.second;
|
2020-07-18 12:30:50 +00:00
|
|
|
m_mods.push_back(mod);
|
|
|
|
}
|
2020-07-22 14:18:18 +00:00
|
|
|
|
|
|
|
// Load the metadata for each mod
|
2020-07-27 13:59:33 +00:00
|
|
|
|
|
|
|
// Unfortunately, the paths are distinguished by their real paths, not mount points
|
|
|
|
// So we must unmount mods temporarily
|
|
|
|
for (const auto& path : m_mountedModPaths)
|
|
|
|
{
|
|
|
|
UnmountMod(path);
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:18:18 +00:00
|
|
|
for (auto& mod : m_mods)
|
|
|
|
{
|
2020-07-23 15:44:38 +00:00
|
|
|
MountMod(mod, "/temp/mod");
|
2020-07-22 19:40:13 +00:00
|
|
|
LoadModData(mod);
|
2020-07-23 15:44:38 +00:00
|
|
|
UnmountMod(mod);
|
2020-07-22 14:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:59:33 +00:00
|
|
|
// Mount back
|
|
|
|
for (const auto& path : m_mountedModPaths)
|
|
|
|
{
|
|
|
|
MountMod(path);
|
|
|
|
}
|
2020-07-22 14:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::ReloadMods()
|
|
|
|
{
|
2020-07-27 13:59:33 +00:00
|
|
|
UnmountAllMountedMods();
|
|
|
|
MountAllMods();
|
2020-07-22 14:18:18 +00:00
|
|
|
ReloadResources();
|
2020-07-18 12:30:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 20:12:51 +00:00
|
|
|
void CModManager::EnableMod(size_t i)
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-20 20:12:51 +00:00
|
|
|
m_mods[i].enabled = true;
|
2020-07-27 13:59:33 +00:00
|
|
|
m_userChanges = true;
|
2020-07-20 20:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::DisableMod(size_t i)
|
|
|
|
{
|
|
|
|
m_mods[i].enabled = false;
|
2020-07-27 13:59:33 +00:00
|
|
|
m_userChanges = true;
|
2020-07-20 20:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CModManager::MoveUp(size_t i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
std::swap(m_mods[i - 1], m_mods[i]);
|
2020-07-27 13:59:33 +00:00
|
|
|
m_userChanges = true;
|
2020-07-20 20:12:51 +00:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
else
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-20 20:12:51 +00:00
|
|
|
return i;
|
2020-07-18 12:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 20:12:51 +00:00
|
|
|
size_t CModManager::MoveDown(size_t i)
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-20 20:12:51 +00:00
|
|
|
if (i != m_mods.size() - 1)
|
|
|
|
{
|
|
|
|
std::swap(m_mods[i], m_mods[i + 1]);
|
2020-07-27 13:59:33 +00:00
|
|
|
m_userChanges = true;
|
2020-07-20 20:12:51 +00:00
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
else
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-20 20:12:51 +00:00
|
|
|
return i;
|
2020-07-18 12:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:59:33 +00:00
|
|
|
bool CModManager::Changes()
|
|
|
|
{
|
|
|
|
std::vector<std::string> paths;
|
|
|
|
for (const auto& mod : m_mods)
|
|
|
|
{
|
|
|
|
if (mod.enabled)
|
|
|
|
{
|
|
|
|
paths.push_back(mod.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths != m_mountedModPaths || m_userChanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::MountAllMods()
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
|
|
|
for (const auto& mod : m_mods)
|
|
|
|
{
|
2020-07-19 13:02:35 +00:00
|
|
|
if (mod.enabled)
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-23 15:44:38 +00:00
|
|
|
MountMod(mod);
|
2020-07-27 13:59:33 +00:00
|
|
|
m_mountedModPaths.push_back(mod.path);
|
2020-07-18 12:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::ReloadResources()
|
|
|
|
{
|
2020-07-18 12:30:50 +00:00
|
|
|
m_app->ReloadResources();
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:02:35 +00:00
|
|
|
void CModManager::SaveMods()
|
|
|
|
{
|
|
|
|
std::vector<std::string> savedNames;
|
|
|
|
savedNames.reserve(m_mods.size());
|
|
|
|
std::transform(m_mods.begin(), m_mods.end(), std::back_inserter(savedNames), [](const Mod& mod) { return mod.name; });
|
|
|
|
GetConfigFile().SetArrayProperty("Mods", "Names", savedNames);
|
|
|
|
|
|
|
|
std::vector<bool> savedEnabled;
|
|
|
|
savedEnabled.reserve(m_mods.size());
|
|
|
|
std::transform(m_mods.begin(), m_mods.end(), std::back_inserter(savedEnabled), [](const Mod& mod) { return mod.enabled; });
|
|
|
|
GetConfigFile().SetArrayProperty("Mods", "Enabled", savedEnabled);
|
|
|
|
|
|
|
|
GetConfigFile().Save();
|
2020-07-27 13:59:33 +00:00
|
|
|
|
|
|
|
m_userChanges = false;
|
2020-07-19 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 20:12:51 +00:00
|
|
|
size_t CModManager::CountMods() const
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-20 20:12:51 +00:00
|
|
|
return m_mods.size();
|
2020-07-18 12:30:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 20:12:51 +00:00
|
|
|
const Mod& CModManager::GetMod(size_t i) const
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-20 20:12:51 +00:00
|
|
|
return m_mods[i];
|
2020-07-18 12:30:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 20:12:51 +00:00
|
|
|
const std::vector<Mod>& CModManager::GetMods() const
|
2020-07-18 12:30:50 +00:00
|
|
|
{
|
2020-07-20 20:12:51 +00:00
|
|
|
return m_mods;
|
2020-07-18 12:30:50 +00:00
|
|
|
}
|
2020-07-22 19:40:13 +00:00
|
|
|
|
|
|
|
void CModManager::LoadModData(Mod& mod)
|
|
|
|
{
|
|
|
|
auto& data = mod.data;
|
|
|
|
|
|
|
|
data.displayName = mod.name;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-07-23 15:44:38 +00:00
|
|
|
CLevelParser levelParser("temp/mod/manifest.txt");
|
2020-07-22 19:40:13 +00:00
|
|
|
if (levelParser.Exists())
|
|
|
|
{
|
|
|
|
levelParser.Load();
|
|
|
|
|
|
|
|
CLevelParserLine* line = nullptr;
|
|
|
|
|
|
|
|
// DisplayName
|
|
|
|
line = levelParser.GetIfDefined("DisplayName");
|
|
|
|
if (line != nullptr && line->GetParam("text")->IsDefined())
|
|
|
|
{
|
|
|
|
data.displayName = line->GetParam("text")->AsString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Author
|
|
|
|
line = levelParser.GetIfDefined("Author");
|
|
|
|
if (line != nullptr && line->GetParam("text")->IsDefined())
|
|
|
|
{
|
|
|
|
data.author = line->GetParam("text")->AsString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version
|
|
|
|
line = levelParser.GetIfDefined("Version");
|
|
|
|
if (line != nullptr)
|
|
|
|
{
|
|
|
|
if (line->GetParam("text")->IsDefined())
|
|
|
|
{
|
|
|
|
data.version = line->GetParam("text")->AsString();
|
|
|
|
}
|
|
|
|
else if (line->GetParam("major")->IsDefined() && line->GetParam("minor")->IsDefined() && line->GetParam("patch")->IsDefined())
|
|
|
|
{
|
2023-08-09 17:32:59 +00:00
|
|
|
auto major = StrUtils::ToString(line->GetParam("major")->AsInt());
|
|
|
|
auto minor = StrUtils::ToString(line->GetParam("minor")->AsInt());
|
|
|
|
auto patch = StrUtils::ToString(line->GetParam("patch")->AsInt());
|
2023-08-09 17:52:44 +00:00
|
|
|
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << major << "." << minor << "." << patch;
|
|
|
|
data.version = stream.str();
|
2020-07-22 19:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Website
|
|
|
|
line = levelParser.GetIfDefined("Website");
|
|
|
|
if (line != nullptr && line->GetParam("text")->IsDefined())
|
|
|
|
{
|
|
|
|
data.website = line->GetParam("text")->AsString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Summary
|
|
|
|
line = levelParser.GetIfDefined("Summary");
|
|
|
|
if (line != nullptr && line->GetParam("text")->IsDefined())
|
|
|
|
{
|
|
|
|
data.summary = line->GetParam("text")->AsString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetLogger()->Warn("No manifest file for mod %s\n", mod.name.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (CLevelParserException& e)
|
|
|
|
{
|
|
|
|
GetLogger()->Warn("Failed parsing manifest for mod %s: %s\n", mod.name.c_str(), e.what());
|
|
|
|
}
|
2020-07-23 15:44:38 +00:00
|
|
|
|
|
|
|
// Changes
|
|
|
|
data.changes = CResourceManager::ListDirectories("temp/mod");
|
2020-07-23 16:07:02 +00:00
|
|
|
auto levelsIt = std::find(data.changes.begin(), data.changes.end(), "levels");
|
|
|
|
if (levelsIt != data.changes.end())
|
|
|
|
{
|
|
|
|
auto levelsDirs = CResourceManager::ListDirectories("temp/mod/levels");
|
|
|
|
if (!levelsDirs.empty())
|
|
|
|
{
|
|
|
|
std::transform(levelsDirs.begin(), levelsDirs.end(), levelsDirs.begin(), [](const std::string& dir) { return "levels/" + dir; });
|
|
|
|
levelsIt = data.changes.erase(levelsIt);
|
|
|
|
data.changes.insert(levelsIt, levelsDirs.begin(), levelsDirs.end());
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::MountMod(const Mod& mod, const std::string& mountPoint)
|
|
|
|
{
|
2020-07-27 13:59:33 +00:00
|
|
|
MountMod(mod.path, mountPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::MountMod(const std::string& path, const std::string& mountPoint)
|
|
|
|
{
|
|
|
|
GetLogger()->Debug("Mounting mod: '%s' at path %s\n", path.c_str(), mountPoint.c_str());
|
|
|
|
CResourceManager::AddLocation(path, true, mountPoint);
|
2020-07-23 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::UnmountMod(const Mod& mod)
|
|
|
|
{
|
2020-07-27 13:59:33 +00:00
|
|
|
UnmountMod(mod.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::UnmountMod(const std::string& path)
|
|
|
|
{
|
|
|
|
if (CResourceManager::LocationExists(path))
|
2020-07-23 15:44:38 +00:00
|
|
|
{
|
2020-07-27 13:59:33 +00:00
|
|
|
GetLogger()->Debug("Unmounting mod: '%s'\n", path.c_str());
|
|
|
|
CResourceManager::RemoveLocation(path);
|
2020-07-23 15:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:59:33 +00:00
|
|
|
void CModManager::UnmountAllMountedMods()
|
2020-07-23 15:44:38 +00:00
|
|
|
{
|
2020-07-27 13:59:33 +00:00
|
|
|
for (const auto& path : m_mountedModPaths)
|
2020-07-23 15:44:38 +00:00
|
|
|
{
|
2020-07-27 13:59:33 +00:00
|
|
|
UnmountMod(path);
|
2020-07-23 15:44:38 +00:00
|
|
|
}
|
2020-07-27 13:59:33 +00:00
|
|
|
m_mountedModPaths.clear();
|
2020-07-22 19:40:13 +00:00
|
|
|
}
|