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