ResourceCache.cpp 21 KB

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