156 lines
4.3 KiB
C
156 lines
4.3 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "error/error.h"
|
|
#include <zip.h>
|
|
|
|
#define ASSET_FILE_NAME_MAX 48
|
|
|
|
typedef struct assetfile_s assetfile_t;
|
|
|
|
typedef errorret_t (*assetfileloader_t)(assetfile_t *file);
|
|
|
|
// Describes a file not yet loaded.
|
|
typedef struct assetfile_s {
|
|
char_t filename[ASSET_FILE_NAME_MAX];
|
|
void *params;
|
|
void *output;
|
|
|
|
zip_stat_t stat;
|
|
zip_int64_t size;
|
|
zip_int64_t position;
|
|
zip_int64_t lastRead;
|
|
zip_file_t *zipFile;
|
|
} assetfile_t;
|
|
|
|
/**
|
|
* Initializes the asset file structure in preparation for loading. This will
|
|
* stat the file but not open the handle.
|
|
*
|
|
* @param file The asset file structure to initialize.
|
|
* @param filename The name of the asset file to load.
|
|
* @param params Optional loader params.
|
|
* @param output Output pointer for the loader.
|
|
* @return Error indicating success or failure.
|
|
*/
|
|
errorret_t assetFileInit(
|
|
assetfile_t *file,
|
|
const char_t *filename,
|
|
void *params,
|
|
void *output
|
|
);
|
|
|
|
/**
|
|
* Opens the asset file for reading. After opening the loader is responsible
|
|
* for closing the file.
|
|
*
|
|
* @param file The asset file to open.
|
|
* @return An error code if the file could not be opened.
|
|
*/
|
|
errorret_t assetFileOpen(assetfile_t *file);
|
|
|
|
/**
|
|
* Rewind the file to the initial position.
|
|
*
|
|
* @param file The asset file to rewind.
|
|
*/
|
|
errorret_t assetFileRewind(assetfile_t *file);
|
|
|
|
/**
|
|
* Read bytes from the asset file. Assumes the file has already been opened
|
|
* prior to trying to read anything.
|
|
*
|
|
* @param file File to read from.
|
|
* @param buffer Buffer to read the file data into., or NULL to skip bytes.
|
|
* @param size Size of the buffer to read into.
|
|
*/
|
|
errorret_t assetFileRead(
|
|
assetfile_t *file,
|
|
void *buffer,
|
|
const size_t bufferSize
|
|
);
|
|
|
|
/**
|
|
* Closes the asset file and releases any resources associated with it.
|
|
*
|
|
* @param file The asset file to close.
|
|
* @return An error code if the file could not be closed properly.
|
|
*/
|
|
errorret_t assetFileClose(assetfile_t *file);
|
|
|
|
/**
|
|
* Disposes the asset file structure, closing any open handles and zeroing
|
|
* out the structure.
|
|
*
|
|
* @param file The asset file to dispose.
|
|
* @return An error code if the file could not be disposed properly.
|
|
*/
|
|
errorret_t assetFileDispose(assetfile_t *file);
|
|
|
|
/**
|
|
* Reads the entire contents of the asset file into a newly allocated buffer.
|
|
* The caller is responsible for freeing the buffer with memoryFree.
|
|
*
|
|
* @param file The asset file to read. Must be initialized but not open.
|
|
* @param outBuffer Receives a pointer to the allocated buffer.
|
|
* @param outSize Receives the number of bytes written to the buffer.
|
|
* @return An error code if the file could not be read.
|
|
*/
|
|
errorret_t assetFileReadEntire(
|
|
assetfile_t *file,
|
|
uint8_t **outBuffer,
|
|
size_t *outSize
|
|
);
|
|
|
|
typedef struct {
|
|
assetfile_t *file;
|
|
uint8_t *readBuffer;
|
|
size_t readBufferSize;
|
|
uint8_t *outBuffer;
|
|
size_t outBufferSize;
|
|
|
|
// A
|
|
size_t bufferStart;
|
|
size_t bufferEnd;
|
|
bool_t eof;//?
|
|
|
|
// Updated each reach:
|
|
size_t lineLength;
|
|
} assetfilelinereader_t;
|
|
|
|
/**
|
|
* Initializes a line reader for the given asset file. The line reader will read
|
|
* lines from the file into the provided line buffer, using the provided buffer
|
|
* for reading chunks of the file.
|
|
*
|
|
* @param file The asset file to read from. Must already be opened.
|
|
* @param readBuffer Buffer to use for reading chunks of the file.
|
|
* @param readBufferSize Size of the read buffer.
|
|
* @param outBuffer Buffer to read lines into. Lines will be null-terminated.
|
|
* @param outBufferSize Size of the output buffer.
|
|
* @return An initialized line reader structure.
|
|
*/
|
|
void assetFileLineReaderInit(
|
|
assetfilelinereader_t *reader,
|
|
assetfile_t *file,
|
|
uint8_t *readBuffer,
|
|
const size_t readBufferSize,
|
|
uint8_t *outBuffer,
|
|
const size_t outBufferSize
|
|
);
|
|
|
|
/**
|
|
* Reads the next line from the asset file into the line buffer. The line
|
|
* buffer is null-terminated and does not include the newline character.
|
|
*
|
|
* @param reader The line reader to read from.
|
|
* @return An error code if a failure occurs, or errorOk() if a line was read
|
|
* successfully. If the end of the file is reached, errorEndOfFile() is
|
|
* returned.
|
|
*/
|
|
errorret_t assetFileLineReaderNext(assetfilelinereader_t *reader); |