1 /* 2 * Copyright 2015 Google Inc. 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 17 // Run this file with the `java_sample.sh` script. 18 19 import MyGame.Sample.Color 20 import MyGame.Sample.Equipment 21 import MyGame.Sample.Monster 22 import MyGame.Sample.Vec3 23 import MyGame.Sample.Weapon 24 25 import com.google.flatbuffers.FlatBufferBuilder 26 27 @kotlin.ExperimentalUnsignedTypes 28 class SampleBinary { 29 30 companion object { 31 // Example how to use FlatBuffers to create and read binary buffers. 32 @JvmStatic mainnull33 fun main(args: Array<String>) { 34 val builder = FlatBufferBuilder(0) 35 36 // Create some weapons for our Monster ('Sword' and 'Axe'). 37 val weaponOneName = builder.createString("Sword") 38 val weaponOneDamage: Short = 3 39 val weaponTwoName = builder.createString("Axe") 40 val weaponTwoDamage: Short = 5 41 42 // Use the `createWeapon()` helper function to create the weapons, since we set every field. 43 val weaps = IntArray(2) 44 weaps[0] = Weapon.createWeapon(builder, weaponOneName, weaponOneDamage) 45 weaps[1] = Weapon.createWeapon(builder, weaponTwoName, weaponTwoDamage) 46 47 // Serialize the FlatBuffer data. 48 val name = builder.createString("Orc") 49 val treasure = byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).asUByteArray() 50 val inv = Monster.createInventoryVector(builder, treasure) 51 val weapons = Monster.createWeaponsVector(builder, weaps) 52 val pos = Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f) 53 54 Monster.startMonster(builder) 55 Monster.addPos(builder, pos) 56 Monster.addName(builder, name) 57 Monster.addColor(builder, Color.Red) 58 Monster.addHp(builder, 300.toShort()) 59 Monster.addInventory(builder, inv) 60 Monster.addWeapons(builder, weapons) 61 Monster.addEquippedType(builder, Equipment.Weapon) 62 Monster.addEquipped(builder, weaps[1]) 63 val orc = Monster.endMonster(builder) 64 65 builder.finish(orc) // You could also call `Monster.finishMonsterBuffer(builder, orc);`. 66 67 // We now have a FlatBuffer that can be stored on disk or sent over a network. 68 69 // ...Code to store to disk or send over a network goes here... 70 71 // Instead, we are going to access it right away, as if we just received it. 72 73 val buf = builder.dataBuffer() 74 75 // Get access to the root: 76 val monster = Monster.getRootAsMonster(buf) 77 78 // Note: We did not set the `mana` field explicitly, so we get back the default value. 79 assert(monster.mana == 150.toShort()) 80 assert(monster.hp == 300.toShort()) 81 assert(monster.name.equals("Orc")) 82 assert(monster.color == Color.Red) 83 assert(monster.pos!!.x == 1.0f) 84 assert(monster.pos!!.y == 2.0f) 85 assert(monster.pos!!.z == 3.0f) 86 87 // Get and test the `inventory` FlatBuffer `vector`. 88 for (i in 0 until monster.inventoryLength) { 89 assert(monster.inventory(i) == i.toUByte()) 90 } 91 92 // Get and test the `weapons` FlatBuffer `vector` of `table`s. 93 val expectedWeaponNames = arrayOf("Sword", "Axe") 94 val expectedWeaponDamages = intArrayOf(3, 5) 95 for (i in 0 until monster.weaponsLength) { 96 assert(monster.weapons(i)!!.name.equals(expectedWeaponNames[i])) 97 assert(monster.weapons(i)!!.damage.toInt() == expectedWeaponDamages[i]) 98 } 99 100 // Get and test the `equipped` FlatBuffer `union`. 101 assert(monster.equippedType == Equipment.Weapon) 102 val equipped = monster.equipped(Weapon()) as Weapon? 103 assert(equipped!!.name.equals("Axe")) 104 assert(equipped.damage == 5.toShort()) 105 106 println("The FlatBuffer was successfully created and verified!") 107 } 108 } 109 } 110