Skip to content

Commit

Permalink
feat: add option to register custom type handler with resource watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Silverlan committed Dec 5, 2024
1 parent 981535d commit d8b24b3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/client/src/util/c_resource_watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ decltype(ECResourceWatcherCallbackType::Shader) ECResourceWatcherCallbackType::S
decltype(ECResourceWatcherCallbackType::ParticleSystem) ECResourceWatcherCallbackType::ParticleSystem = ECResourceWatcherCallbackType {umath::to_integral(E::ParticleSystem)};
decltype(ECResourceWatcherCallbackType::Count) ECResourceWatcherCallbackType::Count = ECResourceWatcherCallbackType {umath::to_integral(E::Count)};
static auto cvMatStreaming = GetClientConVar("cl_material_streaming_enabled");

CResourceWatcherManager::CResourceWatcherManager(NetworkState *nw) : ResourceWatcherManager(nw) {}

void CResourceWatcherManager::ReloadTexture(const std::string &path)
{
auto *nw = m_networkState;
Expand Down
3 changes: 3 additions & 0 deletions core/shared/include/pragma/util/resource_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Model;
class LuaDirectoryWatcherManager;
class DLLNETWORK ResourceWatcherManager {
public:
using TypeHandler = std::function<void(const util::Path &, const std::string &)>;
ResourceWatcherManager(NetworkState *nw);
bool MountDirectory(const std::string &path, bool bAbsolutePath = false);
void Poll();
Expand All @@ -48,6 +49,7 @@ class DLLNETWORK ResourceWatcherManager {
util::ScopeGuard ScopeLock();
bool IsLocked() const;
CallbackHandle AddChangeCallback(EResourceWatcherCallbackType type, const std::function<void(std::reference_wrapper<const std::string>, std::reference_wrapper<const std::string>)> &fcallback);
void RegisterTypeHandler(const std::string &ext, const TypeHandler &handler);
protected:
NetworkState *m_networkState = nullptr;
uint32_t m_lockedCount = 0;
Expand All @@ -63,6 +65,7 @@ class DLLNETWORK ResourceWatcherManager {
std::unordered_map<EResourceWatcherCallbackType, std::vector<CallbackHandle>> m_callbacks;
std::unordered_map<std::string, std::function<void()>> m_watchFiles;
std::vector<std::shared_ptr<DirectoryWatcherCallback>> m_watchers;
std::unordered_map<std::string, TypeHandler> m_typeHandlers;
};

#endif
7 changes: 7 additions & 0 deletions core/shared/src/util/resource_watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ util::ScopeGuard ResourceWatcherManager::ScopeLock()

bool ResourceWatcherManager::IsLocked() const { return m_lockedCount > 0; }

void ResourceWatcherManager::RegisterTypeHandler(const std::string &ext, const TypeHandler &handler) { m_typeHandlers[ext] = handler; }

void ResourceWatcherManager::ReloadMaterial(const std::string &path)
{
auto *nw = m_networkState;
Expand Down Expand Up @@ -140,6 +142,11 @@ void ResourceWatcherManager::OnResourceChanged(const util::Path &rootPath, const
auto &strPath = path.GetString();
auto *nw = m_networkState;
auto *game = nw->GetGameState();
auto it = m_typeHandlers.find(ext);
if(it != m_typeHandlers.end()) {
it->second(path, ext);
return;
}
auto assetType = pragma::asset::determine_type_from_extension(ext);
if(assetType.has_value()) {
if(*assetType == pragma::asset::Type::Model) {
Expand Down

0 comments on commit d8b24b3

Please sign in to comment.