Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::base::ZipAssetManager Class Reference

ZipAssetManager manages all zipfile assets in Ion. More...

#include "zipassetmanager.h"

Public Member Functions

 ~ZipAssetManager ()
 The destructor is public so that the StaticDeleter that destroys the manager can access it. More...
 

Static Public Member Functions

static bool RegisterAssetData (const void *data, size_t data_size)
 Registers a data pointer with a certain length with the manager. More...
 
static bool ContainsFile (const std::string &filename)
 Returns whether the manager contains the passed filename. More...
 
static bool IsFileCached (const std::string &filename)
 Returns whether the file is cached in the manager. More...
 
static std::vector< std::string > GetRegisteredFileNames ()
 Returns the list of registered filenames. More...
 
static std::shared_ptr< const
std::string > 
GetFileDataPtr (const std::string &filename)
 Returns the shared pointer to the data of the passed filename if the manager contains it. More...
 
static const std::string & GetFileData (const std::string &filename)
 Returns the data of the passed filename if the manager contains it. More...
 
static bool GetFileDataNoCache (const std::string &filename, std::string *out)
 As above but the decompressed bytes are not internally cached. More...
 
static bool UpdateFileIfChanged (const std::string &filename, std::chrono::system_clock::time_point *timestamp)
 If the source file of a zipped file is available on disk (based on the file's manifest), this function updates the cached unzipped data from the source file if it has changed since the data was registered and the source file is readable. More...
 
static bool SetFileData (const std::string &filename, const std::string &source)
 Sets the data of the passed filename if the manager contains it. More...
 
static bool SaveFileData (const std::string &filename)
 Attempts to save the latest cached data of the passed filename back to the original source file on disk, using the name in the internal manifest. More...
 
static void Reset ()
 Resets the manager back to its initial, empty state. More...
 

Detailed Description

ZipAssetManager manages all zipfile assets in Ion.

Assets are registered through RegisterAssetData(), which extracts all file names from the zip and adds them to its registry. Use GetFileData() to return the data of a file. Files are only extracted the first time they are requested through GetFileData(); file contents are cached internally after extraction.

Note
zip assets must be explicitly registered through RegisterAssetData().

Definition at line 42 of file zipassetmanager.h.

Constructor & Destructor Documentation

ion::base::ZipAssetManager::~ZipAssetManager ( )

The destructor is public so that the StaticDeleter that destroys the manager can access it.

Definition at line 46 of file zipassetmanager.cc.

References Reset().

Member Function Documentation

bool ion::base::ZipAssetManager::ContainsFile ( const std::string &  filename)
static

Returns whether the manager contains the passed filename.

Definition at line 120 of file zipassetmanager.cc.

const std::string & ion::base::ZipAssetManager::GetFileData ( const std::string &  filename)
static

Returns the data of the passed filename if the manager contains it.

If there is any error while decompressing the file or if the file has not been registered then returns an InvalidReference. The returned reference is only valid so long as the registered file does not change.

Definition at line 158 of file zipassetmanager.cc.

Referenced by ion::text::FontManager::AddFontFromZipasset(), ion::remote::CallTraceHandler::HandleRequest(), ion::remote::ResourceHandler::HandleRequest(), ion::remote::SettingHandler::HandleRequest(), ion::remote::TracingHandler::HandleRequest(), ion::remote::NodeGraphHandler::HandleRequest(), and ion::remote::ShaderHandler::HandleRequest().

bool ion::base::ZipAssetManager::GetFileDataNoCache ( const std::string &  filename,
std::string *  out 
)
static

As above but the decompressed bytes are not internally cached.

Returns true if filename is found or false otherwise. If file data is already cached for filename then this method will clear that cached data.

Definition at line 164 of file zipassetmanager.cc.

References ion::base::IsInvalidReference().

std::shared_ptr< const std::string > ion::base::ZipAssetManager::GetFileDataPtr ( const std::string &  filename)
static

Returns the shared pointer to the data of the passed filename if the manager contains it.

If there is any error while decompressing the file or if the file has not been registered then returns an empty shared pointer. The returned "string" data in the pointer may change if the file changes.

Definition at line 147 of file zipassetmanager.cc.

References ion::base::IsInvalidReference().

std::vector< std::string > ion::base::ZipAssetManager::GetRegisteredFileNames ( )
static

Returns the list of registered filenames.

Definition at line 138 of file zipassetmanager.cc.

bool ion::base::ZipAssetManager::IsFileCached ( const std::string &  filename)
static

Returns whether the file is cached in the manager.

A file is cached once it has been requested with GetFileData. Returns false if the manager does not contain the file.

Definition at line 130 of file zipassetmanager.cc.

bool ion::base::ZipAssetManager::RegisterAssetData ( const void *  data,
size_t  data_size 
)
static

Registers a data pointer with a certain length with the manager.

The data pointer must be valid at least as long as it takes for all of its files to be requested through GetFileData(). Returns if the data is actual zipped data and was successfully registered. Note that if any of the files in the passed data have already been registered, the last version to be registered will be returned from GetFileData(). This replacement of existing data invalidates any use of previous return values of GetFileData() for the replaced file.

Definition at line 50 of file zipassetmanager.cc.

References DCHECK, DCHECK_EQ, DLOG, ion::base::ScopedAllocation< T >::Get(), ion::port::GetCanonicalFilePath(), ion::port::GetFileModificationTime(), ion::base::kShortTerm, ion::base::RemovePrefix(), ion::base::SplitString(), ion::base::StartsWith(), and ion::port::WARNING.

void ion::base::ZipAssetManager::Reset ( )
static

Resets the manager back to its initial, empty state.

This is used primarily for testing and should generally not be necessary elsewhere.

Definition at line 242 of file zipassetmanager.cc.

Referenced by ~ZipAssetManager().

bool ion::base::ZipAssetManager::SaveFileData ( const std::string &  filename)
static

Attempts to save the latest cached data of the passed filename back to the original source file on disk, using the name in the internal manifest.

Returns whether the file was successfully written. This may fail if the file does not exist, is not writable, or if filename does not appear in the manifest (e.g., if it was registered manually through RegisterAssetData()), or if the manager does not contain the file.

Definition at line 223 of file zipassetmanager.cc.

References ion::base::IsInvalidReference(), and ion::port::OpenFile().

bool ion::base::ZipAssetManager::SetFileData ( const std::string &  filename,
const std::string &  source 
)
static

Sets the data of the passed filename if the manager contains it.

Returns whether the source was succesfully updated (e.g., that the manager contains filename). Note that changing file data invalidates any existing returned reference from GetFileData for that file.

Definition at line 209 of file zipassetmanager.cc.

bool ion::base::ZipAssetManager::UpdateFileIfChanged ( const std::string &  filename,
std::chrono::system_clock::time_point *  timestamp 
)
static

If the source file of a zipped file is available on disk (based on the file's manifest), this function updates the cached unzipped data from the source file if it has changed since the data was registered and the source file is readable.

Reads the modification time of the passed filename into timestamp and returns true iff the manager contains the file and it is available; false otherwise.

Definition at line 252 of file zipassetmanager.cc.

References ion::port::GetFileModificationTime(), length, and ion::port::OpenFile().


The documentation for this class was generated from the following files: