2
0

winVFS.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "platformWin32/platformWin32.h"
  23. #include "platform/platformVFS.h"
  24. #include "console/console.h"
  25. #include "core/stream/memstream.h"
  26. #include "core/util/zip/zipArchive.h"
  27. #include "core/util/safeDelete.h"
  28. #include "VFSRes.h"
  29. //-----------------------------------------------------------------------------
  30. struct Win32VFSState
  31. {
  32. S32 mRefCount;
  33. HGLOBAL mResData;
  34. MemStream *mZipStream;
  35. Zip::ZipArchive *mZip;
  36. Win32VFSState() : mResData(NULL), mZip(NULL), mRefCount(0), mZipStream(NULL)
  37. {
  38. }
  39. };
  40. static Win32VFSState gVFSState;
  41. //-----------------------------------------------------------------------------
  42. Zip::ZipArchive *openEmbeddedVFSArchive()
  43. {
  44. if(gVFSState.mZip)
  45. {
  46. ++gVFSState.mRefCount;
  47. return gVFSState.mZip;
  48. }
  49. HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_ZIPFILE), dT("RT_RCDATA"));
  50. if(hRsrc == NULL)
  51. return NULL;
  52. if((gVFSState.mResData = LoadResource(NULL, hRsrc)) == NULL)
  53. return NULL;
  54. void * mem = LockResource(gVFSState.mResData);
  55. if(mem != NULL)
  56. {
  57. U32 size = SizeofResource(NULL, hRsrc);
  58. gVFSState.mZipStream = new MemStream(size, mem, true, false);
  59. gVFSState.mZip = new Zip::ZipArchive;
  60. if(gVFSState.mZip->openArchive(gVFSState.mZipStream))
  61. {
  62. ++gVFSState.mRefCount;
  63. return gVFSState.mZip;
  64. }
  65. SAFE_DELETE(gVFSState.mZip);
  66. SAFE_DELETE(gVFSState.mZipStream);
  67. }
  68. FreeResource(gVFSState.mResData);
  69. gVFSState.mResData = NULL;
  70. return NULL;
  71. }
  72. void closeEmbeddedVFSArchive()
  73. {
  74. if(gVFSState.mRefCount == 0)
  75. return;
  76. --gVFSState.mRefCount;
  77. if(gVFSState.mRefCount < 1)
  78. {
  79. SAFE_DELETE(gVFSState.mZip);
  80. SAFE_DELETE(gVFSState.mZipStream);
  81. if(gVFSState.mResData)
  82. {
  83. FreeResource(gVFSState.mResData);
  84. gVFSState.mResData = NULL;
  85. }
  86. }
  87. }