- Support for Zip Filesystem. It is now possible to zip the entire Res folder ("store" method preferred, zip so that the root of the zip has ai, player, etc...) in one single file for read only. Write access is done in another folder (hardcoded to be User/ for now, can be updated depending on platforms, etc...

-- zipFS has several limitations...
--- in a general way, seekg doesn't work... so getting a file's size needs to be done through JFileSystem.
--- getLine on files open with zipFS doesn't work so great. Not sure if it is a normal issue because files are open in binary or not... JFileSystem therefore offers a "readIntoString" function that needs to be used instead of the usual "getline" technique. However getLine can then be used on a stream connected to the string.

-- tested on Windows and PSP, I also made sure android still works, but haven't tested zip support on Android.
-- I tried to maintain backwards compatibility, but this might break on some platforms, if I broke some platforms and you can't find a way to fix them, please contact me and we'll figure something out
-- This removes wagic::ifstream. I didn't reimplement the securities that were involved in this, apologies for that. Might be useful to reimplement such securities in JFileSystem
-- I haven't tested options/profiles in a deep way, it is possible I broke that.
This commit is contained in:
wagic.the.homebrew
2011-08-21 09:04:59 +00:00
parent 14bd7b7a24
commit e27cf56fa2
73 changed files with 3539 additions and 936 deletions

View File

@@ -1,28 +1,11 @@
//-------------------------------------------------------------------------------------
//
// JGE++ is a hardware accelerated 2D game SDK for PSP/Windows.
//
// Licensed under the BSD license, see LICENSE in JGE root for details.
//
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
//
//-------------------------------------------------------------------------------------
#ifndef _J_FILE_SYSTEM_H_
#define _J_FILE_SYSTEM_H_
#ifndef _FILE_SYSTEM_H_
#define _FILE_SYSTEM_H_
#define JGE_GET_RES(filename) JFileSystem::GetInstance()->GetResourceFile(filename)
#define JGE_GET_RESPATH() JFileSystem::GetInstance()->GetResourceRoot()
#include <stdio.h>
#include <vector>
#include <map>
#include "zfsystem.h"
#include <string>
#if defined (PSP)
#include <pspiofilemgr.h>
#include <pspiofilemgr_fcntl.h>
#endif
using zip_file_system::filesystem;
using zip_file_system::izfstream;
using namespace std;
#include "unzip/unzip.h"
@@ -40,22 +23,30 @@ class JZipCache {
public:
JZipCache();
~JZipCache();
map<string,unz_file_pos *> dir;
map<string, filesystem::file_info> dir;
};
class JFileSystem
{
class JFileSystem {
private:
string mSystemFSPath, mUserFSPath;
filesystem * mSystemFS, * mUserFS;
static JFileSystem* mInstance;
izfstream mFile;
map<string,JZipCache *>mZipCache;
string mZipFileName;
int mFileSize;
char *mPassword;
bool mZipAvailable;
void preloadZip(const string& filename);
izfstream mZipFile;
filesystem::file_info * mCurrentFileInZip;
std::vector<std::string>& scanRealFolder(const std::string& folderName, std::vector<std::string>& results);
public:
//////////////////////////////////////////////////////////////////////////
/// Get the singleton instance
///
//////////////////////////////////////////////////////////////////////////
static JFileSystem* GetInstance();
static void Destroy();
//////////////////////////////////////////////////////////////////////////
/// Attach ZIP archive to the file system.
@@ -74,12 +65,26 @@ public:
//////////////////////////////////////////////////////////////////////////
void DetachZipFile();
// Manually Clear the zip cache
void clearZipCache();
//////////////////////////////////////////////////////////////////////////
/// Get the singleton instance
///
//////////////////////////////////////////////////////////////////////////
static JFileSystem* GetInstance();
static void Destroy();
//////////////////////////////////////////////////////////////////////////
/// Open file for reading.
///
//////////////////////////////////////////////////////////////////////////
bool OpenFile(const string &filename);
//Fills the vector results with a list of children of the given folder
std::vector<std::string>& scanfolder(const std::string& folderName, std::vector<std::string>& results);
std::vector<std::string> scanfolder(const std::string& folderName);
//////////////////////////////////////////////////////////////////////////
/// Read data from file.
///
@@ -96,6 +101,7 @@ public:
///
//////////////////////////////////////////////////////////////////////////
int GetFileSize();
int GetFileSize(izfstream & file);
//////////////////////////////////////////////////////////////////////////
/// Close file.
@@ -109,37 +115,47 @@ public:
/// @resourceRoot - New root.
///
//////////////////////////////////////////////////////////////////////////
void SetResourceRoot(const string& resourceRoot);
string GetResourceRoot();
void SetSystemRoot(const string& resourceRoot);
string GetSystemRoot() { return mSystemFSPath; };
// Returns a string prefixed with the resource path
string GetResourceFile(string filename);
void SetUSerRoot(const string& resourceRoot);
string GetUserRoot() { return mUserFSPath; };
bool openForRead(izfstream & File, const string & FilePath);
bool readIntoString(const string & FilePath, string & target);
bool openForWrite(ofstream & File, const string & FilePath, ios_base::openmode mode = ios_base::out );
bool Rename(string from, string to);
//Returns true if strFilename exists somewhere in the fileSystem
bool FileExists(const string& strFilename);
//Returns true if strdirname exists somewhere in the fileSystem, and is a directory
bool DirExists(const string& strDirname);
static void init( const string & userPath, const string & systemPath = "");
// AVOID Using This function!!!
/*
This function is deprecated, but some code is still using it
It used to give a pathname to a file in the file system.
Now with the support of zip resources, a pathname does not make sense anymore
However some of our code still relies on "physical" files not being in zip.
So this call is now super heavy: it checks where the file is, and if it's in a zip, it extracts
it to the user Filesystem, assuming that whoever called this needs to access the file through its pathname later on
*/
string GetResourceFile(string filename);
// Manually Clear the zip cache
void clearZipCache();
protected:
JFileSystem();
JFileSystem(const string & userPath, const string & systemPath = "");
~JFileSystem();
private:
static JFileSystem* mInstance;
map<string,JZipCache *>mZipCache;
string mResourceRoot;
string mZipFileName;
char *mPassword;
bool mZipAvailable;
void preloadZip(const string& filename);
#if defined (PSP)
SceUID mFile;
#else
FILE *mFile;
#endif
unzFile mZipFile;
int mFileSize;
};
#endif
#endif