ResourceMapRouter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include "JSONValue.h"
  24. #include "JSONFile.h"
  25. #include "ResourceCache.h"
  26. #include "ResourceMapRouter.h"
  27. namespace Atomic
  28. {
  29. ResourceMapRouter::ResourceMapRouter(Context* context, const String& mapFile) : ResourceRouter(context)
  30. {
  31. if (mapFile.Length())
  32. {
  33. ResourceCache* cache = GetSubsystem<ResourceCache>();
  34. if (!cache)
  35. {
  36. ATOMIC_LOGERROR("ResourceMapRouter::ResourceMapRouter - ResourceCache subsystem not found");
  37. return;
  38. }
  39. SharedPtr<JSONFile> jsonFile = cache->GetTempResource<JSONFile>(mapFile);
  40. context_->RegisterSubsystem(this);
  41. if (jsonFile.NotNull())
  42. {
  43. if (Load(jsonFile->GetRoot()))
  44. {
  45. cache->AddResourceRouter(this);
  46. }
  47. else
  48. {
  49. ATOMIC_LOGERROR("ResourceMapRouter::ResourceMapRouter - Unable to load resource map json");
  50. }
  51. }
  52. else
  53. {
  54. ATOMIC_LOGERROR("ResourceMapRouter::ResourceMapRouter - Unable to parse resource map json");
  55. }
  56. }
  57. }
  58. bool ResourceMapRouter::Load(const JSONValue& json)
  59. {
  60. const JSONValue& assetMap = json.Get("assetMap");
  61. if (!assetMap.IsObject())
  62. return false;
  63. ConstJSONObjectIterator itr = assetMap.Begin();
  64. while (itr != assetMap.End())
  65. {
  66. StringVector tags = itr->first_.Split(';');
  67. if (tags.Size() == 2)
  68. {
  69. StringHash::RegisterSignificantString(tags[0]);
  70. StringHash::RegisterSignificantString(tags[1]);
  71. resourceMap_[tags[0]][tags[1]] = itr->second_.GetString();
  72. }
  73. itr++;
  74. }
  75. return true;
  76. }
  77. void ResourceMapRouter::Route(String& name, StringHash type, ResourceRequest requestType)
  78. {
  79. if (type == StringHash::ZERO)
  80. return;
  81. HashMap<StringHash, HashMap<StringHash, String>>::ConstIterator itr = resourceMap_.Find(type);
  82. if (itr == resourceMap_.End())
  83. {
  84. return;
  85. }
  86. HashMap<StringHash, String>::ConstIterator mitr = itr->second_.Find(name);
  87. if (mitr == itr->second_.End())
  88. {
  89. return;
  90. }
  91. name = mitr->second_;
  92. }
  93. }