1 package org.hedgewars.hedgeroid.netplay; |
|
2 import java.nio.Buffer; |
|
3 import java.util.Collections; |
|
4 |
|
5 import android.util.Log; |
|
6 |
|
7 import com.sun.jna.Callback; |
|
8 import com.sun.jna.Library; |
|
9 import com.sun.jna.Native; |
|
10 import com.sun.jna.NativeLong; |
|
11 import com.sun.jna.Pointer; |
|
12 import com.sun.jna.PointerType; |
|
13 import com.sun.jna.Structure; |
|
14 |
|
15 class Flib { |
|
16 static { |
|
17 System.loadLibrary("SDL_net"); |
|
18 } |
|
19 public static final JnaFrontlib INSTANCE = (JnaFrontlib)Native.loadLibrary("frontlib", JnaFrontlib.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, FrontlibTypeMapper.INSTANCE)); |
|
20 |
|
21 // Hook frontlib logging into Android logging |
|
22 private static final JnaFrontlib.LogCallback logCb = new JnaFrontlib.LogCallback() { |
|
23 public void callback(int level, String message) { |
|
24 if(level >= JnaFrontlib.FLIB_LOGLEVEL_ERROR) { |
|
25 Log.e("Frontlib", message); |
|
26 } else if(level == JnaFrontlib.FLIB_LOGLEVEL_WARNING){ |
|
27 Log.w("Frontlib", message); |
|
28 } else if(level == JnaFrontlib.FLIB_LOGLEVEL_INFO){ |
|
29 Log.i("Frontlib", message); |
|
30 } else if(level <= JnaFrontlib.FLIB_LOGLEVEL_DEBUG){ |
|
31 Log.d("Frontlib", message); |
|
32 } |
|
33 } |
|
34 }; |
|
35 static { |
|
36 INSTANCE.flib_log_setLevel(JnaFrontlib.FLIB_LOGLEVEL_WARNING); |
|
37 INSTANCE.flib_log_setCallback(logCb); |
|
38 } |
|
39 } |
|
40 |
|
41 public interface JnaFrontlib extends Library { |
|
42 static final int NETCONN_STATE_CONNECTING = 0; |
|
43 static final int NETCONN_STATE_LOBBY = 1; |
|
44 static final int NETCONN_STATE_ROOM = 2; |
|
45 static final int NETCONN_STATE_INGAME = 3; |
|
46 static final int NETCONN_STATE_DISCONNECTED = 10; |
|
47 |
|
48 static final int NETCONN_DISCONNECT_NORMAL = 0; |
|
49 static final int NETCONN_DISCONNECT_SERVER_TOO_OLD = 1; |
|
50 static final int NETCONN_DISCONNECT_AUTH_FAILED = 2; |
|
51 static final int NETCONN_DISCONNECT_CONNLOST = 3; |
|
52 static final int NETCONN_DISCONNECT_INTERNAL_ERROR = 100; |
|
53 |
|
54 static final int NETCONN_ROOMLEAVE_ABANDONED = 0; |
|
55 static final int NETCONN_ROOMLEAVE_KICKED = 1; |
|
56 |
|
57 static final int NETCONN_MSG_TYPE_PLAYERINFO = 0; |
|
58 static final int NETCONN_MSG_TYPE_SERVERMESSAGE = 1; |
|
59 static final int NETCONN_MSG_TYPE_WARNING = 2; |
|
60 static final int NETCONN_MSG_TYPE_ERROR = 3; |
|
61 |
|
62 static final int NETCONN_MAPCHANGE_FULL = 0; |
|
63 static final int NETCONN_MAPCHANGE_MAP = 1; |
|
64 static final int NETCONN_MAPCHANGE_MAPGEN = 2; |
|
65 static final int NETCONN_MAPCHANGE_DRAWNMAP = 3; |
|
66 static final int NETCONN_MAPCHANGE_MAZE_SIZE = 4; |
|
67 static final int NETCONN_MAPCHANGE_TEMPLATE = 5; |
|
68 static final int NETCONN_MAPCHANGE_THEME = 6; |
|
69 static final int NETCONN_MAPCHANGE_SEED = 7; |
|
70 |
|
71 static final int GAME_END_FINISHED = 0; |
|
72 static final int GAME_END_INTERRUPTED = 1; |
|
73 static final int GAME_END_HALTED = 2; |
|
74 static final int GAME_END_ERROR = 3; |
|
75 |
|
76 static class NetconnPtr extends PointerType { } |
|
77 static class MapconnPtr extends PointerType { } |
|
78 static class GameconnPtr extends PointerType { } |
|
79 static class MetaschemePtr extends PointerType { } |
|
80 |
|
81 static class RoomArrayPtr extends PointerType { |
|
82 /** |
|
83 * Returns the (native-owned) rooms in this list |
|
84 */ |
|
85 public Room[] getRooms(int count) { |
|
86 Pointer ptr = getPointer(); |
|
87 if(ptr == null) { |
|
88 return new Room[0]; |
|
89 } |
|
90 Pointer[] untypedPtrs = ptr.getPointerArray(0, count); |
|
91 Room[] result = new Room[count]; |
|
92 for(int i=0; i<count; i++) { |
|
93 result[i] = RoomPtr.deref(untypedPtrs[i]); |
|
94 } |
|
95 return result; |
|
96 } |
|
97 } |
|
98 |
|
99 static class RoomPtr extends PointerType { |
|
100 public RoomPtr() { super(); } |
|
101 public RoomPtr(Pointer ptr) { super(ptr); } |
|
102 |
|
103 public Room deref() { |
|
104 return deref(getPointer()); |
|
105 } |
|
106 |
|
107 public static Room deref(Pointer p) { |
|
108 RoomStruct r = new RoomStruct(p); |
|
109 r.read(); |
|
110 return new Room(r.name, r.map, r.scheme, r.weapons, r.owner, r.playerCount, r.teamCount, r.inProgress); |
|
111 } |
|
112 } |
|
113 |
|
114 static class TeamPtr extends PointerType { } |
|
115 static class WeaponsetPtr extends PointerType { } |
|
116 static class MapRecipePtr extends PointerType { } |
|
117 static class SchemePtr extends PointerType { } |
|
118 static class GameSetupPtr extends PointerType { } |
|
119 |
|
120 static class RoomStruct extends Structure { |
|
121 public static class byVal extends RoomStruct implements Structure.ByValue {} |
|
122 public static class byRef extends RoomStruct implements Structure.ByReference {} |
|
123 private static String[] FIELD_ORDER = new String[] {"inProgress", "name", "playerCount", "teamCount", "owner", "map", "scheme", "weapons"}; |
|
124 |
|
125 public RoomStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
126 public RoomStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
127 |
|
128 public boolean inProgress; |
|
129 public String name; |
|
130 public int playerCount; |
|
131 public int teamCount; |
|
132 public String owner; |
|
133 public String map; |
|
134 public String scheme; |
|
135 public String weapons; |
|
136 } |
|
137 |
|
138 public static interface VoidCallback extends Callback { |
|
139 void callback(Pointer context); |
|
140 } |
|
141 |
|
142 public static interface StrCallback extends Callback { |
|
143 void callback(Pointer context, String arg1); |
|
144 } |
|
145 |
|
146 public static interface IntCallback extends Callback { |
|
147 void callback(Pointer context, int arg1); |
|
148 } |
|
149 |
|
150 public static interface IntStrCallback extends Callback { |
|
151 void callback(Pointer context, int arg1, String arg2); |
|
152 } |
|
153 |
|
154 public static interface StrIntCallback extends Callback { |
|
155 void callback(Pointer context, String arg1, int arg2); |
|
156 } |
|
157 |
|
158 public static interface StrStrCallback extends Callback { |
|
159 void callback(Pointer context, String arg1, String arg2); |
|
160 } |
|
161 |
|
162 public static interface RoomCallback extends Callback { |
|
163 void callback(Pointer context, RoomPtr arg1); |
|
164 } |
|
165 |
|
166 public static interface RoomListCallback extends Callback { |
|
167 void callback(Pointer context, RoomArrayPtr arg1, int count); |
|
168 } |
|
169 |
|
170 public static interface StrRoomCallback extends Callback { |
|
171 void callback(Pointer context, String arg1, RoomPtr arg2); |
|
172 } |
|
173 |
|
174 public static interface BoolCallback extends Callback { |
|
175 void callback(Pointer context, boolean arg1); |
|
176 } |
|
177 |
|
178 public static interface StrBoolCallback extends Callback { |
|
179 void callback(Pointer context, String arg1, boolean arg2); |
|
180 } |
|
181 |
|
182 public static interface TeamCallback extends Callback { |
|
183 void callback(Pointer context, TeamPtr arg1); |
|
184 } |
|
185 |
|
186 public static interface BytesCallback extends Callback { |
|
187 void callback(Pointer context, Pointer buffer, NativeLong size); |
|
188 } |
|
189 |
|
190 public static interface BytesBoolCallback extends Callback { |
|
191 void callback(Pointer context, Pointer buffer, NativeLong size, boolean arg3); |
|
192 } |
|
193 |
|
194 public static interface SchemeCallback extends Callback { |
|
195 void callback(Pointer context, SchemePtr arg1); |
|
196 } |
|
197 |
|
198 public static interface MapIntCallback extends Callback { |
|
199 void callback(Pointer context, MapRecipePtr arg1, int arg2); |
|
200 } |
|
201 |
|
202 public static interface WeaponsetCallback extends Callback { |
|
203 void callback(Pointer context, WeaponsetPtr arg1); |
|
204 } |
|
205 |
|
206 public static interface MapimageCallback extends Callback { |
|
207 void callback(Pointer context, Pointer buffer, int hedgehogCount); |
|
208 } |
|
209 |
|
210 public static interface LogCallback extends Callback { |
|
211 void callback(int level, String logMessage); |
|
212 } |
|
213 |
|
214 int flib_init(); |
|
215 void flib_quit(); |
|
216 |
|
217 NetconnPtr flib_netconn_create(String playerName, MetaschemePtr meta, String dataDirPath, String host, int port); |
|
218 void flib_netconn_destroy(NetconnPtr conn); |
|
219 |
|
220 void flib_netconn_tick(NetconnPtr conn); |
|
221 boolean flib_netconn_is_chief(NetconnPtr conn); |
|
222 boolean flib_netconn_is_in_room_context(NetconnPtr conn); |
|
223 String flib_netconn_get_playername(NetconnPtr conn); |
|
224 GameSetupPtr flib_netconn_create_gamesetup(NetconnPtr conn); |
|
225 int flib_netconn_send_quit(NetconnPtr conn, String quitmsg); |
|
226 int flib_netconn_send_chat(NetconnPtr conn, String chat); |
|
227 int flib_netconn_send_teamchat(NetconnPtr conn, String msg); |
|
228 int flib_netconn_send_password(NetconnPtr conn, String passwd); |
|
229 int flib_netconn_send_nick(NetconnPtr conn, String nick); |
|
230 int flib_netconn_send_request_roomlist(NetconnPtr conn); |
|
231 int flib_netconn_send_joinRoom(NetconnPtr conn, String room); |
|
232 int flib_netconn_send_createRoom(NetconnPtr conn, String room); |
|
233 int flib_netconn_send_renameRoom(NetconnPtr conn, String roomName); |
|
234 int flib_netconn_send_leaveRoom(NetconnPtr conn, String msg); |
|
235 int flib_netconn_send_toggleReady(NetconnPtr conn); |
|
236 int flib_netconn_send_addTeam(NetconnPtr conn, TeamPtr team); |
|
237 int flib_netconn_send_removeTeam(NetconnPtr conn, String teamname); |
|
238 int flib_netconn_send_engineMessage(NetconnPtr conn, Buffer message, NativeLong size); // TODO check if NativeLong==size_t |
|
239 int flib_netconn_send_teamHogCount(NetconnPtr conn, String teamname, int hogcount); |
|
240 int flib_netconn_send_teamColor(NetconnPtr conn, String teamname, int colorIndex); |
|
241 int flib_netconn_send_weaponset(NetconnPtr conn, WeaponsetPtr weaponset); |
|
242 int flib_netconn_send_map(NetconnPtr conn, MapRecipePtr map); |
|
243 int flib_netconn_send_mapName(NetconnPtr conn, String mapName); |
|
244 int flib_netconn_send_mapGen(NetconnPtr conn, int mapGen); |
|
245 int flib_netconn_send_mapTemplate(NetconnPtr conn, int templateFilter); |
|
246 int flib_netconn_send_mapMazeSize(NetconnPtr conn, int mazeSize); |
|
247 int flib_netconn_send_mapSeed(NetconnPtr conn, String seed); |
|
248 int flib_netconn_send_mapTheme(NetconnPtr conn, String theme); |
|
249 int flib_netconn_send_mapDrawdata(NetconnPtr conn, Buffer drawData, NativeLong size); |
|
250 int flib_netconn_send_script(NetconnPtr conn, String scriptName); |
|
251 int flib_netconn_send_scheme(NetconnPtr conn, SchemePtr scheme); |
|
252 int flib_netconn_send_roundfinished(NetconnPtr conn, boolean withoutError); |
|
253 int flib_netconn_send_ban(NetconnPtr conn, String playerName); |
|
254 int flib_netconn_send_kick(NetconnPtr conn, String playerName); |
|
255 int flib_netconn_send_playerInfo(NetconnPtr conn, String playerName); |
|
256 int flib_netconn_send_playerFollow(NetconnPtr conn, String playerName); |
|
257 int flib_netconn_send_startGame(NetconnPtr conn); |
|
258 int flib_netconn_send_toggleRestrictJoins(NetconnPtr conn); |
|
259 int flib_netconn_send_toggleRestrictTeams(NetconnPtr conn); |
|
260 int flib_netconn_send_clearAccountsCache(NetconnPtr conn); |
|
261 int flib_netconn_send_setServerVar(NetconnPtr conn, String name, String value); |
|
262 int flib_netconn_send_getServerVars(NetconnPtr conn); |
|
263 |
|
264 void flib_netconn_onMessage(NetconnPtr conn, IntStrCallback callback, Pointer context); |
|
265 void flib_netconn_onChat(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
266 void flib_netconn_onConnected(NetconnPtr conn, VoidCallback callback, Pointer context); |
|
267 void flib_netconn_onDisconnected(NetconnPtr conn, IntStrCallback callback, Pointer context); |
|
268 void flib_netconn_onRoomlist(NetconnPtr conn, RoomListCallback callback, Pointer context); |
|
269 void flib_netconn_onRoomAdd(NetconnPtr conn, RoomCallback callback, Pointer context); |
|
270 void flib_netconn_onRoomDelete(NetconnPtr conn, StrCallback callback, Pointer context); |
|
271 void flib_netconn_onRoomUpdate(NetconnPtr conn, StrRoomCallback callback, Pointer context); |
|
272 void flib_netconn_onLobbyJoin(NetconnPtr conn, StrCallback callback, Pointer context); |
|
273 void flib_netconn_onLobbyLeave(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
274 void flib_netconn_onNickTaken(NetconnPtr conn, StrCallback callback, Pointer context); |
|
275 void flib_netconn_onPasswordRequest(NetconnPtr conn, StrCallback callback, Pointer context); |
|
276 void flib_netconn_onEnterRoom(NetconnPtr conn, BoolCallback callback, Pointer context); |
|
277 void flib_netconn_onRoomChiefStatus(NetconnPtr conn, BoolCallback callback, Pointer context); |
|
278 void flib_netconn_onReadyState(NetconnPtr conn, StrBoolCallback callback, Pointer context); |
|
279 void flib_netconn_onLeaveRoom(NetconnPtr conn, IntStrCallback callback, Pointer context); |
|
280 void flib_netconn_onTeamAdd(NetconnPtr conn, TeamCallback callback, Pointer context); |
|
281 void flib_netconn_onTeamDelete(NetconnPtr conn, StrCallback callback, Pointer context); |
|
282 void flib_netconn_onRoomJoin(NetconnPtr conn, StrCallback callback, Pointer context); |
|
283 void flib_netconn_onRoomLeave(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
284 void flib_netconn_onRunGame(NetconnPtr conn, VoidCallback callback, Pointer context); |
|
285 void flib_netconn_onTeamAccepted(NetconnPtr conn, StrCallback callback, Pointer context); |
|
286 void flib_netconn_onHogCountChanged(NetconnPtr conn, StrIntCallback callback, Pointer context); |
|
287 void flib_netconn_onTeamColorChanged(NetconnPtr conn, StrIntCallback callback, Pointer context); |
|
288 void flib_netconn_onEngineMessage(NetconnPtr conn, BytesCallback callback, Pointer context); |
|
289 void flib_netconn_onCfgScheme(NetconnPtr conn, SchemeCallback callback, Pointer context); |
|
290 void flib_netconn_onMapChanged(NetconnPtr conn, MapIntCallback callback, Pointer context); |
|
291 void flib_netconn_onScriptChanged(NetconnPtr conn, StrCallback callback, Pointer context); |
|
292 void flib_netconn_onWeaponsetChanged(NetconnPtr conn, WeaponsetCallback callback, Pointer context); |
|
293 void flib_netconn_onAdminAccess(NetconnPtr conn, VoidCallback callback, Pointer context); |
|
294 void flib_netconn_onServerVar(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
295 |
|
296 // Gameconn |
|
297 GameconnPtr flib_gameconn_create(String playerName, GameSetupPtr setup, boolean netgame); |
|
298 GameconnPtr flib_gameconn_create_playdemo(Buffer demo, NativeLong size); |
|
299 GameconnPtr flib_gameconn_create_loadgame(String playerName, Buffer save, NativeLong size); |
|
300 GameconnPtr flib_gameconn_create_campaign(String playerName, String seed, String script); |
|
301 |
|
302 void flib_gameconn_destroy(GameconnPtr conn); |
|
303 int flib_gameconn_getport(GameconnPtr conn); |
|
304 void flib_gameconn_tick(GameconnPtr conn); |
|
305 |
|
306 int flib_gameconn_send_enginemsg(GameconnPtr conn, Buffer data, NativeLong len); |
|
307 int flib_gameconn_send_textmsg(GameconnPtr conn, int msgtype, String msg); |
|
308 int flib_gameconn_send_chatmsg(GameconnPtr conn, String playername, String msg); |
|
309 |
|
310 void flib_gameconn_onConnect(GameconnPtr conn, VoidCallback callback, Pointer context); |
|
311 void flib_gameconn_onDisconnect(GameconnPtr conn, IntCallback callback, Pointer context); |
|
312 void flib_gameconn_onErrorMessage(GameconnPtr conn, StrCallback callback, Pointer context); |
|
313 void flib_gameconn_onChat(GameconnPtr conn, StrBoolCallback callback, Pointer context); |
|
314 void flib_gameconn_onGameRecorded(GameconnPtr conn, BytesBoolCallback callback, Pointer context); |
|
315 void flib_gameconn_onEngineMessage(GameconnPtr conn, BytesCallback callback, Pointer context); |
|
316 |
|
317 // MapConn |
|
318 MapconnPtr flib_mapconn_create(MapRecipePtr mapdesc); |
|
319 void flib_mapconn_destroy(MapconnPtr conn); |
|
320 int flib_mapconn_getport(MapconnPtr conn); |
|
321 void flib_mapconn_onSuccess(MapconnPtr conn, MapimageCallback callback, Pointer context); |
|
322 void flib_mapconn_onFailure(MapconnPtr conn, StrCallback callback, Pointer context); |
|
323 void flib_mapconn_tick(MapconnPtr conn); |
|
324 |
|
325 // GameSetup |
|
326 void flib_gamesetup_destroy(GameSetupPtr gamesetup); |
|
327 GameSetupPtr flib_gamesetup_copy(GameSetupPtr gamesetup); |
|
328 |
|
329 // MapRecipe |
|
330 public static final int MAPGEN_REGULAR = 0; |
|
331 public static final int MAPGEN_MAZE = 1; |
|
332 public static final int MAPGEN_DRAWN = 2; |
|
333 public static final int MAPGEN_NAMED = 3; |
|
334 |
|
335 public static final int TEMPLATEFILTER_ALL = 0; |
|
336 public static final int TEMPLATEFILTER_SMALL = 1; |
|
337 public static final int TEMPLATEFILTER_MEDIUM = 2; |
|
338 public static final int TEMPLATEFILTER_LARGE = 3; |
|
339 public static final int TEMPLATEFILTER_CAVERN = 4; |
|
340 public static final int TEMPLATEFILTER_WACKY = 5; |
|
341 |
|
342 public static final int MAZE_SIZE_SMALL_TUNNELS = 0; |
|
343 public static final int MAZE_SIZE_MEDIUM_TUNNELS = 1; |
|
344 public static final int MAZE_SIZE_LARGE_TUNNELS = 2; |
|
345 public static final int MAZE_SIZE_SMALL_ISLANDS = 3; |
|
346 public static final int MAZE_SIZE_MEDIUM_ISLANDS = 4; |
|
347 public static final int MAZE_SIZE_LARGE_ISLANDS = 5; |
|
348 |
|
349 MapRecipePtr flib_map_create_regular(String seed, String theme, int templateFilter); |
|
350 MapRecipePtr flib_map_create_maze(String seed, String theme, int mazeSize); |
|
351 MapRecipePtr flib_map_create_named(String seed, String name); |
|
352 MapRecipePtr flib_map_create_drawn(String seed, String theme, Buffer drawData, NativeLong drawDataSize); |
|
353 MapRecipePtr flib_map_copy(MapRecipePtr map); |
|
354 MapRecipePtr flib_map_retain(MapRecipePtr map); |
|
355 void flib_map_release(MapRecipePtr map); |
|
356 |
|
357 // Metascheme |
|
358 MetaschemePtr flib_metascheme_from_ini(String filename); |
|
359 MetaschemePtr flib_metascheme_retain(MetaschemePtr metainfo); |
|
360 void flib_metascheme_release(MetaschemePtr metainfo); |
|
361 |
|
362 public static final int FLIB_LOGLEVEL_ALL = -100; |
|
363 public static final int FLIB_LOGLEVEL_DEBUG = -1; |
|
364 public static final int FLIB_LOGLEVEL_INFO = 0; |
|
365 public static final int FLIB_LOGLEVEL_WARNING = 1; |
|
366 public static final int FLIB_LOGLEVEL_ERROR = 2; |
|
367 public static final int FLIB_LOGLEVEL_NONE = 100; |
|
368 |
|
369 void flib_log_setLevel(int level); |
|
370 void flib_log_setCallback(LogCallback callback); |
|
371 } |
|