|
1 |
| - |
2 |
| ---Start of Global Scope--------------------------------------------------------- |
3 |
| - |
4 |
| --- Data which is streamed |
5 |
| -local SHORT = 1346 |
6 |
| -local BYTE = 76 |
7 |
| -local LONG = 2464321 |
8 |
| - |
9 |
| ---End of Global Scope----------------------------------------------------------- |
10 |
| - |
11 |
| ---Start of Function and Event Scope----------------------------------------------- |
12 |
| - |
13 |
| ---@packStringString() |
14 |
| -local function packString() |
15 |
| - --'HBL' = Unsinged short (H), unsinged byte (B) and unsinged long (L) |
16 |
| - --More information about the format can be found in the string component description |
17 |
| - local binaryString = string.pack('HBL', SHORT, BYTE, LONG) |
18 |
| - -- Writing data to file |
19 |
| - local file = File.open('/private/TestStringPacks.bin', 'wb') |
20 |
| - assert(file, 'File could not be opened to write') |
21 |
| - File.write(file, binaryString) |
22 |
| - File.close(file) |
23 |
| -end |
24 |
| - |
25 |
| ---@unpackString() |
26 |
| -local function unpackString() |
27 |
| - -- Reading binary data from file |
28 |
| - local file = File.open('/private/TestStringPacks.bin', 'rb') |
29 |
| - assert(file, 'File could not be opened to read') |
30 |
| - local data = File.read(file) |
31 |
| - File.close(file) |
32 |
| - |
33 |
| - -- Unpacking binary data read from file |
34 |
| - local value1, value2, value3 = string.unpack('HBL', data) |
35 |
| - print('value 1 read: ' .. tostring(value1)) |
36 |
| - print('value 2 read: ' .. tostring(value2)) |
37 |
| - print('value 3 read: ' .. tostring(value3)) |
38 |
| -end |
39 |
| - |
40 |
| ---Declaration of the 'main' function as an entry point for the event loop. |
41 |
| ---@main() |
42 |
| -local function main() |
43 |
| - packString() |
44 |
| - unpackString() |
45 |
| -end |
46 |
| ---The following registration is part of the global scope which runs once after startup |
47 |
| ---Registration of the 'main' function to the Engine.OnStarted event |
48 |
| -Script.register('Engine.OnStarted', main) |
49 |
| - |
50 |
| ---End of Function and Event Scope----------------------------------------------- |
| 1 | + |
| 2 | +--Start of Global Scope--------------------------------------------------------- |
| 3 | + |
| 4 | +-- Data which is streamed |
| 5 | +local SHORT = 1346 |
| 6 | +local BYTE = 76 |
| 7 | +local LONG = 2464321 |
| 8 | + |
| 9 | +--End of Global Scope----------------------------------------------------------- |
| 10 | + |
| 11 | +--Start of Function and Event Scope----------------------------------------------- |
| 12 | + |
| 13 | +local function packString() |
| 14 | + --'HBL' = Unsinged short (H), unsinged byte (B) and unsinged long (L) |
| 15 | + --More information about the format can be found in the string component description |
| 16 | + local binaryString = string.pack('HBL', SHORT, BYTE, LONG) |
| 17 | + -- Writing data to file |
| 18 | + local file = File.open('/private/TestStringPacks.bin', 'wb') |
| 19 | + assert(file, 'File could not be opened to write') |
| 20 | + File.write(file, binaryString) |
| 21 | + File.close(file) |
| 22 | +end |
| 23 | + |
| 24 | +local function unpackString() |
| 25 | + -- Reading binary data from file |
| 26 | + local file = File.open('/private/TestStringPacks.bin', 'rb') |
| 27 | + assert(file, 'File could not be opened to read') |
| 28 | + local data = File.read(file) |
| 29 | + File.close(file) |
| 30 | + |
| 31 | + -- Unpacking binary data read from file |
| 32 | + local value1, value2, value3 = string.unpack('HBL', data) |
| 33 | + print('value 1 read: ' .. tostring(value1)) |
| 34 | + print('value 2 read: ' .. tostring(value2)) |
| 35 | + print('value 3 read: ' .. tostring(value3)) |
| 36 | +end |
| 37 | + |
| 38 | +---Declaration of the 'main' function as an entry point for the event loop. |
| 39 | +local function main() |
| 40 | + packString() |
| 41 | + unpackString() |
| 42 | +end |
| 43 | + |
| 44 | +--The following registration is part of the global scope which runs once after startup |
| 45 | +--Registration of the 'main' function to the Engine.OnStarted event |
| 46 | +Script.register('Engine.OnStarted', main) |
| 47 | + |
| 48 | +--End of Function and Event Scope----------------------------------------------- |
0 commit comments