feat(Core/Modules): Preparation to implement progression-system module. (#8837)

This commit is contained in:
UltraNix
2021-11-06 14:06:12 +01:00
committed by GitHub
parent 93bbff4cca
commit 70d8b88f3b
13 changed files with 253 additions and 97 deletions

View File

@@ -39,9 +39,9 @@ struct UpdateFetcher::DirectoryEntry
UpdateFetcher::UpdateFetcher(Path const& sourceDirectory,
std::function<void(std::string const&)> const& apply,
std::function<void(Path const& path)> const& applyFile,
std::function<QueryResult(std::string const&)> const& retrieve, std::string const& dbModuleName_) :
std::function<QueryResult(std::string const&)> const& retrieve, std::string const& dbModuleName, std::vector<std::string> const* setDirectories /*= nullptr*/) :
_sourceDirectory(std::make_unique<Path>(sourceDirectory)), _apply(apply), _applyFile(applyFile),
_retrieve(retrieve), _dbModuleName(dbModuleName_)
_retrieve(retrieve), _dbModuleName(dbModuleName), _setDirectories(setDirectories)
{
}
@@ -96,53 +96,71 @@ UpdateFetcher::DirectoryStorage UpdateFetcher::ReceiveIncludedDirectories() cons
{
DirectoryStorage directories;
QueryResult const result = _retrieve("SELECT `path`, `state` FROM `updates_include`");
if (!result)
return directories;
do
if (_setDirectories)
{
Field* fields = result->Fetch();
std::string path = fields[0].GetString();
std::string state = fields[1].GetString();
if (path.substr(0, 1) == "$")
path = _sourceDirectory->generic_string() + path.substr(1);
Path const p(path);
if (!is_directory(p))
for (auto const& itr : *_setDirectories)
{
LOG_WARN("sql.updates", "DBUpdater: Given update include directory \"%s\" does not exist, skipped!", p.generic_string().c_str());
continue;
std::string path = _sourceDirectory->generic_string() + itr;
Path const p(path);
if (!is_directory(p))
continue;
DirectoryEntry const entry = {p, AppliedFileEntry::StateConvert("MODULE")};
directories.push_back(entry);
LOG_TRACE("sql.updates", "Added applied extra file \"%s\" from remote.", p.filename().generic_string().c_str());
}
DirectoryEntry const entry = { p, AppliedFileEntry::StateConvert(state) };
directories.push_back(entry);
LOG_TRACE("sql.updates", "Added applied file \"%s\" '%s' state from remote.", p.filename().generic_string().c_str(), state.c_str());
} while (result->NextRow());
std::vector<std::string> moduleList;
auto const& _modulesTokens = Acore::Tokenize(AC_MODULES_LIST, ',', true);
for (auto const& itr : _modulesTokens)
moduleList.emplace_back(itr);
// data/sql
for (auto const& itr : moduleList)
}
else
{
std::string path = _sourceDirectory->generic_string() + "/modules/" + itr + "/data/sql/" + _dbModuleName; // modules/mod-name/data/sql/db-world
QueryResult const result = _retrieve("SELECT `path`, `state` FROM `updates_include`");
if (!result)
return directories;
Path const p(path);
if (!is_directory(p))
continue;
do
{
Field* fields = result->Fetch();
DirectoryEntry const entry = { p, AppliedFileEntry::StateConvert("CUSTOM") };
directories.push_back(entry);
std::string path = fields[0].GetString();
std::string state = fields[1].GetString();
if (path.substr(0, 1) == "$")
path = _sourceDirectory->generic_string() + path.substr(1);
LOG_TRACE("sql.updates", "Added applied modules file \"%s\" from remote.", p.filename().generic_string().c_str());
Path const p(path);
if (!is_directory(p))
{
LOG_WARN("sql.updates", "DBUpdater: Given update include directory \"%s\" does not exist, skipped!", p.generic_string().c_str());
continue;
}
DirectoryEntry const entry = {p, AppliedFileEntry::StateConvert(state)};
directories.push_back(entry);
LOG_TRACE("sql.updates", "Added applied file \"%s\" '%s' state from remote.", p.filename().generic_string().c_str(), state.c_str());
} while (result->NextRow());
std::vector<std::string> moduleList;
auto const& _modulesTokens = Acore::Tokenize(AC_MODULES_LIST, ',', true);
for (auto const& itr : _modulesTokens) moduleList.emplace_back(itr);
// data/sql
for (auto const& itr : moduleList)
{
std::string path = _sourceDirectory->generic_string() + "/modules/" + itr + "/data/sql/" + _dbModuleName; // modules/mod-name/data/sql/db-world
Path const p(path);
if (!is_directory(p))
continue;
DirectoryEntry const entry = {p, AppliedFileEntry::StateConvert("MODULE")};
directories.push_back(entry);
LOG_TRACE("sql.updates", "Added applied modules file \"%s\" from remote.", p.filename().generic_string().c_str());
}
}
return directories;
@@ -201,6 +219,11 @@ UpdateResult UpdateFetcher::Update(bool const redundancyChecks,
int32 const cleanDeadReferencesMaxCount) const
{
LocaleFileStorage const available = GetFileList();
if (_setDirectories && available.empty())
{
return UpdateResult();
}
AppliedFileStorage applied = ReceiveAppliedFiles();
size_t countRecentUpdates = 0;
@@ -347,37 +370,51 @@ UpdateResult UpdateFetcher::Update(bool const redundancyChecks,
// Apply default updates
for (auto const& availableQuery : available)
{
if (availableQuery.second != CUSTOM)
if (availableQuery.second != CUSTOM && availableQuery.second != MODULE)
ApplyUpdateFile(availableQuery);
}
// Apply only custom updates
// Apply only custom/module updates
for (auto const& availableQuery : available)
{
if (availableQuery.second == CUSTOM)
if (availableQuery.second == CUSTOM || availableQuery.second == MODULE)
ApplyUpdateFile(availableQuery);
}
// Cleanup up orphaned entries (if enabled)
if (!applied.empty())
if (!applied.empty() && !_setDirectories)
{
bool const doCleanup = (cleanDeadReferencesMaxCount < 0) || (applied.size() <= static_cast<size_t>(cleanDeadReferencesMaxCount));
AppliedFileStorage toCleanup;
for (auto const& entry : applied)
{
LOG_WARN("sql.updates", ">> The file \'%s\' was applied to the database, but is missing in" \
" your update directory now!", entry.first.c_str());
if (entry.second.state != MODULE)
{
LOG_WARN("sql.updates",
">> The file \'%s\' was applied to the database, but is missing in"
" your update directory now!",
entry.first.c_str());
if (doCleanup)
LOG_INFO("sql.updates", "Deleting orphaned entry \'%s\'...", entry.first.c_str());
if (doCleanup)
{
LOG_INFO("sql.updates", "Deleting orphaned entry \'%s\'...", entry.first.c_str());
toCleanup.insert(entry);
}
}
}
if (doCleanup)
CleanUp(applied);
else
if (!toCleanup.empty())
{
LOG_ERROR("sql.updates", "Cleanup is disabled! There were " SZFMTD " dirty files applied to your database, " \
"but they are now missing in your source directory!", applied.size());
if (doCleanup)
CleanUp(toCleanup);
else
{
LOG_ERROR("sql.updates",
"Cleanup is disabled! There were " SZFMTD " dirty files applied to your database, "
"but they are now missing in your source directory!",
toCleanup.size());
}
}
}