105 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // Copyright (c) 2022 Dominic Masters
 | |
| // 
 | |
| // This software is released under the MIT License.
 | |
| // https://opensource.org/licenses/MIT
 | |
| 
 | |
| #include "AssetLoader.hpp"
 | |
| 
 | |
| using namespace Dawn;
 | |
| 
 | |
| AssetLoader::AssetLoader(std::string fileName) {
 | |
|   assertTrue(fileName.size() > 0, "AssetLoader::AssetLoader: fileName must be greater than 0");
 | |
| 
 | |
|   this->fileName = fileName;
 | |
|   this->handle = nullptr;
 | |
| }
 | |
| 
 | |
| void AssetLoader::open() {
 | |
|   assertNull(this->handle, "AssetLoader::open: File is already open");
 | |
|   std::string pathFull = DAWN_ASSET_BUILD_PREFIX + this->fileName;
 | |
|   this->handle = fopen(pathFull.c_str(), "rb");
 | |
|   assertNotNull(this->handle, "AssetLoader::open: Failed to open file");
 | |
| }
 | |
| 
 | |
| int32_t AssetLoader::close() {
 | |
|   assertNotNull(this->handle, "AssetLoader::close: File is not open");
 | |
|   int32_t ret = fclose(this->handle);
 | |
|   this->handle = nullptr;
 | |
|   return ret;
 | |
| }
 | |
| 
 | |
| size_t AssetLoader::read(uint8_t *buffer, size_t size) {
 | |
|   assertNotNull(buffer, "AssetLoader::read: buffer must not be null");
 | |
|   assertTrue(size > 0, "AssetLoader::read: size must be greater than 0");
 | |
|   assertNotNull(this->handle, "AssetLoader::read: File is not open");
 | |
|   return fread(buffer, 1, size, this->handle);
 | |
| }
 | |
| 
 | |
| int32_t AssetLoader::end() {
 | |
|   assertNotNull(this->handle, "AssetLoader::end: File is not open");
 | |
|   return fseek(this->handle, 0, SEEK_END);
 | |
| }
 | |
| 
 | |
| size_t AssetLoader::skip(size_t n) {
 | |
|   assertTrue(n > 0, "AssetLoader::skip: n must be greater than 0");
 | |
|   assertNotNull(this->handle, "AssetLoader::skip: File is not open");
 | |
|   return fseek(this->handle, n, SEEK_CUR);
 | |
| }
 | |
| 
 | |
| int32_t AssetLoader::rewind() {
 | |
|   assertNotNull(this->handle, "AssetLoader::rewind: File is not open");
 | |
|   return fseek(this->handle, 0, SEEK_SET);
 | |
| }
 | |
| 
 | |
| size_t AssetLoader::getPosition() {
 | |
|   assertNotNull(this->handle, "AssetLoader::getPosition: File is not open");
 | |
|   return ftell(this->handle);
 | |
| }
 | |
| 
 | |
| size_t AssetLoader::loadRaw(uint8_t **buffer) {
 | |
|   size_t length, read;
 | |
| 
 | |
|   assertNotNull(buffer, "AssetLoader::loadRaw: buffer must not be null");
 | |
| 
 | |
|   // Open a buffer.
 | |
|   this->open();
 | |
| 
 | |
|   // Read the count of bytes in the file
 | |
|   this->end();
 | |
|   length = this->getPosition();
 | |
| 
 | |
|   // Are we only reading the size?
 | |
|   if(buffer == nullptr) {
 | |
|     this->close();
 | |
|     return length;
 | |
|   }
 | |
| 
 | |
|   // Reset to start
 | |
|   this->rewind();
 | |
| 
 | |
|   // Read the string then close the file handle.
 | |
|   *buffer = static_cast<uint8_t *>(memoryAllocate(sizeof(uint8_t) * length));
 | |
|   read = this->read(*buffer, length);
 | |
|   this->close();
 | |
| 
 | |
|   // Did we read successfully?
 | |
|   if(read < length) {
 | |
|     throw "Failed to read all bytes of " + this->fileName;
 | |
|   }
 | |
| 
 | |
|   // Read successfully, return the read bytes.
 | |
|   return read;
 | |
| }
 | |
| 
 | |
| size_t AssetLoader::setPosition(size_t position) {
 | |
|   assertTrue(position >= 0, "AssetLoader::setPosition: position must be greater than or equal to 0");
 | |
|   this->rewind();
 | |
|   return this->skip(position);
 | |
| }
 | |
| 
 | |
| AssetLoader::~AssetLoader() {
 | |
|   if(this->handle != nullptr) {
 | |
|     this->close();
 | |
|     this->handle = nullptr;
 | |
|   }
 | |
| } |