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
This commit is contained in:
Michael Flaherty 2018-07-14 17:00:17 -07:00 committed by Kyle Sanderson
parent e4862dade8
commit 32d12ea4a6
4 changed files with 56 additions and 6 deletions

View File

@ -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<size_t>(-1))
{
pos = position;
}
if (pos >= elements.length())
{
return false;
}
elements.remove(pos);
--position;
return true;
}

View File

@ -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 {

View File

@ -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;

View File

@ -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 {