Files
wagic/JGE/include/JFileSystem.h
wagic.the.homebrew e27cf56fa2 - 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.
2011-08-21 09:04:59 +00:00

161 lines
5.1 KiB
C++

#ifndef _J_FILE_SYSTEM_H_
#define _J_FILE_SYSTEM_H_
#include "zfsystem.h"
#include <string>
using zip_file_system::filesystem;
using zip_file_system::izfstream;
using namespace std;
#include "unzip/unzip.h"
using namespace std;
//////////////////////////////////////////////////////////////////////////
/// Interface for low level file access with ZIP archive support. All
/// file operations in JGE are handled through this class so if a ZIP
/// archive is attached, all the resources will be loaded from the
/// archive file.
///
//////////////////////////////////////////////////////////////////////////
class JZipCache {
public:
JZipCache();
~JZipCache();
map<string, filesystem::file_info> dir;
};
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:
//////////////////////////////////////////////////////////////////////////
/// Attach ZIP archive to the file system.
///
/// @param zipfile - Name of ZIP archive.
/// @param password - Password for the ZIP archive. Default is NULL.
///
/// @return Status of the attach operation.
///
//////////////////////////////////////////////////////////////////////////
bool AttachZipFile(const string &zipfile, char *password = NULL);
//////////////////////////////////////////////////////////////////////////
/// Release the attached ZIP archive.
///
//////////////////////////////////////////////////////////////////////////
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.
///
/// @param buffer - Buffer for reading.
/// @param size - Number of bytes to read.
///
/// @return Number of bytes read.
///
//////////////////////////////////////////////////////////////////////////
int ReadFile(void *buffer, int size);
//////////////////////////////////////////////////////////////////////////
/// Get size of file.
///
//////////////////////////////////////////////////////////////////////////
int GetFileSize();
int GetFileSize(izfstream & file);
//////////////////////////////////////////////////////////////////////////
/// Close file.
///
//////////////////////////////////////////////////////////////////////////
void CloseFile();
//////////////////////////////////////////////////////////////////////////
/// Set root for all the following file operations
///
/// @resourceRoot - New root.
///
//////////////////////////////////////////////////////////////////////////
void SetSystemRoot(const string& resourceRoot);
string GetSystemRoot() { return mSystemFSPath; };
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);
protected:
JFileSystem(const string & userPath, const string & systemPath = "");
~JFileSystem();
};
#endif