ResourceCache.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "CoreEvents.h"
  26. #include "FileSystem.h"
  27. #include "FileWatcher.h"
  28. #include "Image.h"
  29. #include "Log.h"
  30. #include "PackageFile.h"
  31. #include "ResourceCache.h"
  32. #include "ResourceEvents.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  36. {
  37. static const String checkDirs[] = {
  38. "Fonts",
  39. "Materials",
  40. "Models",
  41. "Music",
  42. "Objects",
  43. "Particle",
  44. "PostProcess",
  45. "Scenes",
  46. "Scripts",
  47. "Sounds",
  48. "Shaders",
  49. "Techniques",
  50. "Textures",
  51. "UI",
  52. ""
  53. };
  54. static const SharedPtr<Resource> noResource;
  55. OBJECTTYPESTATIC(ResourceCache);
  56. ResourceCache::ResourceCache(Context* context) :
  57. Object(context),
  58. autoReloadResources_(false)
  59. {
  60. SubscribeToEvent(E_BEGINFRAME, HANDLER(ResourceCache, HandleBeginFrame));
  61. }
  62. ResourceCache::~ResourceCache()
  63. {
  64. }
  65. bool ResourceCache::AddResourceDir(const String& pathName)
  66. {
  67. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  68. if (!fileSystem || !fileSystem->DirExists(pathName))
  69. {
  70. LOGERROR("Could not open directory " + pathName);
  71. return false;
  72. }
  73. String fixedPath = AddTrailingSlash(pathName);
  74. // Check that the same path does not already exist
  75. for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
  76. {
  77. if (!resourceDirs_[i].Compare(fixedPath, false))
  78. return true;
  79. }
  80. resourceDirs_.Push(fixedPath);
  81. // Scan the path for files recursively and add their hash-to-name mappings
  82. Vector<String> fileNames;
  83. fileSystem->ScanDir(fileNames, fixedPath, "*.*", SCAN_FILES, true);
  84. for (unsigned i = 0; i < fileNames.Size(); ++i)
  85. StoreNameHash(fileNames[i]);
  86. // If resource auto-reloading active, create a file watcher for the directory
  87. if (autoReloadResources_)
  88. {
  89. SharedPtr<FileWatcher> watcher(new FileWatcher(context_));
  90. watcher->StartWatching(fixedPath, true);
  91. fileWatchers_.Push(watcher);
  92. }
  93. LOGINFO("Added resource path " + fixedPath);
  94. return true;
  95. }
  96. void ResourceCache::AddPackageFile(PackageFile* package, bool addAsFirst)
  97. {
  98. // Do not add packages that failed to load
  99. if (!package || !package->GetNumFiles())
  100. return;
  101. if (addAsFirst)
  102. packages_.Insert(packages_.Begin(), SharedPtr<PackageFile>(package));
  103. else
  104. packages_.Push(SharedPtr<PackageFile>(package));
  105. // Scan the package for files and add their hash-to-name mappings
  106. const HashMap<String, PackageEntry>& entries = package->GetEntries();
  107. for (HashMap<String, PackageEntry>::ConstIterator i = entries.Begin(); i != entries.End(); ++i)
  108. StoreNameHash(i->first_);
  109. LOGINFO("Added resource package " + package->GetName());
  110. }
  111. bool ResourceCache::AddManualResource(Resource* resource)
  112. {
  113. if (!resource)
  114. {
  115. LOGERROR("Null manual resource");
  116. return false;
  117. }
  118. const String& name = resource->GetName();
  119. if (name.Empty())
  120. {
  121. LOGERROR("Manual resource with empty name, can not add");
  122. return false;
  123. }
  124. StoreNameHash(name);
  125. resource->ResetUseTimer();
  126. resourceGroups_[resource->GetType()].resources_[resource->GetNameHash()] = resource;
  127. UpdateResourceGroup(resource->GetType());
  128. return true;
  129. }
  130. void ResourceCache::RemoveResourceDir(const String& path)
  131. {
  132. String fixedPath = AddTrailingSlash(path);
  133. for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
  134. {
  135. if (!resourceDirs_[i].Compare(path, false))
  136. {
  137. resourceDirs_.Erase(i);
  138. if (fileWatchers_.Size() > i)
  139. fileWatchers_.Erase(i);
  140. LOGINFO("Removed resource path " + fixedPath);
  141. return;
  142. }
  143. }
  144. }
  145. void ResourceCache::RemovePackageFile(PackageFile* package, bool releaseResources, bool forceRelease)
  146. {
  147. for (Vector<SharedPtr<PackageFile> >::Iterator i = packages_.Begin(); i != packages_.End(); ++i)
  148. {
  149. if (*i == package)
  150. {
  151. if (releaseResources)
  152. ReleasePackageResources(*i, forceRelease);
  153. LOGINFO("Removed resource package " + (*i)->GetName());
  154. packages_.Erase(i);
  155. return;
  156. }
  157. }
  158. }
  159. void ResourceCache::RemovePackageFile(const String& fileName, bool releaseResources, bool forceRelease)
  160. {
  161. // Compare the name and extension only, not the path
  162. String fileNameNoPath = GetFileNameAndExtension(fileName);
  163. for (Vector<SharedPtr<PackageFile> >::Iterator i = packages_.Begin(); i != packages_.End(); ++i)
  164. {
  165. if (!GetFileNameAndExtension((*i)->GetName()).Compare(fileNameNoPath, false))
  166. {
  167. if (releaseResources)
  168. ReleasePackageResources(*i, forceRelease);
  169. LOGINFO("Removed resource package " + (*i)->GetName());
  170. packages_.Erase(i);
  171. return;
  172. }
  173. }
  174. }
  175. void ResourceCache::ReleaseResource(ShortStringHash type, const String& name, bool force)
  176. {
  177. ReleaseResource(type, StringHash(name), force);
  178. }
  179. void ResourceCache::ReleaseResource(ShortStringHash type, StringHash nameHash, bool force)
  180. {
  181. const SharedPtr<Resource>& existingRes = FindResource(type, nameHash);
  182. if (!existingRes)
  183. return;
  184. // If other references exist, do not release, unless forced
  185. if (existingRes.Refs() == 1 || force)
  186. {
  187. resourceGroups_[type].resources_.Erase(nameHash);
  188. UpdateResourceGroup(type);
  189. }
  190. }
  191. void ResourceCache::ReleaseResources(ShortStringHash type, bool force)
  192. {
  193. bool released = false;
  194. for (HashMap<ShortStringHash, ResourceGroup>::Iterator i = resourceGroups_.Begin();
  195. i != resourceGroups_.End(); ++i)
  196. {
  197. if (i->first_ == type)
  198. {
  199. for (HashMap<StringHash, SharedPtr<Resource> >::Iterator j = i->second_.resources_.Begin();
  200. j != i->second_.resources_.End();)
  201. {
  202. HashMap<StringHash, SharedPtr<Resource> >::Iterator current = j++;
  203. // If other references exist, do not release, unless forced
  204. if (current->second_.Refs() == 1 || force)
  205. {
  206. i->second_.resources_.Erase(current);
  207. released = true;
  208. }
  209. }
  210. }
  211. }
  212. if (released)
  213. UpdateResourceGroup(type);
  214. }
  215. void ResourceCache::ReleaseResources(ShortStringHash type, const String& partialName, bool force)
  216. {
  217. bool released = false;
  218. for (HashMap<ShortStringHash, ResourceGroup>::Iterator i = resourceGroups_.Begin();
  219. i != resourceGroups_.End(); ++i)
  220. {
  221. if (i->first_ == type)
  222. {
  223. for (HashMap<StringHash, SharedPtr<Resource> >::Iterator j = i->second_.resources_.Begin();
  224. j != i->second_.resources_.End();)
  225. {
  226. HashMap<StringHash, SharedPtr<Resource> >::Iterator current = j++;
  227. if (current->second_->GetName().Find(partialName) != String::NPOS)
  228. {
  229. // If other references exist, do not release, unless forced
  230. if (current->second_.Refs() == 1 || force)
  231. {
  232. i->second_.resources_.Erase(current);
  233. released = true;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. if (released)
  240. UpdateResourceGroup(type);
  241. }
  242. void ResourceCache::ReleaseAllResources(bool force)
  243. {
  244. for (HashMap<ShortStringHash, ResourceGroup>::Iterator i = resourceGroups_.Begin();
  245. i != resourceGroups_.End(); ++i)
  246. {
  247. bool released = false;
  248. for (HashMap<StringHash, SharedPtr<Resource> >::Iterator j = i->second_.resources_.Begin();
  249. j != i->second_.resources_.End();)
  250. {
  251. HashMap<StringHash, SharedPtr<Resource> >::Iterator current = j++;
  252. // If other references exist, do not release, unless forced
  253. if ((current->second_.Refs() == 1 && current->second_.WeakRefs() == 0) || force)
  254. {
  255. i->second_.resources_.Erase(current);
  256. released = true;
  257. }
  258. }
  259. if (released)
  260. UpdateResourceGroup(i->first_);
  261. }
  262. }
  263. bool ResourceCache::ReloadResource(Resource* resource)
  264. {
  265. if (!resource)
  266. return false;
  267. resource->SendEvent(E_RELOADSTARTED);
  268. bool success = false;
  269. SharedPtr<File> file = GetFile(resource->GetName());
  270. if (file)
  271. success = resource->Load(*(file.Get()));
  272. if (success)
  273. {
  274. resource->ResetUseTimer();
  275. UpdateResourceGroup(resource->GetType());
  276. resource->SendEvent(E_RELOADFINISHED);
  277. return true;
  278. }
  279. // If reloading failed, remove the resource from cache
  280. resource->SendEvent(E_RELOADFAILED);
  281. ReleaseResource(resource->GetType(), resource->GetNameHash());
  282. return false;
  283. }
  284. void ResourceCache::SetMemoryBudget(ShortStringHash type, unsigned budget)
  285. {
  286. resourceGroups_[type].memoryBudget_ = budget;
  287. }
  288. void ResourceCache::SetAutoReloadResources(bool enable)
  289. {
  290. if (enable != autoReloadResources_)
  291. {
  292. if (enable)
  293. {
  294. for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
  295. {
  296. SharedPtr<FileWatcher> watcher(new FileWatcher(context_));
  297. watcher->StartWatching(resourceDirs_[i], true);
  298. fileWatchers_.Push(watcher);
  299. }
  300. }
  301. else
  302. fileWatchers_.Clear();
  303. autoReloadResources_ = enable;
  304. }
  305. }
  306. SharedPtr<File> ResourceCache::GetFile(const String& nameIn)
  307. {
  308. String name = SanitateResourceName(nameIn);
  309. // Check first the packages
  310. for (unsigned i = 0; i < packages_.Size(); ++i)
  311. {
  312. if (packages_[i]->Exists(name))
  313. return SharedPtr<File>(new File(context_, packages_[i], name));
  314. }
  315. // Then the filesystem
  316. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  317. if (fileSystem)
  318. {
  319. for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
  320. {
  321. if (fileSystem->FileExists(resourceDirs_[i] + name))
  322. {
  323. // Construct the file first with full path, then rename it to not contain the resource path,
  324. // so that the file's name can be used in further GetFile() calls (for example over the network)
  325. SharedPtr<File> file(new File(context_, resourceDirs_[i] + name));
  326. file->SetName(name);
  327. return file;
  328. }
  329. }
  330. }
  331. LOGERROR("Could not find resource " + name);
  332. return SharedPtr<File>();
  333. }
  334. Resource* ResourceCache::GetResource(ShortStringHash type, const String& nameIn)
  335. {
  336. String name = SanitateResourceName(nameIn);
  337. // Add the name to the hash map, so if this is an unknown resource, the error will not be unintelligible
  338. StoreNameHash(name);
  339. return GetResource(type, StringHash(name));
  340. }
  341. Resource* ResourceCache::GetResource(ShortStringHash type, StringHash nameHash)
  342. {
  343. // If null hash, return null pointer immediately
  344. if (!nameHash)
  345. return 0;
  346. const SharedPtr<Resource>& existing = FindResource(type, nameHash);
  347. if (existing)
  348. return existing;
  349. SharedPtr<Resource> resource;
  350. const String& name = GetResourceName(nameHash);
  351. if (name.Empty())
  352. {
  353. LOGERROR("Could not load unknown resource " + String(nameHash));
  354. return 0;
  355. }
  356. // Make sure the pointer is non-null and is a Resource subclass
  357. resource = DynamicCast<Resource>(context_->CreateObject(type));
  358. if (!resource)
  359. {
  360. LOGERROR("Could not load unknown resource type " + String(type));
  361. return 0;
  362. }
  363. // Attempt to load the resource
  364. SharedPtr<File> file = GetFile(name);
  365. if (!file)
  366. return 0;
  367. LOGDEBUG("Loading resource " + name);
  368. resource->SetName(file->GetName());
  369. if (!resource->Load(*(file.Get())))
  370. return 0;
  371. // Store to cache
  372. resource->ResetUseTimer();
  373. resourceGroups_[type].resources_[nameHash] = resource;
  374. UpdateResourceGroup(type);
  375. return resource;
  376. }
  377. void ResourceCache::GetResources(PODVector<Resource*>& result, ShortStringHash type) const
  378. {
  379. result.Clear();
  380. HashMap<ShortStringHash, ResourceGroup>::ConstIterator i = resourceGroups_.Find(type);
  381. if (i != resourceGroups_.End())
  382. {
  383. for (HashMap<StringHash, SharedPtr<Resource> >::ConstIterator j = i->second_.resources_.Begin();
  384. j != i->second_.resources_.End(); ++j)
  385. result.Push(j->second_);
  386. }
  387. }
  388. bool ResourceCache::Exists(const String& name) const
  389. {
  390. for (unsigned i = 0; i < packages_.Size(); ++i)
  391. {
  392. if (packages_[i]->Exists(name))
  393. return true;
  394. }
  395. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  396. if (fileSystem)
  397. {
  398. for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
  399. {
  400. if (fileSystem->FileExists(resourceDirs_[i] + name))
  401. return true;
  402. }
  403. }
  404. return false;
  405. }
  406. bool ResourceCache::Exists(StringHash nameHash) const
  407. {
  408. return Exists(GetResourceName(nameHash));
  409. }
  410. unsigned ResourceCache::GetMemoryBudget(ShortStringHash type) const
  411. {
  412. HashMap<ShortStringHash, ResourceGroup>::ConstIterator i = resourceGroups_.Find(type);
  413. if (i != resourceGroups_.End())
  414. return i->second_.memoryBudget_;
  415. else
  416. return 0;
  417. }
  418. unsigned ResourceCache::GetMemoryUse(ShortStringHash type) const
  419. {
  420. HashMap<ShortStringHash, ResourceGroup>::ConstIterator i = resourceGroups_.Find(type);
  421. if (i != resourceGroups_.End())
  422. return i->second_.memoryUse_;
  423. else
  424. return 0;
  425. }
  426. unsigned ResourceCache::GetTotalMemoryUse() const
  427. {
  428. unsigned total = 0;
  429. for (HashMap<ShortStringHash, ResourceGroup>::ConstIterator i = resourceGroups_.Begin(); i != resourceGroups_.End(); ++i)
  430. total += i->second_.memoryUse_;
  431. return total;
  432. }
  433. const String& ResourceCache::GetResourceName(StringHash nameHash) const
  434. {
  435. HashMap<StringHash, String>::ConstIterator i = hashToName_.Find(nameHash);
  436. if (i == hashToName_.End())
  437. return String::EMPTY;
  438. else
  439. return i->second_;
  440. }
  441. String ResourceCache::GetResourceFileName(const String& name) const
  442. {
  443. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  444. if (fileSystem)
  445. {
  446. for (unsigned i = 0; i < resourceDirs_.Size(); ++i)
  447. {
  448. if (fileSystem->FileExists(resourceDirs_[i] + name))
  449. return resourceDirs_[i] + name;
  450. }
  451. }
  452. return String();
  453. }
  454. String ResourceCache::GetPreferredResourceDir(const String& path) const
  455. {
  456. String fixedPath = AddTrailingSlash(path);
  457. bool pathHasKnownDirs = false;
  458. bool parentHasKnownDirs = false;
  459. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  460. // If no filesystem, can not check directory existence, so just return the original path
  461. if (!fileSystem)
  462. return fixedPath;
  463. for (unsigned i = 0; !checkDirs[i].Empty(); ++i)
  464. {
  465. if (fileSystem->DirExists(fixedPath + checkDirs[i]))
  466. {
  467. pathHasKnownDirs = true;
  468. break;
  469. }
  470. }
  471. if (!pathHasKnownDirs)
  472. {
  473. String parentPath = GetParentPath(fixedPath);
  474. for (unsigned i = 0; !checkDirs[i].Empty(); ++i)
  475. {
  476. if (fileSystem->DirExists(parentPath + checkDirs[i]))
  477. {
  478. parentHasKnownDirs = true;
  479. break;
  480. }
  481. }
  482. // If path does not have known subdirectories, but the parent path has, use the parent instead
  483. if (parentHasKnownDirs)
  484. fixedPath = parentPath;
  485. }
  486. return fixedPath;
  487. }
  488. String ResourceCache::SanitateResourceName(const String& nameIn) const
  489. {
  490. // Sanitate unsupported constructs from the resource name
  491. String name = GetInternalPath(nameIn);
  492. name.Replace("../", "");
  493. name.Replace("./", "");
  494. return name;
  495. }
  496. void ResourceCache::StoreNameHash(const String& name)
  497. {
  498. if (name.Empty())
  499. return;
  500. StringHash hash(name);
  501. // If entry exists, check for difference (collision)
  502. HashMap<StringHash, String>::Iterator i = hashToName_.Find(hash);
  503. if (i != hashToName_.End())
  504. {
  505. if (i->second_.Compare(name, false))
  506. LOGERROR("Resource hash collision " + i->second_ + " vs " + name);
  507. i->second_ = name;
  508. }
  509. else
  510. hashToName_[hash] = name;
  511. }
  512. const SharedPtr<Resource>& ResourceCache::FindResource(ShortStringHash type, StringHash nameHash)
  513. {
  514. HashMap<ShortStringHash, ResourceGroup>::Iterator i = resourceGroups_.Find(type);
  515. if (i == resourceGroups_.End())
  516. return noResource;
  517. HashMap<StringHash, SharedPtr<Resource> >::Iterator j = i->second_.resources_.Find(nameHash);
  518. if (j == i->second_.resources_.End())
  519. return noResource;
  520. return j->second_;
  521. }
  522. void ResourceCache::ReleasePackageResources(PackageFile* package, bool force)
  523. {
  524. HashSet<ShortStringHash> affectedGroups;
  525. const HashMap<String, PackageEntry>& entries = package->GetEntries();
  526. for (HashMap<String, PackageEntry>::ConstIterator i = entries.Begin(); i != entries.End(); ++i)
  527. {
  528. StringHash nameHash(i->first_);
  529. // We do not know the actual resource type, so search all type containers
  530. for (HashMap<ShortStringHash, ResourceGroup>::Iterator j = resourceGroups_.Begin();
  531. j != resourceGroups_.End(); ++j)
  532. {
  533. HashMap<StringHash, SharedPtr<Resource> >::Iterator k = j->second_.resources_.Find(nameHash);
  534. if (k != j->second_.resources_.End())
  535. {
  536. // If other references exist, do not release, unless forced
  537. if (k->second_.Refs() == 1 || force)
  538. {
  539. j->second_.resources_.Erase(k);
  540. affectedGroups.Insert(j->first_);
  541. }
  542. break;
  543. }
  544. }
  545. }
  546. for (HashSet<ShortStringHash>::Iterator i = affectedGroups.Begin(); i != affectedGroups.End(); ++i)
  547. UpdateResourceGroup(*i);
  548. }
  549. void ResourceCache::UpdateResourceGroup(ShortStringHash type)
  550. {
  551. HashMap<ShortStringHash, ResourceGroup>::Iterator i = resourceGroups_.Find(type);
  552. if (i == resourceGroups_.End())
  553. return;
  554. for (;;)
  555. {
  556. unsigned totalSize = 0;
  557. unsigned oldestTimer = 0;
  558. HashMap<StringHash, SharedPtr<Resource> >::Iterator oldestResource = i->second_.resources_.End();
  559. for (HashMap<StringHash, SharedPtr<Resource> >::Iterator j = i->second_.resources_.Begin();
  560. j != i->second_.resources_.End(); ++j)
  561. {
  562. totalSize += j->second_->GetMemoryUse();
  563. unsigned useTimer = j->second_->GetUseTimer();
  564. if (useTimer > oldestTimer)
  565. {
  566. oldestTimer = useTimer;
  567. oldestResource = j;
  568. }
  569. }
  570. i->second_.memoryUse_ = totalSize;
  571. // If memory budget defined and is exceeded, remove the oldest resource and loop again
  572. // (resources in use always return a zero timer and can not be removed)
  573. if (i->second_.memoryBudget_ && i->second_.memoryUse_ > i->second_.memoryBudget_ &&
  574. oldestResource != i->second_.resources_.End())
  575. {
  576. LOGDEBUG("Resource group " + oldestResource->second_->GetTypeName() + " over memory budget, releasing resource " +
  577. oldestResource->second_->GetName());
  578. i->second_.resources_.Erase(oldestResource);
  579. }
  580. else
  581. break;
  582. }
  583. }
  584. void ResourceCache::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  585. {
  586. if (!autoReloadResources_)
  587. return;
  588. for (unsigned i = 0; i < fileWatchers_.Size(); ++i)
  589. {
  590. String fileName;
  591. while (fileWatchers_[i]->GetNextChange(fileName))
  592. {
  593. // If the filename is a resource we keep track of, reload it
  594. for (HashMap<ShortStringHash, ResourceGroup>::Iterator j = resourceGroups_.Begin(); j != resourceGroups_.End(); ++j)
  595. {
  596. HashMap<StringHash, SharedPtr<Resource> >::Iterator k = j->second_.resources_.Find(StringHash(fileName));
  597. if (k != j->second_.resources_.End())
  598. {
  599. LOGDEBUG("Reloading changed resource " + fileName);
  600. ReloadResource(k->second_);
  601. }
  602. }
  603. }
  604. }
  605. }
  606. void RegisterResourceLibrary(Context* context)
  607. {
  608. Image::RegisterObject(context);
  609. XMLFile::RegisterObject(context);
  610. }
  611. }