voxelcore/src/assets/AssetsLoader.hpp
2026-08-30 23:40:12 +03:00

157 lines
3.4 KiB
C++

#pragma once
#include "delegates.hpp"
#include "interfaces/Task.hpp"
#include "typedefs.hpp"
#include "Assets.hpp"
#include "data/dv.hpp"
#include "io/fwd.hpp"
#include <functional>
#include <map>
#include <set>
#include <memory>
#include <queue>
#include <string>
#include <utility>
class ResPaths;
class AssetsLoader;
class Content;
class Engine;
namespace gui {
class GUI;
}
struct AssetCfg {
virtual ~AssetCfg() {
}
};
struct LayoutCfg : AssetCfg {
gui::GUI* gui;
scriptenv env;
LayoutCfg(gui::GUI* gui, scriptenv env) : gui(gui), env(std::move(env)) {}
};
struct SoundCfg : AssetCfg {
bool keepPCM;
SoundCfg(bool keepPCM) : keepPCM(keepPCM) {}
};
struct FontCfg : AssetCfg {
int size;
FontCfg(int size) : size(size) {}
};
enum class AtlasType {
ATLAS, SEPARATE
};
struct AtlasCfg : AssetCfg {
AtlasType type;
AtlasCfg(AtlasType type) : type(type) {}
};
struct PostEffectCfg : AssetCfg {
bool advanced;
PostEffectCfg(bool advanced) : advanced(advanced) {}
};
struct ModelCfg : AssetCfg {
bool squashed;
ModelCfg(bool squashed) : squashed(squashed) {}
};
using aloader_func = std::function<
assetload::
postfunc(
AssetsLoader&,
const ResPaths&,
const std::string&,
const std::string&,
std::shared_ptr<AssetCfg>)>;
struct aloader_entry {
AssetType tag;
std::string filename;
std::string alias;
std::shared_ptr<AssetCfg> config;
};
using AssetFullId = std::pair<std::string, AssetType>;
struct AssetsLoadInfo {
std::map<AssetFullId, aloader_entry> processedEntries;
std::multimap<io::path, AssetFullId> referencedAssets;
};
class AssetsLoader {
public:
AssetsLoader(Engine& engine, Assets& assets, const ResPaths& paths);
AssetsLoader(const AssetsLoader&) = delete;
void addLoader(AssetType tag, aloader_func func);
/// @brief Enqueue asset load
/// @param tag asset type
/// @param filename asset file path
/// @param alias internal asset name
/// @param settings asset loading settings (based on asset type)
void add(
AssetType tag,
const std::string& filename,
const std::string& alias,
std::shared_ptr<AssetCfg> settings = nullptr,
bool overwrite = false
);
bool hasNext() const;
/// @throws assetload::error
void loadNext();
std::shared_ptr<Task> startTask(runnable onDone, int maxWorkers);
const ResPaths& getPaths() const;
aloader_func getLoader(AssetType tag);
/// @brief Enqueue core and content assets
/// @param content engine content
void addDefaults(const Content* content);
static bool loadExternalTexture(
AssetsLoader& loader,
const std::string& name,
const std::vector<io::path>& alternatives
);
int addReload(const io::path& path);
void attachToFile(const io::path& file, AssetFullId assetId);
Assets& getAssets();
Engine& getEngine();
private:
Engine& engine;
Assets& assets;
std::map<AssetType, aloader_func> loaders;
std::queue<aloader_entry> entries;
std::set<AssetFullId> enqueued;
const ResPaths& paths;
AssetsLoadInfo& assetsLoadInfo;
void processPreload(
AssetType tag, const std::string& name, const dv::value& map
);
void processPreloadList(AssetType tag, const dv::value& list);
void processPreloadConfig(const io::path& file);
void processPreloadConfigs(const Content* content);
};