scenePersist.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "lighting/common/scenePersist.h"
  24. #include "lighting/lightingInterfaces.h"
  25. #include "scene/sceneManager.h"
  26. #include "lighting/lightManager.h"
  27. U32 PersistInfo::smFileVersion = 0x11;
  28. PersistInfo::~PersistInfo()
  29. {
  30. for(U32 i = 0; i < mChunks.size(); i++)
  31. delete mChunks[i];
  32. }
  33. //------------------------------------------------------------------------------
  34. bool PersistInfo::read(Stream & stream)
  35. {
  36. U32 version;
  37. if(!stream.read(&version) || version != smFileVersion)
  38. return(false);
  39. U32 numChunks;
  40. if(!stream.read(&numChunks))
  41. return(false);
  42. if(numChunks == 0)
  43. return(false);
  44. // read in all the chunks
  45. for(U32 i = 0; i < numChunks; i++)
  46. {
  47. U32 chunkType;
  48. if(!stream.read(&chunkType))
  49. return(false);
  50. // MissionChunk must be first chunk
  51. if(i == 0 && chunkType != PersistChunk::MissionChunkType)
  52. return(false);
  53. if(i != 0 && chunkType == PersistChunk::MissionChunkType)
  54. return(false);
  55. // Create the right chunk for the system
  56. bool bChunkFound = false;
  57. SceneLightingInterfaces sli = LIGHTMGR->getSceneLightingInterface()->mAvailableSystemInterfaces;
  58. for(SceneLightingInterface** itr = sli.begin(); itr != sli.end() && !bChunkFound; itr++)
  59. {
  60. PersistInfo::PersistChunk* pc = (*itr)->createPersistChunk(chunkType);
  61. if (pc != NULL)
  62. {
  63. mChunks.push_back(pc);
  64. bChunkFound = true;
  65. }
  66. }
  67. if (!bChunkFound)
  68. {
  69. // create the chunk
  70. switch(chunkType)
  71. {
  72. case PersistChunk::MissionChunkType:
  73. mChunks.push_back(new PersistInfo::MissionChunk);
  74. break;
  75. default:
  76. return(false);
  77. break;
  78. }
  79. }
  80. // load the chunk info
  81. if(!mChunks[i]->read(stream))
  82. return(false);
  83. }
  84. return(true);
  85. }
  86. bool PersistInfo::write(Stream & stream)
  87. {
  88. if(!stream.write(smFileVersion))
  89. return(false);
  90. if(!stream.write((U32)mChunks.size()))
  91. return(false);
  92. for(U32 i = 0; i < mChunks.size(); i++)
  93. {
  94. if(!stream.write(mChunks[i]->mChunkType))
  95. return(false);
  96. if(!mChunks[i]->write(stream))
  97. return(false);
  98. }
  99. return(true);
  100. }
  101. //------------------------------------------------------------------------------
  102. // Class SceneLighting::PersistInfo::PersistChunk
  103. //------------------------------------------------------------------------------
  104. bool PersistInfo::PersistChunk::read(Stream & stream)
  105. {
  106. if(!stream.read(&mChunkCRC))
  107. return(false);
  108. return(true);
  109. }
  110. bool PersistInfo::PersistChunk::write(Stream & stream)
  111. {
  112. if(!stream.write(mChunkCRC))
  113. return(false);
  114. return(true);
  115. }
  116. //------------------------------------------------------------------------------
  117. // Class SceneLighting::PersistInfo::MissionChunk
  118. //------------------------------------------------------------------------------
  119. PersistInfo::MissionChunk::MissionChunk()
  120. {
  121. mChunkType = PersistChunk::MissionChunkType;
  122. }