From 32d12ea4a6ea8f2bbf55b6d8618b3e5b71cfb1ef Mon Sep 17 00:00:00 2001 From: Michael Flaherty Date: Sat, 14 Jul 2018 17:00:17 -0700 Subject: [PATCH] Preserve old DataPack behavior when overwriting data (#848) * Match old DataPack behavior when overwriting data * Make RemoveItem more flexible * Ditch implied RemoveItem behavior & asher fixes * KyleS nits - but fixed before he says them * Add back implicit behavior * Update CDataPack.cpp Committing to the spec. * Update CDataPack.h * fixup removing last item if explicitly requested * Fix logic for accepting pack pos, rather than index * Fixup IsReadable * headache is over now --- core/logic/CDataPack.cpp | 21 +++++++++++++++++++++ core/logic/CDataPack.h | 1 + core/logic/smn_datapacks.cpp | 24 ++++++++++++++++++++++++ plugins/include/datapack.inc | 16 ++++++++++------ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/core/logic/CDataPack.cpp b/core/logic/CDataPack.cpp index 7336e61e..ba971756 100644 --- a/core/logic/CDataPack.cpp +++ b/core/logic/CDataPack.cpp @@ -223,3 +223,24 @@ void *CDataPack::ReadMemory(size_t *size) const return ptr; } + +bool CDataPack::RemoveItem(size_t pos) +{ + if (!elements.length()) + { + return false; + } + + if (pos == static_cast(-1)) + { + pos = position; + } + if (pos >= elements.length()) + { + return false; + } + + elements.remove(pos); + --position; + return true; +} diff --git a/core/logic/CDataPack.h b/core/logic/CDataPack.h index 83999080..8377449a 100644 --- a/core/logic/CDataPack.h +++ b/core/logic/CDataPack.h @@ -76,6 +76,7 @@ public: void Initialize(); inline size_t GetCapacity() const { return this->elements.length(); }; inline CDataPackType GetCurrentType(void) const { return this->elements[this->position].type; }; + bool RemoveItem(size_t pos = -1); private: typedef union { diff --git a/core/logic/smn_datapacks.cpp b/core/logic/smn_datapacks.cpp index 2a2a12a1..7d38905a 100644 --- a/core/logic/smn_datapacks.cpp +++ b/core/logic/smn_datapacks.cpp @@ -99,6 +99,12 @@ static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } + bool insert = (params[0] >= 3) ? params[3] : false; + if (!insert) + { + pDataPack->RemoveItem(); + } + pDataPack->PackCell(params[2]); return 1; @@ -120,6 +126,12 @@ static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } + bool insert = (params[0] >= 3) ? params[3] : false; + if (!insert) + { + pDataPack->RemoveItem(); + } + pDataPack->PackFloat(sp_ctof(params[2])); return 1; @@ -141,6 +153,12 @@ static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } + bool insert = (params[0] >= 3) ? params[3] : false; + if (!insert) + { + pDataPack->RemoveItem(); + } + char *str; pContext->LocalToString(params[2], &str); pDataPack->PackString(str); @@ -164,6 +182,12 @@ static cell_t smn_WritePackFunction(IPluginContext *pContext, const cell_t *para return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } + bool insert = (params[0] >= 3) ? params[3] : false; + if (!insert) + { + pDataPack->RemoveItem(); + } + pDataPack->PackFunction(params[2]); return 1; diff --git a/plugins/include/datapack.inc b/plugins/include/datapack.inc index 12b7a3b1..4263372e 100644 --- a/plugins/include/datapack.inc +++ b/plugins/include/datapack.inc @@ -50,22 +50,26 @@ methodmap DataPack < Handle // Packs a normal cell into a data pack. // // @param cell Cell to add. - public native void WriteCell(any cell); + // @param insert Determines whether mid-pack writes will insert instead of overwrite. + public native void WriteCell(any cell, bool insert = false); // Packs a float into a data pack. // // @param val Float to add. - public native void WriteFloat(float val); + // @param insert Determines whether mid-pack writes will insert instead of overwrite. + public native void WriteFloat(float val, bool insert = false); // Packs a string into a data pack. // // @param str String to add. - public native void WriteString(const char[] str); + // @param insert Determines whether mid-pack writes will insert instead of overwrite. + public native void WriteString(const char[] str, bool insert = false); // Packs a function pointer into a data pack. // // @param fktptr Function pointer to add. - public native void WriteFunction(Function fktptr); + // @param insert Determines whether mid-pack writes will insert instead of overwrite. + public native void WriteFunction(Function fktptr, bool insert = false); // Reads a cell from a data pack. // @@ -96,8 +100,8 @@ methodmap DataPack < Handle // Returns whether or not a specified number of bytes from the data pack // position to the end can be read. // - // @param bytes Number of bytes to simulate reading. - public native bool IsReadable(int bytes); + // @param unused Unused variable. Exists for backwards compatability. + public native bool IsReadable(int unused = 0); // The read or write position in a data pack. property DataPackPos Position {