119 lines
3.0 KiB
C
119 lines
3.0 KiB
C
/**
|
|
* Copyright (c) 2021 Dominic Msters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "asset.h"
|
|
|
|
size_t assetRawLoad(char *assetName, uint8_t *buffer) {
|
|
assetbuffer_t *asset;
|
|
size_t length, read;
|
|
|
|
// Open a buffer.
|
|
asset = assetBufferOpen(assetName);
|
|
if(asset == NULL) return 0;
|
|
|
|
// Read the count of bytes in the file
|
|
assetBufferEnd(asset);
|
|
length = assetBufferGetCurrentPosition(asset);// Get our current position (the end)
|
|
|
|
// Are we only reading the size?
|
|
if(buffer == NULL) {
|
|
assetBufferClose(asset);
|
|
return length;
|
|
}
|
|
|
|
// Reset to start
|
|
assetBufferStart(asset);
|
|
|
|
// Read and seal the string.
|
|
read = assetBufferRead(asset, buffer, length);
|
|
assetBufferClose(asset); // Close the buffer.
|
|
|
|
// Did we read successfully?
|
|
if(read < length) return 0;
|
|
return read;
|
|
}
|
|
|
|
char * assetStringLoad(char *assetName) {
|
|
size_t length;
|
|
char *string;
|
|
|
|
length = assetRawLoad(assetName, NULL);
|
|
if(length == 0) return NULL;
|
|
|
|
string = malloc(length + 1);// +1 for null terminator
|
|
if(string == NULL) return NULL;
|
|
|
|
length = assetRawLoad(assetName, string);
|
|
if(length == 0) {
|
|
free(string);
|
|
return NULL;
|
|
}
|
|
|
|
string[length] = '\0';// Null terminate
|
|
return string;
|
|
}
|
|
|
|
assetbuffer_t * assetBufferOpen(char *assetName) {
|
|
// Get the directory based on the raw input by creating a new string.
|
|
FILE *fptr;
|
|
size_t lenAsset = strlen(assetName);// Get the length of asset
|
|
size_t lenPrefix = strlen(ASSET_PREFIX);// Get the length of the prefix
|
|
|
|
// Create str to house both the prefix and asset, and null terminator
|
|
char *joined = malloc(lenAsset + lenPrefix + 1);
|
|
if(joined == NULL) return NULL;// Mem okay?
|
|
|
|
joined[0] = '\0';//Start at null
|
|
strcat(joined, ASSET_PREFIX);//Add prefix
|
|
strcat(joined, assetName);//Add body
|
|
|
|
printf("Opening up %s\n", joined);
|
|
|
|
// Open the file pointer now.
|
|
fptr = fopen(joined, "rb");
|
|
free(joined);// Free the string we just created
|
|
if(!fptr) return NULL;// File available?
|
|
return (assetbuffer_t *)fptr;
|
|
}
|
|
|
|
bool assetBufferClose(assetbuffer_t *buffer) {
|
|
return fclose((FILE *)buffer);
|
|
}
|
|
|
|
int32_t assetBufferRead(assetbuffer_t *buffer, char *data, size_t size) {
|
|
return (int32_t)fread(data, 1, size, (FILE *)buffer);
|
|
}
|
|
|
|
int32_t assetBufferEnd(assetbuffer_t *buffer) {
|
|
return feof((FILE *)buffer);
|
|
}
|
|
|
|
int32_t assetBufferStart(assetbuffer_t *buffer) {
|
|
return fseek((FILE *)buffer, 0, SEEK_SET);
|
|
}
|
|
|
|
int32_t assetBufferSkip(assetbuffer_t *buffer, size_t n) {
|
|
return fseek((FILE *)buffer, n, SEEK_CUR);
|
|
}
|
|
|
|
size_t assetBufferGetCurrentPosition(assetbuffer_t *buffer) {
|
|
return ftell((FILE *)buffer);
|
|
}
|
|
|
|
void assetScripterAppend(scripter_t *scripter, char *fileName) {
|
|
char *data;
|
|
|
|
data = assetStringLoad(fileName);
|
|
duk_push_global_object(scripter->context);
|
|
duk_push_lstring(scripter->context, data, strlen(data));
|
|
|
|
if(duk_peval(scripter->context) != 0) {
|
|
printf("Error running: %s\n", duk_safe_to_string(scripter->context, -1));
|
|
}
|
|
|
|
free(data);
|
|
} |