From a704ee046a089061b3215dec6a35d43c481f24bf Mon Sep 17 00:00:00 2001 From: Syping Date: Sun, 8 Jan 2023 17:45:54 +0100 Subject: [PATCH] add setData() and ragephoto_setphotodata() functions RagePhoto: add setData() and ragephoto_setphotodata() functions RagePhotoA: wrap setData() function to ragephoto_setphotodata() RagePhotoC: update header for ragephoto_setphotodata() --- src/RagePhoto.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++---- src/RagePhoto.h | 1 + src/RagePhotoA.h | 4 +++ src/RagePhotoC.h | 24 +++++++++---- 4 files changed, 106 insertions(+), 14 deletions(-) diff --git a/src/RagePhoto.cpp b/src/RagePhoto.cpp index 092c11f..ea94721 100644 --- a/src/RagePhoto.cpp +++ b/src/RagePhoto.cpp @@ -937,6 +937,77 @@ inline void RagePhoto::setBufferOffsets(RagePhotoData *ragePhotoData) ragePhotoData->endOfFile = ragePhotoData->descOffset + ragePhotoData->descBuffer + 12; } +bool RagePhoto::setData(RagePhotoData *ragePhotoData, bool takeOwnership) +{ + std::free(m_data->jpeg); + std::free(m_data->description); + std::free(m_data->json); + std::free(m_data->header); + std::free(m_data->title); + delete m_data; + + if (takeOwnership) { + m_data = ragePhotoData; + } + else { + m_data = new RagePhotoData { 0 }; + + m_data->photoFormat = ragePhotoData->photoFormat; + + if (ragePhotoData->header) { + const size_t headerSize = strlen(ragePhotoData->header) + 1; + m_data->header = static_cast(std::malloc(headerSize)); + if (!m_data->header) + return false; + std::memcpy(m_data->header, ragePhotoData->header, headerSize); + m_data->headerSum = ragePhotoData->headerSum; + } + + if (ragePhotoData->jpeg) { + m_data->jpeg = static_cast(std::malloc(ragePhotoData->jpegSize)); + if (!m_data->jpeg) + return false; + std::memcpy(m_data->jpeg, ragePhotoData->jpeg, ragePhotoData->jpegSize); + m_data->jpegSize = ragePhotoData->jpegSize; + m_data->photoBuffer = ragePhotoData->photoBuffer; + } + + if (ragePhotoData->json) { + const size_t jsonSize = strlen(ragePhotoData->json) + 1; + m_data->json = static_cast(std::malloc(jsonSize)); + if (!m_data->json) + return false; + std::memcpy(m_data->json, ragePhotoData->json, jsonSize); + m_data->jsonBuffer = ragePhotoData->jsonBuffer; + } + + if (ragePhotoData->title) { + const size_t titleSize = strlen(ragePhotoData->title) + 1; + m_data->title = static_cast(std::malloc(titleSize)); + if (!m_data->title) + return false; + std::memcpy(m_data->title, ragePhotoData->title, titleSize); + m_data->titlBuffer = ragePhotoData->titlBuffer; + } + + if (ragePhotoData->description) { + const size_t descriptionSize = strlen(ragePhotoData->description) + 1; + m_data->description = static_cast(std::malloc(descriptionSize)); + if (!m_data->description) + return false; + std::memcpy(m_data->description, ragePhotoData->description, descriptionSize); + m_data->descBuffer = ragePhotoData->descBuffer; + } + + m_data->unnamedSum1 = ragePhotoData->unnamedSum1; + m_data->unnamedSum2 = ragePhotoData->unnamedSum2; + + setBufferOffsets(); + } + + return true; +} + void RagePhoto::setDescription(const char *description, uint32_t bufferSize) { if (!writeDataChar(description, &m_data->description)) { @@ -1054,13 +1125,13 @@ void ragephoto_clear(ragephoto_t instance) ragePhoto->clear(); } -int ragephoto_load(ragephoto_t instance, const char *data, size_t size) +ragephoto_bool_t ragephoto_load(ragephoto_t instance, const char *data, size_t size) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->load(data, size); } -int ragephoto_loadfile(ragephoto_t instance, const char *filename) +ragephoto_bool_t ragephoto_loadfile(ragephoto_t instance, const char *filename) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->loadFile(filename); @@ -1152,25 +1223,25 @@ size_t ragephoto_getsavesizef(ragephoto_t instance, uint32_t photoFormat) return ragePhoto->saveSize(photoFormat); } -int ragephoto_save(ragephoto_t instance, char *data) +ragephoto_bool_t ragephoto_save(ragephoto_t instance, char *data) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->save(data); } -int ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat) +ragephoto_bool_t ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->save(data, photoFormat); } -int ragephoto_savefile(ragephoto_t instance, const char *filename) +ragephoto_bool_t ragephoto_savefile(ragephoto_t instance, const char *filename) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->saveFile(filename); } -int ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat) +ragephoto_bool_t ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->saveFile(filename, photoFormat); @@ -1188,6 +1259,12 @@ void ragephoto_setbufferoffsets(ragephoto_t instance) ragePhoto->setBufferOffsets(); } +void ragephoto_setphotodata(ragephoto_t instance, RagePhotoData *ragePhotoData, ragephoto_bool_t takeOwnership) +{ + RagePhoto *ragePhoto = static_cast(instance); + ragePhoto->setData(ragePhotoData, takeOwnership); +} + void ragephoto_setphotodesc(ragephoto_t instance, const char *description, uint32_t bufferSize) { RagePhoto *ragePhoto = static_cast(instance); @@ -1200,7 +1277,7 @@ void ragephoto_setphotoformat(ragephoto_t instance, uint32_t photoFormat) ragePhoto->setFormat(photoFormat); } -int ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize) +ragephoto_bool_t ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->setPhoto(data, size, bufferSize); diff --git a/src/RagePhoto.h b/src/RagePhoto.h index 31d4629..254ef2f 100644 --- a/src/RagePhoto.h +++ b/src/RagePhoto.h @@ -147,6 +147,7 @@ public: static void setBufferDefault(RagePhotoData *ragePhotoData); /**< Sets all cross-format Buffer to default size. */ void setBufferOffsets(); /**< Moves all Buffer offsets to correct position. */ static void setBufferOffsets(RagePhotoData *ragePhotoData); /**< Moves all Buffer offsets to correct position. */ + bool setData(RagePhotoData *ragePhotoData, bool takeOwnership = true); /**< Sets the internal RagePhotoData object. */ void setDescription(const char *description, uint32_t bufferSize = 0); /**< Sets the Photo description. */ void setFormat(uint32_t photoFormat); /**< Sets the Photo Format (GTA V or RDR 2). */ void setJson(const char *json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */ diff --git a/src/RagePhotoA.h b/src/RagePhotoA.h index 5f897d7..b77fcd1 100644 --- a/src/RagePhotoA.h +++ b/src/RagePhotoA.h @@ -226,6 +226,10 @@ public: void setBufferOffsets() { ragephoto_setbufferoffsets(instance); } + /** Sets the internal RagePhotoData object. */ + bool setData(RagePhotoData *ragePhotoData, bool takeOwnership = true) { + ragephoto_setphotodata(instance, ragePhotoData, takeOwnership); + } /** Sets the Photo description. */ void setDescription(const char *description, uint32_t bufferSize = 0) { ragephoto_setphotodesc(instance, description, bufferSize); diff --git a/src/RagePhotoC.h b/src/RagePhotoC.h index 799eb54..55a832c 100644 --- a/src/RagePhotoC.h +++ b/src/RagePhotoC.h @@ -35,6 +35,9 @@ extern "C" { /** RagePhoto C++ class typedef for C API. */ typedef void* ragephoto_t; +/** RagePhoto bool typedef for C API. */ +typedef int32_t ragephoto_bool_t; + /** Opens a \p ragephoto_t instance. */ LIBRAGEPHOTO_C_EXPORT ragephoto_t ragephoto_open(); @@ -48,13 +51,13 @@ LIBRAGEPHOTO_C_EXPORT void ragephoto_clear(ragephoto_t instance); * \param data Photo data * \param size Photo data size */ -LIBRAGEPHOTO_C_EXPORT int ragephoto_load(ragephoto_t instance, const char *data, size_t size); +LIBRAGEPHOTO_C_EXPORT ragephoto_bool_t ragephoto_load(ragephoto_t instance, const char *data, size_t size); /** Loads a Photo from a file. * \param instance \p ragephoto_t instance * \param filename File to load */ -LIBRAGEPHOTO_C_EXPORT int ragephoto_loadfile(ragephoto_t instance, const char *filename); +LIBRAGEPHOTO_C_EXPORT ragephoto_bool_t ragephoto_loadfile(ragephoto_t instance, const char *filename); /** Returns the last error occurred. * \param instance \p ragephoto_t instance @@ -128,27 +131,27 @@ LIBRAGEPHOTO_C_EXPORT size_t ragephoto_getsavesizef(ragephoto_t instance, uint32 * \param instance \p ragephoto_t instance * \param data Photo data */ -LIBRAGEPHOTO_C_EXPORT int ragephoto_save(ragephoto_t instance, char *data); +LIBRAGEPHOTO_C_EXPORT ragephoto_bool_t ragephoto_save(ragephoto_t instance, char *data); /** Saves a Photo to a char*. * \param instance \p ragephoto_t instance * \param data Photo data * \param photoFormat Photo Format (GTA V or RDR 2) */ -LIBRAGEPHOTO_C_EXPORT int ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat); +LIBRAGEPHOTO_C_EXPORT ragephoto_bool_t ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat); /** Saves a Photo to a file. * \param instance \p ragephoto_t instance * \param filename File to save */ -LIBRAGEPHOTO_C_EXPORT int ragephoto_savefile(ragephoto_t instance, const char *filename); +LIBRAGEPHOTO_C_EXPORT ragephoto_bool_t ragephoto_savefile(ragephoto_t instance, const char *filename); /** Saves a Photo to a file. * \param instance \p ragephoto_t instance * \param filename File to save * \param photoFormat Photo Format (GTA V or RDR 2) */ -LIBRAGEPHOTO_C_EXPORT int ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat); +LIBRAGEPHOTO_C_EXPORT ragephoto_bool_t ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat); /** Sets all cross-format Buffer to default size. * \param instance \p ragephoto_t instance @@ -160,6 +163,13 @@ LIBRAGEPHOTO_C_EXPORT void ragephoto_setbufferdefault(ragephoto_t instance); */ LIBRAGEPHOTO_C_EXPORT void ragephoto_setbufferoffsets(ragephoto_t instance); +/** Sets the internal RagePhotoData object. +* \param instance \p ragephoto_t instance +* \param ragePhotoData RagePhotoData object +* \param takeOwnership True takes ownership, false copies source object +*/ +LIBRAGEPHOTO_C_EXPORT void ragephoto_setphotodata(ragephoto_t instance, RagePhotoData *ragePhotoData, ragephoto_bool_t takeOwnership); + /** Sets the Photo description. * \param instance \p ragephoto_t instance * \param description Description @@ -183,7 +193,7 @@ LIBRAGEPHOTO_C_EXPORT void ragephoto_setphotoformat(ragephoto_t instance, uint32 * * Default bufferSize: ragephoto_defpbuf_gta5() or ragephoto_defpbuf_rdr2() */ -LIBRAGEPHOTO_C_EXPORT int ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize); +LIBRAGEPHOTO_C_EXPORT ragephoto_bool_t ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize); /** Sets the Photo JSON data. * \param instance \p ragephoto_t instance