forked from ManIsCat2/sm64coopdx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbytestring-packet-example.lua
More file actions
132 lines (105 loc) · 4.86 KB
/
bytestring-packet-example.lua
File metadata and controls
132 lines (105 loc) · 4.86 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-- name: Bytestring Packet Example
-- description: Shows a way to send and receive bytestring packets. Send a packet with /example [1|2].
-- Here is the lua documentation for string.pack() and string.unpack():
-- https://www.lua.org/manual/5.3/manual.html#6.4.2
-- It explains the different format strings used in this example mod.
---------------------------------------------------------------------------------------------------
local PACKET_EXAMPLE_1 = 1
local PACKET_EXAMPLE_2 = 2
---------------------------------------------------------------------------------------------------
function send_example_1(byte_param, short_param, long_param, float_param, double_param)
local bytestring = ''
-------------- PACKET ID --------------
.. string.pack("<B", PACKET_EXAMPLE_1)
---------------------------------------
.. string.pack("<b", byte_param)
.. string.pack("<h", short_param)
.. string.pack("<l", long_param)
.. string.pack("<f", float_param)
.. string.pack("<d", double_param)
---------------------------------------
network_send_bytestring(true, bytestring)
djui_chat_message_create('Sent bytestring packet example 1:')
djui_chat_message_create(' byte_param: ' .. byte_param)
djui_chat_message_create(' short_param: ' .. short_param)
djui_chat_message_create(' long_param: ' .. long_param)
djui_chat_message_create(' float_param: ' .. float_param)
djui_chat_message_create(' double_param: ' .. double_param)
end
function on_packet_bytestring_receive_example_1(bytestring)
local offset = 1
local function unpack(fmt)
local value
value, offset = string.unpack(fmt, bytestring, offset)
return value
end
-------------- PACKET ID --------------
local packet_id = unpack("<B")
---------------------------------------
local byte_param = unpack("<b")
local short_param = unpack("<h")
local long_param = unpack("<l")
local float_param = unpack("<f")
local double_param = unpack("<d")
---------------------------------------
djui_chat_message_create('Received bytestring packet example 1:')
djui_chat_message_create(' byte_param: ' .. byte_param)
djui_chat_message_create(' short_param: ' .. short_param)
djui_chat_message_create(' long_param: ' .. long_param)
djui_chat_message_create(' float_param: ' .. float_param)
djui_chat_message_create(' double_param: ' .. double_param)
end
---------------------------------------------------------------------------------------------------
function send_example_2(long_param, string_param)
local bytestring = ''
-------------- PACKET ID --------------
.. string.pack("<B", PACKET_EXAMPLE_2)
---------------------------------------
.. string.pack("<l", long_param)
.. string.pack("<s2", string_param)
---------------------------------------
network_send_bytestring(true, bytestring)
djui_chat_message_create('Sent bytestring packet example 2:')
djui_chat_message_create(' byte_param: ' .. long_param)
djui_chat_message_create(' string_param: ' .. string_param)
end
function on_packet_bytestring_receive_example_2(bytestring)
local offset = 1
local function unpack(fmt)
local value
value, offset = string.unpack(fmt, bytestring, offset)
return value
end
-------------- PACKET ID --------------
local packet_id = unpack("<B")
---------------------------------------
local long_param = unpack("<l")
local string_param = unpack("<s2")
---------------------------------------
djui_chat_message_create('Received bytestring packet example 2:')
djui_chat_message_create(' long_param: ' .. long_param)
djui_chat_message_create(' string_param: ' .. string_param)
end
---------------------------------------------------------------------------------------------------
local sPacketTable = {
[PACKET_EXAMPLE_1] = on_packet_bytestring_receive_example_1,
[PACKET_EXAMPLE_2] = on_packet_bytestring_receive_example_2,
}
local function on_packet_bytestring_receive(bytestring)
local packet_id = string.unpack("<B", bytestring, 1)
if sPacketTable[packet_id] ~= nil then
sPacketTable[packet_id](bytestring)
end
end
hook_event(HOOK_ON_PACKET_BYTESTRING_RECEIVE, on_packet_bytestring_receive)
---------------------------------------------------------------------------------------------------
hook_chat_command("example", "[1|2]", function(msg)
if msg == '1' then
send_example_1(math.random(-127, 127), math.random(-32767, 32767), math.random(-2147483647, 2147483647), math.random(), math.random())
return true
elseif string.sub(msg, 1, 1) == "2" then
send_example_2(math.random(-2147483647, 2147483647), msg)
return true
end
return false
end)