Saveload: Change SlSaveToVector to be templated, add SlSaveToTempBuffer
This commit is contained in:
@@ -659,7 +659,7 @@ static void Save_PLYP()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<byte> buffer = SlSaveToVector([](void *) {
|
std::vector<byte> buffer = SlSaveToVector([]() {
|
||||||
SlWriteUint32((uint32)_network_company_server_id.size());
|
SlWriteUint32((uint32)_network_company_server_id.size());
|
||||||
MemoryDumper::GetCurrent()->CopyBytes((const uint8 *)_network_company_server_id.data(), _network_company_server_id.size());
|
MemoryDumper::GetCurrent()->CopyBytes((const uint8 *)_network_company_server_id.data(), _network_company_server_id.size());
|
||||||
|
|
||||||
@@ -681,7 +681,7 @@ static void Save_PLYP()
|
|||||||
} else {
|
} else {
|
||||||
SlWriteByte(0);
|
SlWriteByte(0);
|
||||||
}
|
}
|
||||||
}, nullptr);
|
});
|
||||||
|
|
||||||
|
|
||||||
uint8 mac[16]; /* Message authentication code */
|
uint8 mac[16]; /* Message authentication code */
|
||||||
|
@@ -2052,23 +2052,25 @@ void SlAutolength(AutolengthProc *proc, void *arg)
|
|||||||
_sl.dumper->CopyBytes(result.first, result.second);
|
_sl.dumper->CopyBytes(result.first, result.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
uint8 SlSaveToTempBufferSetup()
|
||||||
* Run proc, saving result to a std::vector
|
|
||||||
* @param proc The callback procedure that is called
|
|
||||||
* @param arg The variable that will be used for the callback procedure
|
|
||||||
*/
|
|
||||||
std::vector<byte> SlSaveToVector(AutolengthProc *proc, void *arg)
|
|
||||||
{
|
{
|
||||||
assert(_sl.action == SLA_SAVE);
|
assert(_sl.action == SLA_SAVE);
|
||||||
NeedLength orig_need_length = _sl.need_length;
|
NeedLength orig_need_length = _sl.need_length;
|
||||||
|
|
||||||
_sl.need_length = NL_NONE;
|
_sl.need_length = NL_NONE;
|
||||||
_sl.dumper->StartAutoLength();
|
_sl.dumper->StartAutoLength();
|
||||||
proc(arg);
|
|
||||||
|
return (uint8) orig_need_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<byte *, size_t> SlSaveToTempBufferRestore(uint8 state)
|
||||||
|
{
|
||||||
|
NeedLength orig_need_length = (NeedLength)state;
|
||||||
|
|
||||||
auto result = _sl.dumper->StopAutoLength();
|
auto result = _sl.dumper->StopAutoLength();
|
||||||
/* Setup length */
|
/* Setup length */
|
||||||
_sl.need_length = orig_need_length;
|
_sl.need_length = orig_need_length;
|
||||||
return std::vector<uint8>(result.first, result.first + result.second);
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
SlLoadFromBufferState SlLoadFromBufferSetup(const byte *buffer, size_t length)
|
SlLoadFromBufferState SlLoadFromBufferSetup(const byte *buffer, size_t length)
|
||||||
|
@@ -712,12 +712,41 @@ void SlSetArrayIndex(uint index);
|
|||||||
int SlIterateArray();
|
int SlIterateArray();
|
||||||
|
|
||||||
void SlAutolength(AutolengthProc *proc, void *arg);
|
void SlAutolength(AutolengthProc *proc, void *arg);
|
||||||
std::vector<uint8> SlSaveToVector(AutolengthProc *proc, void *arg);
|
|
||||||
size_t SlGetFieldLength();
|
size_t SlGetFieldLength();
|
||||||
void SlSetLength(size_t length);
|
void SlSetLength(size_t length);
|
||||||
size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld);
|
size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld);
|
||||||
size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt);
|
size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run proc, saving result in the autolength temp buffer
|
||||||
|
* @param proc The callback procedure that is called
|
||||||
|
* @return Span of the saved data, in the autolength temp buffer
|
||||||
|
*/
|
||||||
|
template <typename F>
|
||||||
|
span<byte> SlSaveToTempBuffer(F proc)
|
||||||
|
{
|
||||||
|
extern uint8 SlSaveToTempBufferSetup();
|
||||||
|
extern std::pair<byte *, size_t> SlSaveToTempBufferRestore(uint8 state);
|
||||||
|
|
||||||
|
uint8 state = SlSaveToTempBufferSetup();
|
||||||
|
proc();
|
||||||
|
auto result = SlSaveToTempBufferRestore(state);
|
||||||
|
return span<byte>(result.first, result.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run proc, saving result to a std::vector
|
||||||
|
* This is implemented in terms of SlSaveToTempBuffer
|
||||||
|
* @param proc The callback procedure that is called
|
||||||
|
* @return a vector containing the saved data
|
||||||
|
*/
|
||||||
|
template <typename F>
|
||||||
|
std::vector<uint8> SlSaveToVector(F proc)
|
||||||
|
{
|
||||||
|
span<byte> result = SlSaveToTempBuffer(proc);
|
||||||
|
return std::vector<uint8>(result.begin(), result.end());
|
||||||
|
}
|
||||||
|
|
||||||
struct SlLoadFromBufferState {
|
struct SlLoadFromBufferState {
|
||||||
size_t old_obj_len;
|
size_t old_obj_len;
|
||||||
byte *old_bufp;
|
byte *old_bufp;
|
||||||
|
Reference in New Issue
Block a user