resourceDictionary.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, 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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "io/resource/resourceManager.h"
  24. #include "collection/findIterator.h"
  25. //----------------------------------------------------------------------------
  26. //----------------------------------------------------------------------------
  27. ResDictionary::ResDictionary()
  28. {
  29. entryCount = 0;
  30. hashTableSize = 1023; //DefaultTableSize;
  31. hashTable = new ResourceObject *[hashTableSize];
  32. S32 i;
  33. for(i = 0; i < hashTableSize; i++)
  34. hashTable[i] = NULL;
  35. }
  36. ResDictionary::~ResDictionary()
  37. {
  38. // we assume the resources are purged before we destroy
  39. // the dictionary
  40. delete[] hashTable;
  41. }
  42. S32 ResDictionary::hash(StringTableEntry path, StringTableEntry file)
  43. {
  44. return ((U32)((((dsize_t)path) >> 2) + (((dsize_t)file) >> 2) )) % hashTableSize;
  45. }
  46. void ResDictionary::insert(ResourceObject *obj, StringTableEntry path, StringTableEntry file)
  47. {
  48. if(path)
  49. {
  50. char fullPath[1024];
  51. Platform::makeFullPathName(path, fullPath, sizeof(fullPath));
  52. path = StringTable->insert(fullPath);
  53. }
  54. obj->name = file;
  55. obj->path = path;
  56. S32 idx = hash(path, file);
  57. obj->nextEntry = hashTable[idx];
  58. hashTable[idx] = obj;
  59. entryCount++;
  60. if(entryCount > hashTableSize) {
  61. ResourceObject *head = NULL, *temp, *walk;
  62. for(idx = 0; idx < hashTableSize;idx++) {
  63. walk = hashTable[idx];
  64. while(walk)
  65. {
  66. temp = walk->nextEntry;
  67. walk->nextEntry = head;
  68. head = walk;
  69. walk = temp;
  70. }
  71. }
  72. delete[] hashTable;
  73. hashTableSize = 2 * hashTableSize - 1;
  74. hashTable = new ResourceObject *[hashTableSize];
  75. for(idx = 0; idx < hashTableSize; idx++)
  76. hashTable[idx] = NULL;
  77. walk = head;
  78. while(walk)
  79. {
  80. temp = walk->nextEntry;
  81. idx = hash(walk);
  82. walk->nextEntry = hashTable[idx];
  83. hashTable[idx] = walk;
  84. walk = temp;
  85. }
  86. }
  87. }
  88. ResourceObject* ResDictionary::find(StringTableEntry path, StringTableEntry name)
  89. {
  90. if(path)
  91. {
  92. char fullPath[1024];
  93. Platform::makeFullPathName(path, fullPath, sizeof(fullPath));
  94. path = StringTable->insert(fullPath);
  95. }
  96. for(ResourceObject *walk = hashTable[hash(path, name)]; walk; walk = walk->nextEntry)
  97. if(walk->name == name && walk->path == path)
  98. return walk;
  99. return NULL;
  100. }
  101. ResourceObject* ResDictionary::find(StringTableEntry path, StringTableEntry name, StringTableEntry zipPath, StringTableEntry zipName)
  102. {
  103. if(path)
  104. {
  105. char fullPath[1024];
  106. Platform::makeFullPathName(path, fullPath, sizeof(fullPath));
  107. path = StringTable->insert(fullPath);
  108. }
  109. for(ResourceObject *walk = hashTable[hash(path, name)]; walk; walk = walk->nextEntry)
  110. if(walk->name == name && walk->path == path && walk->zipName == zipName && walk->zipPath == zipPath)
  111. return walk;
  112. return NULL;
  113. }
  114. ResourceObject* ResDictionary::find(StringTableEntry path, StringTableEntry name, U32 flags)
  115. {
  116. if(path)
  117. {
  118. char fullPath[1024];
  119. Platform::makeFullPathName(path, fullPath, sizeof(fullPath));
  120. path = StringTable->insert(fullPath);
  121. }
  122. for(ResourceObject *walk = hashTable[hash(path, name)]; walk; walk = walk->nextEntry)
  123. if(walk->name == name && walk->path == path && U32(walk->flags) == flags)
  124. return walk;
  125. return NULL;
  126. }
  127. void ResDictionary::pushBehind(ResourceObject *resObj, S32 flagMask)
  128. {
  129. remove(resObj);
  130. entryCount++;
  131. ResourceObject **walk = &hashTable[hash(resObj)];
  132. for(; *walk; walk = &(*walk)->nextEntry)
  133. {
  134. if(!((*walk)->flags & flagMask))
  135. {
  136. resObj->nextEntry = *walk;
  137. *walk = resObj;
  138. return;
  139. }
  140. }
  141. resObj->nextEntry = NULL;
  142. *walk = resObj;
  143. }
  144. void ResDictionary::remove(ResourceObject *resObj)
  145. {
  146. for(ResourceObject **walk = &hashTable[hash(resObj)]; *walk; walk = &(*walk)->nextEntry)
  147. {
  148. if(*walk == resObj)
  149. {
  150. entryCount--;
  151. *walk = resObj->nextEntry;
  152. return;
  153. }
  154. }
  155. }