| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include "Common/CommonTypes.h" |
| 7 |
#include "VideoCommon/Assets/CustomAssetLibrary.h" |
| 8 |
|
| 9 |
#include <atomic> |
| 10 |
#include <memory> |
| 11 |
#include <mutex> |
| 12 |
|
| 13 |
namespace VideoCommon |
| 14 |
{ |
| 15 |
|
| 16 |
|
| 17 |
class CustomAsset |
| 18 |
{ |
| 19 |
public: |
| 20 |
using ClockType = std::chrono::steady_clock; |
| 21 |
using TimeType = ClockType::time_point; |
| 22 |
|
| 23 |
CustomAsset(std::shared_ptr<CustomAssetLibrary> library, CustomAssetLibrary::AssetID asset_id, |
| 24 |
u64 session_id); |
| 25 |
virtual ~CustomAsset() = default; |
| 26 |
CustomAsset(const CustomAsset&) = delete; |
| 27 |
CustomAsset(CustomAsset&&) = delete; |
| 28 |
CustomAsset& operator=(const CustomAsset&) = delete; |
| 29 |
CustomAsset& operator=(CustomAsset&&) = delete; |
| 30 |
|
| 31 |
|
| 32 |
std::size_t Load(); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
std::size_t Unload(); |
| 37 |
|
| 38 |
|
| 39 |
TimeType GetLastLoadedTime() const; |
| 40 |
|
| 41 |
|
| 42 |
const CustomAssetLibrary::AssetID& GetAssetId() const; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
std::size_t GetHandle() const; |
| 48 |
|
| 49 |
protected: |
| 50 |
const std::shared_ptr<CustomAssetLibrary> m_owning_library; |
| 51 |
|
| 52 |
private: |
| 53 |
virtual CustomAssetLibrary::LoadInfo LoadImpl(const CustomAssetLibrary::AssetID& asset_id) = 0; |
| 54 |
virtual void UnloadImpl() = 0; |
| 55 |
CustomAssetLibrary::AssetID m_asset_id; |
| 56 |
std::size_t m_handle; |
| 57 |
|
| 58 |
mutable std::mutex m_info_lock; |
| 59 |
std::size_t m_bytes_loaded = 0; |
| 60 |
std::atomic<TimeType> m_last_loaded_time = {}; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
template <typename UnderlyingType> |
| 70 |
class CustomLoadableAsset : public CustomAsset |
| 71 |
{ |
| 72 |
public: |
| 73 |
using CustomAsset::CustomAsset; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
[[nodiscard]] std::shared_ptr<UnderlyingType> GetData() const |
| 80 |
{ |
| 81 |
std::lock_guard lk(m_data_lock); |
| 82 |
if (m_loaded) |
| 83 |
return m_data; |
| 84 |
return nullptr; |
| 85 |
} |
| 86 |
|
| 87 |
protected: |
| 88 |
bool m_loaded = false; |
| 89 |
mutable std::mutex m_data_lock; |
| 90 |
std::shared_ptr<UnderlyingType> m_data; |
| 91 |
|
| 92 |
private: |
| 93 |
void UnloadImpl() override |
| 94 |
{ |
| 95 |
std::lock_guard lk(m_data_lock); |
| 96 |
m_loaded = false; |
| 97 |
m_data.reset(); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
template <typename AssetType> |
| 109 |
struct CachedAsset |
| 110 |
{ |
| 111 |
std::shared_ptr<AssetType> m_asset; |
| 112 |
CustomAsset::TimeType m_cached_write_time; |
| 113 |
}; |
| 114 |
|
| 115 |
} |