mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-08-18 06:55:48 +03:00
Added System::VirtualFileSystem.
--HG-- branch : dev-2.1
This commit is contained in:
@@ -17,7 +17,6 @@ using namespace EE::Math;
|
||||
#include <eepp/system/pack.hpp>
|
||||
#include <eepp/system/resourcemanager.hpp>
|
||||
#include <eepp/system/container.hpp>
|
||||
#include <eepp/system/packmanager.hpp>
|
||||
#include <eepp/system/filesystem.hpp>
|
||||
using namespace EE::System;
|
||||
|
||||
|
||||
@@ -27,5 +27,10 @@
|
||||
#include <eepp/system/base64.hpp>
|
||||
#include <eepp/system/md5.hpp>
|
||||
#include <eepp/system/translator.hpp>
|
||||
#include <eepp/system/iostream.hpp>
|
||||
#include <eepp/system/iostreamfile.hpp>
|
||||
#include <eepp/system/iostreamzip.hpp>
|
||||
#include <eepp/system/iostreampak.hpp>
|
||||
#include <eepp/system/virtualfilesystem.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -15,10 +15,10 @@ class Container {
|
||||
virtual ~Container();
|
||||
|
||||
/** @brief Add to the list the resource. */
|
||||
T * add( T * Resource );
|
||||
T * add( T * resource );
|
||||
|
||||
/** @brief Remove from the list the resource. */
|
||||
bool remove( T * Resource );
|
||||
bool remove( T * resource );
|
||||
|
||||
/** @returns The number of resources added to the container. */
|
||||
Uint32 count();
|
||||
@@ -27,30 +27,26 @@ class Container {
|
||||
};
|
||||
|
||||
template <class T>
|
||||
Container<T>::Container()
|
||||
{
|
||||
}
|
||||
Container<T>::Container() {}
|
||||
|
||||
template <class T>
|
||||
Container<T>::~Container()
|
||||
{
|
||||
}
|
||||
Container<T>::~Container() {}
|
||||
|
||||
template <class T>
|
||||
T * Container<T>::add( T * Resource ) {
|
||||
if ( NULL != Resource ) {
|
||||
mResources.push_back( Resource );
|
||||
T * Container<T>::add( T * resource ) {
|
||||
if ( NULL != resource ) {
|
||||
mResources.push_back( resource );
|
||||
|
||||
return Resource;
|
||||
return resource;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool Container<T>::remove( T * Resource ) {
|
||||
if ( NULL != Resource ) {
|
||||
mResources.remove( Resource );
|
||||
bool Container<T>::remove( T * resource ) {
|
||||
if ( NULL != resource ) {
|
||||
mResources.remove( resource );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class EE_API IOStreamPak : public IOStream {
|
||||
** @param pack Pack to open from path
|
||||
** @param path Path of the file in the pack file
|
||||
**/
|
||||
IOStreamPak( Pak * pack, const std::string& path );
|
||||
IOStreamPak( Pak * pack, const std::string& path, bool writeMode = false );
|
||||
|
||||
virtual ~IOStreamPak();
|
||||
|
||||
|
||||
@@ -74,6 +74,10 @@ class EE_API Pack : protected Mutex {
|
||||
virtual IOStream * getFileStream( const std::string& path ) = 0;
|
||||
protected:
|
||||
bool mIsOpen;
|
||||
|
||||
void onPackOpened();
|
||||
|
||||
void onPackClosed();
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace EE { namespace System {
|
||||
/** @brief The Pack Manager keep track of the instanciated Packs.
|
||||
It's used to find files from any open pack.
|
||||
*/
|
||||
class EE_API PackManager : public Container<Pack> {
|
||||
class EE_API PackManager : protected Container<Pack> {
|
||||
SINGLETON_DECLARE_HEADERS(PackManager)
|
||||
|
||||
public:
|
||||
@@ -36,6 +36,8 @@ class EE_API PackManager : public Container<Pack> {
|
||||
*/
|
||||
void setFallbackToPacks( const bool& fallback );
|
||||
protected:
|
||||
friend class Pack;
|
||||
|
||||
bool mFallback;
|
||||
|
||||
PackManager();
|
||||
|
||||
@@ -10,6 +10,8 @@ class EE_API SafeDataPointer {
|
||||
public:
|
||||
SafeDataPointer();
|
||||
|
||||
SafeDataPointer( Uint32 size );
|
||||
|
||||
SafeDataPointer( Uint8 * data, Uint32 size );
|
||||
|
||||
/** @brief The destructor deletes the buffer */
|
||||
|
||||
68
include/eepp/system/virtualfilesystem.hpp
Normal file
68
include/eepp/system/virtualfilesystem.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef EE_VIRTUALFILESYSTEM_HPP
|
||||
#define EE_VIRTUALFILESYSTEM_HPP
|
||||
|
||||
#include <eepp/system/singleton.hpp>
|
||||
#include <eepp/system/container.hpp>
|
||||
#include <eepp/system/iostream.hpp>
|
||||
#include <eepp/system/pack.hpp>
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
class EE_API VirtualFileSystem : protected Container<Pack> {
|
||||
SINGLETON_DECLARE_HEADERS(VirtualFileSystem)
|
||||
|
||||
public:
|
||||
std::vector<std::string> filesGetInPath( std::string path );
|
||||
|
||||
Pack * getPackFromFile( std::string path );
|
||||
|
||||
IOStream * getFileFromPath( const std::string& path );
|
||||
|
||||
bool fileExists( const std::string& path );
|
||||
protected:
|
||||
friend class Pack;
|
||||
|
||||
class vfsFile {
|
||||
public:
|
||||
std::string path;
|
||||
Pack * pack;
|
||||
|
||||
vfsFile() {}
|
||||
|
||||
vfsFile( std::string path, Pack * pack ) :
|
||||
path( path ),
|
||||
pack( pack )
|
||||
{}
|
||||
};
|
||||
|
||||
class vfsDirectory {
|
||||
public:
|
||||
vfsDirectory() {}
|
||||
std::string path;
|
||||
std::map<std::string,vfsFile> files;
|
||||
std::map<std::string,vfsDirectory> directories;
|
||||
};
|
||||
|
||||
VirtualFileSystem();
|
||||
|
||||
void onResourceAdd( Pack * resource );
|
||||
|
||||
void onResourceRemove( Pack * resource );
|
||||
|
||||
void addFile( std::string path, Pack * pack );
|
||||
|
||||
void removePackFromDirectory( Pack * resource, vfsDirectory& directory );
|
||||
|
||||
vfsDirectory mRoot;
|
||||
};
|
||||
|
||||
class EE_API VFS {
|
||||
public:
|
||||
static VirtualFileSystem * instance() {
|
||||
return VirtualFileSystem::instance();
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
@@ -33,6 +33,7 @@
|
||||
../../include/eepp/system/iostreampak.hpp
|
||||
../../include/eepp/system/iostreamzip.hpp
|
||||
../../include/eepp/system/translator.hpp
|
||||
../../include/eepp/system/virtualfilesystem.hpp
|
||||
../../include/eepp/ui/uieventmouse.hpp
|
||||
../../include/eepp/ui/uigridlayout.hpp
|
||||
../../include/eepp/ui/uiimage.hpp
|
||||
@@ -85,6 +86,7 @@
|
||||
../../src/eepp/system/iostreampak.cpp
|
||||
../../src/eepp/system/iostreamzip.cpp
|
||||
../../src/eepp/system/translator.cpp
|
||||
../../src/eepp/system/virtualfilesystem.cpp
|
||||
../../src/eepp/ui/uigridlayout.cpp
|
||||
../../src/eepp/ui/uiimage.cpp
|
||||
../../src/eepp/ui/uilayout.cpp
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <eepp/graphics/renderer/renderergl3.hpp>
|
||||
#include <eepp/graphics/renderer/renderergl3cp.hpp>
|
||||
#include <eepp/graphics/renderer/renderergles2.hpp>
|
||||
#include <eepp/system/packmanager.hpp>
|
||||
|
||||
namespace EE { namespace Graphics {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <eepp/graphics/textureatlasmanager.hpp>
|
||||
#include <eepp/graphics/texturepacker.hpp>
|
||||
#include <eepp/graphics/textureatlas.hpp>
|
||||
#include <eepp/system/packmanager.hpp>
|
||||
#include <eepp/system/iostreamfile.hpp>
|
||||
#include <eepp/system/iostreammemory.hpp>
|
||||
#include <eepp/graphics/packerhelper.hpp>
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <eepp/graphics/renderer/opengl.hpp>
|
||||
#include <eepp/graphics/renderer/renderer.hpp>
|
||||
#include <eepp/system/iostreamfile.hpp>
|
||||
#include <eepp/system/packmanager.hpp>
|
||||
#include <SOIL2/src/SOIL2/stb_image.h>
|
||||
#include <SOIL2/src/SOIL2/SOIL2.h>
|
||||
#include <jpeg-compressor/jpgd.h>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
IOStreamPak::IOStreamPak(Pak * pack, const std::string & path) :
|
||||
IOStreamPak::IOStreamPak( Pak * pack, const std::string & path, bool writeMode ) :
|
||||
mFile( NULL ),
|
||||
mPos( 0 ),
|
||||
mOpen( false )
|
||||
@@ -13,7 +13,7 @@ IOStreamPak::IOStreamPak(Pak * pack, const std::string & path) :
|
||||
if ( -1 != ( index = pack->exists( path ) ) ) {
|
||||
mEntry = pack->getPackEntry( (Uint32)index );
|
||||
|
||||
mFile = eeNew( IOStreamFile, ( pack->getPackPath() ) );
|
||||
mFile = eeNew( IOStreamFile, ( pack->getPackPath(), ( writeMode ? std::ios::out : std::ios::in ) | std::ios::binary ) );
|
||||
|
||||
if ( mFile->isOpen() ) {
|
||||
mFile->seek( mEntry.file_position );
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <eepp/system/pack.hpp>
|
||||
#include <eepp/system/packmanager.hpp>
|
||||
#include <eepp/system/virtualfilesystem.hpp>
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
@@ -18,4 +19,12 @@ bool Pack::isOpen() const {
|
||||
return mIsOpen;
|
||||
}
|
||||
|
||||
void Pack::onPackOpened() {
|
||||
VirtualFileSystem::instance()->onResourceAdd( this );
|
||||
}
|
||||
|
||||
void Pack::onPackClosed() {
|
||||
VirtualFileSystem::instance()->onResourceRemove( this );
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
@@ -70,6 +70,8 @@ bool Pak::open( const std::string& path ) {
|
||||
|
||||
mIsOpen = true;
|
||||
|
||||
onPackOpened();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -85,6 +87,8 @@ bool Pak::close() {
|
||||
|
||||
mIsOpen = false;
|
||||
|
||||
onPackClosed();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,12 @@ SafeDataPointer::SafeDataPointer() :
|
||||
{
|
||||
}
|
||||
|
||||
SafeDataPointer::SafeDataPointer( Uint32 size ) :
|
||||
Data( eeNewArray( Uint8, size ) ),
|
||||
DataSize( size )
|
||||
{
|
||||
}
|
||||
|
||||
SafeDataPointer::SafeDataPointer( Uint8 *data, Uint32 size ) :
|
||||
Data( data ),
|
||||
DataSize( size )
|
||||
|
||||
139
src/eepp/system/virtualfilesystem.cpp
Normal file
139
src/eepp/system/virtualfilesystem.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <eepp/system/virtualfilesystem.hpp>
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
SINGLETON_DECLARE_IMPLEMENTATION(VirtualFileSystem)
|
||||
|
||||
static std::vector<std::string> vfsSplitPath( std::string& path ) {
|
||||
#if EE_PLATFORM == EE_PLATFORM_WIN
|
||||
if ( path.find_first_of( '\\' ) != std::string::npos ) {
|
||||
String::replaceAll( path, "\\", "/" );
|
||||
}
|
||||
#endif
|
||||
return String::split( path, '/' );
|
||||
}
|
||||
|
||||
VirtualFileSystem::VirtualFileSystem() {
|
||||
}
|
||||
|
||||
std::vector<std::string> VirtualFileSystem::filesGetInPath( std::string path ) {
|
||||
std::vector<std::string> files;
|
||||
std::vector<std::string> paths = vfsSplitPath( path );
|
||||
vfsDirectory * curDir = &mRoot;
|
||||
size_t pos = 0;
|
||||
|
||||
if ( paths.size() >= 1 ) {
|
||||
do {
|
||||
if ( pos < paths.size() ) {
|
||||
if ( curDir->directories.find( paths[pos] ) == curDir->directories.end() ) {
|
||||
return files;
|
||||
}
|
||||
|
||||
curDir = &curDir->directories[ paths[pos] ];
|
||||
}
|
||||
|
||||
pos++;
|
||||
} while ( pos < paths.size() );
|
||||
}
|
||||
|
||||
for ( auto it = curDir->files.begin(); it != curDir->files.end(); ++it ) {
|
||||
vfsFile& file = it->second;
|
||||
files.push_back( file.path );
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
Pack * VirtualFileSystem::getPackFromFile( std::string path ) {
|
||||
std::vector<std::string> paths = vfsSplitPath( path );
|
||||
vfsDirectory * curDir = &mRoot;
|
||||
size_t pos = 0;
|
||||
|
||||
if ( paths.size() >= 1 ) {
|
||||
do {
|
||||
if ( pos == paths.size() - 1 ) {
|
||||
if ( curDir->files.find( paths[pos] ) != curDir->files.end() ) {
|
||||
return curDir->files[ paths[pos] ].pack;
|
||||
}
|
||||
} else {
|
||||
if ( curDir->directories.find( paths[pos] ) == curDir->directories.end() ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
curDir = &curDir->directories[ paths[pos] ];
|
||||
}
|
||||
|
||||
pos++;
|
||||
} while ( pos < paths.size() );
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
IOStream * VirtualFileSystem::getFileFromPath( const std::string& path ) {
|
||||
Pack * pack = getPackFromFile( path );
|
||||
return NULL != pack ? pack->getFileStream( path ) : NULL;
|
||||
}
|
||||
|
||||
bool VirtualFileSystem::fileExists( const std::string& path ) {
|
||||
return NULL != getPackFromFile( path );
|
||||
}
|
||||
|
||||
void VirtualFileSystem::onResourceAdd( Pack * resource ) {
|
||||
add( resource );
|
||||
|
||||
std::vector<std::string> files = resource->getFileList();
|
||||
|
||||
for ( auto it = files.begin(); it != files.end(); ++it ) {
|
||||
addFile( *it, resource );
|
||||
}
|
||||
}
|
||||
|
||||
void VirtualFileSystem::onResourceRemove( Pack * resource ) {
|
||||
remove( resource );
|
||||
removePackFromDirectory( resource, mRoot );
|
||||
}
|
||||
|
||||
void VirtualFileSystem::addFile( std::string path , Pack * pack ) {
|
||||
std::vector<std::string> paths = vfsSplitPath( path );
|
||||
vfsDirectory * curDir = &mRoot;
|
||||
size_t pos = 0;
|
||||
|
||||
if ( paths.size() >= 1 ) {
|
||||
do {
|
||||
if ( pos == paths.size() - 1 ) {
|
||||
curDir->files[ paths[pos] ] = vfsFile( path, pack );
|
||||
} else {
|
||||
if ( curDir->directories.find( paths[pos] ) == curDir->directories.end() ) {
|
||||
curDir->directories[ paths[pos] ] = vfsDirectory();
|
||||
}
|
||||
|
||||
curDir = &curDir->directories[ paths[pos] ];
|
||||
}
|
||||
|
||||
pos++;
|
||||
} while ( pos < paths.size() );
|
||||
}
|
||||
}
|
||||
|
||||
void VirtualFileSystem::removePackFromDirectory( Pack * resource, vfsDirectory& directory ) {
|
||||
std::vector<std::string> removeList;
|
||||
|
||||
for ( auto it = directory.files.begin(); it != directory.files.end(); ++it ) {
|
||||
vfsFile& file = it->second;
|
||||
|
||||
if ( resource == file.pack ) {
|
||||
removeList.push_back( it->first );
|
||||
}
|
||||
}
|
||||
|
||||
for( auto it = removeList.begin(); it != removeList.end(); ++it ) {
|
||||
directory.files.erase( *it );
|
||||
}
|
||||
|
||||
for ( auto it = directory.directories.begin(); it != directory.directories.end(); ++it ) {
|
||||
removePackFromDirectory( resource, it->second );
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -25,6 +25,8 @@ bool Zip::create( const std::string& path ) {
|
||||
|
||||
mIsOpen = true;
|
||||
|
||||
onPackOpened();
|
||||
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
@@ -44,6 +46,8 @@ bool Zip::open( const std::string& path ) {
|
||||
|
||||
mIsOpen = true;
|
||||
|
||||
onPackOpened();
|
||||
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
@@ -63,6 +67,8 @@ bool Zip::close() {
|
||||
|
||||
mZip = NULL;
|
||||
|
||||
onPackClosed();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -234,13 +240,14 @@ std::vector<std::string> Zip::getFileList() {
|
||||
|
||||
Int32 numfiles = zip_get_num_files( mZip );
|
||||
|
||||
tmpv.resize( numfiles );
|
||||
|
||||
for ( Int32 i = 0; i < numfiles; i++ ) {
|
||||
struct zip_stat zs;
|
||||
|
||||
if ( -1 != zip_stat_index( mZip, i, 0, &zs ) )
|
||||
tmpv[i] = std::string ( zs.name );
|
||||
if ( -1 != zip_stat_index( mZip, i, 0, &zs ) ) {
|
||||
if ( zs.size > 0 ) {
|
||||
tmpv.push_back( std::string ( zs.name ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tmpv;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <eepp/graphics/globalbatchrenderer.hpp>
|
||||
#include <eepp/graphics/framebuffer.hpp>
|
||||
#include <eepp/graphics/renderer/renderer.hpp>
|
||||
#include <eepp/system/packmanager.hpp>
|
||||
#include <pugixml/pugixml.hpp>
|
||||
#include <eepp/ui/uiwidgetcreator.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <eepp/window/engine.hpp>
|
||||
#include <eepp/system/packmanager.hpp>
|
||||
#include <eepp/system/virtualfilesystem.hpp>
|
||||
#include <eepp/system/inifile.hpp>
|
||||
#include <eepp/graphics/texturefactory.hpp>
|
||||
#include <eepp/graphics/fontmanager.hpp>
|
||||
@@ -81,6 +82,8 @@ Engine::~Engine() {
|
||||
|
||||
Graphics::Private::VertexBufferManager::destroySingleton();
|
||||
|
||||
VirtualFileSystem::destroySingleton();
|
||||
|
||||
Log::destroySingleton();
|
||||
|
||||
#ifdef EE_SSL_SUPPORT
|
||||
|
||||
@@ -1171,14 +1171,13 @@ void EETest::loadTextures() {
|
||||
|
||||
Uint32 i;
|
||||
|
||||
PakTest = eeNew( Zip, () );
|
||||
|
||||
#ifndef EE_GLES
|
||||
|
||||
#if defined( EE_X11_PLATFORM ) || EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX
|
||||
Engine::instance()->enableSharedGLContext();
|
||||
#endif
|
||||
|
||||
PakTest = eeNew( Zip, () );
|
||||
PakTest->open( MyPath + "test.zip" );
|
||||
|
||||
std::vector<std::string> files = PakTest->getFileList();
|
||||
|
||||
Reference in New Issue
Block a user