166 lines
4.5 KiB
C++
166 lines
4.5 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#pragma once
|
|
#include "dawnlibs.hpp"
|
|
|
|
extern "C" {
|
|
#include <archive.h>
|
|
#include <archive_entry.h>
|
|
}
|
|
|
|
#define ASSET_LOADER_BUFFER_SIZE 32768
|
|
|
|
/**
|
|
* Method invoked by the libarchive internals to read bytes from the archive
|
|
* file pointer.
|
|
*
|
|
* @param archive Archive requesting the read.
|
|
* @param data Data pointer passed to the archive.
|
|
* @param buffer Pointer to where the buffer pointer should be stored.
|
|
* @return Count of bytes read.
|
|
*/
|
|
ssize_t assetDataLoaderArchiveRead(
|
|
struct archive *archive,
|
|
void *data,
|
|
const void **buffer
|
|
);
|
|
|
|
/**
|
|
* Method invoked by the libarchive internals to seek the archive file pointer.
|
|
*
|
|
* @param archive Archive requesting the seek.
|
|
* @param data Data pointer passed to the archive.
|
|
* @param offset Offset to seek to.
|
|
* @param whence Whence to seek from.
|
|
* @return The new offset.
|
|
*/
|
|
int64_t assetDataLoaderArchiveSeek(
|
|
struct archive *archive,
|
|
void *data,
|
|
int64_t offset,
|
|
int32_t whence
|
|
);
|
|
|
|
/**
|
|
* Method invoked by the libarchive internals to open the archive file pointer.
|
|
*
|
|
* @param archive Archive requesting the open.
|
|
* @param data Data pointer passed to the archive.
|
|
* @return 0 if success, otherwise for failure.
|
|
*/
|
|
int32_t assetDataLoaderArchiveOpen(struct archive *a, void *data);
|
|
|
|
/**
|
|
* Method invoked by the libarchive internals to close the archive file pointer.
|
|
*
|
|
* @param archive Archive requesting the close.
|
|
* @param data Data pointer passed to the archive.
|
|
* @return 0 if success, otherwise for failure.
|
|
*/
|
|
int32_t assetDataLoaderArchiveClose(struct archive *a, void *data);
|
|
|
|
namespace Dawn {
|
|
class AssetDataLoader {
|
|
protected:
|
|
struct archive *assetArchive = nullptr;
|
|
struct archive_entry *assetArchiveEntry = nullptr;
|
|
size_t position;
|
|
std::string fileName;
|
|
|
|
public:
|
|
uint8_t buffer[ASSET_LOADER_BUFFER_SIZE];
|
|
FILE *assetArchiveFile = nullptr;
|
|
|
|
/**
|
|
* Unimplemented method intended to be implemented by the platform that
|
|
* will be used to request a File pointer to the asset.
|
|
*
|
|
* @return Pointer to the opened asset archive.
|
|
*/
|
|
FILE * openAssetArchiveFile();
|
|
|
|
/**
|
|
* Create a new asset loader. Asset Loaders can be used to load data from
|
|
* a file in a myriad of ways.
|
|
*
|
|
* @param fileName File name of the asset that is to be loaded.
|
|
*/
|
|
AssetDataLoader(std::string filename);
|
|
|
|
/**
|
|
* Platform-centric method to open a file buffer to an asset.
|
|
*/
|
|
void open();
|
|
|
|
/**
|
|
* Closes the previously ppened asset.
|
|
* @return 0 if successful, otherwise false.
|
|
*/
|
|
int32_t close();
|
|
|
|
/**
|
|
* Read bytes from buffer.
|
|
* @param buffer Pointer to a ubyte array to buffer data into.
|
|
* @param size Length of the data buffer (How many bytes to read).
|
|
* @return The count of bytes read.
|
|
*/
|
|
size_t read(uint8_t *buffer, size_t size);
|
|
|
|
/**
|
|
* Reads bytes from the buffer until a given delimiter is found. Returned
|
|
* position will be the index of the delimiter within the buffer.
|
|
*
|
|
* @param buffer Buffer to read into.
|
|
* @param maxSize Maximum size of the buffer.
|
|
* @param delimiter Delimiter to read until.
|
|
* @return The count of bytes read (including null terminator)
|
|
*/
|
|
size_t readUntil(
|
|
uint8_t *buffer,
|
|
const size_t maxSize,
|
|
const char_t delimiter
|
|
);
|
|
|
|
/**
|
|
* Get the size of the asset.
|
|
* @return The size of the asset in bytes.
|
|
*/
|
|
size_t getSize();
|
|
|
|
/**
|
|
* Skips the read head forward to a given position.
|
|
*
|
|
* @param n Count of bytes to progress the read head by.
|
|
* @return Count of bytes progressed.
|
|
*/
|
|
size_t skip(size_t n);
|
|
|
|
/**
|
|
* Rewind the read head to the beginning of the file.
|
|
*/
|
|
void rewind();
|
|
|
|
/**
|
|
* Sets the absolute position of the read head within the buffer of the
|
|
* file.
|
|
*
|
|
* @param absolutePosition Absolute position to set the read head to.
|
|
*/
|
|
size_t setPosition(const size_t absolutePosition);
|
|
|
|
/**
|
|
* Returns the current position of the read head.
|
|
*
|
|
* @return The current read head position.
|
|
*/
|
|
size_t getPosition();
|
|
|
|
/**
|
|
* Cleanup the asset loader.
|
|
*/
|
|
virtual ~AssetDataLoader();
|
|
};
|
|
} |