diff --git a/libultraship/libultraship/ResourceMgr.cpp b/libultraship/libultraship/ResourceMgr.cpp index 58f1a8a83..6c6a0175f 100644 --- a/libultraship/libultraship/ResourceMgr.cpp +++ b/libultraship/libultraship/ResourceMgr.cpp @@ -14,8 +14,9 @@ namespace Ship { gameVersion = OOT_UNKNOWN; - if (OTR->IsMainMPQValid()) + if (OTR->IsMainMPQValid()) { Start(); + } } ResourceMgr::~ResourceMgr() { @@ -87,10 +88,11 @@ namespace Ship { OTR->LoadFile(ToLoad->path, true, ToLoad); - if (!ToLoad->bHasLoadError) + if (!ToLoad->bHasLoadError) { FileCache[ToLoad->path] = ToLoad->bIsLoaded && !ToLoad->bHasLoadError ? ToLoad : nullptr; + } - SPDLOG_DEBUG("Loaded File {} on ResourceMgr thread", ToLoad->path); + SPDLOG_TRACE("Loaded File {} on ResourceMgr thread", ToLoad->path); ToLoad->FileLoadNotifier.notify_all(); } @@ -123,12 +125,10 @@ namespace Ship { } } - if (!ToLoad->file->bHasLoadError) - { + if (!ToLoad->file->bHasLoadError) { auto UnmanagedRes = ResourceLoader::LoadResource(ToLoad->file); - if (UnmanagedRes != nullptr) - { + if (UnmanagedRes != nullptr) { UnmanagedRes->resMgr = this; auto Res = std::shared_ptr(UnmanagedRes); @@ -142,17 +142,14 @@ namespace Ship { SPDLOG_DEBUG("Loaded Resource {} on ResourceMgr thread", ToLoad->file->path); Res->file = nullptr; - } - else { + } else { ToLoad->bHasResourceLoaded = false; ToLoad->resource = nullptr; SPDLOG_ERROR("Resource load FAILED {} on ResourceMgr thread", ToLoad->file->path); } } - } - else - { + } else { ToLoad->bHasResourceLoaded = false; ToLoad->resource = nullptr; } @@ -163,13 +160,11 @@ namespace Ship { SPDLOG_INFO("Resource Manager LoadResourceThread ended"); } - uint32_t ResourceMgr::GetGameVersion() - { + uint32_t ResourceMgr::GetGameVersion() { return gameVersion; } - void ResourceMgr::SetGameVersion(uint32_t newGameVersion) - { + void ResourceMgr::SetGameVersion(uint32_t newGameVersion) { gameVersion = newGameVersion; } @@ -206,24 +201,23 @@ namespace Ship { auto resCacheFind = ResourceCache.find(FilePath); if (resCacheFind != ResourceCache.end() && - resCacheFind->second.use_count() > 0) - { + resCacheFind->second.use_count() > 0) { return resCacheFind->second; - } - else + } else { return nullptr; + } } std::shared_ptr ResourceMgr::LoadResource(const char* FilePath) { auto Res = LoadResourceAsync(FilePath); - if (std::holds_alternative>(Res)) + if (std::holds_alternative>(Res)) { return std::get>(Res); + } auto& Promise = std::get>(Res); - if (!Promise->bHasResourceLoaded) - { + if (!Promise->bHasResourceLoaded) { std::unique_lock Lock(Promise->resourceLoadMutex); while (!Promise->bHasResourceLoaded) { Promise->resourceLoadNotifier.wait(Lock); @@ -234,8 +228,9 @@ namespace Ship { } std::variant, std::shared_ptr> ResourceMgr::LoadResourceAsync(const char* FilePath) { - if (FilePath[0] == '_' && FilePath[1] == '_' && FilePath[2] == 'O' && FilePath[3] == 'T' && FilePath[4] == 'R' && FilePath[5] == '_' && FilePath[6] == '_') + if (FilePath[0] == '_' && FilePath[1] == '_' && FilePath[2] == 'O' && FilePath[3] == 'T' && FilePath[4] == 'R' && FilePath[5] == '_' && FilePath[6] == '_') { FilePath += 7; + } const std::lock_guard ResLock(ResourceLoadMutex); auto resCacheFind = ResourceCache.find(FilePath); @@ -248,21 +243,16 @@ namespace Ship { std::shared_ptr FileData = LoadFile(FilePath); Promise->file = FileData; - if (Promise->file->bHasLoadError) - { + if (Promise->file->bHasLoadError) { Promise->bHasResourceLoaded = true; - } - else - { + } else { Promise->bHasResourceLoaded = false; ResourceLoadQueue.push(Promise); ResourceLoadNotifier.notify_all(); } return Promise; - } - else - { + } else { return resCacheFind->second; } } @@ -273,8 +263,7 @@ namespace Ship { for (DWORD i = 0; i < fileList.size(); i++) { auto resource = LoadResourceAsync(fileList.operator[](i).cFileName); - if (std::holds_alternative>(resource)) - { + if (std::holds_alternative>(resource)) { auto promise = std::make_shared(); promise->bHasResourceLoaded = true; promise->resource = std::get>(resource); @@ -304,8 +293,7 @@ namespace Ship { return LoadedList; } - std::shared_ptr>> ResourceMgr::DirtyDirectory(std::string SearchMask) - { + std::shared_ptr>> ResourceMgr::DirtyDirectory(std::string& SearchMask) { auto PromiseList = CacheDirectoryAsync(SearchMask); auto LoadedList = std::make_shared>>(); @@ -317,8 +305,9 @@ namespace Ship { Promise->resourceLoadNotifier.wait(Lock); } - if (Promise->resource != nullptr) + if (Promise->resource != nullptr) { Promise->resource->isDirty = true; + } LoadedList->push_back(Promise->resource); } @@ -326,8 +315,7 @@ namespace Ship { return LoadedList; } - std::shared_ptr> ResourceMgr::ListFiles(std::string SearchMask) - { + std::shared_ptr> ResourceMgr::ListFiles(std::string SearchMask) { auto result = std::make_shared>(); auto fileList = OTR->ListFiles(SearchMask); diff --git a/libultraship/libultraship/ResourceMgr.h b/libultraship/libultraship/ResourceMgr.h index 0d9f40f86..404933ceb 100644 --- a/libultraship/libultraship/ResourceMgr.h +++ b/libultraship/libultraship/ResourceMgr.h @@ -40,7 +40,7 @@ namespace Ship { std::variant, std::shared_ptr> LoadResourceAsync(const char* FilePath); std::shared_ptr>> CacheDirectory(const std::string& SearchMask); std::shared_ptr>> CacheDirectoryAsync(const std::string& SearchMask); - std::shared_ptr>> DirtyDirectory(std::string SearchMask); + std::shared_ptr>> DirtyDirectory(std::string& SearchMask); std::shared_ptr> ListFiles(std::string SearchMask); protected: