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()
This commit is contained in:
Syping 2023-01-08 17:45:54 +01:00
parent 24d6f58b74
commit a704ee046a
4 changed files with 106 additions and 14 deletions

View file

@ -937,6 +937,77 @@ inline void RagePhoto::setBufferOffsets(RagePhotoData *ragePhotoData)
ragePhotoData->endOfFile = ragePhotoData->descOffset + ragePhotoData->descBuffer + 12; 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<char*>(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<char*>(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<char*>(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<char*>(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<char*>(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) void RagePhoto::setDescription(const char *description, uint32_t bufferSize)
{ {
if (!writeDataChar(description, &m_data->description)) { if (!writeDataChar(description, &m_data->description)) {
@ -1054,13 +1125,13 @@ void ragephoto_clear(ragephoto_t instance)
ragePhoto->clear(); 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<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->load(data, size); 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<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->loadFile(filename); return ragePhoto->loadFile(filename);
@ -1152,25 +1223,25 @@ size_t ragephoto_getsavesizef(ragephoto_t instance, uint32_t photoFormat)
return ragePhoto->saveSize(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<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->save(data); 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<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->save(data, photoFormat); 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<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->saveFile(filename); 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<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->saveFile(filename, photoFormat); return ragePhoto->saveFile(filename, photoFormat);
@ -1188,6 +1259,12 @@ void ragephoto_setbufferoffsets(ragephoto_t instance)
ragePhoto->setBufferOffsets(); ragePhoto->setBufferOffsets();
} }
void ragephoto_setphotodata(ragephoto_t instance, RagePhotoData *ragePhotoData, ragephoto_bool_t takeOwnership)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
ragePhoto->setData(ragePhotoData, takeOwnership);
}
void ragephoto_setphotodesc(ragephoto_t instance, const char *description, uint32_t bufferSize) void ragephoto_setphotodesc(ragephoto_t instance, const char *description, uint32_t bufferSize)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
@ -1200,7 +1277,7 @@ void ragephoto_setphotoformat(ragephoto_t instance, uint32_t photoFormat)
ragePhoto->setFormat(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<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->setPhoto(data, size, bufferSize); return ragePhoto->setPhoto(data, size, bufferSize);

View file

@ -147,6 +147,7 @@ public:
static void setBufferDefault(RagePhotoData *ragePhotoData); /**< Sets all cross-format Buffer to default size. */ static void setBufferDefault(RagePhotoData *ragePhotoData); /**< Sets all cross-format Buffer to default size. */
void setBufferOffsets(); /**< Moves all Buffer offsets to correct position. */ void setBufferOffsets(); /**< Moves all Buffer offsets to correct position. */
static void setBufferOffsets(RagePhotoData *ragePhotoData); /**< 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 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 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. */ void setJson(const char *json, uint32_t bufferSize = 0); /**< Sets the Photo JSON data. */

View file

@ -226,6 +226,10 @@ public:
void setBufferOffsets() { void setBufferOffsets() {
ragephoto_setbufferoffsets(instance); ragephoto_setbufferoffsets(instance);
} }
/** Sets the internal RagePhotoData object. */
bool setData(RagePhotoData *ragePhotoData, bool takeOwnership = true) {
ragephoto_setphotodata(instance, ragePhotoData, takeOwnership);
}
/** Sets the Photo description. */ /** Sets the Photo description. */
void setDescription(const char *description, uint32_t bufferSize = 0) { void setDescription(const char *description, uint32_t bufferSize = 0) {
ragephoto_setphotodesc(instance, description, bufferSize); ragephoto_setphotodesc(instance, description, bufferSize);

View file

@ -35,6 +35,9 @@ extern "C" {
/** RagePhoto C++ class typedef for C API. */ /** RagePhoto C++ class typedef for C API. */
typedef void* ragephoto_t; typedef void* ragephoto_t;
/** RagePhoto bool typedef for C API. */
typedef int32_t ragephoto_bool_t;
/** Opens a \p ragephoto_t instance. */ /** Opens a \p ragephoto_t instance. */
LIBRAGEPHOTO_C_EXPORT ragephoto_t ragephoto_open(); 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 data Photo data
* \param size Photo data size * \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. /** Loads a Photo from a file.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
* \param filename File to load * \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. /** Returns the last error occurred.
* \param instance \p ragephoto_t instance * \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 instance \p ragephoto_t instance
* \param data Photo data * \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*. /** Saves a Photo to a char*.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
* \param data Photo data * \param data Photo data
* \param photoFormat Photo Format (GTA V or RDR 2) * \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. /** Saves a Photo to a file.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
* \param filename File to save * \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. /** Saves a Photo to a file.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
* \param filename File to save * \param filename File to save
* \param photoFormat Photo Format (GTA V or RDR 2) * \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. /** Sets all cross-format Buffer to default size.
* \param instance \p ragephoto_t instance * \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); 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. /** Sets the Photo description.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
* \param description Description * \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() * 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. /** Sets the Photo JSON data.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance