2015-03-17 20:59:39 +00:00
# include "filesystemarchive.hpp"
2021-12-14 20:36:11 +00:00
# include <algorithm>
2022-05-24 21:18:21 +00:00
# include <filesystem>
2015-03-17 20:59:39 +00:00
2018-08-14 15:42:41 +00:00
# include <components/debug/debuglog.hpp>
2022-07-16 16:19:56 +00:00
# include <components/files/constrainedfilestream.hpp>
2018-08-14 15:42:41 +00:00
2015-03-17 20:59:39 +00:00
namespace VFS
{
2022-06-19 11:28:33 +00:00
FileSystemArchive : : FileSystemArchive ( const std : : filesystem : : path & path )
2015-03-17 20:59:39 +00:00
: mBuiltIndex ( false )
, mPath ( path )
{
}
void FileSystemArchive : : listResources ( std : : map < std : : string , File * > & out , char ( * normalize_function ) ( char ) )
{
if ( ! mBuiltIndex )
{
2022-06-19 11:28:33 +00:00
const auto str = mPath . string ( ) ; //TODO(Project579): This will probably break in windows with unicode paths
size_t prefix = str . size ( ) ;
2015-03-17 20:59:39 +00:00
2022-06-19 11:28:33 +00:00
if ( ! mPath . empty ( ) & & str [ prefix - 1 ] ! = ' \\ ' & & str [ prefix - 1 ] ! = ' / ' )
2015-03-17 20:59:39 +00:00
+ + prefix ;
2022-06-19 11:28:33 +00:00
for ( const auto & i :
std : : filesystem : : recursive_directory_iterator ( mPath ) )
2015-03-17 20:59:39 +00:00
{
2022-06-19 11:28:33 +00:00
if ( std : : filesystem : : is_directory ( i ) )
2015-03-17 20:59:39 +00:00
continue ;
2022-06-19 11:28:33 +00:00
const auto & path = i . path ( ) ;
const auto & proper = path . string ( ) ; //TODO(Project579): This will probably break in windows with unicode paths
2015-03-17 20:59:39 +00:00
2022-06-19 11:28:33 +00:00
FileSystemArchiveFile file ( path ) ;
2015-03-17 20:59:39 +00:00
std : : string searchable ;
2022-06-19 11:28:33 +00:00
std : : transform ( std : : next ( proper . begin ( ) , static_cast < std : : string : : difference_type > ( prefix ) ) , proper . end ( ) , std : : back_inserter ( searchable ) , normalize_function ) ;
2015-03-17 20:59:39 +00:00
2021-06-09 20:25:46 +00:00
const auto inserted = mIndex . insert ( std : : make_pair ( searchable , file ) ) ;
if ( ! inserted . second )
2022-06-19 11:28:33 +00:00
Log ( Debug : : Warning ) < < " Warning: found duplicate file for ' " < < proper < < " ', please check your file system for two files with the same name in different cases. " ; //TODO(Project579): This will probably break in windows with unicode paths
2021-06-09 20:25:46 +00:00
else
out [ inserted . first - > first ] = & inserted . first - > second ;
2015-03-17 20:59:39 +00:00
}
mBuiltIndex = true ;
}
2021-06-09 20:25:46 +00:00
else
2015-03-17 20:59:39 +00:00
{
2021-06-09 20:25:46 +00:00
for ( index : : iterator it = mIndex . begin ( ) ; it ! = mIndex . end ( ) ; + + it )
{
out [ it - > first ] = & it - > second ;
}
2015-03-17 20:59:39 +00:00
}
}
2020-12-29 20:45:59 +00:00
bool FileSystemArchive : : contains ( const std : : string & file , char ( * normalize_function ) ( char ) ) const
{
2021-05-01 21:21:21 +00:00
return mIndex . find ( file ) ! = mIndex . end ( ) ;
2020-12-29 20:45:59 +00:00
}
std : : string FileSystemArchive : : getDescription ( ) const
{
2022-06-19 11:28:33 +00:00
return std : : string { " DIR: " } + mPath . string ( ) ; //TODO(Project579): This will probably break in windows with unicode paths
2020-12-29 20:45:59 +00:00
}
2015-03-17 20:59:39 +00:00
// ----------------------------------------------------------------------------------
2022-06-19 11:28:33 +00:00
FileSystemArchiveFile : : FileSystemArchiveFile ( const std : : filesystem : : path & path )
2015-03-17 20:59:39 +00:00
: mPath ( path )
{
}
Files : : IStreamPtr FileSystemArchiveFile : : open ( )
{
2022-04-10 15:25:26 +00:00
return Files : : openConstrainedFileStream ( mPath ) ;
2015-03-17 20:59:39 +00:00
}
}