Exposing some file access results to user layer #1420

This commit is contained in:
Ray 2020-11-22 00:10:16 +01:00
parent 36dc302c25
commit bb9d734f69
6 changed files with 70 additions and 39 deletions

View File

@ -2299,8 +2299,10 @@ unsigned char *DecompressData(unsigned char *compData, int compDataLength, int *
// Save integer value to storage file (to defined position) // Save integer value to storage file (to defined position)
// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer) // NOTE: Storage positions is directly related to file memory layout (4 bytes each integer)
void SaveStorageValue(unsigned int position, int value) bool SaveStorageValue(unsigned int position, int value)
{ {
bool success = false;
#if defined(SUPPORT_DATA_STORAGE) #if defined(SUPPORT_DATA_STORAGE)
char path[512] = { 0 }; char path[512] = { 0 };
#if defined(PLATFORM_ANDROID) #if defined(PLATFORM_ANDROID)
@ -2355,7 +2357,7 @@ void SaveStorageValue(unsigned int position, int value)
dataPtr[position] = value; dataPtr[position] = value;
} }
SaveFileData(path, newFileData, newDataSize); success = SaveFileData(path, newFileData, newDataSize);
RL_FREE(newFileData); RL_FREE(newFileData);
} }
else else
@ -2367,10 +2369,12 @@ void SaveStorageValue(unsigned int position, int value)
int *dataPtr = (int *)fileData; int *dataPtr = (int *)fileData;
dataPtr[position] = value; dataPtr[position] = value;
SaveFileData(path, fileData, dataSize); success = SaveFileData(path, fileData, dataSize);
RL_FREE(fileData); RL_FREE(fileData);
} }
#endif #endif
return success;
} }
// Load integer value from storage file (from defined position) // Load integer value from storage file (from defined position)

View File

@ -823,8 +823,10 @@ void UnloadMesh(Mesh mesh)
} }
// Export mesh data to file // Export mesh data to file
void ExportMesh(Mesh mesh, const char *fileName) bool ExportMesh(Mesh mesh, const char *fileName)
{ {
bool success = false;
if (IsFileExtension(fileName, ".obj")) if (IsFileExtension(fileName, ".obj"))
{ {
// Estimated data size, it should be enough... // Estimated data size, it should be enough...
@ -875,7 +877,7 @@ void ExportMesh(Mesh mesh, const char *fileName)
bytesCount += sprintf(txtData + bytesCount, "\n"); bytesCount += sprintf(txtData + bytesCount, "\n");
// NOTE: Text data length exported is determined by '\0' (NULL) character // NOTE: Text data length exported is determined by '\0' (NULL) character
SaveFileText(fileName, txtData); success = SaveFileText(fileName, txtData);
RL_FREE(txtData); RL_FREE(txtData);
} }
@ -883,6 +885,8 @@ void ExportMesh(Mesh mesh, const char *fileName)
{ {
// TODO: Support additional file formats to export mesh vertex data // TODO: Support additional file formats to export mesh vertex data
} }
return success;
} }
// Load materials from model file // Load materials from model file

View File

