winVFS.cpp

Engine/source/platformWin32/winVFS.cpp

More...

Classes:

Public Variables

Detailed Description

Public Variables

Win32VFSState gVFSState 

Public Functions

closeEmbeddedVFSArchive()

openEmbeddedVFSArchive()

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platformWin32/platformWin32.h"
 25#include "platform/platformVFS.h"
 26#include "console/console.h"
 27
 28#include "core/stream/memstream.h"
 29#include "core/util/zip/zipArchive.h"
 30
 31#include "core/util/safeDelete.h"
 32
 33#include "VFSRes.h"
 34
 35//-----------------------------------------------------------------------------
 36
 37struct Win32VFSState
 38{
 39   S32 mRefCount;
 40
 41   HGLOBAL mResData;
 42
 43   MemStream *mZipStream;
 44   Zip::ZipArchive *mZip;
 45
 46   Win32VFSState() : mResData(NULL), mZip(NULL), mRefCount(0), mZipStream(NULL)
 47   {
 48   }
 49};
 50
 51static Win32VFSState gVFSState;
 52
 53//-----------------------------------------------------------------------------
 54
 55Zip::ZipArchive *openEmbeddedVFSArchive()
 56{
 57   if(gVFSState.mZip)
 58   {
 59      ++gVFSState.mRefCount;
 60      return gVFSState.mZip;
 61   }
 62
 63   HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_ZIPFILE), dT("RT_RCDATA"));
 64
 65   if(hRsrc == NULL)
 66      return NULL;
 67   
 68   if((gVFSState.mResData = LoadResource(NULL, hRsrc)) == NULL)
 69      return NULL;
 70
 71   void * mem = LockResource(gVFSState.mResData);
 72   if(mem != NULL)
 73   {
 74      U32 size = SizeofResource(NULL, hRsrc);
 75      gVFSState.mZipStream = new MemStream(size, mem, true, false);
 76      gVFSState.mZip = new Zip::ZipArchive;
 77
 78      if(gVFSState.mZip->openArchive(gVFSState.mZipStream))
 79      {
 80         ++gVFSState.mRefCount;
 81         return gVFSState.mZip;
 82      }
 83
 84      SAFE_DELETE(gVFSState.mZip);
 85      SAFE_DELETE(gVFSState.mZipStream);
 86   }
 87
 88   FreeResource(gVFSState.mResData);
 89   gVFSState.mResData = NULL;
 90
 91   return NULL;
 92}
 93
 94void closeEmbeddedVFSArchive()
 95{
 96   if(gVFSState.mRefCount == 0)
 97      return;
 98
 99   --gVFSState.mRefCount;
100
101   if(gVFSState.mRefCount < 1)
102   {
103      SAFE_DELETE(gVFSState.mZip);
104      SAFE_DELETE(gVFSState.mZipStream);
105      
106      if(gVFSState.mResData)
107      {
108         FreeResource(gVFSState.mResData);
109         gVFSState.mResData = NULL;
110      }
111   }
112}
113