game_machine 0.0.11 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +12 -1
  3. data/Gemfile.lock +32 -47
  4. data/Rakefile +0 -27
  5. data/bin/bundle_run.sh +1 -0
  6. data/bin/game_machine +29 -27
  7. data/config/cluster.conf +6 -5
  8. data/config/default.conf +164 -0
  9. data/config/game_machine.sql +33 -0
  10. data/config/game_messages.proto +87 -25
  11. data/config/gamecloud.conf +140 -0
  12. data/config/messages.proto +46 -53
  13. data/config/test.conf +149 -0
  14. data/game_machine.gemspec +10 -5
  15. data/games/boot.rb +3 -0
  16. data/games/example/data/game_data.yml +4 -4
  17. data/games/example/lib/aggressive_npc.rb +1 -1
  18. data/games/example/lib/game.rb +1 -2
  19. data/games/example/lib/player_register.rb +1 -1
  20. data/games/routes.rb +9 -0
  21. data/games/tutorial/boot.rb +12 -0
  22. data/games/tutorial/item_manager.rb +256 -0
  23. data/games/tutorial/object_store.rb +55 -0
  24. data/games/tutorial/seed.rb +52 -0
  25. data/games/tutorial/sql_store.rb +30 -0
  26. data/java/project/build.gradle +134 -0
  27. data/java/project/component.erb +719 -0
  28. data/java/{gradle.properties → project/gradle.properties} +1 -1
  29. data/java/project/gradle/wrapper/gradle-wrapper.jar +0 -0
  30. data/java/{gradle → project/gradle}/wrapper/gradle-wrapper.properties +2 -2
  31. data/java/{gradlew → project/gradlew} +0 -0
  32. data/java/{gradlew.bat → project/gradlew.bat} +0 -0
  33. data/java/project/local_lib/AdminUi.jar +0 -0
  34. data/java/{local_lib/protostuff-compiler-1.0.7-jarjar.jar → project/local_lib/protostuff-compiler-1.0.8-jarjar.jar} +0 -0
  35. data/java/project/local_lib/sigar/libsigar-amd64-freebsd-6.so +0 -0
  36. data/java/project/local_lib/sigar/libsigar-amd64-linux.so +0 -0
  37. data/java/project/local_lib/sigar/libsigar-amd64-solaris.so +0 -0
  38. data/java/project/local_lib/sigar/libsigar-ia64-hpux-11.sl +0 -0
  39. data/java/project/local_lib/sigar/libsigar-ia64-linux.so +0 -0
  40. data/java/project/local_lib/sigar/libsigar-pa-hpux-11.sl +0 -0
  41. data/java/project/local_lib/sigar/libsigar-ppc-aix-5.so +0 -0
  42. data/java/project/local_lib/sigar/libsigar-ppc-linux.so +0 -0
  43. data/java/project/local_lib/sigar/libsigar-ppc64-aix-5.so +0 -0
  44. data/java/project/local_lib/sigar/libsigar-ppc64-linux.so +0 -0
  45. data/java/project/local_lib/sigar/libsigar-s390x-linux.so +0 -0
  46. data/java/project/local_lib/sigar/libsigar-sparc-solaris.so +0 -0
  47. data/java/project/local_lib/sigar/libsigar-sparc64-solaris.so +0 -0
  48. data/java/project/local_lib/sigar/libsigar-universal-macosx.dylib +0 -0
  49. data/java/project/local_lib/sigar/libsigar-universal64-macosx.dylib +0 -0
  50. data/java/project/local_lib/sigar/libsigar-x86-freebsd-5.so +0 -0
  51. data/java/project/local_lib/sigar/libsigar-x86-freebsd-6.so +0 -0
  52. data/java/project/local_lib/sigar/libsigar-x86-linux.so +0 -0
  53. data/java/project/local_lib/sigar/libsigar-x86-solaris.so +0 -0
  54. data/java/project/local_lib/sigar/sigar-amd64-winnt.dll +0 -0
  55. data/java/project/local_lib/sigar/sigar-x86-winnt.dll +0 -0
  56. data/java/project/local_lib/sigar/sigar-x86-winnt.lib +0 -0
  57. data/java/project/model.erb +99 -0
  58. data/java/{settings.gradle → project/settings.gradle} +0 -0
  59. data/java/project/src/main/java/com/game_machine/authentication/DefaultAuthenticator.java +28 -0
  60. data/java/project/src/main/java/com/game_machine/authentication/PlayerAuthenticator.java +6 -0
  61. data/java/project/src/main/java/com/game_machine/authentication/PublicAuthenticator.java +20 -0
  62. data/java/{src → project/src}/main/java/com/game_machine/core/ActorFactory.java +0 -0
  63. data/java/{src → project/src}/main/java/com/game_machine/core/ActorUtil.java +13 -0
  64. data/java/project/src/main/java/com/game_machine/core/AuthorizedPlayers.java +23 -0
  65. data/java/project/src/main/java/com/game_machine/core/ClientMessageDecoder.java +36 -0
  66. data/java/project/src/main/java/com/game_machine/core/ClientMessageEncoder.java +19 -0
  67. data/java/project/src/main/java/com/game_machine/core/CloudClient.java +298 -0
  68. data/java/{src → project/src}/main/java/com/game_machine/core/CommandProxy.java +0 -0
  69. data/java/project/src/main/java/com/game_machine/core/Commands.java +20 -0
  70. data/java/project/src/main/java/com/game_machine/core/DatastoreCommands.java +43 -0
  71. data/java/project/src/main/java/com/game_machine/core/DbConnectionPool.java +72 -0
  72. data/java/project/src/main/java/com/game_machine/core/DefaultMovementVerifier.java +56 -0
  73. data/java/{src → project/src}/main/java/com/game_machine/core/EntitySerializer.java +0 -0
  74. data/java/project/src/main/java/com/game_machine/core/EntityTracking.java +119 -0
  75. data/java/{src → project/src}/main/java/com/game_machine/core/EventStreamHandler.java +1 -1
  76. data/java/project/src/main/java/com/game_machine/core/GameActor.java +73 -0
  77. data/java/project/src/main/java/com/game_machine/core/GameMachineLoader.java +43 -0
  78. data/java/project/src/main/java/com/game_machine/core/GameMessageActor.java +44 -0
  79. data/java/project/src/main/java/com/game_machine/core/Grid.java +255 -0
  80. data/java/{src → project/src}/main/java/com/game_machine/core/GridValue.java +0 -0
  81. data/java/project/src/main/java/com/game_machine/core/Hashring.java +66 -0
  82. data/java/{src → project/src}/main/java/com/game_machine/core/IActorFactory.java +0 -0
  83. data/java/project/src/main/java/com/game_machine/core/LocalLinkedBuffer.java +20 -0
  84. data/java/project/src/main/java/com/game_machine/core/MessageGateway.java +120 -0
  85. data/java/project/src/main/java/com/game_machine/core/MessagePersister.java +26 -0
  86. data/java/project/src/main/java/com/game_machine/core/MonoProxy.java +39 -0
  87. data/java/project/src/main/java/com/game_machine/core/MovementVerifier.java +7 -0
  88. data/java/{src → project/src}/main/java/com/game_machine/core/NetMessage.java +10 -6
  89. data/java/project/src/main/java/com/game_machine/core/PersistentMessage.java +9 -0
  90. data/java/project/src/main/java/com/game_machine/core/PlayerCommands.java +31 -0
  91. data/java/project/src/main/java/com/game_machine/core/TcpServer.java +100 -0
  92. data/java/project/src/main/java/com/game_machine/core/TcpServerHandler.java +54 -0
  93. data/java/project/src/main/java/com/game_machine/core/TcpServerInitializer.java +32 -0
  94. data/java/project/src/main/java/com/game_machine/core/UdpClient.java +86 -0
  95. data/java/{src → project/src}/main/java/com/game_machine/core/UdpServer.java +18 -27
  96. data/java/{src → project/src}/main/java/com/game_machine/core/UdpServerHandler.java +23 -26
  97. data/java/project/src/main/java/com/game_machine/core/Vector3.java +159 -0
  98. data/java/project/src/main/java/com/game_machine/orm/models/PlayerItem.java +118 -0
  99. data/java/project/src/main/java/com/game_machine/orm/models/TestObject.java +110 -0
  100. data/java/project/src/main/java/com/game_machine/tutorial/LootGenerator.java +26 -0
  101. data/java/{src → project/src}/main/resources/game_machine.java.stg +3 -1
  102. data/java/project/src/main/resources/logback.properties +13 -0
  103. data/java/project/src/main/resources/logback.xml +76 -0
  104. data/java/{src → project/src}/main/resources/protostuff.properties +0 -0
  105. data/java/src/main/java/game/MyGameActor.java +26 -0
  106. data/lib/game_machine.rb +17 -16
  107. data/lib/game_machine/actor.rb +1 -1
  108. data/lib/game_machine/actor/base.rb +8 -31
  109. data/lib/game_machine/actor/builder.rb +5 -6
  110. data/lib/game_machine/actor/game_actor.rb +55 -0
  111. data/lib/game_machine/actor/reloadable.rb +6 -1
  112. data/lib/game_machine/akka.rb +26 -32
  113. data/lib/game_machine/app_config.rb +39 -26
  114. data/lib/game_machine/application.rb +56 -62
  115. data/lib/game_machine/client_manager.rb +14 -8
  116. data/lib/game_machine/cloud_updater.rb +51 -0
  117. data/lib/game_machine/cluster_monitor.rb +3 -3
  118. data/lib/game_machine/commands.rb +1 -1
  119. data/lib/game_machine/commands/misc_commands.rb +4 -8
  120. data/lib/game_machine/commands/player_commands.rb +8 -0
  121. data/lib/game_machine/console.rb +1 -0
  122. data/lib/game_machine/console/build.rb +57 -24
  123. data/lib/game_machine/console/bundle.rb +95 -0
  124. data/lib/game_machine/console/deploy.rb +30 -0
  125. data/lib/game_machine/console/install.rb +70 -36
  126. data/lib/game_machine/console/server.rb +2 -69
  127. data/lib/game_machine/data_store.rb +111 -15
  128. data/lib/game_machine/data_stores/couchbase.rb +8 -3
  129. data/lib/game_machine/data_stores/gamecloud.rb +93 -0
  130. data/lib/game_machine/data_stores/jdbc.rb +98 -0
  131. data/lib/game_machine/default_handlers.rb +2 -0
  132. data/lib/game_machine/default_handlers/team_handler.rb +51 -0
  133. data/lib/game_machine/default_handlers/zone_manager.rb +30 -0
  134. data/lib/game_machine/endpoints.rb +0 -4
  135. data/lib/game_machine/endpoints/udp_incoming.rb +13 -5
  136. data/lib/game_machine/endpoints/udp_outgoing.rb +15 -9
  137. data/lib/game_machine/game_systems.rb +0 -2
  138. data/lib/game_machine/game_systems/agents/controller.rb +2 -2
  139. data/lib/game_machine/game_systems/entity_tracking.rb +0 -3
  140. data/lib/game_machine/game_systems/region_manager.rb +3 -2
  141. data/lib/game_machine/game_systems/region_service.rb +2 -2
  142. data/lib/game_machine/game_systems/remote_echo.rb +10 -0
  143. data/lib/game_machine/game_systems/team_manager.rb +2 -11
  144. data/lib/game_machine/grid.rb +5 -18
  145. data/lib/game_machine/handlers/authentication.rb +1 -9
  146. data/lib/game_machine/handlers/game.rb +27 -2
  147. data/lib/game_machine/handlers/player_authentication.rb +87 -0
  148. data/lib/game_machine/handlers/request.rb +9 -11
  149. data/lib/game_machine/hocon_config.rb +81 -0
  150. data/lib/game_machine/java_lib.rb +14 -1
  151. data/lib/game_machine/logger.rb +10 -23
  152. data/lib/game_machine/models.rb +1 -0
  153. data/lib/game_machine/mono_server.rb +6 -1
  154. data/lib/game_machine/object_db.rb +12 -6
  155. data/lib/game_machine/protobuf.rb +1 -1
  156. data/lib/game_machine/protobuf/game_messages.rb +13 -3
  157. data/lib/game_machine/protobuf/generate.rb +107 -5
  158. data/lib/game_machine/restart_watcher.rb +1 -1
  159. data/lib/game_machine/routes.rb +23 -0
  160. data/lib/game_machine/scheduler.rb +1 -1
  161. data/lib/game_machine/securerandom.rb +2 -0
  162. data/lib/game_machine/system_stats.rb +28 -7
  163. data/lib/game_machine/version.rb +1 -1
  164. data/lib/game_machine/wavefront_ext.rb +47 -0
  165. data/lib/game_machine/write_behind_cache.rb +24 -9
  166. data/mono/server/Makefile +1 -1
  167. data/mono/server/Newtonsoft.Json.dll +0 -0
  168. data/mono/server/build.bat +1 -1
  169. data/mono/server/callable.cs +9 -0
  170. data/mono/server/echo.cs +17 -0
  171. data/mono/server/message_router.cs +16 -23
  172. data/mono/server/messages.cs +1792 -417
  173. data/mono/server/protobuf-net.dll +0 -0
  174. data/mono/server/server.cs +120 -0
  175. data/mono/server/server.exe +0 -0
  176. data/pathfinding/astar.cpp +149 -0
  177. data/pathfinding/build.sh +6 -0
  178. data/pathfinding/build.txt +16 -0
  179. data/pathfinding/crowd.cpp +194 -0
  180. data/pathfinding/include/astar.h +49 -0
  181. data/pathfinding/include/common.h +5 -0
  182. data/pathfinding/include/crowd.h +43 -0
  183. data/pathfinding/include/micropather.h +511 -0
  184. data/pathfinding/include/navmesh.h +114 -0
  185. data/pathfinding/include/pathfinder.h +24 -0
  186. data/pathfinding/main.cpp +108 -17
  187. data/pathfinding/micropather.cpp +1062 -0
  188. data/pathfinding/navmesh.cpp +408 -0
  189. data/pathfinding/overrides/DetourCrowd.cpp +1446 -0
  190. data/pathfinding/overrides/DetourNavMeshQuery.cpp +3551 -0
  191. data/pathfinding/overrides/DetourNavMeshQuery.h +538 -0
  192. data/pathfinding/pathfinder.cpp +117 -0
  193. data/pathfinding/{bin → premake}/premake4 +0 -0
  194. data/pathfinding/premake/premake4.exe +0 -0
  195. data/pathfinding/premake4.lua +12 -3
  196. data/spec/actor/actor_spec.rb +0 -7
  197. data/spec/client_manager_spec.rb +1 -1
  198. data/spec/couchproxy_spec.rb +38 -0
  199. data/spec/entity_persistence_spec.rb +129 -0
  200. data/spec/game_systems/team_manager_spec.rb +2 -2
  201. data/spec/hashring_spec.rb +17 -39
  202. data/spec/java_grid_spec.rb +0 -2
  203. data/spec/misc_spec.rb +111 -0
  204. data/spec/mono_spec.rb +50 -3
  205. data/spec/reliable_message_spec.rb +38 -0
  206. data/spec/spec_helper.rb +4 -4
  207. data/spec/spec_helper_minimal.rb +10 -0
  208. data/web/app.rb +108 -86
  209. data/web/config/trinidad.yml +1 -0
  210. data/web/views/add_player.erb +25 -0
  211. data/web/views/index.erb +0 -0
  212. data/web/views/layout.erb +48 -0
  213. data/web/views/login.erb +25 -0
  214. data/web/views/players.erb +24 -0
  215. metadata +209 -94
  216. data/config/config.example.yml +0 -100
  217. data/config/regions.example.yml +0 -9
  218. data/games/example/lib/authentication_handler.rb +0 -69
  219. data/games/models.rb +0 -3
  220. data/games/models/clan_member.rb +0 -8
  221. data/games/models/clan_profile.rb +0 -9
  222. data/games/models/player.rb +0 -7
  223. data/games/plugins.rb +0 -1
  224. data/games/plugins/team_handler.rb +0 -49
  225. data/games/preload.rb +0 -13
  226. data/java/.gitignore +0 -1
  227. data/java/build.gradle +0 -95
  228. data/java/component.erb +0 -396
  229. data/java/gradle/wrapper/gradle-wrapper.jar +0 -0
  230. data/java/src/main/java/com/game_machine/core/GameMachineLoader.java +0 -25
  231. data/java/src/main/java/com/game_machine/core/Grid.java +0 -195
  232. data/java/src/main/resources/logback.xml +0 -14
  233. data/java/src/main/resources/logging.properties +0 -3
  234. data/lib/game_machine/actor/mono_actor.rb +0 -89
  235. data/lib/game_machine/auth_handlers/base.rb +0 -21
  236. data/lib/game_machine/auth_handlers/public.rb +0 -34
  237. data/lib/game_machine/endpoints/mono_gateway.rb +0 -87
  238. data/lib/game_machine/endpoints/tcp.rb +0 -51
  239. data/lib/game_machine/endpoints/tcp_handler.rb +0 -75
  240. data/lib/game_machine/endpoints/udp.rb +0 -88
  241. data/lib/game_machine/game_loader.rb +0 -46
  242. data/lib/game_machine/game_systems/region_settings.rb +0 -13
  243. data/lib/game_machine/hashring.rb +0 -48
  244. data/lib/game_machine/settings.rb +0 -11
  245. data/mono/server/actor.cs +0 -37
  246. data/mono/server/iactor.cs +0 -11
  247. data/mono/server/message_util.cs +0 -29
  248. data/mono/server/proxy_client.cs +0 -73
  249. data/mono/server/proxy_server.cs +0 -30
  250. data/mono/server/test_actor.cs +0 -33
  251. data/pathfinding/include/pathfind.h +0 -167
  252. data/pathfinding/pathfind.cpp +0 -174
  253. data/pathfinding/pathfinder.cs +0 -66
  254. data/script/server.sh +0 -109
  255. data/script/watch.sh +0 -11
  256. data/spec/commands/navigation_commands_spec.rb +0 -51
  257. data/spec/game_systems/entity_tracking_spec.rb +0 -64
  258. data/spec/navigation/detour_navmesh_spec.rb +0 -34
  259. data/spec/navigation/detour_path_spec.rb +0 -25
  260. data/spec/udp_server_spec.rb +0 -10
  261. data/web/controllers/auth_controller.rb +0 -19
  262. data/web/controllers/base_controller.rb +0 -16
  263. data/web/controllers/index_controller.rb +0 -7
  264. data/web/controllers/log_controller.rb +0 -47
  265. data/web/controllers/messages_controller.rb +0 -59
  266. data/web/controllers/player_register_controller.rb +0 -15
  267. data/web/views/game_messages.haml +0 -45
  268. data/web/views/index.haml +0 -6
  269. data/web/views/layout.haml +0 -41
  270. data/web/views/logs.haml +0 -32
  271. data/web/views/player_register.haml +0 -22
  272. data/web/views/player_registered.haml +0 -2
  273. data/web/views/register_layout.haml +0 -22
  274. data/web/views/restart.haml +0 -35
@@ -0,0 +1,55 @@
1
+ module Tutorial
2
+ class ObjectStore
3
+ include GameMachine
4
+ include GameMachine::Commands
5
+
6
+ attr_reader :player_items_cache
7
+
8
+ def initialize
9
+ @player_items_cache = {}
10
+ end
11
+
12
+ def all_for_player(player_id)
13
+ if entity = MessageLib::Entity.store_get(player_id,'player_items',5000)
14
+ player_items_cache[player_id] = entity.get_player_items
15
+ else
16
+ player_items_cache[player_id] = MessageLib::PlayerItems.new
17
+ save_player_items(player_items_cache[player_id],player_id)
18
+ end
19
+
20
+ player_items_cache[player_id].get_player_item_list || []
21
+ end
22
+
23
+ def delete_player_item(id,player_id)
24
+ ensure_player_cache(player_id)
25
+ player_items = player_items_cache[player_id]
26
+ player_item = MessageLib::PlayerItem.new.set_id(id)
27
+ player_items.remove_player_item_by_id(player_item)
28
+ save_player_items(player_items,player_id)
29
+ end
30
+
31
+ def save_player_item(player_item,player_id)
32
+ ensure_player_cache(player_id)
33
+ player_items = player_items_cache[player_id]
34
+ player_items.remove_player_item_by_id(player_item)
35
+ player_items.add_player_item(player_item)
36
+ save_player_items(player_items,player_id)
37
+ end
38
+
39
+ private
40
+
41
+ def ensure_player_cache(player_id)
42
+ unless player_items_cache.has_key?(player_id)
43
+ all_for_player(player_id)
44
+ end
45
+ end
46
+
47
+ def save_player_items(player_items,player_id)
48
+ entity = MessageLib::Entity.new
49
+ entity.set_player_items(player_items)
50
+ entity.set_id('player_items')
51
+ entity.store_set(player_id)
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,52 @@
1
+
2
+ items = [
3
+ {
4
+ 'id' => 'hp',
5
+ 'name' => 'Healing Potion',
6
+ 'quantity' => -1,
7
+ 'consumable' => GameMachine::MessageLib::Consumable.new.set_type('health').set_size('small')
8
+ },
9
+
10
+ {
11
+ 'id' => 'sw',
12
+ 'name' => 'Sword',
13
+ 'quantity' => 100,
14
+ 'weapon' => GameMachine::MessageLib::Weapon.new.set_attack(5).set_delay(3),
15
+ 'cost' => GameMachine::MessageLib::Cost.new.set_amount(5).set_currency('gold')
16
+ },
17
+
18
+ {
19
+ 'id' => 'helm',
20
+ 'name' => 'Helm',
21
+ 'quantity' => 1000
22
+ },
23
+
24
+ {
25
+ 'id' => 'shoes',
26
+ 'name' => 'Shoes',
27
+ 'quantity' => -1
28
+ },
29
+
30
+ {
31
+ 'id' => 'gold',
32
+ 'name' => 'Gold',
33
+ 'quantity' => -1
34
+ }
35
+ ]
36
+
37
+ items.each do |item|
38
+ player_item = GameMachine::MessageLib::PlayerItem.new
39
+ player_item.set_id(item['id'])
40
+ player_item.set_name(item['name'])
41
+ player_item.set_quantity(item['quantity'])
42
+
43
+ ['consumable','weapon','cost'].each do |component|
44
+ if item[component]
45
+ player_item.send("set_#{component}".to_sym,item[component])
46
+ end
47
+ end
48
+
49
+ unless player_item.db_save('global')
50
+ puts player_item.dbErrors.inspect
51
+ end
52
+ end
@@ -0,0 +1,30 @@
1
+ module Tutorial
2
+ class SqlStore
3
+ include GameMachine
4
+
5
+ def all_for_player(player_id)
6
+ MessageLib::PlayerItem.db_find_all(player_id)
7
+ end
8
+
9
+ def find_by_id(id,player_id,in_transaction=false)
10
+ if in_transaction
11
+ MessageLib::PlayerItem.db_find(id,player_id,true)
12
+ else
13
+ MessageLib::PlayerItem.db_find(id,player_id)
14
+ end
15
+ end
16
+
17
+ def save_player_item(player_item,player_id,in_transaction=false)
18
+ if in_transaction
19
+ player_item.db_save(player_id,true)
20
+ else
21
+ player_item.db_save_async(player_id)
22
+ end
23
+ end
24
+
25
+ def delete_player_item(id,player_id)
26
+ MessageLib::PlayerItem.db_delete_async(id,player_id)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,134 @@
1
+ apply plugin: 'java'
2
+ apply plugin: 'maven'
3
+ apply plugin: 'eclipse'
4
+ apply plugin:'scala'
5
+
6
+
7
+ buildscript {
8
+ repositories { jcenter() }
9
+ dependencies {
10
+ classpath 'com.github.jengelman.gradle.plugins:shadow:1.0.2'
11
+ classpath group: 'org.javalite', name: 'activejdbc', version: '1.4.9'
12
+ classpath group: 'org.javalite', name: 'activejdbc-instrumentation', version: '1.4.9'
13
+ }
14
+ }
15
+ apply plugin: 'com.github.johnrengelman.shadow'
16
+
17
+ group = 'game_machine'
18
+ version = '0.0.1'
19
+
20
+
21
+ description = """"""
22
+
23
+ sourceCompatibility = 1.7
24
+ targetCompatibility = 1.7
25
+
26
+ repositories {
27
+ flatDir(dirs: 'third_party')
28
+ mavenCentral()
29
+ maven {
30
+ url "https://oss.sonatype.org/content/repositories/snapshots/"
31
+ }
32
+
33
+ }
34
+
35
+ dependencies {
36
+ compile 'org.slf4j:slf4j-api:1.7.7'
37
+ compile 'ch.qos.logback:logback-classic:1.1.2'
38
+ compile 'ch.qos.logback:logback-core:1.1.2'
39
+ compile 'org.codehaus.janino:janino:2.7.5'
40
+ compile group: 'com.typesafe.akka', name: "akka-actor_$scala_version", version:"$akka_version"
41
+ compile group: 'com.typesafe.akka', name: "akka-remote_$scala_version", version:"$akka_version"
42
+ compile group: 'com.typesafe.akka', name: "akka-testkit_$scala_version", version:"$akka_version"
43
+ compile group: 'com.typesafe.akka', name: "akka-kernel_$scala_version", version:"$akka_version"
44
+ compile group: 'com.typesafe.akka', name: "akka-agent_$scala_version", version:"$akka_version"
45
+ compile group: 'com.typesafe.akka', name: "akka-contrib_$scala_version", version:"$akka_version"
46
+ compile group: 'com.typesafe.akka', name: "akka-cluster_$scala_version", version:"$akka_version"
47
+
48
+ compile 'com.dyuproject.protostuff:protostuff-uberjar:1.0.8'
49
+ compile 'com.dyuproject.protostuff:protostuff:1.0.8'
50
+ compile 'com.dyuproject.protostuff:protostuff-json:1.0.8'
51
+
52
+ //compile group: 'com.google.code.findbugs', name: 'jsr305', version:'2.0.1'
53
+ //compile group: 'org.javassist', name: 'javassist', version:'3.17.1-GA'
54
+ compile 'com.couchbase.client:couchbase-client:1.4.2'
55
+ //compile 'redis.clients:jedis:2.2.1'
56
+ //compile 'org.mapdb:mapdb:1.0.3'
57
+ compile 'org.postgresql:postgresql:9.3-1102-jdbc41'
58
+ compile 'io.netty:netty-all:4.0.21.Final'
59
+ //compile 'com.jolbox:bonecp:0.8.0.RELEASE'
60
+ compile 'com.zaxxer:HikariCP-java6:2.0.1'
61
+ compile 'mysql:mysql-connector-java:5.1.32'
62
+ compile 'org.javalite:activejdbc:1.4.10-SNAPSHOT'
63
+ compile 'org.javalite:activejdbc-instrumentation:1.4.10-SNAPSHOT'
64
+ //compile 'org.apache.httpcomponents:httpclient:4.4-alpha1'
65
+ compile 'org.apache.httpcomponents:httpclient:4.3.5'
66
+ compile 'org.mindrot:jbcrypt:0.3m'
67
+ compile 'org.fusesource:sigar:1.6.4'
68
+ }
69
+
70
+ import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer
71
+ import org.javalite.instrumentation.Instrumentation
72
+
73
+ sourceSets {
74
+ main {
75
+ java.srcDirs = ['src/main/java', '../src/main/java']
76
+ }
77
+ }
78
+
79
+
80
+ shadowJar {
81
+ transform(AppendingTransformer) {
82
+ resource = 'reference.conf'
83
+ }
84
+ }
85
+
86
+ task wrapper(type: Wrapper) {
87
+ gradleVersion = '2.0'
88
+ }
89
+
90
+ test {
91
+ //useTestNG()
92
+ //outputs.upToDateWhen { false }
93
+ //testLogging.showStandardStreams = true
94
+ //dependsOn cleanTest
95
+ //testLogging { exceptionFormat "full" }
96
+ }
97
+
98
+ task install_libs(type: Copy) {
99
+ into "$buildDir/../lib"
100
+
101
+ from configurations.runtime
102
+ from "$buildDir/libs"
103
+ from "$buildDir/../local_lib/sigar"
104
+ }
105
+
106
+ def addUrlIfNotPresent( to, what ) {
107
+ if ( !to || !what ) { return }
108
+ def urls = to.URLs.toList()
109
+ switch( what.class ) {
110
+ case URL: what = new File( what.toURI() ); break
111
+ case String: what = new File( what ); break
112
+ case GString: what = new File( what.toString() ); break
113
+ case File: break; // ok
114
+ default:
115
+ println "Don't know how to deal with $what as it is not an URL nor a File"
116
+ System.exit( 1 )
117
+ }
118
+
119
+ if ( what.directory && !what.exists() ) { what.mkdirs() }
120
+ def url = what.toURI().toURL()
121
+ if ( !urls.contains( url ) && ( what.directory || !urls.find{ it.path.endsWith( what.name ) } ) ) {
122
+ to.addURL( url )
123
+ }
124
+ }
125
+
126
+ compileJava.doLast {
127
+ Instrumentation instrumentation = new Instrumentation()
128
+
129
+ instrumentation.outputDirectory = sourceSets.main.output.classesDir.getPath()
130
+ addUrlIfNotPresent this.class.classLoader.rootLoader, sourceSets.main.output.classesDir.getPath()
131
+ addUrlIfNotPresent Instrumentation.class.classLoader, sourceSets.main.output.classesDir.getPath()
132
+ instrumentation.instrument()
133
+ }
134
+
@@ -0,0 +1,719 @@
1
+
2
+ package GameMachine.Messages;
3
+
4
+ import com.game_machine.core.GameMachineLoader;
5
+ import com.game_machine.core.ActorUtil;
6
+ import scala.concurrent.Await;
7
+ import scala.concurrent.Future;
8
+ import scala.concurrent.duration.Duration;
9
+ import akka.actor.ActorSelection;
10
+ import akka.pattern.AskableActorSelection;
11
+ import akka.util.Timeout;
12
+ import java.util.concurrent.TimeUnit;
13
+
14
+ import io.netty.buffer.ByteBuf;
15
+ import io.netty.buffer.Unpooled;
16
+
17
+ import java.io.Externalizable;
18
+ import java.io.IOException;
19
+ import java.io.ObjectInput;
20
+ import java.io.ObjectOutput;
21
+ import java.util.ArrayList;
22
+ import java.util.Iterator;
23
+ import java.util.List;
24
+ import java.io.UnsupportedEncodingException;
25
+
26
+ import com.dyuproject.protostuff.ByteString;
27
+ import com.dyuproject.protostuff.GraphIOUtil;
28
+ import com.dyuproject.protostuff.Input;
29
+ import com.dyuproject.protostuff.Message;
30
+ import com.dyuproject.protostuff.Output;
31
+ import com.dyuproject.protostuff.ProtobufOutput;
32
+
33
+ import java.io.ByteArrayOutputStream;
34
+ import com.dyuproject.protostuff.JsonIOUtil;
35
+ import com.dyuproject.protostuff.LinkedBuffer;
36
+ import com.dyuproject.protostuff.ProtobufIOUtil;
37
+ import com.dyuproject.protostuff.runtime.RuntimeSchema;
38
+
39
+ import GameMachine.Messages.Entity;
40
+ import com.game_machine.core.LocalLinkedBuffer;
41
+ import java.nio.charset.Charset;
42
+
43
+ import com.game_machine.core.DatastoreCommands;
44
+
45
+ <% if persistent %>
46
+ import com.game_machine.core.PersistentMessage;
47
+ import com.game_machine.core.ActorUtil;
48
+ import akka.actor.ActorSelection;
49
+ import org.javalite.activejdbc.Errors;
50
+ <% end %>
51
+
52
+ import org.javalite.activejdbc.Model;
53
+ import com.dyuproject.protostuff.Schema;
54
+ import com.dyuproject.protostuff.UninitializedMessageException;
55
+
56
+
57
+ <% if persistent %>
58
+ public final class <%=klass%> implements Externalizable, Message<<%=klass%>>, Schema<<%=klass%>>, PersistentMessage
59
+ <% else %>
60
+ public final class <%=klass%> implements Externalizable, Message<<%=klass%>>, Schema<<%=klass%>>
61
+ <% end %>
62
+
63
+ {
64
+
65
+ <% message.nestedEnumGroups.each do |group| %>
66
+ public enum <%=group.name %> implements com.dyuproject.protostuff.EnumLite<<%=group.name %>>
67
+ {
68
+ <% group.values.each do |value| %>
69
+ <%= "#{value.getName}(#{value.getNumber})" %><%= value.getNumber == (group.values.length - 1) ? ";" : "," %>
70
+ <% end %>
71
+
72
+ public final int number;
73
+
74
+ private Corpus (int number)
75
+ {
76
+ this.number = number;
77
+ }
78
+
79
+ public int getNumber()
80
+ {
81
+ return number;
82
+ }
83
+
84
+ public static Corpus valueOf(int number)
85
+ {
86
+ switch(number)
87
+ {
88
+ <% group.values.each do |value| %>
89
+ case <%= "#{value.getNumber}: return (#{value.getName})" %>;
90
+ <% end %>
91
+ default: return null;
92
+ }
93
+ }
94
+ }
95
+ <% end %>
96
+
97
+
98
+ public static Schema<<%=klass%>> getSchema()
99
+ {
100
+ return DEFAULT_INSTANCE;
101
+ }
102
+
103
+ public static <%=klass%> getDefaultInstance()
104
+ {
105
+ return DEFAULT_INSTANCE;
106
+ }
107
+
108
+ static final <%=klass%> DEFAULT_INSTANCE = new <%=klass%>();
109
+
110
+ <% if persistent %>
111
+ public Errors dbErrors;
112
+ public String persistPlayerId;
113
+ public String getPersistPlayerId() {
114
+ return persistPlayerId;
115
+ }
116
+
117
+ public String persistAction;
118
+ public String getPersistAction() {
119
+ return persistAction;
120
+ }
121
+ <% end %>
122
+
123
+ <% message.getFields.each do |field| %>
124
+ <% if field.isRepeated %>
125
+ public List<<%=get_type(field)%>> <%=field.name %>;
126
+ <% else %>
127
+ <% if field.default_value.nil? %>
128
+ public <%=get_type(field)%> <%=field.name %>;
129
+ <% else %>
130
+ public <%=get_type(field)%> <%=field.name %>; // = <%= field.default_value %>;
131
+ <% end %>
132
+
133
+ <% end %>
134
+ <% end %>
135
+
136
+
137
+
138
+ public <%=klass%>()
139
+ {
140
+
141
+ }
142
+
143
+
144
+
145
+ <% message.getFields.each do |field| %>
146
+ <% if field.name == 'id' %>
147
+ public static String scopeId(String playerId, String id) {
148
+ return playerId + "##" + id;
149
+ }
150
+
151
+ public static String unscopeId(String id) {
152
+ if (id.contains("##")) {
153
+ String[] parts = id.split("##");
154
+ return parts[1];
155
+ } else {
156
+ throw new RuntimeException("Expected "+id+" to contain ##");
157
+ }
158
+ }
159
+
160
+ public void storeSet(String scope) {
161
+ <%=klass%> clone = this.clone();
162
+ clone.id = scopeId(scope,this.id);
163
+ ActorSelection sel = ActorUtil.findDistributed("GameMachine::ObjectDb", clone.id);
164
+ sel.tell(clone, null);
165
+ }
166
+
167
+ public static void storeDelete(String scope, String id) {
168
+ String scopedId = scopeId(scope,id);
169
+ ActorSelection sel = ActorUtil.findDistributed("GameMachine::ObjectDb", scopedId);
170
+ ObjectdbDel del = new ObjectdbDel().setEntityId(scopedId);
171
+ sel.tell(del, null);
172
+ }
173
+
174
+ public static <%=klass%> storeGet(String scope, String id, int timeout) {
175
+ String scopedId = scopeId(scope,id);
176
+ ObjectdbGet get = new ObjectdbGet().setEntityId(scopedId).setKlass("<%=klass%>");
177
+ ActorSelection sel = ActorUtil.findDistributed("GameMachine::ObjectDb", scopedId);
178
+ Timeout t = new Timeout(Duration.create(timeout, TimeUnit.MILLISECONDS));
179
+ AskableActorSelection askable = new AskableActorSelection(sel);
180
+ <%=klass%> message;
181
+ Future<Object> future = askable.ask(get,t);
182
+ try {
183
+ message = (<%=klass%>) Await.result(future, t.duration());
184
+ } catch (Exception e) {
185
+ return null;
186
+ }
187
+ if (message == null) {
188
+ return null;
189
+ }
190
+ message.id = unscopeId(message.id);
191
+ return message;
192
+ }
193
+
194
+ <% end %>
195
+ <% end %>
196
+
197
+
198
+ <% if persistent %>
199
+
200
+ public void dbSaveAsync(String playerId) {
201
+ persistPlayerId = playerId;
202
+ persistAction = "save";
203
+ ActorSelection sel = ActorUtil.getSelectionByName("message_persister");
204
+ sel.tell(this, null);
205
+ }
206
+
207
+ public Boolean dbSave(String playerId) {
208
+ return dbSave(playerId,false);
209
+ }
210
+
211
+ public Boolean dbSave(String playerId, boolean inTransaction) {
212
+ if (!inTransaction) {
213
+ com.game_machine.orm.models.<%=klass%>.open();
214
+ }
215
+
216
+ com.game_machine.orm.models.<%=klass%> model = com.game_machine.orm.models.<%=klass%>.findFirst("<%=klass.underscore%>_id = ? and player_id = ?", this.id, playerId);
217
+ if (model == null) {
218
+ model = new com.game_machine.orm.models.<%=klass%>();
219
+ toModel(model,playerId);
220
+ } else {
221
+ toModel(model,null);
222
+ }
223
+ <% message_fields.each do |message_field| %>
224
+ if (this.has<%= message_field %>()) {
225
+ this.<%= varname(message_field) %>.toModel(model,null);
226
+ } else {
227
+ <%= message_field %>.clearModel(model);
228
+ }
229
+ <% end %>
230
+
231
+ Boolean res = model.save();
232
+ if (!res) {
233
+ dbErrors = model.errors();
234
+ }
235
+ if (!inTransaction) {
236
+ com.game_machine.orm.models.<%=klass%>.close();
237
+ }
238
+ return res;
239
+ }
240
+
241
+ public static void dbDeleteAsync(String id, String playerId) {
242
+ <%=klass%> message = new <%=klass%>();
243
+ message.setId(id);
244
+ message.persistPlayerId = playerId;
245
+ message.persistAction = "delete";
246
+ ActorSelection sel = ActorUtil.getSelectionByName("message_persister");
247
+ sel.tell(message, null);
248
+ }
249
+
250
+ public Boolean dbDelete(String playerId) {
251
+ Boolean result;
252
+ com.game_machine.orm.models.<%=klass%>.open();
253
+ com.game_machine.orm.models.<%=klass%> model = com.game_machine.orm.models.<%=klass%>.findFirst("<%=klass.underscore%>_id = ? and player_id = ?", this.id, playerId);
254
+ if (model != null) {
255
+ result = model.delete();
256
+ } else {
257
+ result = false;
258
+ }
259
+ com.game_machine.orm.models.<%=klass%>.close();
260
+ return result;
261
+ }
262
+
263
+ public static <%=klass%> dbFind(String id, String playerId) {
264
+ return dbFind(id, playerId, false);
265
+ }
266
+
267
+ public static <%=klass%> dbFind(String id, String playerId, boolean inTransaction) {
268
+ if (!inTransaction) {
269
+ com.game_machine.orm.models.<%=klass%>.open();
270
+ }
271
+
272
+ com.game_machine.orm.models.<%=klass%> model = com.game_machine.orm.models.<%=klass%>.findFirst("<%=klass.underscore%>_id = ? and player_id = ?", id, playerId);
273
+
274
+ if (!inTransaction) {
275
+ com.game_machine.orm.models.<%=klass%>.close();
276
+ }
277
+
278
+ if (model == null) {
279
+ return null;
280
+ } else {
281
+ <%=klass%> <%= varname(klass) %> = fromModel(model);
282
+ <% message_fields.each do |message_field| %>
283
+ <%= varname(klass) %>.<%= varname(message_field) %> = <%= message_field %>.fromModel(model);
284
+ <% end %>
285
+ return <%= varname(klass) %>;
286
+ }
287
+ }
288
+
289
+ public static List<<%=klass%>> dbFindAll(String playerId) {
290
+ com.game_machine.orm.models.<%=klass%>.open();
291
+ List<com.game_machine.orm.models.<%=klass%>> models = com.game_machine.orm.models.<%=klass%>.where("player_id = ?", playerId);
292
+ List<<%=klass%>> messages = new ArrayList<<%=klass%>>();
293
+ for (com.game_machine.orm.models.<%=klass%> model : models) {
294
+ <%=klass%> <%= varname(klass) %> = fromModel(model);
295
+ <% message_fields.each do |message_field| %>
296
+ <%= varname(klass) %>.<%= varname(message_field) %> = <%= message_field %>.fromModel(model);
297
+ <% end %>
298
+ messages.add(<%= varname(klass) %>);
299
+ }
300
+ com.game_machine.orm.models.<%=klass%>.close();
301
+ return messages;
302
+ }
303
+
304
+ public static List<<%=klass%>> dbWhere(String query, Object ... params) {
305
+ com.game_machine.orm.models.<%=klass%>.open();
306
+ List<com.game_machine.orm.models.<%=klass%>> models = com.game_machine.orm.models.<%=klass%>.where(query, params);
307
+ List<<%=klass%>> messages = new ArrayList<<%=klass%>>();
308
+ for (com.game_machine.orm.models.<%=klass%> model : models) {
309
+ <%=klass%> <%= varname(klass) %> = fromModel(model);
310
+ <% message_fields.each do |message_field| %>
311
+ <%= varname(klass) %>.<%= varname(message_field) %> = <%= message_field %>.fromModel(model);
312
+ <% end %>
313
+ messages.add(<%= varname(klass) %>);
314
+ }
315
+ com.game_machine.orm.models.<%=klass%>.close();
316
+ return messages;
317
+ }
318
+
319
+
320
+ <% end %>
321
+
322
+ <% unless klass == 'Entity' %>
323
+
324
+ public static void clearModel(Model model) {
325
+ <% message.getFields.each do |field| %>
326
+ <% next unless simple_value?(field) %>
327
+ <% next if field.isRepeated %>
328
+ <% field_name = field.name.slice(0,1).capitalize + field.name.slice(1..-1) %>
329
+ model.set("<%=sql_column_name(klass,field)%>",null);
330
+ <% end %>
331
+ }
332
+
333
+ public void toModel(Model model, String playerId) {
334
+ <% message.getFields.each do |field| %>
335
+ <% next unless simple_value?(field) %>
336
+ <% next if field.isRepeated %>
337
+ <% field_name = field.name.slice(0,1).capitalize + field.name.slice(1..-1) %>
338
+ if (<%= field.name %> != null) {
339
+ model.set<%=get_type(field) %>("<%=sql_column_name(klass,field)%>",<%= field.name %>);
340
+ }
341
+ <% end %>
342
+
343
+ if (playerId != null) {
344
+ model.set("player_id",playerId);
345
+ }
346
+ }
347
+
348
+ public static <%=klass%> fromModel(Model model) {
349
+ boolean hasFields = false;
350
+ <%=klass%> message = new <%=klass%>();
351
+ <% message.getFields.each do |field| %>
352
+ <% next unless simple_value?(field) %>
353
+ <% next if field.isRepeated %>
354
+ <% field_name = field.name.slice(0,1).capitalize + field.name.slice(1..-1) %>
355
+
356
+ <%=get_type(field) %> <%= field.name %>Field = model.get<%=get_type(field) %>("<%=sql_column_name(klass,field)%>");
357
+ if (<%= field.name %>Field != null) {
358
+ message.set<%= field_name %>(<%= field.name %>Field);
359
+ hasFields = true;
360
+ }
361
+ <% end %>
362
+ if (hasFields) {
363
+ return message;
364
+ } else {
365
+ return null;
366
+ }
367
+ }
368
+ <% end %>
369
+
370
+ <% if klass == 'Entity' %>
371
+ public ArrayList<String> componentNames() {
372
+ ArrayList<String> names = new ArrayList<String>();
373
+ <% messages.each do |m| %>
374
+ <% if message.getFields.collect {|f| f.name}.include?(varname(m.getName)) %>
375
+ if (this.has<%=m.getName%>()) {
376
+ names.add(this.<%=varname(m.getName)%>.getClass().getSimpleName());
377
+ }
378
+ <% end %>
379
+ <% end %>
380
+ return names;
381
+ }
382
+ <% end %>
383
+
384
+ <% message.getFields.each do |field| %>
385
+ <% field_name = field.name.slice(0,1).capitalize + field.name.slice(1..-1) %>
386
+
387
+ <% if field.isRepeated %>
388
+ public List<<%=get_type(field)%>> get<%=field_name %>List() {
389
+ return <%= field.name %>;
390
+ }
391
+
392
+ public <%=klass%> set<%=field_name %>List(List<<%=get_type(field)%>> <%=field.name%>) {
393
+ this.<%=field.name%> = <%=field.name%>;
394
+ return this;
395
+ }
396
+
397
+ public <%=get_type(field) %> get<%=field_name %>(int index) {
398
+ return <%=field.name%> == null ? null : <%=field.name%>.get(index);
399
+ }
400
+
401
+ public int get<%=field_name %>Count() {
402
+ return <%=field.name%> == null ? 0 : <%=field.name%>.size();
403
+ }
404
+
405
+ public <%=klass%> add<%=field_name %>(<%=get_type(field) %> <%=field.name%>) {
406
+ if(this.<%=field.name%> == null)
407
+ this.<%=field.name%> = new ArrayList<<%=get_type(field) %>>();
408
+ this.<%=field.name%>.add(<%=field.name%>);
409
+ return this;
410
+ }
411
+ <% if messages_index.has_key?(get_type(field)) %>
412
+ <% messages_index[get_type(field)].getFields.each do |simple_field| %>
413
+ <% next unless simple_value?(simple_field) %>
414
+ <% next if simple_field.isRepeated %>
415
+ <% simple_field_name = simple_field.name.slice(0,1).capitalize + simple_field.name.slice(1..-1) %>
416
+
417
+ public <%=klass%> remove<%=field_name %>By<%=simple_field_name %>(<%=get_type(field) %> <%=field.name%>) {
418
+ if(this.<%=field.name%> == null)
419
+ return this;
420
+
421
+ Iterator<<%=get_type(field) %>> itr = this.<%=field.name%>.iterator();
422
+ while (itr.hasNext()) {
423
+ <%=get_type(field) %> obj = itr.next();
424
+
425
+ <% if simple_field.getJavaType.to_s == 'String' %>
426
+ if (<%=field.name%>.<%=simple_field.name%>.equals(obj.<%=simple_field.name%>)) {
427
+ <% else %>
428
+ if (<%=field.name%>.<%=simple_field.name%>.equals(obj.<%=simple_field.name%>)) {
429
+ <% end %>
430
+ itr.remove();
431
+ }
432
+ }
433
+ return this;
434
+ }
435
+
436
+ <% end %>
437
+ <% end %>
438
+
439
+
440
+
441
+
442
+ <% else %>
443
+ public <%=get_type(field)%> get<%=field_name %>() {
444
+ return <%= field.name %>;
445
+ }
446
+
447
+ public <%= field_name == '_EntityId' ? 'void' : klass%> set<%=field_name %>(<%=get_type(field)%> <%=field.name%>) {
448
+ this.<%=field.name%> = <%=field.name%>;
449
+ <%= field_name == '_EntityId' ? '' : 'return this;'%>
450
+ }
451
+
452
+ public Boolean has<%=field_name %>() {
453
+ return <%=field.name%> == null ? false : true;
454
+ }
455
+ <% end %>
456
+ <% end %>
457
+
458
+
459
+ // java serialization
460
+
461
+ public void readExternal(ObjectInput in) throws IOException
462
+ {
463
+ GraphIOUtil.mergeDelimitedFrom(in, this, this);
464
+ }
465
+
466
+ public void writeExternal(ObjectOutput out) throws IOException
467
+ {
468
+ GraphIOUtil.writeDelimitedTo(out, this, this);
469
+ }
470
+
471
+ // message method
472
+
473
+ public Schema<<%=klass%>> cachedSchema()
474
+ {
475
+ return DEFAULT_INSTANCE;
476
+ }
477
+
478
+ // schema methods
479
+
480
+ public <%=klass%> newMessage()
481
+ {
482
+ return new <%=klass%>();
483
+ }
484
+
485
+ public Class<<%=klass%>> typeClass()
486
+ {
487
+ return <%=klass%>.class;
488
+ }
489
+
490
+ public String messageName()
491
+ {
492
+ return <%=klass%>.class.getSimpleName();
493
+ }
494
+
495
+ public String messageFullName()
496
+ {
497
+ return <%=klass%>.class.getName();
498
+ }
499
+
500
+ public boolean isInitialized(<%=klass%> message)
501
+ {
502
+ return true;
503
+ }
504
+
505
+ public void mergeFrom(Input input, <%=klass%> message) throws IOException
506
+ {
507
+ for(int number = input.readFieldNumber(this);; number = input.readFieldNumber(this))
508
+ {
509
+ switch(number)
510
+ {
511
+ case 0:
512
+ return;
513
+ <% message.getFields.each do |field| %>
514
+ case <%=field.number%>:
515
+ <% if field.isRepeated %>
516
+ if(message.<%=field.name%> == null)
517
+ message.<%=field.name%> = new ArrayList<<%=get_type(field)%>>();
518
+ <% if field.isMessageField %>
519
+ message.<%=field.name%>.add(input.mergeObject(null, <%=get_type(field)%>.getSchema()));
520
+ <% elsif field.enumField %>
521
+ message.<%=field.name%>.add(<%=get_type(field)%>.valueOf(input.readEnum()));
522
+ <% else %>
523
+ message.<%=field.name%>.add(input.read<%=field.getClass.getSimpleName%>());
524
+ <% end %>
525
+ break;
526
+ <% else %>
527
+ <% if field.isMessageField %>
528
+ message.<%=field.name%> = input.mergeObject(message.<%=field.name%>, <%=get_type(field)%>.getSchema());
529
+ break;
530
+ <% elsif field.enumField %>
531
+ message.<%=field.name%> = <%=classname(field.name)%>.valueOf(input.readEnum());
532
+ break;
533
+ <% else %>
534
+ message.<%=field.name%> = input.read<%=field.getClass.getSimpleName%>();
535
+ break;
536
+ <% end %>
537
+
538
+ <% end %>
539
+ <% end %>
540
+
541
+ default:
542
+ input.handleUnknownField(number, this);
543
+ }
544
+ }
545
+ }
546
+
547
+
548
+ public void writeTo(Output output, <%=klass%> message) throws IOException
549
+ {
550
+ <% message.getFields.each do |field| %>
551
+
552
+ <% if field.isRequired %>
553
+ if(message.<%=field.name%> == null)
554
+ throw new UninitializedMessageException(message);
555
+ <% end %>
556
+
557
+ <% if field.isRepeated %>
558
+ if(message.<%=field.name%> != null)
559
+ {
560
+ for(<%=get_type(field)%> <%=field.name%> : message.<%=field.name%>)
561
+ {
562
+ if(<%=field.name%> != null) {
563
+ <% if field.isMessageField %>
564
+ output.writeObject(<%=field.number%>, <%=field.name%>, <%=get_type(field)%>.getSchema(), true);
565
+ <% elsif field.enumField %>
566
+ output.writeEnum(<%=field.number%>, <%=field.name%>.number, true);
567
+ <% else %>
568
+ output.write<%=field.getClass.getSimpleName%>(<%=field.number%>, <%=field.name%>, true);
569
+ <% end %>
570
+ }
571
+ }
572
+ }
573
+ <% else %>
574
+ <% if field.isMessageField %>
575
+ if(message.<%=field.name%> != null)
576
+ output.writeObject(<%=field.number%>, message.<%=field.name%>, <%=get_type(field)%>.getSchema(), false);
577
+ <% elsif field.enumField %>
578
+ output.writeEnum(<%=field.number%>, message.<%=field.name%>.number, false);
579
+ <% else %>
580
+ if(message.<%=field.name%> != null)
581
+ output.write<%=field.getClass.getSimpleName%>(<%=field.number%>, message.<%=field.name%>, false);
582
+ <% end %>
583
+
584
+ <% end %>
585
+ <% end %>
586
+
587
+ }
588
+
589
+ public String getFieldName(int number)
590
+ {
591
+ switch(number)
592
+ {
593
+ <% message.getFields.each do |field| %>
594
+ case <%=field.number%>: return "<%=field.name%>";
595
+ <% end %>
596
+ default: return null;
597
+ }
598
+ }
599
+
600
+ public int getFieldNumber(String name)
601
+ {
602
+ final Integer number = __fieldMap.get(name);
603
+ return number == null ? 0 : number.intValue();
604
+ }
605
+
606
+ private static final java.util.HashMap<String,Integer> __fieldMap = new java.util.HashMap<String,Integer>();
607
+ static
608
+ {
609
+ <% message.getFields.each do |field| %>
610
+ __fieldMap.put("<%=field.name%>", <%=field.number%>);
611
+ <% end %>
612
+ }
613
+
614
+ public static List<String> getFields() {
615
+ ArrayList<String> fieldNames = new ArrayList<String>();
616
+ String fieldName = null;
617
+ Integer i = 1;
618
+
619
+ while(true) {
620
+ fieldName = <%=klass%>.getSchema().getFieldName(i);
621
+ if (fieldName == null) {
622
+ break;
623
+ }
624
+ fieldNames.add(fieldName);
625
+ i++;
626
+ }
627
+ return fieldNames;
628
+ }
629
+
630
+ public static <%=klass%> parseFrom(byte[] bytes) {
631
+ <%=klass%> message = new <%=klass%>();
632
+ ProtobufIOUtil.mergeFrom(bytes, message, RuntimeSchema.getSchema(<%=klass%>.class));
633
+ return message;
634
+ }
635
+
636
+ public static <%=klass%> parseFromJson(String json) throws IOException {
637
+ byte[] bytes = json.getBytes(Charset.forName("UTF-8"));
638
+ <%=klass%> message = new <%=klass%>();
639
+ JsonIOUtil.mergeFrom(bytes, message, RuntimeSchema.getSchema(<%=klass%>.class), false);
640
+ return message;
641
+ }
642
+
643
+ public <%=klass%> clone() {
644
+ byte[] bytes = this.toByteArray();
645
+ <%=klass%> <%=varname(klass)%> = <%=klass%>.parseFrom(bytes);
646
+ return <%=varname(klass)%>;
647
+ }
648
+
649
+ public byte[] toByteArray() {
650
+ return toProtobuf();
651
+ //return toJson();
652
+ }
653
+
654
+ public String toJson() throws UnsupportedEncodingException {
655
+ boolean numeric = false;
656
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
657
+ try {
658
+ JsonIOUtil.writeTo(out, this, <%=klass%>.getSchema(), numeric);
659
+ } catch (IOException e) {
660
+ e.printStackTrace();
661
+ throw new RuntimeException("Json encoding failed");
662
+ }
663
+ String json = new String(out.toByteArray(), Charset.forName("UTF-8"));
664
+ return json;
665
+ }
666
+
667
+ public byte[] toPrefixedByteArray() {
668
+ LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
669
+ Schema<<%=klass%>> schema = RuntimeSchema.getSchema(<%=klass%>.class);
670
+
671
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
672
+ final ProtobufOutput output = new ProtobufOutput(buffer);
673
+ try
674
+ {
675
+ schema.writeTo(output, this);
676
+ final int size = output.getSize();
677
+ ProtobufOutput.writeRawVarInt32Bytes(out, size);
678
+ final int msgSize = LinkedBuffer.writeTo(out, buffer);
679
+ assert size == msgSize;
680
+
681
+ buffer.clear();
682
+ return out.toByteArray();
683
+ }
684
+ catch (IOException e)
685
+ {
686
+ throw new RuntimeException("Serializing to a byte array threw an IOException " +
687
+ "(should never happen).", e);
688
+ }
689
+
690
+ }
691
+
692
+ public byte[] toProtobuf() {
693
+ LinkedBuffer buffer = LocalLinkedBuffer.get();
694
+ byte[] bytes = null;
695
+
696
+ try {
697
+ bytes = ProtobufIOUtil.toByteArray(this, RuntimeSchema.getSchema(<%=klass%>.class), buffer);
698
+ buffer.clear();
699
+ } catch (Exception e) {
700
+ e.printStackTrace();
701
+ throw new RuntimeException("Protobuf encoding failed");
702
+ }
703
+ return bytes;
704
+ }
705
+
706
+ public ByteBuf toByteBuf() {
707
+ ByteBuf bb = Unpooled.buffer(512, 2048);
708
+ LinkedBuffer buffer = LinkedBuffer.use(bb.array());
709
+
710
+ try {
711
+ ProtobufIOUtil.writeTo(buffer, this, RuntimeSchema.getSchema(<%=klass%>.class));
712
+ } catch (Exception e) {
713
+ e.printStackTrace();
714
+ throw new RuntimeException("Protobuf encoding failed");
715
+ }
716
+ return bb;
717
+ }
718
+
719
+ }