@ -386,8 +386,8 @@ static Wave LoadMP3(const unsigned char *fileData, unsigned int fileSize); //
#if defined(RAUDIO_STANDALONE) #if defined(RAUDIO_STANDALONE)
static bool IsFileExtension(const char *fileName, const char *ext); // Check file extension static bool IsFileExtension(const char *fileName, const char *ext); // Check file extension
static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read) static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
static void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write) static bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write)
static void SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated static bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -812,7 +812,7 @@ void UpdateSound(Sound sound, const void *data, int samplesCount)
} }
// Export wave data to file // Export wave data to file
void ExportWave(Wave wave, const char *fileName) bool ExportWave(Wave wave, const char *fileName)
{ {
bool success = false; bool success = false;
@ -824,17 +824,20 @@ void ExportWave(Wave wave, const char *fileName)
{ {
// Export raw sample data (without header) // Export raw sample data (without header)
// NOTE: It's up to the user to track wave parameters // NOTE: It's up to the user to track wave parameters
SaveFileData(fileName, wave.data, wave.sampleCount*wave.sampleSize/8); success = SaveFileData(fileName, wave.data, wave.sampleCount*wave.sampleSize/8);
success = true;
} }
if (success) TRACELOG(LOG_INFO, "FILEIO: [%s] Wave data exported successfully", fileName); if (success) TRACELOG(LOG_INFO, "FILEIO: [%s] Wave data exported successfully", fileName);
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export wave data", fileName); else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export wave data", fileName);
return success;
} }
// Export wave sample data to code (.h) // Export wave sample data to code (.h)
void ExportWaveAsCode(Wave wave, const char *fileName) bool ExportWaveAsCode(Wave wave, const char *fileName)
{ {
bool success = false;
#ifndef TEXT_BYTES_PER_LINE #ifndef TEXT_BYTES_PER_LINE
#define TEXT_BYTES_PER_LINE 20 #define TEXT_BYTES_PER_LINE 20
#endif #endif
@ -878,9 +881,11 @@ void ExportWaveAsCode(Wave wave, const char *fileName)
bytesCount += sprintf(txtData + bytesCount, "0x%x };\n", ((unsigned char *)wave.data)[waveDataSize - 1]); bytesCount += sprintf(txtData + bytesCount, "0x%x };\n", ((unsigned char *)wave.data)[waveDataSize - 1]);
// NOTE: Text data length exported is determined by '\0' (NULL) character // NOTE: Text data length exported is determined by '\0' (NULL) character
SaveFileText(fileName, txtData); success = SaveFileText(fileName, txtData);
RL_FREE(txtData); RL_FREE(txtData);
return success;
} }
// Play a sound // Play a sound
@ -1942,6 +1947,8 @@ static Wave LoadWAV(const unsigned char *fileData, unsigned int fileSize)
// NOTE: Using dr_wav library // NOTE: Using dr_wav library
static int SaveWAV(Wave wave, const char *fileName) static int SaveWAV(Wave wave, const char *fileName)
{ {
int success = false;
drwav wav = { 0 }; drwav wav = { 0 };
drwav_data_format format = { 0 }; drwav_data_format format = { 0 };
format.container = drwav_container_riff; format.container = drwav_container_riff;
@ -1952,14 +1959,15 @@ static int SaveWAV(Wave wave, const char *fileName)
unsigned char *fileData = NULL; unsigned char *fileData = NULL;
unsigned int fileDataSize = 0; unsigned int fileDataSize = 0;
drwav_init_memory_write(&wav, &fileData, &fileDataSize, &format, NULL); success = drwav_init_memory_write(&wav, &fileData, &fileDataSize, &format, NULL);
drwav_write_pcm_frames(&wav, wave.sampleCount/wave.channels, wave.data); if (success) success = drwav_write_pcm_frames(&wav, wave.sampleCount/wave.channels, wave.data);
drwav_uninit(&wav); drwav_result result = drwav_uninit(&wav);
SaveFileData(fileName, fileData, fileDataSize); if (result == DRWAV_SUCCESS) success = SaveFileData(fileName, fileData, fileDataSize);
drwav_free(fileData, NULL); drwav_free(fileData, NULL);
return true; return success;
} }
#endif #endif
@ -2110,7 +2118,7 @@ static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead
} }
// Save data to file from buffer // Save data to file from buffer
static void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) static bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
{ {
if (fileName != NULL) if (fileName != NULL)
{ {
@ -2132,7 +2140,7 @@ static void SaveFileData(const char *fileName, void *data, unsigned int bytesToW
} }
// Save text data to file (write), string must be '\0' terminated // Save text data to file (write), string must be '\0' terminated
static void SaveFileText(const char *fileName, char *text) static bool SaveFileText(const char *fileName, char *text)
{ {
if (fileName != NULL) if (fileName != NULL)
{ {

View File

@ -971,9 +971,9 @@ RLAPI int GetRandomValue(int min, int max); // Returns a r
// Files management functions // Files management functions
RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read) RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
RLAPI void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write) RLAPI bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write), returns true on success
RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string
RLAPI void SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated RLAPI bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success
RLAPI bool FileExists(const char *fileName); // Check if file exists RLAPI bool FileExists(const char *fileName); // Check if file exists
RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists
RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension (including point: .png, .wav) RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension (including point: .png, .wav)
@ -995,7 +995,7 @@ RLAPI unsigned char *CompressData(unsigned char *data, int dataLength, int *comp
RLAPI unsigned char *DecompressData(unsigned char *compData, int compDataLength, int *dataLength); // Decompress data (DEFLATE algorithm) RLAPI unsigned char *DecompressData(unsigned char *compData, int compDataLength, int *dataLength); // Decompress data (DEFLATE algorithm)
// Persistent storage management // Persistent storage management
RLAPI void SaveStorageValue(unsigned int position, int value); // Save integer value to storage file (to defined position) RLAPI bool SaveStorageValue(unsigned int position, int value); // Save integer value to storage file (to defined position)
RLAPI int LoadStorageValue(unsigned int position); // Load integer value from storage file (from defined position) RLAPI int LoadStorageValue(unsigned int position); // Load integer value from storage file (from defined position)
RLAPI void OpenURL(const char *url); // Open URL with default system browser (if available) RLAPI void OpenURL(const char *url); // Open URL with default system browser (if available)
@ -1128,8 +1128,8 @@ RLAPI Image LoadImageRaw(const char *fileName, int width, int height, int format
RLAPI Image LoadImageAnim(const char *fileName, int *frames); // Load image sequence from file (frames appended to image.data) RLAPI Image LoadImageAnim(const char *fileName, int *frames); // Load image sequence from file (frames appended to image.data)
RLAPI Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, int dataSize); // Load image from memory buffer, fileType refers to extension: i.e. "png" RLAPI Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, int dataSize); // Load image from memory buffer, fileType refers to extension: i.e. "png"
RLAPI void UnloadImage(Image image); // Unload image from CPU memory (RAM) RLAPI void UnloadImage(Image image); // Unload image from CPU memory (RAM)
RLAPI void ExportImage(Image image, const char *fileName); // Export image data to file RLAPI bool ExportImage(Image image, const char *fileName); // Export image data to file, returns true on success
RLAPI void ExportImageAsCode(Image image, const char *fileName); // Export image as code file defining an array of bytes RLAPI bool ExportImageAsCode(Image image, const char *fileName); // Export image as code file defining an array of bytes
// Image generation functions // Image generation functions
RLAPI Image GenImageColor(int width, int height, Color color); // Generate image: plain color RLAPI Image GenImageColor(int width, int height, Color color); // Generate image: plain color
@ -1322,8 +1322,8 @@ RLAPI void UnloadModel(Model model);
// Mesh loading/unloading functions // Mesh loading/unloading functions
RLAPI Mesh *LoadMeshes(const char *fileName, int *meshCount); // Load meshes from model file RLAPI Mesh *LoadMeshes(const char *fileName, int *meshCount); // Load meshes from model file
RLAPI void ExportMesh(Mesh mesh, const char *fileName); // Export mesh data to file
RLAPI void UnloadMesh(Mesh mesh); // Unload mesh from memory (RAM and/or VRAM) RLAPI void UnloadMesh(Mesh mesh); // Unload mesh from memory (RAM and/or VRAM)
RLAPI bool ExportMesh(Mesh mesh, const char *fileName); // Export mesh data to file
// Material loading/unloading functions // Material loading/unloading functions
RLAPI Material *LoadMaterials(const char *fileName, int *materialCount); // Load materials from model file RLAPI Material *LoadMaterials(const char *fileName, int *materialCount); // Load materials from model file
@ -1445,8 +1445,8 @@ RLAPI Sound LoadSoundFromWave(Wave wave); // Load so
RLAPI void UpdateSound(Sound sound, const void *data, int samplesCount);// Update sound buffer with new data RLAPI void UpdateSound(Sound sound, const void *data, int samplesCount);// Update sound buffer with new data
RLAPI void UnloadWave(Wave wave); // Unload wave data RLAPI void UnloadWave(Wave wave); // Unload wave data
RLAPI void UnloadSound(Sound sound); // Unload sound RLAPI void UnloadSound(Sound sound); // Unload sound
RLAPI void ExportWave(Wave wave, const char *fileName); // Export wave data to file RLAPI bool ExportWave(Wave wave, const char *fileName); // Export wave data to file
RLAPI void ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h) RLAPI bool ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h)
// Wave/Sound management functions // Wave/Sound management functions
RLAPI void PlaySound(Sound sound); // Play a sound RLAPI void PlaySound(Sound sound); // Play a sound

View File

@ -395,7 +395,7 @@ void UnloadImage(Image image)
// Export image data to file // Export image data to file
// NOTE: File format depends on fileName extension // NOTE: File format depends on fileName extension
void ExportImage(Image image, const char *fileName) bool ExportImage(Image image, const char *fileName)
{ {
int success = 0; int success = 0;
@ -436,8 +436,7 @@ void ExportImage(Image image, const char *fileName)
{ {
// Export raw pixel data (without header) // Export raw pixel data (without header)
// NOTE: It's up to the user to track image parameters // NOTE: It's up to the user to track image parameters
SaveFileData(fileName, image.data, GetPixelDataSize(image.width, image.height, image.format)); success = SaveFileData(fileName, image.data, GetPixelDataSize(image.width, image.height, image.format));
success = true;
} }
if (allocatedData) RL_FREE(imgData); if (allocatedData) RL_FREE(imgData);
@ -445,11 +444,15 @@ void ExportImage(Image image, const char *fileName)
if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Image exported successfully", fileName); if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Image exported successfully", fileName);
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export image", fileName); else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export image", fileName);
return success;
} }
// Export image as code file (.h) defining an array of bytes // Export image as code file (.h) defining an array of bytes
void ExportImageAsCode(Image image, const char *fileName) bool ExportImageAsCode(Image image, const char *fileName)
{ {
bool success = false;
#ifndef TEXT_BYTES_PER_LINE #ifndef TEXT_BYTES_PER_LINE
#define TEXT_BYTES_PER_LINE 20 #define TEXT_BYTES_PER_LINE 20
#endif #endif
@ -488,9 +491,11 @@ void ExportImageAsCode(Image image, const char *fileName)
bytesCount += sprintf(txtData + bytesCount, "0x%x };\n", ((unsigned char *)image.data)[dataSize - 1]); bytesCount += sprintf(txtData + bytesCount, "0x%x };\n", ((unsigned char *)image.data)[dataSize - 1]);
// NOTE: Text data length exported is determined by '\0' (NULL) character // NOTE: Text data length exported is determined by '\0' (NULL) character
SaveFileText(fileName, txtData); success = SaveFileText(fileName, txtData);
RL_FREE(txtData); RL_FREE(txtData);
return success;
} }
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@ -4230,12 +4235,12 @@ static int SaveKTX(Image image, const char *fileName)
} }
} }
SaveFileData(fileName, fileData, dataSize); int success = SaveFileData(fileName, fileData, dataSize);
RL_FREE(fileData); // Free file data buffer RL_FREE(fileData); // Free file data buffer
// If all data has been written correctly to file, success = 1 // If all data has been written correctly to file, success = 1
return true; return success;
} }
#endif #endif

