2021-11-26 15:10:53 +01:00
|
|
|
local LuaNetworkOpcode = {
|
|
|
|
|
["WRITE_SIZE"] = 0,
|
|
|
|
|
["READ_SIZE"] = 1,
|
|
|
|
|
["WRITE_UINT8"] = 2,
|
|
|
|
|
["WRITE_INT8"] = 3,
|
|
|
|
|
["WRITE_UINT16"] = 4,
|
|
|
|
|
["WRITE_INT16"] = 5,
|
|
|
|
|
["WRITE_UINT32"] = 6,
|
|
|
|
|
["WRITE_INT32"] = 7,
|
2022-06-21 15:25:56 -04:00
|
|
|
["WRITE_UINT64"] = 8,
|
|
|
|
|
["WRITE_INT64"] = 9,
|
|
|
|
|
["WRITE_FLOAT"] = 10,
|
|
|
|
|
["WRITE_DOUBLE"] = 11,
|
|
|
|
|
["WRITE_STRING"] = 12,
|
|
|
|
|
|
|
|
|
|
["READ_UINT8"] = 13,
|
|
|
|
|
["READ_INT8"] = 14,
|
|
|
|
|
["READ_UINT16"] = 15,
|
|
|
|
|
["READ_INT16"] = 16,
|
|
|
|
|
["READ_UINT32"] = 17,
|
|
|
|
|
["READ_INT32"] = 18,
|
|
|
|
|
["READ_UINT64"] = 19,
|
|
|
|
|
["READ_INT64"] = 20,
|
|
|
|
|
["READ_FLOAT"] = 21,
|
|
|
|
|
["READ_DOUBLE"] = 22,
|
|
|
|
|
["READ_STRING"] = 23,
|
|
|
|
|
["MAKE_CUSTOM_PACKET"] = 24,
|
|
|
|
|
["SEND_CUSTOM_PACKET"] = 25,
|
|
|
|
|
["RESET_CUSTOM_PACKET"] = 26,
|
2021-11-26 15:10:53 +01:00
|
|
|
};
|
|
|
|
|
|
2021-12-01 16:57:15 +01:00
|
|
|
function CreateCustomPacket(opcode,size)
|
2021-11-26 15:10:53 +01:00
|
|
|
local writer = { id = _CLIENT_NETWORK(LuaNetworkOpcode.MAKE_CUSTOM_PACKET,opcode,size) }
|
2021-10-17 11:51:38 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function writer:WriteUInt8(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_UINT8,self.id,value); return self end
|
|
|
|
|
function writer:WriteInt8(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_INT8,self.id,value); return self end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function writer:WriteUInt16(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_UINT16,self.id,value); return self end
|
|
|
|
|
function writer:WriteInt16(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_INT16,self.id,value); return self end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function writer:WriteUInt32(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_UINT32,self.id,value); return self end
|
|
|
|
|
function writer:WriteInt32(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_INT32,self.id,value); return self end
|
2022-06-21 15:25:56 -04:00
|
|
|
|
|
|
|
|
function writer:WriteUInt64(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_UINT64,self.id,value); return self end
|
|
|
|
|
function writer:WriteInt64(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_INT64,self.id,value); return self end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function writer:WriteFloat(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_FLOAT,self.id,value); return self end
|
|
|
|
|
function writer:WriteDouble(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_DOUBLE,self.id,value); return self end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function writer:WriteString(value) _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_STRING,self.id,value); return self end
|
2021-10-23 12:41:53 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function writer:Size() return _CLIENT_NETWORK(LuaNetworkOpcode.WRITE_SIZE,self.id) end
|
2021-10-20 10:11:40 +02:00
|
|
|
|
2021-10-23 12:09:38 +02:00
|
|
|
function writer:Send()
|
2021-11-26 15:10:53 +01:00
|
|
|
_CLIENT_NETWORK(LuaNetworkOpcode.SEND_CUSTOM_PACKET,self.id)
|
2021-10-23 12:09:38 +02:00
|
|
|
return self
|
|
|
|
|
end
|
2021-10-17 11:51:38 +02:00
|
|
|
|
2021-10-23 12:09:38 +02:00
|
|
|
return writer
|
2021-10-17 11:51:38 +02:00
|
|
|
end
|
|
|
|
|
|
2021-10-23 12:05:48 +02:00
|
|
|
function __ReadCustomPacket()
|
2021-10-23 12:09:38 +02:00
|
|
|
local reader = {}
|
2021-11-26 15:10:53 +01:00
|
|
|
function reader:ReadUInt8() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_UINT8) end
|
|
|
|
|
function reader:ReadInt8() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_INT8) end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function reader:ReadUInt16() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_UINT16) end
|
|
|
|
|
function reader:ReadInt16() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_INT16) end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function reader:ReadUInt32() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_UINT32) end
|
|
|
|
|
function reader:ReadInt32() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_INT32) end
|
2022-06-21 15:25:56 -04:00
|
|
|
|
|
|
|
|
function reader:ReadUInt64() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_UINT32) end
|
|
|
|
|
function reader:ReadInt64() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_INT32) end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function reader:ReadFloat() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_FLOAT) end
|
|
|
|
|
function reader:ReadDouble() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_DOUBLE) end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function reader:ReadString() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_STRING) end
|
2021-10-23 12:05:48 +02:00
|
|
|
|
2021-11-26 15:10:53 +01:00
|
|
|
function reader:Size() return _CLIENT_NETWORK(LuaNetworkOpcode.READ_SIZE) end
|
2021-10-23 12:41:53 +02:00
|
|
|
|
2021-10-23 12:09:38 +02:00
|
|
|
return reader
|
2021-10-17 11:51:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- todo: not sure if we can do real callbacks,
|
|
|
|
|
-- so we will just cheat a little and
|
2021-10-23 12:05:48 +02:00
|
|
|
-- execute the string '__FireCustomPacket()' whenever
|
2021-10-17 11:51:38 +02:00
|
|
|
-- we receive a message in c++
|
|
|
|
|
|
|
|
|
|
__callbacks = {}
|
2021-10-23 12:05:48 +02:00
|
|
|
function OnCustomPacket(opcode,cb)
|
2021-10-23 12:09:38 +02:00
|
|
|
if(__callbacks[opcode] == nil) then
|
|
|
|
|
__callbacks[opcode] = {}
|
|
|
|
|
end
|
|
|
|
|
table.insert(__callbacks[opcode],cb)
|
2021-10-17 11:51:38 +02:00
|
|
|
end
|
|
|
|
|
|
2021-10-23 12:05:48 +02:00
|
|
|
function __FireCustomPacket(opcode)
|
2021-10-23 12:09:38 +02:00
|
|
|
if(__callbacks[opcode] == nil) then return end
|
|
|
|
|
for _,v in pairs(__callbacks[opcode]) do
|
|
|
|
|
v(__ReadCustomPacket(v))
|
|
|
|
|
-- if we have multiple consumers
|
2021-11-26 15:10:53 +01:00
|
|
|
_CLIENT_NETWORK(LuaNetworkOpcode.RESET_CUSTOM_PACKET)
|
2021-10-23 12:09:38 +02:00
|
|
|
end
|
2022-06-21 15:25:56 -04:00
|
|
|
end
|