2024-04-13 11:52:42 +02:00
|
|
|
/*
|
2024-04-10 16:53:18 +02:00
|
|
|
* Mirai. Copyright (C) 2024 Vyn
|
|
|
|
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
|
|
|
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Mirai.h"
|
2024-04-11 11:42:13 +02:00
|
|
|
#include "TaskItem.h"
|
2024-04-30 18:18:54 +02:00
|
|
|
#include "cpp-utils/debug.h"
|
2024-08-16 21:35:12 +02:00
|
|
|
#include "mirai-core/Config.h"
|
2024-04-14 14:11:41 +02:00
|
|
|
#include "utils.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <ostream>
|
|
|
|
#include <vector>
|
2024-04-10 16:53:18 +02:00
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
namespace mirai
|
|
|
|
{
|
2024-04-10 16:53:18 +02:00
|
|
|
|
2024-05-09 14:39:10 +02:00
|
|
|
void Mirai::loadResource(std::unique_ptr<BaseResource> &&resource)
|
2024-04-13 11:52:42 +02:00
|
|
|
{
|
2024-05-09 14:39:10 +02:00
|
|
|
resource->load();
|
2024-08-16 21:35:12 +02:00
|
|
|
resources_.push_back(std::move(resource));
|
2024-05-09 14:39:10 +02:00
|
|
|
reloadTags();
|
|
|
|
};
|
2024-04-22 15:45:26 +02:00
|
|
|
|
2024-05-09 14:39:10 +02:00
|
|
|
void Mirai::unloadAllResources()
|
2024-04-22 15:45:26 +02:00
|
|
|
{
|
2024-08-16 21:35:12 +02:00
|
|
|
resources_.clear();
|
2024-04-13 11:52:42 +02:00
|
|
|
}
|
2024-04-10 16:53:18 +02:00
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
void Mirai::save()
|
|
|
|
{
|
2024-08-16 21:35:12 +02:00
|
|
|
for (auto &resource : resources_) {
|
2024-05-09 14:39:10 +02:00
|
|
|
if (resource->isDirty()) {
|
|
|
|
resource->save();
|
|
|
|
resource->setDirty(false);
|
2024-04-14 14:11:41 +02:00
|
|
|
}
|
2024-04-10 16:53:18 +02:00
|
|
|
}
|
2024-04-15 09:37:28 +02:00
|
|
|
reloadTags();
|
2024-04-13 11:52:42 +02:00
|
|
|
}
|
2024-04-10 16:53:18 +02:00
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
void Mirai::removeTask(const TaskItem *taskItem)
|
|
|
|
{
|
2024-08-16 21:35:12 +02:00
|
|
|
for (auto &resource : resources_) {
|
|
|
|
// resource->removeTask(taskItem); // TODO REWORK
|
2024-04-14 14:11:41 +02:00
|
|
|
}
|
2024-04-13 11:52:42 +02:00
|
|
|
}
|
2024-04-10 16:53:18 +02:00
|
|
|
|
2024-05-09 14:39:10 +02:00
|
|
|
std::vector<std::unique_ptr<BaseResource>> &Mirai::getResources()
|
2024-04-14 14:11:41 +02:00
|
|
|
{
|
2024-08-16 21:35:12 +02:00
|
|
|
return resources_;
|
2024-04-14 14:11:41 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 14:39:10 +02:00
|
|
|
std::optional<std::reference_wrapper<BaseResource>> Mirai::getResourceByName(const std::string &name
|
|
|
|
)
|
2024-04-14 14:11:41 +02:00
|
|
|
{
|
2024-05-09 14:39:10 +02:00
|
|
|
auto resourceIterator =
|
2024-08-16 21:35:12 +02:00
|
|
|
std::ranges::find_if(resources_, [&](const std::unique_ptr<BaseResource> &resource) {
|
2024-05-09 14:39:10 +02:00
|
|
|
return resource->getName() == name;
|
|
|
|
});
|
2024-04-14 14:11:41 +02:00
|
|
|
|
2024-08-16 21:35:12 +02:00
|
|
|
if (resourceIterator == resources_.end()) {
|
2024-04-14 14:11:41 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2024-05-09 14:39:10 +02:00
|
|
|
return *(resourceIterator->get());
|
2024-04-14 14:11:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
const std::vector<std::string> &Mirai::getTags()
|
|
|
|
{
|
2024-08-16 21:35:12 +02:00
|
|
|
return tags_;
|
2024-04-10 16:53:18 +02:00
|
|
|
}
|
2024-04-14 14:11:41 +02:00
|
|
|
|
2024-04-15 09:37:28 +02:00
|
|
|
void Mirai::reloadTags()
|
|
|
|
{
|
2024-08-16 21:35:12 +02:00
|
|
|
/*cpputils::debug::Timer reloadingTagsDuration;*/ // TODO REWORK
|
|
|
|
/*tags.clear();*/
|
|
|
|
/*for (auto &resource : resources) {*/
|
|
|
|
/*for (auto &task : resource->getTasks()) {*/
|
|
|
|
/*for (auto &tag : task->getTags()) {*/
|
|
|
|
/*if (!vectorUtils::contains(tags, tag)) {*/
|
|
|
|
/*tags.push_back(tag);*/
|
|
|
|
/*}*/
|
|
|
|
/*}*/
|
|
|
|
/*}*/
|
|
|
|
/*}*/
|
|
|
|
/*reloadingTagsDuration.printTimeElapsed("ReloadingTags");*/
|
2024-04-15 09:37:28 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 11:52:42 +02:00
|
|
|
} // namespace mirai
|