| 27 | std::unordered_map<std::string, std::weak_ptr<File>>(); |
| 28 | |
| 29 | static std::shared_ptr<File> FindFileByPath(const std::string& filePath) { |
| 30 | std::lock_guard<std::mutex> autoLock(globalLocker); |
| 31 | if (filePath.empty()) { |
| 32 | return nullptr; |
| 33 | } |
| 34 | auto result = weakFileMap.find(filePath); |
| 35 | if (result != weakFileMap.end()) { |
| 36 | auto& weak = result->second; |
| 37 | auto file = weak.lock(); |
| 38 | if (file) { |
| 39 | return file; |
| 40 | } |
| 41 | weakFileMap.erase(result); |
| 42 | if (weakFileMap.size() > 50) { // do cleaning. |
| 43 | std::vector<std::string> needRemoveList = {}; |
| 44 | for (auto& item : weakFileMap) { |
| 45 | if (item.second.expired()) { |
| 46 | needRemoveList.push_back(item.first); |
| 47 | } |
| 48 | } |
| 49 | for (auto& item : needRemoveList) { |
| 50 | weakFileMap.erase(item); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | std::shared_ptr<File> File::Load(const std::string& filePath) { |
| 58 | auto byteData = ByteData::FromPath(filePath); |