View File

@ -204,8 +204,10 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
} }
// Save data to file from buffer // Save data to file from buffer
void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
{ {
bool success = false;
if (fileName != NULL) if (fileName != NULL)
{ {
FILE *file = fopen(fileName, "wb"); FILE *file = fopen(fileName, "wb");
@ -218,11 +220,14 @@ void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName); else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName); else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName);
fclose(file); int result = fclose(file);
if (result == 0) success = true;
} }
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName); else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
} }
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid"); else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
return success;
} }
// Load text data from file, returns a '\0' terminated string // Load text data from file, returns a '\0' terminated string
@ -270,8 +275,10 @@ char *LoadFileText(const char *fileName)
} }
// Save text data to file (write), string must be '\0' terminated // Save text data to file (write), string must be '\0' terminated
void SaveFileText(const char *fileName, char *text) bool SaveFileText(const char *fileName, char *text)
{ {
bool success = false;
if (fileName != NULL) if (fileName != NULL)
{ {
FILE *file = fopen(fileName, "wt"); FILE *file = fopen(fileName, "wt");
@ -283,11 +290,14 @@ void SaveFileText(const char *fileName, char *text)
if (count < 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName); if (count < 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName);
else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName); else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName);
fclose(file); int result = fclose(file);
if (result == 0) success = true;
} }
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName); else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
} }
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid"); else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
return success;
} }
#if defined(PLATFORM_ANDROID) #if defined(PLATFORM_ANDROID)