1 /*
2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 
20 
21 package org.hedgewars.hedgeroid.frontlib;
22 import java.io.UnsupportedEncodingException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 
29 import org.hedgewars.hedgeroid.Datastructures.Hog;
30 import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
31 import org.hedgewars.hedgeroid.Datastructures.MetaScheme;
32 import org.hedgewars.hedgeroid.Datastructures.MetaScheme.Mod;
33 import org.hedgewars.hedgeroid.Datastructures.MetaScheme.Setting;
34 import org.hedgewars.hedgeroid.Datastructures.GameConfig;
35 import org.hedgewars.hedgeroid.Datastructures.Room;
36 import org.hedgewars.hedgeroid.Datastructures.Scheme;
37 import org.hedgewars.hedgeroid.Datastructures.Team;
38 import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
39 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes;
40 import org.hedgewars.hedgeroid.Datastructures.Weaponset;
41 
42 import com.sun.jna.Callback;
43 import com.sun.jna.Library;
44 import com.sun.jna.Memory;
45 import com.sun.jna.Pointer;
46 import com.sun.jna.PointerType;
47 import com.sun.jna.Structure;
48 
49 /**
50  * Here is an introduction to the most important aspects of the JNA code.
51  *
52  * This interface permits access to the Hedgewars frontend library (frontlib)
53  * from Java. Each function directly contained in the Frontlib interface
54  * represents a mapped C function. The Structure classes (ending in -Struct) are
55  * mappings of C structs, and the PointerType classes (ending in -Ptr) represent
56  * pointers to structs.
57  *
58  * Quick notes for USING these classes from outside this package:
59  *
60  * Usage should be fairly straightforward, but there are a few surprising
61  * gotchas. First, when you implement callbacks, YOU are responsible for
62  * ensuring that the callback objects are not garbage-collected while they might
63  * still be called! So make sure you keep them in member variables or similar,
64  * because Java will not know if there are still native references to them.
65  *
66  * When using Frontlib from outside its package, you only interact with structs
67  * via the PointerType classes. They allow you to get at the data of the struct
68  * with a function called deref(), which creates a plain normal Java object
69  * representing the data (e.g. SchemePtr.deref() will give you a Scheme object).
70  *
71  * Remember that you usually have to destroy structs that you receive from the
72  * library, because they are owned by the native code, not Java. The recommended
73  * pattern for most cases is to call deref() on the pointer to get a Java object
74  * (that you can keep as long as you like), and then immediately destroy the
75  * struct if it needs destroying. To find out whether and how the struct needs
76  * to be destroyed, see the library's documentation of the function that you got
77  * the struct from.
78  *
79  * To pass new structs to the library, you can use the static createJavaOwned()
80  * function in each PointerType, which creates a new struct from the Java object
81  * you provide, and returns a pointer to that struct that you can pass to
82  * library functions. This new structure's memory is owned and managed by Java
83  * code, so do not destroy it with frontlib functions!
84  *
85  * There is a slight mismatch between the data model for the game setup. The
86  * frontlib supports setting initial health and weaponset per-hog, because the
87  * engine allows for that, but currently neither the networking protocol nor the
88  * PC frontend support this feature, so the Android version does not take
89  * advantage of it either and treats both as per-game settings. The initial
90  * health is contained in the game scheme, the weaponset is explicitly part of
91  * the GameConfig. When converting GameConfig to a native flib_gamesetup, both
92  * are automatically copied to all hogs in the game, and for the reverse
93  * conversion the weaponset of the first hog of the first team is used as the
94  * GameConfig weaponset. This means that GameConfig.weaponset will be null if
95  * there are no teams in the game.
96  *
97  * When starting a network game, you only need to query the GameSetupPtr from
98  * the netconn and use it to create the gameconn - this is preferable to using
99  * your own recreation of the game setup, because that way the same piece of
100  * code is used to determine the game setup on all platforms.
101  *
102  * The "context" parameter of the callbacks is never needed here because JNA
103  * generates function code for each callback object. Don't worry about it, just
104  * pass null for context and ignore the context parameter in the callbacks.
105  *
106  * Finally, the library functions are documented in the actual library, not
107  * here, so check the docs there to find out what exactly each function does!
108  *
109  * Notes about the structure of this class (for the next one who has to touch
110  * this...):
111  *
112  * Java/C interop is quite fiddly and error-prone, so as long as things work,
113  * try to stick to the established patterns.
114  *
115  * Structure types should always be hidden from the outside world, because they
116  * can be misused too easily. For example, if you get a Structure from the
117  * library, change a String value in there and pass it back, JNA will re-write
118  * that string using Java-owned memory without freeing the old native-owned
119  * string, which causes a memory leak and possibly a double-free or other Bad
120  * Things (tm). To avoid problems like this, Structure types are only used
121  * internally, to map existing structures to Java types (without modifying them)
122  * or to create brand-new, Java-owned structures. Both operations are exposed to
123  * the outside through the PointerType classes corresponding to the structures
124  * in question.
125  *
126  * Since all of the struct mapping happens in Java, it is never checked against
127  * the actual struct declarations in the library. That means strange things can
128  * start happening at runtime if the frontlib structs are modified without
129  * changing the mappings here to match. This also applies to the function
130  * signatures: JNA checks whether the functions actually exist when loading the
131  * library, but it has no way of knowing whether the signatures are correct. If
132  * the signatures here deviate from those in the frontlib, you might get stack
133  * corruption.
134  *
135  * In order to check at least the function signatures, take a look at the file
136  * extra/jnacontrol.c in the frontlib sources. You can validate whether the
137  * function signatures are still correct by copy-pasting them into jnaControl.c
138  * and compiling it against the frontlib headers. The typedefs and #defines in
139  * that file will make the compiler see the Java method signatures as C function
140  * declarations. Since the same functions are already declared in the frontlib
141  * headers, the compiler will give you errors if the signatures don't match.
142  */
143 public interface Frontlib extends Library {
144     public static class NetconnPtr extends PointerType { }
145     public static class MapconnPtr extends PointerType { }
146     public static class GameconnPtr extends PointerType { }
147 
148     public static class MetaschemePtr extends PointerType {
deref()149         public MetaScheme deref() {
150             return deref(getPointer());
151         }
152 
deref(Pointer p)153         public static MetaScheme deref(Pointer p) {
154             MetaschemeStruct struct = new MetaschemeStruct(p);
155             struct.read();
156             return struct.toMetaScheme();
157         }
158     }
159 
160     public static class RoomArrayPtr extends PointerType {
getRooms(int count)161         public Room[] getRooms(int count) {
162             Pointer ptr = getPointer();
163             if(ptr == null) {
164                 return new Room[0];
165             }
166             Pointer[] untypedPtrs = ptr.getPointerArray(0, count);
167             Room[] result = new Room[count];
168             for(int i=0; i<count; i++) {
169                 result[i] = RoomPtr.deref(untypedPtrs[i]);
170             }
171             return result;
172         }
173     }
174 
175     public static class RoomPtr extends PointerType {
deref()176         public Room deref() {
177             return deref(getPointer());
178         }
179 
deref(Pointer p)180         public static Room deref(Pointer p) {
181             RoomStruct struct = new RoomStruct(p);
182             struct.read();
183             return struct.toRoomlistRoom();
184         }
185     }
186 
187     public static class TeamPtr extends PointerType {
188         private TeamStruct javaOwnedInstance;
189 
deref()190         public TeamInGame deref() {
191             TeamStruct struct = new TeamStruct(getPointer());
192             struct.read();
193             return struct.toTeamInGame();
194         }
195 
createJavaOwned(Team t)196         public static TeamPtr createJavaOwned(Team t) {
197             return createJavaOwned(new TeamInGame(t, null));
198         }
199 
createJavaOwned(TeamInGame ingameTeam)200         public static TeamPtr createJavaOwned(TeamInGame ingameTeam) {
201             TeamPtr result = new TeamPtr();
202             result.javaOwnedInstance = new TeamStruct();
203             result.javaOwnedInstance.fillFrom(ingameTeam.team, ingameTeam.ingameAttribs);
204             result.javaOwnedInstance.autoWrite();
205             result.setPointer(result.javaOwnedInstance.getPointer());
206             return result;
207         }
208     }
209 
210     public static class WeaponsetPtr extends PointerType {
211         private WeaponsetStruct javaOwnedInstance;
212 
deref()213         public Weaponset deref() {
214             WeaponsetStruct struct = new WeaponsetStruct(getPointer());
215             struct.read();
216             return struct.toWeaponset();
217         }
218 
createJavaOwned(Weaponset weaponset)219         public static WeaponsetPtr createJavaOwned(Weaponset weaponset) {
220             WeaponsetPtr result = new WeaponsetPtr();
221             result.javaOwnedInstance = new WeaponsetStruct();
222             result.javaOwnedInstance.fillFrom(weaponset);
223             result.javaOwnedInstance.autoWrite();
224             result.setPointer(result.javaOwnedInstance.getPointer());
225             return result;
226         }
227     }
228 
229     public static class WeaponsetListPtr extends PointerType {
230         private WeaponsetListStruct javaOwnedInstance;
231 
deref()232         public List<Weaponset> deref() {
233             WeaponsetListStruct struct = new WeaponsetListStruct(getPointer());
234             struct.read();
235             return struct.toWeaponsetList();
236         }
237 
createJavaOwned(List<Weaponset> list)238         public static WeaponsetListPtr createJavaOwned(List<Weaponset> list) {
239             WeaponsetListPtr result = new WeaponsetListPtr();
240             result.javaOwnedInstance = new WeaponsetListStruct();
241             result.javaOwnedInstance.fillFrom(list);
242             result.javaOwnedInstance.autoWrite();
243             result.setPointer(result.javaOwnedInstance.getPointer());
244             return result;
245         }
246     }
247 
248     public static class MapRecipePtr extends PointerType {
249         private MapRecipeStruct javaOwnedInstance;
250 
deref()251         public MapRecipe deref() {
252             MapRecipeStruct struct = new MapRecipeStruct(getPointer());
253             struct.read();
254             return struct.toMapRecipe();
255         }
256 
createJavaOwned(MapRecipe recipe)257         public static MapRecipePtr createJavaOwned(MapRecipe recipe) {
258             MapRecipePtr result = new MapRecipePtr();
259             result.javaOwnedInstance = new MapRecipeStruct();
260             result.javaOwnedInstance.fillFrom(recipe);
261             result.javaOwnedInstance.autoWrite();
262             result.setPointer(result.javaOwnedInstance.getPointer());
263             return result;
264         }
265     }
266 
267     public static class SchemePtr extends PointerType {
268         private SchemeStruct javaOwnedInstance;
269 
deref()270         public Scheme deref() {
271             SchemeStruct struct = new SchemeStruct(getPointer());
272             struct.read();
273             return struct.toScheme();
274         }
275 
createJavaOwned(Scheme scheme)276         public static SchemePtr createJavaOwned(Scheme scheme) {
277             SchemePtr result = new SchemePtr();
278             result.javaOwnedInstance = new SchemeStruct();
279             result.javaOwnedInstance.fillFrom(scheme);
280             result.javaOwnedInstance.autoWrite();
281             result.setPointer(result.javaOwnedInstance.getPointer());
282             return result;
283         }
284     }
285 
286     public static class SchemelistPtr extends PointerType {
287         private SchemelistStruct javaOwnedInstance;
288 
deref()289         public List<Scheme> deref() {
290             SchemelistStruct struct = new SchemelistStruct(getPointer());
291             struct.read();
292             return struct.toSchemeList();
293         }
294 
createJavaOwned(List<Scheme> schemes)295         public static SchemelistPtr createJavaOwned(List<Scheme> schemes) {
296             SchemelistPtr result = new SchemelistPtr();
297             result.javaOwnedInstance = new SchemelistStruct();
298             result.javaOwnedInstance.fillFrom(schemes);
299             result.javaOwnedInstance.autoWrite();
300             result.setPointer(result.javaOwnedInstance.getPointer());
301             return result;
302         }
303     }
304 
305     public static class GameSetupPtr extends PointerType {
306         private GameSetupStruct javaOwnedInstance;
307 
deref()308         public GameConfig deref() {
309             GameSetupStruct struct = new GameSetupStruct(getPointer());
310             struct.read();
311             return struct.toGameConfig();
312         }
313 
createJavaOwned(GameConfig conf)314         public static GameSetupPtr createJavaOwned(GameConfig conf) {
315             GameSetupPtr result = new GameSetupPtr();
316             result.javaOwnedInstance = new GameSetupStruct();
317             result.javaOwnedInstance.fillFrom(conf);
318             result.javaOwnedInstance.autoWrite();
319             result.setPointer(result.javaOwnedInstance.getPointer());
320             return result;
321         }
322     }
323 
324     public static class ByteArrayPtr extends PointerType {
deref(int size)325         public byte[] deref(int size) {
326             return getPointer().getByteArray(0, size);
327         }
328 
deref(ByteArrayPtr ptr, int size)329         public static byte[] deref(ByteArrayPtr ptr, int size) {
330             if(ptr==null && size==0) {
331                 return null;
332             } else {
333                 return ptr.deref(size);
334             }
335         }
336 
createJavaOwned(byte[] buffer)337         public static ByteArrayPtr createJavaOwned(byte[] buffer) {
338             if(buffer == null || buffer.length == 0) {
339                 return null;
340             }
341             // no need for javaOwnedInstance here because PointerType
342             // remembers the memory as its Pointer
343             Pointer ptr = new Memory(buffer.length);
344             ptr.write(0, buffer, 0, buffer.length);
345             ByteArrayPtr result = new ByteArrayPtr();
346             result.setPointer(ptr);
347             return result;
348         }
349     }
350 
351     static class HogStruct extends Structure {
352         public static class ByVal extends HogStruct implements Structure.ByValue {}
353         public static class ByRef extends HogStruct implements Structure.ByReference {}
354 
HogStruct()355         public HogStruct() { super(); }
HogStruct(Pointer ptr)356         public HogStruct(Pointer ptr) { super(ptr); }
357 
358         @Override
getFieldOrder()359         protected List<String> getFieldOrder() {
360             return Arrays.asList("name", "hat", "rounds", "kills", "deaths", "suicides", "difficulty", "initialHealth", "weaponset");
361         }
362 
fillFrom(Hog hog)363         public void fillFrom(Hog hog) {
364             difficulty = hog.level;
365             hat = hog.hat;
366             name = hog.name;
367         }
368 
toHog()369         public Hog toHog() {
370             return new Hog(name, hat, difficulty);
371         }
372 
373         public String name;
374         public String hat;
375 
376         public int rounds;
377         public int kills;
378         public int deaths;
379         public int suicides;
380 
381         public int difficulty;
382 
383         public int initialHealth;
384         public WeaponsetStruct.ByRef weaponset;
385     }
386 
387     static class TeamStruct extends Structure {
388         public static class ByVal extends TeamStruct implements Structure.ByValue {}
389         public static class ByRef extends TeamStruct implements Structure.ByReference {}
390 
TeamStruct()391         public TeamStruct() { super(); }
TeamStruct(Pointer ptr)392         public TeamStruct(Pointer ptr) { super(ptr); }
393 
394         @Override
getFieldOrder()395         protected List<String> getFieldOrder() {
396             return Arrays.asList("hogs", "name", "grave", "fort", "voicepack", "flag", "bindings", "bindingCount", "rounds", "wins", "campaignProgress", "colorIndex", "hogsInGame", "remoteDriven", "ownerName");
397         }
398 
fillFrom(Team team, TeamIngameAttributes attrs)399         public void fillFrom(Team team, TeamIngameAttributes attrs) {
400             if(team != null) {
401                 name = team.name;
402                 grave = team.grave;
403                 flag = team.flag;
404                 voicepack = team.voice;
405                 fort = team.fort;
406                 if(team.hogs.size() != Team.HEDGEHOGS_PER_TEAM) {
407                     throw new IllegalArgumentException();
408                 }
409                 for(int i=0; i<hogs.length; i++) {
410                     hogs[i] = new HogStruct();
411                     hogs[i].fillFrom(team.hogs.get(i));
412                 }
413             }
414 
415             if(attrs != null) {
416                 hogsInGame = attrs.hogCount;
417                 ownerName = attrs.ownerName;
418                 colorIndex = attrs.colorIndex;
419                 remoteDriven = attrs.remoteDriven;
420             }
421         }
422 
fillFrom(TeamInGame team, WeaponsetStruct.ByRef weaponset, int initialHealth)423         public void fillFrom(TeamInGame team, WeaponsetStruct.ByRef weaponset, int initialHealth) {
424             fillFrom(team.team, team.ingameAttribs);
425             for(int i=0; i<hogs.length; i++) {
426                 hogs[i].initialHealth = initialHealth;
427                 hogs[i].weaponset = weaponset;
428             }
429         }
430 
toTeam()431         public Team toTeam() {
432             List<Hog> hogList = new ArrayList<Hog>();
433             for(int i=0; i<hogs.length; i++) {
434                 hogList.add(hogs[i].toHog());
435             }
436             return new Team(name, grave, flag, voicepack, fort, hogList);
437         }
438 
toTeamIngameAttributes()439         public TeamIngameAttributes toTeamIngameAttributes() {
440             return new TeamIngameAttributes(ownerName, colorIndex, hogsInGame, remoteDriven);
441         }
442 
toTeamInGame()443         public TeamInGame toTeamInGame() {
444             return new TeamInGame(toTeam(), toTeamIngameAttributes());
445         }
446 
447         public HogStruct[] hogs = new HogStruct[Team.HEDGEHOGS_PER_TEAM];
448         public String name;
449         public String grave;
450         public String fort;
451         public String voicepack;
452         public String flag;
453 
454         public Pointer bindings;
455         public int bindingCount;
456 
457         public int rounds;
458         public int wins;
459         public int campaignProgress;
460 
461         public int colorIndex;
462         public int hogsInGame;
463         public boolean remoteDriven;
464         public String ownerName;
465     }
466 
467     static class WeaponsetStruct extends Structure {
468         public static class ByVal extends WeaponsetStruct implements Structure.ByValue {}
469         public static class ByRef extends WeaponsetStruct implements Structure.ByReference {}
470 
WeaponsetStruct()471         public WeaponsetStruct() { super(); }
WeaponsetStruct(Pointer ptr)472         public WeaponsetStruct(Pointer ptr) { super(ptr); }
473 
474         @Override
getFieldOrder()475         protected List<String> getFieldOrder() {
476             return Arrays.asList("loadout", "crateprob", "crateammo", "delay", "name");
477         }
478 
fillFrom(Weaponset weaponset)479         public void fillFrom(Weaponset weaponset) {
480             fillWeaponInfo(loadout, weaponset.loadout);
481             fillWeaponInfo(crateprob, weaponset.crateProb);
482             fillWeaponInfo(crateammo, weaponset.crateAmmo);
483             fillWeaponInfo(delay, weaponset.delay);
484             name = weaponset.name;
485         }
486 
fillWeaponInfo(byte[] array, String str)487         private static void fillWeaponInfo(byte[] array, String str) {
488             for(int i=0; i<array.length-1; i++) {
489                 array[i] = (byte) (i<str.length() ? str.charAt(i) : '0');
490             }
491             array[array.length-1] = (byte)0;
492         }
493 
toWeaponset()494         public Weaponset toWeaponset() {
495             return new Weaponset(name, weaponInfoToString(loadout), weaponInfoToString(crateprob), weaponInfoToString(crateammo), weaponInfoToString(delay));
496         }
497 
weaponInfoToString(byte[] array)498         private static String weaponInfoToString(byte[] array) {
499             try {
500                 return new String(array, 0, array.length-1, "ASCII");
501             } catch (UnsupportedEncodingException e) {
502                 throw new AssertionError();
503             }
504         }
505 
506         public byte[] loadout = new byte[Weaponset.WEAPONS_COUNT+1];
507         public byte[] crateprob = new byte[Weaponset.WEAPONS_COUNT+1];
508         public byte[] crateammo = new byte[Weaponset.WEAPONS_COUNT+1];
509         public byte[] delay = new byte[Weaponset.WEAPONS_COUNT+1];
510         public String name;
511     }
512 
513     /**
514      * Represents a flib_weaponset*, for use as part of a flib_weaponset**
515      */
516     static class WeaponsetPointerByReference extends Structure implements Structure.ByReference {
WeaponsetPointerByReference()517         public WeaponsetPointerByReference() { super(); }
WeaponsetPointerByReference(Pointer ptr)518         public WeaponsetPointerByReference(Pointer ptr) { super(ptr); }
519 
520         @Override
getFieldOrder()521         protected List<String> getFieldOrder() {
522             return Arrays.asList("weaponset");
523         }
524 
525         public WeaponsetStruct.ByRef weaponset;
526     }
527 
528     static class WeaponsetListStruct extends Structure {
529         public static class ByVal extends WeaponsetListStruct implements Structure.ByValue {}
530         public static class ByRef extends WeaponsetListStruct implements Structure.ByReference {}
531 
WeaponsetListStruct()532         public WeaponsetListStruct() { super(); }
WeaponsetListStruct(Pointer ptr)533         public WeaponsetListStruct(Pointer ptr) { super(ptr); }
534 
535         @Override
getFieldOrder()536         protected List<String> getFieldOrder() {
537             return Arrays.asList("weaponsetCount", "weaponsets");
538         }
539 
fillFrom(List<Weaponset> list)540         public void fillFrom(List<Weaponset> list) {
541             weaponsetCount = list.size();
542             if(weaponsetCount<=0) {
543                 weaponsets = null;
544             } else {
545                 weaponsets = new WeaponsetPointerByReference();
546                 Structure[] structs = weaponsets.toArray(weaponsetCount);
547 
548                 for(int i=0; i<weaponsetCount; i++) {
549                     WeaponsetPointerByReference pstruct = (WeaponsetPointerByReference)structs[i];
550                     pstruct.weaponset = new WeaponsetStruct.ByRef();
551                     pstruct.weaponset.fillFrom(list.get(i));
552                 }
553             }
554         }
555 
556         /**
557          * Only use on native-owned structs!
558          * Calling this method on a Java-owned struct could cause garbage collection of referenced
559          * structures.
560          */
toWeaponsetList()561         public List<Weaponset> toWeaponsetList() {
562             if(weaponsetCount<=0) {
563                 return new ArrayList<Weaponset>();
564             } else {
565                 List<Weaponset> list = new ArrayList<Weaponset>(weaponsetCount);
566                 Structure[] structs = weaponsets.toArray(weaponsetCount);
567 
568                 for(int i=0; i<weaponsetCount; i++) {
569                     WeaponsetPointerByReference pstruct = (WeaponsetPointerByReference)structs[i];
570                     list.add(pstruct.weaponset.toWeaponset());
571                 }
572                 return list;
573             }
574         }
575 
576         public int weaponsetCount;
577         public WeaponsetPointerByReference weaponsets;
578     }
579 
580     static class RoomStruct extends Structure {
581         public static class ByVal extends RoomStruct implements Structure.ByValue {}
582         public static class ByRef extends RoomStruct implements Structure.ByReference {}
583 
RoomStruct()584         public RoomStruct() { super(); }
RoomStruct(Pointer ptr)585         public RoomStruct(Pointer ptr) { super(ptr); }
586 
587         @Override
getFieldOrder()588         protected List<String> getFieldOrder() {
589             return Arrays.asList("inProgress", "name", "playerCount", "teamCount", "owner", "map", "scheme", "weapons");
590         }
591 
toRoomlistRoom()592         public Room toRoomlistRoom() {
593             return new Room(name, map, scheme, weapons, owner, playerCount, teamCount, inProgress);
594         }
595 
596         public boolean inProgress;
597         public String name;
598         public int playerCount;
599         public int teamCount;
600         public String owner;
601         public String map;
602         public String scheme;
603         public String weapons;
604     }
605 
606     static class MapRecipeStruct extends Structure {
607         public static class ByVal extends MapRecipeStruct implements Structure.ByValue {}
608         public static class ByRef extends MapRecipeStruct implements Structure.ByReference {}
609 
MapRecipeStruct()610         public MapRecipeStruct() { super(); }
MapRecipeStruct(Pointer ptr)611         public MapRecipeStruct(Pointer ptr) { super(ptr); }
612 
613         @Override
getFieldOrder()614         protected List<String> getFieldOrder() {
615             return Arrays.asList("mapgen", "name", "seed", "theme", "drawData", "drawDataSize", "templateFilter", "mazeSize");
616         }
617 
fillFrom(MapRecipe map)618         public void fillFrom(MapRecipe map) {
619             mapgen = map.mapgen;
620             name = map.name;
621             seed = map.seed;
622             theme = map.theme;
623             byte[] buf = map.getDrawData();
624             drawData = ByteArrayPtr.createJavaOwned(buf);
625             drawDataSize = NativeSizeT.valueOf(buf==null ? 0 : buf.length);
626             templateFilter = map.templateFilter;
627             mazeSize = map.mazeSize;
628         }
629 
toMapRecipe()630         public MapRecipe toMapRecipe() {
631             byte[] buf = ByteArrayPtr.deref(drawData, drawDataSize.intValue());
632             return new MapRecipe(mapgen, templateFilter, mazeSize, name, seed, theme, buf);
633         }
634 
635         public int mapgen;
636         public String name;
637         public String seed;
638         public String theme;
639         public ByteArrayPtr drawData;
640         public NativeSizeT drawDataSize;
641         public int templateFilter;
642         public int mazeSize;
643     }
644 
645     static class MetaschemeSettingStruct extends Structure {
646         public static class ByVal extends MetaschemeSettingStruct implements Structure.ByValue {}
647         public static class ByRef extends MetaschemeSettingStruct implements Structure.ByReference {}
648 
MetaschemeSettingStruct()649         public MetaschemeSettingStruct() { super(); }
MetaschemeSettingStruct(Pointer ptr)650         public MetaschemeSettingStruct(Pointer ptr) { super(ptr); }
651 
652         @Override
getFieldOrder()653         protected List<String> getFieldOrder() {
654             return Arrays.asList("name", "engineCommand", "maxMeansInfinity", "times1000", "min", "max", "def");
655         }
656 
fillFrom(Setting setting)657         public void fillFrom(Setting setting) {
658             name = setting.name;
659             engineCommand = setting.engineCommand;
660             maxMeansInfinity = setting.maxMeansInfinity;
661             times1000 = setting.times1000;
662             min = setting.min;
663             max = setting.max;
664             def = setting.def;
665         }
666 
toMetaSchemeSetting()667         public MetaScheme.Setting toMetaSchemeSetting() {
668             return new MetaScheme.Setting(name, engineCommand, maxMeansInfinity, times1000, min, max, def);
669         }
670 
671         public String name;
672         public String engineCommand;
673         public boolean maxMeansInfinity;
674         public boolean times1000;
675         public int min;
676         public int max;
677         public int def;
678     }
679 
680     static class MetaschemeModStruct extends Structure {
681         public static class ByVal extends MetaschemeModStruct implements Structure.ByValue {}
682         public static class ByRef extends MetaschemeModStruct implements Structure.ByReference {}
683 
MetaschemeModStruct()684         public MetaschemeModStruct() { super(); }
MetaschemeModStruct(Pointer ptr)685         public MetaschemeModStruct(Pointer ptr) { super(ptr); }
686 
687         @Override
getFieldOrder()688         protected List<String> getFieldOrder() {
689             return Arrays.asList("name", "bitmaskIndex");
690         }
691 
fillFrom(Mod mod)692         public void fillFrom(Mod mod) {
693             name = mod.name;
694             bitmaskIndex = mod.bitmaskIndex;
695         }
696 
toMetaSchemeMod()697         public MetaScheme.Mod toMetaSchemeMod() {
698             return new MetaScheme.Mod(name, bitmaskIndex);
699         }
700 
701         public String name;
702         public int bitmaskIndex;
703 
704     }
705 
706     static class MetaschemeStruct extends Structure {
707         public static class ByVal extends MetaschemeStruct implements Structure.ByValue {}
708         public static class ByRef extends MetaschemeStruct implements Structure.ByReference {}
709 
MetaschemeStruct()710         public MetaschemeStruct() { super(); }
MetaschemeStruct(Pointer ptr)711         public MetaschemeStruct(Pointer ptr) { super(ptr); }
712 
713         @Override
getFieldOrder()714         protected List<String> getFieldOrder() {
715             return Arrays.asList("settingCount", "modCount", "settings", "mods");
716         }
717 
718         /**
719          * Only use on native-owned structs!
720          * Calling this method on a Java-owned struct could cause garbage collection of referenced
721          * structures.
722          */
toMetaScheme()723         public MetaScheme toMetaScheme() {
724             List<MetaScheme.Setting> settingList = new ArrayList<MetaScheme.Setting>(settingCount);
725             List<MetaScheme.Mod> modList = new ArrayList<MetaScheme.Mod>(modCount);
726 
727             Structure[] settingStructs = settings.toArray(settingCount);
728             Structure[] modStructs = mods.toArray(modCount);
729 
730             for(int i=0; i<settingCount; i++) {
731                 MetaschemeSettingStruct mss = (MetaschemeSettingStruct)settingStructs[i];
732                 settingList.add(mss.toMetaSchemeSetting());
733             }
734 
735             for(int i=0; i<modCount; i++) {
736                 MetaschemeModStruct mms = (MetaschemeModStruct)modStructs[i];
737                 modList.add(mms.toMetaSchemeMod());
738             }
739 
740             return new MetaScheme(modList, settingList);
741         }
742 
743         public int settingCount;
744         public int modCount;
745         public MetaschemeSettingStruct.ByRef settings;
746         public MetaschemeModStruct.ByRef mods;
747     }
748 
749     static class SchemeStruct extends Structure {
750         public static class ByVal extends SchemeStruct implements Structure.ByValue {}
751         public static class ByRef extends SchemeStruct implements Structure.ByReference {}
752 
SchemeStruct()753         public SchemeStruct() { super(); }
SchemeStruct(Pointer ptr)754         public SchemeStruct(Pointer ptr) { super(ptr); }
755 
756         @Override
getFieldOrder()757         protected List<String> getFieldOrder() {
758             return Arrays.asList("name", "settings", "mods");
759         }
760 
fillFrom(Scheme scheme)761         public void fillFrom(Scheme scheme) {
762             MetaScheme meta = MetaScheme.INSTANCE;
763             name = scheme.name;
764             settings = new Memory(AndroidTypeMapper.NATIVE_INT_SIZE * meta.settings.size());
765             for(int i=0; i<meta.settings.size(); i++) {
766                 Integer value = scheme.settings.get(meta.settings.get(i).name);
767                 settings.setInt(AndroidTypeMapper.NATIVE_INT_SIZE*i, value);
768             }
769             mods = new Memory(AndroidTypeMapper.NATIVE_BOOL_SIZE * meta.mods.size());
770             for(int i=0; i<meta.mods.size(); i++) {
771                 Boolean value = scheme.mods.get(meta.mods.get(i).name);
772                 mods.setByte(AndroidTypeMapper.NATIVE_BOOL_SIZE*i, (byte)(value ? 1 : 0));
773             }
774         }
775 
toScheme()776         public Scheme toScheme() {
777             Map<String, Integer> settingsMap = new HashMap<String, Integer>();
778             MetaScheme meta = MetaScheme.INSTANCE;
779             for(int i=0; i<meta.settings.size(); i++) {
780                 settingsMap.put(meta.settings.get(i).name, settings.getInt(AndroidTypeMapper.NATIVE_INT_SIZE*i));
781             }
782             Map<String, Boolean> modsMap = new HashMap<String, Boolean>();
783             for(int i=0; i<meta.mods.size(); i++) {
784                 modsMap.put(meta.mods.get(i).name, mods.getByte(i) != 0 ? Boolean.TRUE : Boolean.FALSE);
785             }
786             return new Scheme(name, settingsMap, modsMap);
787         }
788 
789         public String name;
790         public Pointer settings;
791         public Pointer mods;
792     }
793 
794     /**
795      * Represents a flib_scheme*, for use as part of a flib_scheme**
796      */
797     static class SchemePointerByReference extends Structure implements Structure.ByReference {
SchemePointerByReference()798         public SchemePointerByReference() { super(); }
SchemePointerByReference(Pointer ptr)799         public SchemePointerByReference(Pointer ptr) { super(ptr); }
800 
801         @Override
getFieldOrder()802         protected List<String> getFieldOrder() {
803             return Arrays.asList("scheme");
804         }
805 
806         public SchemeStruct.ByRef scheme;
807     }
808 
809     static class SchemelistStruct extends Structure {
810         public static class ByVal extends SchemelistStruct implements Structure.ByValue {}
811         public static class ByRef extends SchemelistStruct implements Structure.ByReference {}
812 
SchemelistStruct()813         public SchemelistStruct() { super(); }
SchemelistStruct(Pointer ptr)814         public SchemelistStruct(Pointer ptr) { super(ptr); }
815 
816         @Override
getFieldOrder()817         protected List<String> getFieldOrder() {
818             return Arrays.asList("schemeCount", "schemes");
819         }
820 
fillFrom(List<Scheme> schemeList)821         public void fillFrom(List<Scheme> schemeList) {
822             schemeCount = schemeList.size();
823             if(schemeCount<=0) {
824                 schemes = null;
825             } else {
826                 schemes = new SchemePointerByReference();
827                 Structure[] schemePtrStructs = schemes.toArray(schemeCount);
828 
829                 for(int i=0; i<this.schemeCount; i++) {
830                     SchemePointerByReference spbr = (SchemePointerByReference)schemePtrStructs[i];
831                     spbr.scheme = new SchemeStruct.ByRef();
832                     spbr.scheme.fillFrom(schemeList.get(i));
833                 }
834             }
835         }
836 
837         /**
838          * Only use on native-owned structs!
839          * Calling this method on a Java-owned struct could cause garbage collection of referenced
840          * structures.
841          */
toSchemeList()842         public List<Scheme> toSchemeList() {
843             if(schemeCount<=0) {
844                 return new ArrayList<Scheme>();
845             } else {
846                 List<Scheme> schemeList = new ArrayList<Scheme>(schemeCount);
847 
848                 Structure[] schemePtrStructs = schemes.toArray(schemeCount);
849 
850                 for(int i=0; i<schemeCount; i++) {
851                     SchemePointerByReference spbr2 = (SchemePointerByReference)schemePtrStructs[i];
852                     schemeList.add(spbr2.scheme.toScheme());
853                 }
854                 return schemeList;
855             }
856         }
857 
858         public int schemeCount;
859         public SchemePointerByReference schemes;
860     }
861 
862     /**
863      * Represents a flib_team*, for use as part of a flib_team**
864      */
865     static class TeamPointerByReference extends Structure implements Structure.ByReference {
TeamPointerByReference()866         public TeamPointerByReference() { super(); }
TeamPointerByReference(Pointer ptr)867         public TeamPointerByReference(Pointer ptr) { super(ptr); }
868 
869         @Override
getFieldOrder()870         protected List<String> getFieldOrder() {
871             return Arrays.asList("team");
872         }
873 
874         public TeamStruct.ByRef team;
875     }
876 
877     static class TeamlistStruct extends Structure {
878         public static class ByVal extends TeamlistStruct implements Structure.ByValue {}
879         public static class ByRef extends TeamlistStruct implements Structure.ByReference {}
880 
TeamlistStruct()881         public TeamlistStruct() { super(); }
TeamlistStruct(Pointer ptr)882         public TeamlistStruct(Pointer ptr) { super(ptr); }
883 
884         @Override
getFieldOrder()885         protected List<String> getFieldOrder() {
886             return Arrays.asList("teamCount", "teams");
887         }
888 
fillFrom(List<TeamInGame> teamList, WeaponsetStruct.ByRef weaponset, int initialHealth)889         public void fillFrom(List<TeamInGame> teamList, WeaponsetStruct.ByRef weaponset, int initialHealth) {
890             teamCount = teamList.size();
891             if(teamCount <= 0) {
892                 teams = null;
893             } else {
894                 teams = new TeamPointerByReference();
895                 Structure[] teamPtrStructs = teams.toArray(teamCount);
896 
897                 for(int i=0; i<this.teamCount; i++) {
898                     TeamPointerByReference tpbr = (TeamPointerByReference)teamPtrStructs[i];
899                     tpbr.team = new TeamStruct.ByRef();
900                     tpbr.team.fillFrom(teamList.get(i), weaponset, initialHealth);
901                 }
902             }
903         }
904 
toTeamInGameList()905         public List<TeamInGame> toTeamInGameList() {
906             if(teamCount<=0) {
907                 return new ArrayList<TeamInGame>();
908             } else {
909                 List<TeamInGame> result = new ArrayList<TeamInGame>(teamCount);
910                 Structure[] structs = teams.toArray(teamCount);
911 
912                 for(int i=0; i<teamCount; i++) {
913                     TeamPointerByReference struct = (TeamPointerByReference)structs[i];
914                     result.add(struct.team.toTeamInGame());
915                 }
916                 return result;
917             }
918         }
919 
920         public int teamCount;
921         public TeamPointerByReference teams;
922     }
923 
924     static class GameSetupStruct extends Structure {
925         public static class ByVal extends GameSetupStruct implements Structure.ByValue {}
926         public static class ByRef extends GameSetupStruct implements Structure.ByReference {}
927 
GameSetupStruct()928         public GameSetupStruct() { super(); }
GameSetupStruct(Pointer ptr)929         public GameSetupStruct(Pointer ptr) { super(ptr); }
930 
931         @Override
getFieldOrder()932         protected List<String> getFieldOrder() {
933             return Arrays.asList("script", "gamescheme", "map", "teamlist");
934         }
935 
fillFrom(GameConfig conf)936         public void fillFrom(GameConfig conf) {
937             script = conf.style;
938             gamescheme = new SchemeStruct.ByRef();
939             gamescheme.fillFrom(conf.scheme);
940             map = new MapRecipeStruct.ByRef();
941             map.fillFrom(conf.map);
942 
943             /*
944              * At this point we deviate from the usual copying pattern because the frontlib
945              * expects per-hog weapons and initial health, but the UI models them as per-
946              * game, so we extract them from the config here and pass them on to be included
947              * in each hog.
948              */
949             WeaponsetStruct.ByRef wss = new WeaponsetStruct.ByRef();
950             wss.fillFrom(conf.weaponset);
951             int initialHealth = conf.scheme.getHealth();
952 
953             teamlist = new TeamlistStruct.ByRef();
954             teamlist.fillFrom(conf.teams, wss, initialHealth);
955         }
956 
toGameConfig()957         public GameConfig toGameConfig() {
958             Scheme scheme = gamescheme != null ? gamescheme.toScheme() : null;
959             MapRecipe mapRecipe = map != null ? map.toMapRecipe() : null;
960             List<TeamInGame> teams = teamlist != null ? teamlist.toTeamInGameList() : null;
961 
962             WeaponsetStruct weaponsetStruct = teamlist != null && teamlist.teamCount>0 ? teamlist.teams.team.hogs[0].weaponset : null;
963             Weaponset weaponset = weaponsetStruct != null ? weaponsetStruct.toWeaponset() : null;
964             return new GameConfig(script, scheme, mapRecipe, teams, weaponset);
965         }
966 
967         public String script;
968         public SchemeStruct.ByRef gamescheme;
969         public MapRecipeStruct.ByRef map;
970         public TeamlistStruct.ByRef teamlist;
971     }
972 
973     /*
974      * Callback interfaces. The context parameter is never needed here and
975      * should always be ignored. Be sure to keep a reference to each callback
976      * for as long as they might be called by native code, to avoid premature
977      * garbage collection.
978      */
979     public static interface VoidCallback extends Callback {
callback(Pointer context)980         void callback(Pointer context);
981     }
982 
983     public static interface StrCallback extends Callback {
callback(Pointer context, String arg1)984         void callback(Pointer context, String arg1);
985     }
986 
987     public static interface IntCallback extends Callback {
callback(Pointer context, int arg1)988         void callback(Pointer context, int arg1);
989     }
990 
991     public static interface IntStrCallback extends Callback {
callback(Pointer context, int arg1, String arg2)992         void callback(Pointer context, int arg1, String arg2);
993     }
994 
995     public static interface StrIntCallback extends Callback {
callback(Pointer context, String arg1, int arg2)996         void callback(Pointer context, String arg1, int arg2);
997     }
998 
999     public static interface StrStrCallback extends Callback {
callback(Pointer context, String arg1, String arg2)1000         void callback(Pointer context, String arg1, String arg2);
1001     }
1002 
1003     public static interface StrStrBoolCallback extends Callback {
callback(Pointer context, String arg1, String arg2, boolean arg3)1004         void callback(Pointer context, String arg1, String arg2, boolean arg3);
1005     }
1006 
1007     public static interface RoomCallback extends Callback {
callback(Pointer context, RoomPtr arg1)1008         void callback(Pointer context, RoomPtr arg1);
1009     }
1010 
1011     public static interface RoomListCallback extends Callback {
callback(Pointer context, RoomArrayPtr arg1, int count)1012         void callback(Pointer context, RoomArrayPtr arg1, int count);
1013     }
1014 
1015     public static interface StrRoomCallback extends Callback {
callback(Pointer context, String arg1, RoomPtr arg2)1016         void callback(Pointer context, String arg1, RoomPtr arg2);
1017     }
1018 
1019     public static interface BoolCallback extends Callback {
callback(Pointer context, boolean arg1)1020         void callback(Pointer context, boolean arg1);
1021     }
1022 
1023     public static interface StrBoolCallback extends Callback {
callback(Pointer context, String arg1, boolean arg2)1024         void callback(Pointer context, String arg1, boolean arg2);
1025     }
1026 
1027     public static interface TeamCallback extends Callback {
callback(Pointer context, TeamPtr arg1)1028         void callback(Pointer context, TeamPtr arg1);
1029     }
1030 
1031     public static interface BytesCallback extends Callback {
callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size)1032         void callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size);
1033     }
1034 
1035     public static interface BytesBoolCallback extends Callback {
callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size, boolean arg3)1036         void callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size, boolean arg3);
1037     }
1038 
1039     public static interface SchemeCallback extends Callback {
callback(Pointer context, SchemePtr arg1)1040         void callback(Pointer context, SchemePtr arg1);
1041     }
1042 
1043     public static interface MapIntCallback extends Callback {
callback(Pointer context, MapRecipePtr arg1, int arg2)1044         void callback(Pointer context, MapRecipePtr arg1, int arg2);
1045     }
1046 
1047     public static interface WeaponsetCallback extends Callback {
callback(Pointer context, WeaponsetPtr arg1)1048         void callback(Pointer context, WeaponsetPtr arg1);
1049     }
1050 
1051     public static interface MapimageCallback extends Callback {
callback(Pointer context, ByteArrayPtr buffer, int hedgehogCount)1052         void callback(Pointer context, ByteArrayPtr buffer, int hedgehogCount);
1053     }
1054 
1055     public static interface LogCallback extends Callback {
callback(int level, String logMessage)1056         void callback(int level, String logMessage);
1057     }
1058 
1059     // frontlib.h
flib_init()1060     int flib_init();
flib_quit()1061     void flib_quit();
1062 
1063     // hwconsts.h
flib_get_teamcolor(int colorIndex)1064     int flib_get_teamcolor(int colorIndex);
flib_get_teamcolor_count()1065     int flib_get_teamcolor_count();
flib_get_hedgehogs_per_team()1066     int flib_get_hedgehogs_per_team();
flib_get_weapons_count()1067     int flib_get_weapons_count();
flib_get_metascheme()1068     MetaschemePtr flib_get_metascheme();
1069 
1070     // net/netconn.h
1071     static final int NETCONN_STATE_CONNECTING = 0;
1072     static final int NETCONN_STATE_LOBBY = 1;
1073     static final int NETCONN_STATE_ROOM = 2;
1074     static final int NETCONN_STATE_DISCONNECTED = 10;
1075 
1076     static final int NETCONN_DISCONNECT_NORMAL = 0;
1077     static final int NETCONN_DISCONNECT_SERVER_TOO_OLD = 1;
1078     static final int NETCONN_DISCONNECT_AUTH_FAILED = 2;
1079     static final int NETCONN_DISCONNECT_CONNLOST = 3;
1080     static final int NETCONN_DISCONNECT_INTERNAL_ERROR = 100;
1081 
1082     static final int NETCONN_ROOMLEAVE_ABANDONED = 0;
1083     static final int NETCONN_ROOMLEAVE_KICKED = 1;
1084 
1085     static final int NETCONN_MSG_TYPE_PLAYERINFO = 0;
1086     static final int NETCONN_MSG_TYPE_SERVERMESSAGE = 1;
1087     static final int NETCONN_MSG_TYPE_WARNING = 2;
1088     static final int NETCONN_MSG_TYPE_ERROR = 3;
1089 
1090     static final int NETCONN_MAPCHANGE_FULL = 0;
1091     static final int NETCONN_MAPCHANGE_MAP = 1;
1092     static final int NETCONN_MAPCHANGE_MAPGEN = 2;
1093     static final int NETCONN_MAPCHANGE_DRAWNMAP = 3;
1094     static final int NETCONN_MAPCHANGE_MAZE_SIZE = 4;
1095     static final int NETCONN_MAPCHANGE_TEMPLATE = 5;
1096     static final int NETCONN_MAPCHANGE_THEME = 6;
1097     static final int NETCONN_MAPCHANGE_SEED = 7;
1098 
flib_netconn_create(String playerName, String dataDirPath, String host, int port)1099     NetconnPtr flib_netconn_create(String playerName, String dataDirPath, String host, int port);
flib_netconn_destroy(NetconnPtr conn)1100     void flib_netconn_destroy(NetconnPtr conn);
1101 
flib_netconn_tick(NetconnPtr conn)1102     void flib_netconn_tick(NetconnPtr conn);
flib_netconn_is_chief(NetconnPtr conn)1103     boolean flib_netconn_is_chief(NetconnPtr conn);
flib_netconn_get_playername(NetconnPtr conn)1104     String flib_netconn_get_playername(NetconnPtr conn);
flib_netconn_create_gamesetup(NetconnPtr conn)1105     GameSetupPtr flib_netconn_create_gamesetup(NetconnPtr conn);
flib_netconn_send_quit(NetconnPtr conn, String quitmsg)1106     int flib_netconn_send_quit(NetconnPtr conn, String quitmsg);
flib_netconn_send_chat(NetconnPtr conn, String chat)1107     int flib_netconn_send_chat(NetconnPtr conn, String chat);
flib_netconn_send_teamchat(NetconnPtr conn, String msg)1108     int flib_netconn_send_teamchat(NetconnPtr conn, String msg);
flib_netconn_send_password(NetconnPtr conn, String passwd)1109     int flib_netconn_send_password(NetconnPtr conn, String passwd);
flib_netconn_send_nick(NetconnPtr conn, String nick)1110     int flib_netconn_send_nick(NetconnPtr conn, String nick);
flib_netconn_send_request_roomlist(NetconnPtr conn)1111     int flib_netconn_send_request_roomlist(NetconnPtr conn);
flib_netconn_send_joinRoom(NetconnPtr conn, String room)1112     int flib_netconn_send_joinRoom(NetconnPtr conn, String room);
flib_netconn_send_createRoom(NetconnPtr conn, String room)1113     int flib_netconn_send_createRoom(NetconnPtr conn, String room);
flib_netconn_send_renameRoom(NetconnPtr conn, String roomName)1114     int flib_netconn_send_renameRoom(NetconnPtr conn, String roomName);
flib_netconn_send_leaveRoom(NetconnPtr conn, String msg)1115     int flib_netconn_send_leaveRoom(NetconnPtr conn, String msg);
flib_netconn_send_toggleReady(NetconnPtr conn)1116     int flib_netconn_send_toggleReady(NetconnPtr conn);
flib_netconn_send_addTeam(NetconnPtr conn, TeamPtr team)1117     int flib_netconn_send_addTeam(NetconnPtr conn, TeamPtr team);
flib_netconn_send_removeTeam(NetconnPtr conn, String teamname)1118     int flib_netconn_send_removeTeam(NetconnPtr conn, String teamname);
flib_netconn_send_engineMessage(NetconnPtr conn, ByteArrayPtr message, NativeSizeT size)1119     int flib_netconn_send_engineMessage(NetconnPtr conn, ByteArrayPtr message, NativeSizeT size);
flib_netconn_send_teamHogCount(NetconnPtr conn, String teamname, int hogcount)1120     int flib_netconn_send_teamHogCount(NetconnPtr conn, String teamname, int hogcount);
flib_netconn_send_teamColor(NetconnPtr conn, String teamname, int colorIndex)1121     int flib_netconn_send_teamColor(NetconnPtr conn, String teamname, int colorIndex);
flib_netconn_send_weaponset(NetconnPtr conn, WeaponsetPtr weaponset)1122     int flib_netconn_send_weaponset(NetconnPtr conn, WeaponsetPtr weaponset);
flib_netconn_send_map(NetconnPtr conn, MapRecipePtr map)1123     int flib_netconn_send_map(NetconnPtr conn, MapRecipePtr map);
flib_netconn_send_mapName(NetconnPtr conn, String mapName)1124     int flib_netconn_send_mapName(NetconnPtr conn, String mapName);
flib_netconn_send_mapGen(NetconnPtr conn, int mapGen)1125     int flib_netconn_send_mapGen(NetconnPtr conn, int mapGen);
flib_netconn_send_mapTemplate(NetconnPtr conn, int templateFilter)1126     int flib_netconn_send_mapTemplate(NetconnPtr conn, int templateFilter);
flib_netconn_send_mapMazeSize(NetconnPtr conn, int mazeSize)1127     int flib_netconn_send_mapMazeSize(NetconnPtr conn, int mazeSize);
flib_netconn_send_mapSeed(NetconnPtr conn, String seed)1128     int flib_netconn_send_mapSeed(NetconnPtr conn, String seed);
flib_netconn_send_mapTheme(NetconnPtr conn, String theme)1129     int flib_netconn_send_mapTheme(NetconnPtr conn, String theme);
flib_netconn_send_mapDrawdata(NetconnPtr conn, ByteArrayPtr drawData, NativeSizeT size)1130     int flib_netconn_send_mapDrawdata(NetconnPtr conn, ByteArrayPtr drawData, NativeSizeT size);
flib_netconn_send_script(NetconnPtr conn, String scriptName)1131     int flib_netconn_send_script(NetconnPtr conn, String scriptName);
flib_netconn_send_scheme(NetconnPtr conn, SchemePtr scheme)1132     int flib_netconn_send_scheme(NetconnPtr conn, SchemePtr scheme);
flib_netconn_send_roundfinished(NetconnPtr conn, boolean withoutError)1133     int flib_netconn_send_roundfinished(NetconnPtr conn, boolean withoutError);
flib_netconn_send_ban(NetconnPtr conn, String playerName)1134     int flib_netconn_send_ban(NetconnPtr conn, String playerName);
flib_netconn_send_kick(NetconnPtr conn, String playerName)1135     int flib_netconn_send_kick(NetconnPtr conn, String playerName);
flib_netconn_send_playerInfo(NetconnPtr conn, String playerName)1136     int flib_netconn_send_playerInfo(NetconnPtr conn, String playerName);
flib_netconn_send_playerFollow(NetconnPtr conn, String playerName)1137     int flib_netconn_send_playerFollow(NetconnPtr conn, String playerName);
flib_netconn_send_startGame(NetconnPtr conn)1138     int flib_netconn_send_startGame(NetconnPtr conn);
flib_netconn_send_toggleRestrictJoins(NetconnPtr conn)1139     int flib_netconn_send_toggleRestrictJoins(NetconnPtr conn);
flib_netconn_send_toggleRestrictTeams(NetconnPtr conn)1140     int flib_netconn_send_toggleRestrictTeams(NetconnPtr conn);
flib_netconn_send_clearAccountsCache(NetconnPtr conn)1141     int flib_netconn_send_clearAccountsCache(NetconnPtr conn);
flib_netconn_send_setServerVar(NetconnPtr conn, String name, String value)1142     int flib_netconn_send_setServerVar(NetconnPtr conn, String name, String value);
flib_netconn_send_getServerVars(NetconnPtr conn)1143     int flib_netconn_send_getServerVars(NetconnPtr conn);
1144 
flib_netconn_onMessage(NetconnPtr conn, IntStrCallback callback, Pointer context)1145     void flib_netconn_onMessage(NetconnPtr conn, IntStrCallback callback, Pointer context);
flib_netconn_onClientFlags(NetconnPtr conn, StrStrBoolCallback callback, Pointer context)1146     void flib_netconn_onClientFlags(NetconnPtr conn, StrStrBoolCallback callback, Pointer context);
flib_netconn_onChat(NetconnPtr conn, StrStrCallback callback, Pointer context)1147     void flib_netconn_onChat(NetconnPtr conn, StrStrCallback callback, Pointer context);
flib_netconn_onConnected(NetconnPtr conn, VoidCallback callback, Pointer context)1148     void flib_netconn_onConnected(NetconnPtr conn, VoidCallback callback, Pointer context);
flib_netconn_onDisconnected(NetconnPtr conn, IntStrCallback callback, Pointer context)1149     void flib_netconn_onDisconnected(NetconnPtr conn, IntStrCallback callback, Pointer context);
flib_netconn_onRoomlist(NetconnPtr conn, RoomListCallback callback, Pointer context)1150     void flib_netconn_onRoomlist(NetconnPtr conn, RoomListCallback callback, Pointer context);
flib_netconn_onRoomAdd(NetconnPtr conn, RoomCallback callback, Pointer context)1151     void flib_netconn_onRoomAdd(NetconnPtr conn, RoomCallback callback, Pointer context);
flib_netconn_onRoomDelete(NetconnPtr conn, StrCallback callback, Pointer context)1152     void flib_netconn_onRoomDelete(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onRoomUpdate(NetconnPtr conn, StrRoomCallback callback, Pointer context)1153     void flib_netconn_onRoomUpdate(NetconnPtr conn, StrRoomCallback callback, Pointer context);
flib_netconn_onLobbyJoin(NetconnPtr conn, StrCallback callback, Pointer context)1154     void flib_netconn_onLobbyJoin(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onLobbyLeave(NetconnPtr conn, StrStrCallback callback, Pointer context)1155     void flib_netconn_onLobbyLeave(NetconnPtr conn, StrStrCallback callback, Pointer context);
flib_netconn_onNickTaken(NetconnPtr conn, StrCallback callback, Pointer context)1156     void flib_netconn_onNickTaken(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onPasswordRequest(NetconnPtr conn, StrCallback callback, Pointer context)1157     void flib_netconn_onPasswordRequest(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onEnterRoom(NetconnPtr conn, BoolCallback callback, Pointer context)1158     void flib_netconn_onEnterRoom(NetconnPtr conn, BoolCallback callback, Pointer context);
flib_netconn_onLeaveRoom(NetconnPtr conn, IntStrCallback callback, Pointer context)1159     void flib_netconn_onLeaveRoom(NetconnPtr conn, IntStrCallback callback, Pointer context);
flib_netconn_onTeamAdd(NetconnPtr conn, TeamCallback callback, Pointer context)1160     void flib_netconn_onTeamAdd(NetconnPtr conn, TeamCallback callback, Pointer context);
flib_netconn_onTeamDelete(NetconnPtr conn, StrCallback callback, Pointer context)1161     void flib_netconn_onTeamDelete(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onRoomJoin(NetconnPtr conn, StrCallback callback, Pointer context)1162     void flib_netconn_onRoomJoin(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onRoomLeave(NetconnPtr conn, StrStrCallback callback, Pointer context)1163     void flib_netconn_onRoomLeave(NetconnPtr conn, StrStrCallback callback, Pointer context);
flib_netconn_onRunGame(NetconnPtr conn, VoidCallback callback, Pointer context)1164     void flib_netconn_onRunGame(NetconnPtr conn, VoidCallback callback, Pointer context);
flib_netconn_onTeamAccepted(NetconnPtr conn, StrCallback callback, Pointer context)1165     void flib_netconn_onTeamAccepted(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onHogCountChanged(NetconnPtr conn, StrIntCallback callback, Pointer context)1166     void flib_netconn_onHogCountChanged(NetconnPtr conn, StrIntCallback callback, Pointer context);
flib_netconn_onTeamColorChanged(NetconnPtr conn, StrIntCallback callback, Pointer context)1167     void flib_netconn_onTeamColorChanged(NetconnPtr conn, StrIntCallback callback, Pointer context);
flib_netconn_onEngineMessage(NetconnPtr conn, BytesCallback callback, Pointer context)1168     void flib_netconn_onEngineMessage(NetconnPtr conn, BytesCallback callback, Pointer context);
flib_netconn_onSchemeChanged(NetconnPtr conn, SchemeCallback callback, Pointer context)1169     void flib_netconn_onSchemeChanged(NetconnPtr conn, SchemeCallback callback, Pointer context);
flib_netconn_onMapChanged(NetconnPtr conn, MapIntCallback callback, Pointer context)1170     void flib_netconn_onMapChanged(NetconnPtr conn, MapIntCallback callback, Pointer context);
flib_netconn_onScriptChanged(NetconnPtr conn, StrCallback callback, Pointer context)1171     void flib_netconn_onScriptChanged(NetconnPtr conn, StrCallback callback, Pointer context);
flib_netconn_onWeaponsetChanged(NetconnPtr conn, WeaponsetCallback callback, Pointer context)1172     void flib_netconn_onWeaponsetChanged(NetconnPtr conn, WeaponsetCallback callback, Pointer context);
flib_netconn_onServerVar(NetconnPtr conn, StrStrCallback callback, Pointer context)1173     void flib_netconn_onServerVar(NetconnPtr conn, StrStrCallback callback, Pointer context);
1174 
1175     // ipc/gameconn.h
1176     static final int GAME_END_FINISHED = 0;
1177     static final int GAME_END_INTERRUPTED = 1;
1178     static final int GAME_END_HALTED = 2;
1179     static final int GAME_END_ERROR = 3;
1180 
flib_gameconn_create(String playerName, GameSetupPtr setup, boolean netgame)1181     GameconnPtr flib_gameconn_create(String playerName, GameSetupPtr setup, boolean netgame);
flib_gameconn_create_playdemo(ByteArrayPtr demo, NativeSizeT size)1182     GameconnPtr flib_gameconn_create_playdemo(ByteArrayPtr demo, NativeSizeT size);
flib_gameconn_create_loadgame(String playerName, ByteArrayPtr save, NativeSizeT size)1183     GameconnPtr flib_gameconn_create_loadgame(String playerName, ByteArrayPtr save, NativeSizeT size);
flib_gameconn_create_campaign(String playerName, String seed, String script)1184     GameconnPtr flib_gameconn_create_campaign(String playerName, String seed, String script);
1185 
flib_gameconn_destroy(GameconnPtr conn)1186     void flib_gameconn_destroy(GameconnPtr conn);
flib_gameconn_getport(GameconnPtr conn)1187     int flib_gameconn_getport(GameconnPtr conn);
flib_gameconn_tick(GameconnPtr conn)1188     void flib_gameconn_tick(GameconnPtr conn);
1189 
flib_gameconn_send_enginemsg(GameconnPtr conn, ByteArrayPtr data, NativeSizeT len)1190     int flib_gameconn_send_enginemsg(GameconnPtr conn, ByteArrayPtr data, NativeSizeT len);
flib_gameconn_send_textmsg(GameconnPtr conn, int msgtype, String msg)1191     int flib_gameconn_send_textmsg(GameconnPtr conn, int msgtype, String msg);
flib_gameconn_send_chatmsg(GameconnPtr conn, String playername, String msg)1192     int flib_gameconn_send_chatmsg(GameconnPtr conn, String playername, String msg);
flib_gameconn_send_quit(GameconnPtr conn)1193     int flib_gameconn_send_quit(GameconnPtr conn);
flib_gameconn_send_cmd(GameconnPtr conn, String cmdString)1194     int flib_gameconn_send_cmd(GameconnPtr conn, String cmdString);
1195 
flib_gameconn_onConnect(GameconnPtr conn, VoidCallback callback, Pointer context)1196     void flib_gameconn_onConnect(GameconnPtr conn, VoidCallback callback, Pointer context);
flib_gameconn_onDisconnect(GameconnPtr conn, IntCallback callback, Pointer context)1197     void flib_gameconn_onDisconnect(GameconnPtr conn, IntCallback callback, Pointer context);
flib_gameconn_onErrorMessage(GameconnPtr conn, StrCallback callback, Pointer context)1198     void flib_gameconn_onErrorMessage(GameconnPtr conn, StrCallback callback, Pointer context);
flib_gameconn_onChat(GameconnPtr conn, StrBoolCallback callback, Pointer context)1199     void flib_gameconn_onChat(GameconnPtr conn, StrBoolCallback callback, Pointer context);
flib_gameconn_onGameRecorded(GameconnPtr conn, BytesBoolCallback callback, Pointer context)1200     void flib_gameconn_onGameRecorded(GameconnPtr conn, BytesBoolCallback callback, Pointer context);
flib_gameconn_onEngineMessage(GameconnPtr conn, BytesCallback callback, Pointer context)1201     void flib_gameconn_onEngineMessage(GameconnPtr conn, BytesCallback callback, Pointer context);
1202 
1203     // ipc/mapconn.h
1204     public static final int MAPIMAGE_WIDTH = 256;
1205     public static final int MAPIMAGE_HEIGHT = 128;
1206     public static final int MAPIMAGE_BYTES = (MAPIMAGE_WIDTH/8*MAPIMAGE_HEIGHT);
1207 
flib_mapconn_create(MapRecipePtr mapdesc)1208     MapconnPtr flib_mapconn_create(MapRecipePtr mapdesc);
flib_mapconn_destroy(MapconnPtr conn)1209     void flib_mapconn_destroy(MapconnPtr conn);
flib_mapconn_getport(MapconnPtr conn)1210     int flib_mapconn_getport(MapconnPtr conn);
flib_mapconn_onSuccess(MapconnPtr conn, MapimageCallback callback, Pointer context)1211     void flib_mapconn_onSuccess(MapconnPtr conn, MapimageCallback callback, Pointer context);
flib_mapconn_onFailure(MapconnPtr conn, StrCallback callback, Pointer context)1212     void flib_mapconn_onFailure(MapconnPtr conn, StrCallback callback, Pointer context);
flib_mapconn_tick(MapconnPtr conn)1213     void flib_mapconn_tick(MapconnPtr conn);
1214 
1215     // model/map.h
1216     public static final int MAPGEN_REGULAR = 0;
1217     public static final int MAPGEN_MAZE = 1;
1218     public static final int MAPGEN_DRAWN = 2;
1219     public static final int MAPGEN_NAMED = 3;
1220 
1221     public static final int TEMPLATEFILTER_ALL = 0;
1222     public static final int TEMPLATEFILTER_SMALL = 1;
1223     public static final int TEMPLATEFILTER_MEDIUM = 2;
1224     public static final int TEMPLATEFILTER_LARGE = 3;
1225     public static final int TEMPLATEFILTER_CAVERN = 4;
1226     public static final int TEMPLATEFILTER_WACKY = 5;
1227 
1228     public static final int MAZE_SIZE_SMALL_TUNNELS = 0;
1229     public static final int MAZE_SIZE_MEDIUM_TUNNELS = 1;
1230     public static final int MAZE_SIZE_LARGE_TUNNELS = 2;
1231     public static final int MAZE_SIZE_SMALL_ISLANDS = 3;
1232     public static final int MAZE_SIZE_MEDIUM_ISLANDS = 4;
1233     public static final int MAZE_SIZE_LARGE_ISLANDS = 5;
1234 
1235     // model/schemelist.h
flib_schemelist_from_ini(String filename)1236     SchemelistPtr flib_schemelist_from_ini(String filename);
flib_schemelist_to_ini(String filename, SchemelistPtr list)1237     int flib_schemelist_to_ini(String filename, SchemelistPtr list);
flib_schemelist_destroy(SchemelistPtr list)1238     void flib_schemelist_destroy(SchemelistPtr list);
1239 
1240     // model/team.h
flib_team_from_ini(String filename)1241     TeamPtr flib_team_from_ini(String filename);
flib_team_to_ini(String filename, TeamPtr team)1242     int flib_team_to_ini(String filename, TeamPtr team);
flib_team_destroy(TeamPtr team)1243     void flib_team_destroy(TeamPtr team);
1244 
1245     // model/weapon.h
flib_weaponsetlist_from_ini(String filename)1246     WeaponsetListPtr flib_weaponsetlist_from_ini(String filename);
flib_weaponsetlist_to_ini(String filename, WeaponsetListPtr weaponsets)1247     int flib_weaponsetlist_to_ini(String filename, WeaponsetListPtr weaponsets);
flib_weaponsetlist_destroy(WeaponsetListPtr list)1248     void flib_weaponsetlist_destroy(WeaponsetListPtr list);
1249 
1250     // model/gamesetup.h
flib_gamesetup_destroy(GameSetupPtr gamesetup)1251     void flib_gamesetup_destroy(GameSetupPtr gamesetup);
1252 
1253     // util/logging.h
1254     public static final int FLIB_LOGLEVEL_ALL = -100;
1255     public static final int FLIB_LOGLEVEL_DEBUG = -1;
1256     public static final int FLIB_LOGLEVEL_INFO = 0;
1257     public static final int FLIB_LOGLEVEL_WARNING = 1;
1258     public static final int FLIB_LOGLEVEL_ERROR = 2;
1259     public static final int FLIB_LOGLEVEL_NONE = 100;
1260 
flib_log_setLevel(int level)1261     void flib_log_setLevel(int level);
flib_log_setCallback(LogCallback callback)1262     void flib_log_setCallback(LogCallback callback);
1263 }