winVFS.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /*
  23. ** Alive and Ticking
  24. ** (c) Copyright 2006 Burnt Wasp
  25. ** All Rights Reserved.
  26. **
  27. ** Filename: winZipCat.cc
  28. ** Author: Tom Bampton
  29. ** Created: 28/10/2006
  30. ** Purpose:
  31. ** Amazing Zip Tricks
  32. **
  33. */
  34. #include "platformWin32/platformWin32.h"
  35. #include "platform/platformVFS.h"
  36. #include "console/console.h"
  37. #include "io/memstream.h"
  38. #include "io/zip/zipArchive.h"
  39. #include "memory/safeDelete.h"
  40. #include "VFSRes.h"
  41. //////////////////////////////////////////////////////////////////////////
  42. struct Win32VFSState
  43. {
  44. S32 mRefCount;
  45. HGLOBAL mResData;
  46. MemStream *mZipStream;
  47. Zip::ZipArchive *mZip;
  48. Win32VFSState() : mResData(NULL), mZip(NULL), mRefCount(0), mZipStream(NULL)
  49. {
  50. }
  51. };
  52. static Win32VFSState gVFSState;
  53. //////////////////////////////////////////////////////////////////////////
  54. Zip::ZipArchive *openEmbeddedVFSArchive()
  55. {
  56. if(gVFSState.mZip)
  57. {
  58. ++gVFSState.mRefCount;
  59. return gVFSState.mZip;
  60. }
  61. HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_ZIPFILE), dT("RT_RCDATA"));
  62. if(hRsrc == NULL)
  63. return NULL;
  64. if((gVFSState.mResData = LoadResource(NULL, hRsrc)) == NULL)
  65. return NULL;
  66. void *mem;
  67. if(mem = LockResource(gVFSState.mResData))
  68. {
  69. U32 size = SizeofResource(NULL, hRsrc);
  70. gVFSState.mZipStream = new MemStream(size, mem, true, false);
  71. gVFSState.mZip = new Zip::ZipArchive;
  72. if(gVFSState.mZip->openArchive(gVFSState.mZipStream))
  73. {
  74. ++gVFSState.mRefCount;
  75. return gVFSState.mZip;
  76. }
  77. SAFE_DELETE(gVFSState.mZip);
  78. SAFE_DELETE(gVFSState.mZipStream);
  79. }
  80. FreeResource(gVFSState.mResData);
  81. gVFSState.mResData = NULL;
  82. return NULL;
  83. }
  84. void closeEmbeddedVFSArchive()
  85. {
  86. if(gVFSState.mRefCount == 0)
  87. return;
  88. --gVFSState.mRefCount;
  89. if(gVFSState.mRefCount < 1)
  90. {
  91. SAFE_DELETE(gVFSState.mZip);
  92. SAFE_DELETE(gVFSState.mZipStream);
  93. if(gVFSState.mResData)
  94. {
  95. FreeResource(gVFSState.mResData);
  96. gVFSState.mResData = NULL;
  97. }
  98. }
  99. }