Skip to content

Commit

Permalink
UnloadResourceAsync (#766)
Browse files Browse the repository at this point in the history
* Add `UnloadResourcesAsync` to handle threaded unloading.
Make `UnloadResources` non-async.
Add common `UnloadResourcesProcess` to handle the actual unloading.

* Clang again

* Add priority argument to `UnloadResourcesAsync`
  • Loading branch information
Malkierian authored Dec 18, 2024
1 parent b7e715e commit 0aa813d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/resource/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,24 +342,34 @@ void ResourceManager::DirtyResources(const ResourceFilter& filter) {
});
}

void ResourceManager::UnloadResources(const ResourceFilter& filter) {
mThreadPool->submit_task([this, filter]() -> void {
auto list = GetArchiveManager()->ListFiles(filter.IncludeMasks, filter.ExcludeMasks);
void ResourceManager::DirtyResources(const std::string& searchMask) {
DirtyResources({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive });
}

for (const auto& key : *list.get()) {
UnloadResource({ key, mDefaultCacheOwner, mDefaultCacheArchive });
}
});
void ResourceManager::UnloadResourcesAsync(const std::string& searchMask, BS::priority_t priority) {
UnloadResourcesAsync({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive }, priority);
}

void ResourceManager::DirtyResources(const std::string& searchMask) {
DirtyResources({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive });
void ResourceManager::UnloadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority) {
mThreadPool->submit_task([this, filter]() -> void { UnloadResourcesProcess(filter); }, priority);
}

void ResourceManager::UnloadResources(const std::string& searchMask) {
UnloadResources({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive });
}

void ResourceManager::UnloadResources(const ResourceFilter& filter) {
UnloadResourcesProcess(filter);
}

void ResourceManager::UnloadResourcesProcess(const ResourceFilter& filter) {
auto list = GetArchiveManager()->ListFiles(filter.IncludeMasks, filter.ExcludeMasks);

for (const auto& key : *list.get()) {
UnloadResource({ key, mDefaultCacheOwner, mDefaultCacheArchive });
}
}

std::shared_ptr<ArchiveManager> ResourceManager::GetArchiveManager() {
return mArchiveManager;
}
Expand Down
3 changes: 3 additions & 0 deletions src/resource/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ class ResourceManager {
void DirtyResources(const ResourceFilter& filter);
void UnloadResources(const std::string& searchMask);
void UnloadResources(const ResourceFilter& filter);
void UnloadResourcesAsync(const std::string& searchMask, BS::priority_t priority = BS::pr::normal);
void UnloadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority = BS::pr::normal);

bool OtrSignatureCheck(const char* fileName);
bool IsAltAssetsEnabled();
void SetAltAssetsEnabled(bool isEnabled);

protected:
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResourcesProcess(const ResourceFilter& filter);
void UnloadResourcesProcess(const ResourceFilter& filter);
std::variant<ResourceLoadError, std::shared_ptr<IResource>> CheckCache(const ResourceIdentifier& identifier,
bool loadExact = false);
std::shared_ptr<File> LoadFileProcess(const ResourceIdentifier& identifier,
Expand Down

0 comments on commit 0aa813d

Please sign in to comment.