blob: d397bbb3f4475bf9cb010e072bc1477e9fe937b9 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
2 * Copyright 2018 Dan Field. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import 'package:flat_buffers/flat_buffers.dart' as fb;
James Kuszmaul8e62b022022-03-22 09:33:25 -070018import './monster_my_game.sample_generated.dart' as my_game;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070019
20// Example how to use FlatBuffers to create and read binary buffers.
21
22void main() {
23 builderTest();
24 objectBuilderTest();
25}
26
27void builderTest() {
James Kuszmaul8e62b022022-03-22 09:33:25 -070028 final builder = fb.Builder(initialSize: 1024);
29 final int? weaponOneName = builder.writeString("Sword");
Austin Schuhe89fa2d2019-08-14 20:24:23 -070030 final int weaponOneDamage = 3;
31
James Kuszmaul8e62b022022-03-22 09:33:25 -070032 final int? weaponTwoName = builder.writeString("Axe");
Austin Schuhe89fa2d2019-08-14 20:24:23 -070033 final int weaponTwoDamage = 5;
34
James Kuszmaul8e62b022022-03-22 09:33:25 -070035 final swordBuilder = my_game.WeaponBuilder(builder)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070036 ..begin()
37 ..addNameOffset(weaponOneName)
38 ..addDamage(weaponOneDamage);
39 final int sword = swordBuilder.finish();
40
James Kuszmaul8e62b022022-03-22 09:33:25 -070041 final axeBuilder = my_game.WeaponBuilder(builder)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070042 ..begin()
43 ..addNameOffset(weaponTwoName)
44 ..addDamage(weaponTwoDamage);
45 final int axe = axeBuilder.finish();
46
47 // Serialize a name for our monster, called "Orc".
James Kuszmaul8e62b022022-03-22 09:33:25 -070048 final int? name = builder.writeString('Orc');
Austin Schuhe89fa2d2019-08-14 20:24:23 -070049
50 // Create a list representing the inventory of the Orc. Each number
51 // could correspond to an item that can be claimed after he is slain.
52 final List<int> treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
53 final inventory = builder.writeListUint8(treasure);
54 final weapons = builder.writeList([sword, axe]);
55
56 // Struct builders are very easy to reuse.
James Kuszmaul8e62b022022-03-22 09:33:25 -070057 final vec3Builder = my_game.Vec3Builder(builder);
Austin Schuhe89fa2d2019-08-14 20:24:23 -070058
59 vec3Builder.finish(4.0, 5.0, 6.0);
60 vec3Builder.finish(1.0, 2.0, 3.0);
61 // Set his hit points to 300 and his mana to 150.
62 final int hp = 300;
63 final int mana = 150;
64
James Kuszmaul8e62b022022-03-22 09:33:25 -070065 final monster = my_game.MonsterBuilder(builder)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070066 ..begin()
67 ..addNameOffset(name)
68 ..addInventoryOffset(inventory)
69 ..addWeaponsOffset(weapons)
James Kuszmaul8e62b022022-03-22 09:33:25 -070070 ..addEquippedType(my_game.EquipmentTypeId.Weapon)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070071 ..addEquippedOffset(axe)
72 ..addHp(hp)
73 ..addMana(mana)
74 ..addPos(vec3Builder.finish(1.0, 2.0, 3.0))
James Kuszmaul8e62b022022-03-22 09:33:25 -070075 ..addColor(my_game.Color.Red);
Austin Schuhe89fa2d2019-08-14 20:24:23 -070076
77 final int monsteroff = monster.finish();
James Kuszmaul8e62b022022-03-22 09:33:25 -070078 builder.finish(monsteroff);
79 if (verify(builder.buffer)) {
Austin Schuhe89fa2d2019-08-14 20:24:23 -070080 print(
81 "The FlatBuffer was successfully created with a builder and verified!");
82 }
83}
84
85void objectBuilderTest() {
86 // Create the builder here so we can use it for both weapons and equipped
87 // the actual data will only be written to the buffer once.
James Kuszmaul8e62b022022-03-22 09:33:25 -070088 var axe = my_game.WeaponObjectBuilder(name: 'Axe', damage: 5);
Austin Schuhe89fa2d2019-08-14 20:24:23 -070089
James Kuszmaul8e62b022022-03-22 09:33:25 -070090 var monsterBuilder = my_game.MonsterObjectBuilder(
91 pos: my_game.Vec3ObjectBuilder(x: 1.0, y: 2.0, z: 3.0),
Austin Schuhe89fa2d2019-08-14 20:24:23 -070092 mana: 150,
93 hp: 300,
94 name: 'Orc',
95 inventory: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
James Kuszmaul8e62b022022-03-22 09:33:25 -070096 color: my_game.Color.Red,
97 weapons: [my_game.WeaponObjectBuilder(name: 'Sword', damage: 3), axe],
98 equippedType: my_game.EquipmentTypeId.Weapon,
Austin Schuhe89fa2d2019-08-14 20:24:23 -070099 equipped: axe,
100 );
101
102 var buffer = monsterBuilder.toBytes();
103
104 // We now have a FlatBuffer we can store on disk or send over a network.
105
106 // ** file/network code goes here :) **
107
108 // Instead, we're going to access it right away (as if we just received it).
109 if (verify(buffer)) {
110 print(
111 "The FlatBuffer was successfully created with an object builder and verified!");
112 }
113}
114
115bool verify(List<int> buffer) {
116 // Get access to the root:
James Kuszmaul8e62b022022-03-22 09:33:25 -0700117 var monster = my_game.Monster(buffer);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700118
119 // Get and test some scalar types from the FlatBuffer.
120 assert(monster.hp == 80);
121 assert(monster.mana == 150); // default
122 assert(monster.name == "MyMonster");
123
124 // Get and test a field of the FlatBuffer's `struct`.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700125 var pos = monster.pos!;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700126 assert(pos.z == 3.0);
127
128 // Get a test an element from the `inventory` FlatBuffer's `vector`.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700129 var inv = monster.inventory!;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700130 assert(inv.length == 10);
131 assert(inv[9] == 9);
132
133 // Get and test the `weapons` FlatBuffers's `vector`.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700134 var expectedWeaponNames = ["Sword", "Axe"];
135 var expectedWeaponDamages = [3, 5];
136 var weps = monster.weapons!;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700137 for (int i = 0; i < weps.length; i++) {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700138 assert(weps[i].name == expectedWeaponNames[i]);
139 assert(weps[i].damage == expectedWeaponDamages[i]);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700140 }
141
142 // Get and test the `Equipment` union (`equipped` field).
James Kuszmaul8e62b022022-03-22 09:33:25 -0700143 assert(monster.equippedType!.value == my_game.EquipmentTypeId.Weapon.value);
144 assert(monster.equippedType == my_game.EquipmentTypeId.Weapon);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700145
James Kuszmaul8e62b022022-03-22 09:33:25 -0700146 assert(monster.equipped is my_game.Weapon);
147 var equipped = monster.equipped as my_game.Weapon;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700148 assert(equipped.name == "Axe");
149 assert(equipped.damage == 5);
150
151 print(monster);
152 return true;
153}