xref: /aosp_15_r20/external/flatbuffers/samples/sample_binary.lua (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker-- need to update the Lua path to point to the local flatbuffers implementation
2*890232f2SAndroid Build Coastguard Workerpackage.path = string.format("../lua/?.lua;%s",package.path)
3*890232f2SAndroid Build Coastguard Workerpackage.path = string.format("./lua/?.lua;%s",package.path)
4*890232f2SAndroid Build Coastguard Worker
5*890232f2SAndroid Build Coastguard Worker-- require the library
6*890232f2SAndroid Build Coastguard Workerlocal flatbuffers = require("flatbuffers")
7*890232f2SAndroid Build Coastguard Worker
8*890232f2SAndroid Build Coastguard Workerlocal binaryArray = flatbuffers.binaryArray-- for hex dump utility
9*890232f2SAndroid Build Coastguard Worker
10*890232f2SAndroid Build Coastguard Worker-- require the files generated from the schema
11*890232f2SAndroid Build Coastguard Workerlocal weapon = require("MyGame.Sample.Weapon")
12*890232f2SAndroid Build Coastguard Workerlocal monster = require("MyGame.Sample.Monster")
13*890232f2SAndroid Build Coastguard Workerlocal vec3 = require("MyGame.Sample.Vec3")
14*890232f2SAndroid Build Coastguard Workerlocal color = require("MyGame.Sample.Color")
15*890232f2SAndroid Build Coastguard Workerlocal equipment = require("MyGame.Sample.Equipment")
16*890232f2SAndroid Build Coastguard Worker
17*890232f2SAndroid Build Coastguard Worker-- get access to the builder, providing an array of size 1024
18*890232f2SAndroid Build Coastguard Workerlocal builder = flatbuffers.Builder(1024)
19*890232f2SAndroid Build Coastguard Worker
20*890232f2SAndroid Build Coastguard Workerlocal weaponOne = builder:CreateString("Sword")
21*890232f2SAndroid Build Coastguard Workerlocal weaponTwo = builder:CreateString("Axe")
22*890232f2SAndroid Build Coastguard Worker
23*890232f2SAndroid Build Coastguard Worker-- Create the first 'Weapon'
24*890232f2SAndroid Build Coastguard Workerweapon.Start(builder)
25*890232f2SAndroid Build Coastguard Workerweapon.AddName(builder, weaponOne)
26*890232f2SAndroid Build Coastguard Workerweapon.AddDamage(builder, 3)
27*890232f2SAndroid Build Coastguard Workerlocal sword = weapon.End(builder)
28*890232f2SAndroid Build Coastguard Worker
29*890232f2SAndroid Build Coastguard Worker-- Create the second 'Weapon'
30*890232f2SAndroid Build Coastguard Workerweapon.Start(builder)
31*890232f2SAndroid Build Coastguard Workerweapon.AddName(builder, weaponTwo)
32*890232f2SAndroid Build Coastguard Workerweapon.AddDamage(builder, 5)
33*890232f2SAndroid Build Coastguard Workerlocal axe = weapon.End(builder)
34*890232f2SAndroid Build Coastguard Worker
35*890232f2SAndroid Build Coastguard Worker-- Serialize a name for our mosnter, called 'orc'
36*890232f2SAndroid Build Coastguard Workerlocal name = builder:CreateString("Orc")
37*890232f2SAndroid Build Coastguard Worker
38*890232f2SAndroid Build Coastguard Worker-- Create a `vector` representing the inventory of the Orc. Each number
39*890232f2SAndroid Build Coastguard Worker-- could correspond to an item that can be claimed after he is slain.
40*890232f2SAndroid Build Coastguard Worker-- Note: Since we prepend the bytes, this loop iterates in reverse.
41*890232f2SAndroid Build Coastguard Workermonster.StartInventoryVector(builder, 10)
42*890232f2SAndroid Build Coastguard Workerfor i=10,1,-1 do
43*890232f2SAndroid Build Coastguard Worker    builder:PrependByte(i)
44*890232f2SAndroid Build Coastguard Workerend
45*890232f2SAndroid Build Coastguard Workerlocal inv = builder:EndVector(10)
46*890232f2SAndroid Build Coastguard Worker
47*890232f2SAndroid Build Coastguard Worker-- Create a FlatBuffer vector and prepend the weapons.
48*890232f2SAndroid Build Coastguard Worker-- Note: Since we prepend the data, prepend them in reverse order.
49*890232f2SAndroid Build Coastguard Workermonster.StartWeaponsVector(builder, 2)
50*890232f2SAndroid Build Coastguard Workerbuilder:PrependUOffsetTRelative(axe)
51*890232f2SAndroid Build Coastguard Workerbuilder:PrependUOffsetTRelative(sword)
52*890232f2SAndroid Build Coastguard Workerlocal weapons = builder:EndVector(2)
53*890232f2SAndroid Build Coastguard Worker
54*890232f2SAndroid Build Coastguard Worker-- Create our monster by using Start() andEnd()
55*890232f2SAndroid Build Coastguard Workermonster.Start(builder)
56*890232f2SAndroid Build Coastguard Workermonster.AddPos(builder, vec3.CreateVec3(builder, 1.0, 2.0, 3.0))
57*890232f2SAndroid Build Coastguard Workermonster.AddHp(builder, 300)
58*890232f2SAndroid Build Coastguard Workermonster.AddName(builder, name)
59*890232f2SAndroid Build Coastguard Workermonster.AddInventory(builder, inv)
60*890232f2SAndroid Build Coastguard Workermonster.AddColor(builder, color.Red)
61*890232f2SAndroid Build Coastguard Workermonster.AddWeapons(builder, weapons)
62*890232f2SAndroid Build Coastguard Workermonster.AddEquippedType(builder, equipment.Weapon)
63*890232f2SAndroid Build Coastguard Workermonster.AddEquipped(builder, axe)
64*890232f2SAndroid Build Coastguard Workerlocal orc = monster.End(builder)
65*890232f2SAndroid Build Coastguard Worker
66*890232f2SAndroid Build Coastguard Worker-- Call 'Finish()' to instruct the builder that this monster is complete.
67*890232f2SAndroid Build Coastguard Workerbuilder:Finish(orc)
68*890232f2SAndroid Build Coastguard Worker
69*890232f2SAndroid Build Coastguard Worker-- Get the flatbuffer as a string containing the binary data
70*890232f2SAndroid Build Coastguard Workerlocal bufAsString = builder:Output()
71*890232f2SAndroid Build Coastguard Worker
72*890232f2SAndroid Build Coastguard Worker-- Convert the string representation into binary array Lua structure
73*890232f2SAndroid Build Coastguard Workerlocal buf = flatbuffers.binaryArray.New(bufAsString)
74*890232f2SAndroid Build Coastguard Worker
75*890232f2SAndroid Build Coastguard Worker-- Get an accessor to the root object insert the buffer
76*890232f2SAndroid Build Coastguard Workerlocal mon = monster.GetRootAsMonster(buf, 0)
77*890232f2SAndroid Build Coastguard Worker
78*890232f2SAndroid Build Coastguard Workerassert(mon:Mana() == 150)
79*890232f2SAndroid Build Coastguard Workerassert(mon:Hp() == 300)
80*890232f2SAndroid Build Coastguard Workerassert(mon:Name() == "Orc")
81*890232f2SAndroid Build Coastguard Workerassert(mon:Color() == color.Red)
82*890232f2SAndroid Build Coastguard Workerassert(mon:Pos():X() == 1.0)
83*890232f2SAndroid Build Coastguard Workerassert(mon:Pos():Y() == 2.0)
84*890232f2SAndroid Build Coastguard Workerassert(mon:Pos():Z() == 3.0)
85*890232f2SAndroid Build Coastguard Worker
86*890232f2SAndroid Build Coastguard Workerfor i=1,mon:InventoryLength() do
87*890232f2SAndroid Build Coastguard Worker    assert(mon:Inventory(i) == i)
88*890232f2SAndroid Build Coastguard Workerend
89*890232f2SAndroid Build Coastguard Worker
90*890232f2SAndroid Build Coastguard Workerlocal expected = {
91*890232f2SAndroid Build Coastguard Worker    {w = 'Sword', d = 3},
92*890232f2SAndroid Build Coastguard Worker    {w = 'Axe', d = 5}
93*890232f2SAndroid Build Coastguard Worker}
94*890232f2SAndroid Build Coastguard Worker
95*890232f2SAndroid Build Coastguard Workerfor i=1,mon:WeaponsLength() do
96*890232f2SAndroid Build Coastguard Worker   assert(mon:Weapons(i):Name() == expected[i].w)
97*890232f2SAndroid Build Coastguard Worker   assert(mon:Weapons(i):Damage() == expected[i].d)
98*890232f2SAndroid Build Coastguard Workerend
99*890232f2SAndroid Build Coastguard Worker
100*890232f2SAndroid Build Coastguard Workerassert(mon:EquippedType() == equipment.Weapon)
101*890232f2SAndroid Build Coastguard Worker
102*890232f2SAndroid Build Coastguard Workerlocal unionWeapon = weapon.New()
103*890232f2SAndroid Build Coastguard WorkerunionWeapon:Init(mon:Equipped().bytes,mon:Equipped().pos)
104*890232f2SAndroid Build Coastguard Workerassert(unionWeapon:Name() == "Axe")
105*890232f2SAndroid Build Coastguard Workerassert(unionWeapon:Damage() == 5)
106*890232f2SAndroid Build Coastguard Worker
107*890232f2SAndroid Build Coastguard Workerprint("The Lua FlatBuffer example was successfully created and verified!")