File: bytearray.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (57 lines) | stat: -rw-r--r-- 1,914 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "bytearray.h"

#include "network/multi.h"
#include "network/multimsgs.h"
#include "network/multiutil.h"

namespace scripting {
namespace api {

bytearray_h::bytearray_h() = default;
bytearray_h::bytearray_h(SCP_vector<uint8_t> data) : m_data(std::move(data)) {}
const SCP_vector<uint8_t>& bytearray_h::data() const
{
	return m_data;
}

void bytearray_h::serialize(lua_State* L, const scripting::ade_table_entry& /*tableEntry*/, const luacpp::LuaValue& value, ubyte* data, int& packet_size) {
	bytearray_h arr;
	value.getValue(l_Bytearray.Get(&arr));
	if (arr.m_data.size() > 0xffU) {
		LuaError(L, "bytearray too large to be network serialized. Maxium size is %d bytes!", 0xff);
		throw lua_net_exception("Bytearray too large to be network serialized.");
	}
	else if (MAX_PACKET_SIZE - packet_size < static_cast<int>(arr.m_data.size())) {
		LuaError(L, "Packet is too full to accomodate bytearray. Put less into one RPC call!");
		throw lua_net_exception("Packet is too full to accomodate bytearray.");
	}
	uint8_t size = static_cast<uint8_t>(arr.m_data.size());
	ADD_DATA(size);
	ADD_DATA_BLOCK(arr.m_data.data(), size);
}

void bytearray_h::deserialize(lua_State* /*L*/, const scripting::ade_table_entry& /*tableEntry*/, char* data_ptr, ubyte* data, int& offset) {
	uint8_t size;
	uint8_t read[0xff];
	SCP_vector<uint8_t> assign;
	GET_DATA(size);
	GET_DATA_BLOCK(read, size);
	assign.assign(read, read + size);
	new(data_ptr) bytearray_h(std::move(assign));
}

//**********HANDLE: bytearray
ADE_OBJ(l_Bytearray, bytearray_h, "bytearray", "An array of binary data");

ADE_FUNC(__len, l_Bytearray, nullptr, "The number of bytes in this array", "number", "The length in bytes")
{
	bytearray_h* array = nullptr;
	if (!ade_get_args(L, "o", l_Bytearray.GetPtr(&array))) {
		return ade_set_args(L, "i", 0);
	}

	return ade_set_args(L, "i", array->data().size());
}

} // namespace api
} // namespace scripting