24#include "aeongames/CRC.hpp"
36 static int read_inflated_data ( FILE* source, uint8_t* buffer, uint32_t buffer_size )
40 unsigned char in[CHUNK];
47 strm.next_in = Z_NULL;
48 ret = inflateInit ( &strm );
53 strm.avail_out = buffer_size;
54 strm.next_out = buffer;
59 strm.avail_in =
static_cast<uInt
> ( fread ( in, 1, CHUNK, source ) );
60 if ( ferror ( source ) )
62 ( void ) inflateEnd ( &strm );
65 if ( strm.avail_in == 0 )
70 ret = inflate ( &strm, Z_NO_FLUSH );
71 assert ( ret != Z_STREAM_ERROR );
78 ( void ) inflateEnd ( &strm );
83 while ( ret != Z_STREAM_END );
85 ( void ) inflateEnd ( &strm );
86 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
91 if ( std::filesystem::is_directory ( mPath ) )
93 static const std::string format (
"/" );
94 static const std::regex separator (
"\\\\+" );
95 for (
auto& i : std::filesystem::recursive_directory_iterator ( mPath ) )
97 if ( std::filesystem::is_regular_file ( i ) )
99 std::string location{std::regex_replace ( std::filesystem::relative ( i, mPath ).
string(), separator, format ) };
100 mIndexTable[
crc32i ( location.data(), location.size() )] = location;
108 mPath ( aPackage.mPath ),
109 mIndexTable ( std::move ( aPackage.mIndexTable ) ) {}
121 if ( std::filesystem::is_directory ( mPath ) )
123 auto it = mIndexTable.find ( crc );
124 if ( it != mIndexTable.end() )
126 return std::filesystem::file_size ( mPath / it->second );
137 LoadFile (
crc32i ( aFilename.data(), aFilename.size() ), buffer, buffer_size );
141 if ( std::filesystem::is_directory ( mPath ) )
143 auto it = mIndexTable.find ( crc );
144 if ( it != mIndexTable.end() )
147 file.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
148 file.open ( mPath / it->second, std::ifstream::in | std::ifstream::binary );
149 file.read (
reinterpret_cast<char*
> ( buffer ), buffer_size );
Header for the PKG file specification.
DLL const std::filesystem::path & GetPath() const
Get the path associated with this package.
DLL size_t GetFileSize(uint32_t crc) const
DLL void LoadFile(uint32_t crc, void *buffer, size_t buffer_size) const
DLL ~Package()
Destructor.
DLL const std::unordered_map< uint32_t, std::string > & GetIndexTable() const
Get the package index table.
DLL Package(const std::string &aPath)
Construct a Package from a file path.
<- This is here just for the literals
uint32_t crc32i(const char *message, size_t size, uint32_t previous_crc)
Compute the CRC32 of a given message, continuing from a previous CRC value.