infinipaint/src/ResourceManager.hpp

69 lines
2.8 KiB
C++
Raw Normal View History

/*
* InfiniPaint
* Copyright (C) 2025-2026 Yousef Khadadeh
*
* 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 <https://www.gnu.org/licenses/>.
*/
2025-07-06 20:43:53 +03:00
#pragma once
2025-12-16 12:02:08 +03:00
#include <Helpers/NetworkingObjects/NetObjOwnerPtr.hpp>
2025-07-06 20:43:53 +03:00
#include <include/codec/SkCodec.h>
2025-12-16 12:02:08 +03:00
#include "Helpers/NetworkingObjects/NetObjID.hpp"
2025-07-06 20:43:53 +03:00
#include "ResourceDisplay/ResourceDisplay.hpp"
#include <Helpers/Networking/NetLibrary.hpp>
2025-12-16 12:02:08 +03:00
#include <Helpers/NetworkingObjects/NetObjUnorderedSet.hpp>
2026-01-01 23:28:26 +03:00
#include <Helpers/VersionNumber.hpp>
2025-07-06 20:43:53 +03:00
class World;
struct ResourceData {
std::string name;
std::shared_ptr<std::string> data;
template <typename Archive> void save(Archive& a) const {
a(name, *data);
}
template <typename Archive> void load(Archive& a) {
data = std::make_shared<std::string>();
a(name, *data);
}
};
class ResourceManager {
public:
ResourceManager(World& initWorld);
void update();
void init_client_callbacks();
2025-12-16 12:02:08 +03:00
void init_server_callbacks();
2025-12-16 12:54:12 +03:00
NetworkingObjects::NetObjTemporaryPtr<ResourceData> add_resource_file(const std::filesystem::path& filePath);
const NetworkingObjects::NetObjOwnerPtr<ResourceData>& add_resource(const ResourceData& resource);
2025-12-16 12:02:08 +03:00
ResourceDisplay* get_display_data(const NetworkingObjects::NetObjID& fileID);
const std::vector<NetworkingObjects::NetObjOwnerPtr<ResourceData>>& resource_list();
float get_resource_retrieval_progress(const NetworkingObjects::NetObjID& id);
2025-12-29 19:57:36 +03:00
std::unordered_map<NetworkingObjects::NetObjID, ResourceData> copy_resource_set_to_map(const std::unordered_set<NetworkingObjects::NetObjID>& resourceSet) const;
void clear_display_cache();
2026-01-01 23:28:26 +03:00
void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version);
void save_file(cereal::PortableBinaryOutputArchive& a) const;
World& world;
2025-07-06 20:43:53 +03:00
private:
2025-12-16 12:02:08 +03:00
std::unordered_map<NetworkingObjects::NetObjID, std::unique_ptr<ResourceDisplay>> displays;
std::unordered_map<NetworkingObjects::NetObjID, std::weak_ptr<NetServer::ClientData>> resourcesBeingRetrieved;
2025-12-16 12:02:08 +03:00
std::vector<NetworkingObjects::NetObjOwnerPtr<ResourceData>> resourceList;
2025-07-06 20:43:53 +03:00
static std::array<const SkCodecs::Decoder, 5> decoders;
};