|
1 package org.hedgewars.hedgeroid.frontlib; |
|
2 import java.nio.Buffer; |
|
3 import java.util.ArrayList; |
|
4 import java.util.HashMap; |
|
5 import java.util.List; |
|
6 import java.util.Map; |
|
7 |
|
8 import org.hedgewars.hedgeroid.Datastructures.Hog; |
|
9 import org.hedgewars.hedgeroid.Datastructures.MetaScheme; |
|
10 import org.hedgewars.hedgeroid.Datastructures.MetaScheme.Mod; |
|
11 import org.hedgewars.hedgeroid.Datastructures.MetaScheme.Setting; |
|
12 import org.hedgewars.hedgeroid.Datastructures.RoomlistRoom; |
|
13 import org.hedgewars.hedgeroid.Datastructures.Scheme; |
|
14 import org.hedgewars.hedgeroid.Datastructures.Team; |
|
15 import org.hedgewars.hedgeroid.Datastructures.TeamInGame; |
|
16 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes; |
|
17 import org.hedgewars.hedgeroid.EngineProtocol.GameConfig; |
|
18 |
|
19 import com.sun.jna.Callback; |
|
20 import com.sun.jna.Library; |
|
21 import com.sun.jna.Memory; |
|
22 import com.sun.jna.NativeLong; |
|
23 import com.sun.jna.Pointer; |
|
24 import com.sun.jna.PointerType; |
|
25 import com.sun.jna.Structure; |
|
26 |
|
27 public interface Frontlib extends Library { |
|
28 static final int NATIVE_INT_SIZE = 4; |
|
29 static final int NATIVE_BOOL_SIZE = 1; |
|
30 |
|
31 static final int NETCONN_STATE_CONNECTING = 0; |
|
32 static final int NETCONN_STATE_LOBBY = 1; |
|
33 static final int NETCONN_STATE_ROOM = 2; |
|
34 static final int NETCONN_STATE_INGAME = 3; |
|
35 static final int NETCONN_STATE_DISCONNECTED = 10; |
|
36 |
|
37 static final int NETCONN_DISCONNECT_NORMAL = 0; |
|
38 static final int NETCONN_DISCONNECT_SERVER_TOO_OLD = 1; |
|
39 static final int NETCONN_DISCONNECT_AUTH_FAILED = 2; |
|
40 static final int NETCONN_DISCONNECT_CONNLOST = 3; |
|
41 static final int NETCONN_DISCONNECT_INTERNAL_ERROR = 100; |
|
42 |
|
43 static final int NETCONN_ROOMLEAVE_ABANDONED = 0; |
|
44 static final int NETCONN_ROOMLEAVE_KICKED = 1; |
|
45 |
|
46 static final int NETCONN_MSG_TYPE_PLAYERINFO = 0; |
|
47 static final int NETCONN_MSG_TYPE_SERVERMESSAGE = 1; |
|
48 static final int NETCONN_MSG_TYPE_WARNING = 2; |
|
49 static final int NETCONN_MSG_TYPE_ERROR = 3; |
|
50 |
|
51 static final int NETCONN_MAPCHANGE_FULL = 0; |
|
52 static final int NETCONN_MAPCHANGE_MAP = 1; |
|
53 static final int NETCONN_MAPCHANGE_MAPGEN = 2; |
|
54 static final int NETCONN_MAPCHANGE_DRAWNMAP = 3; |
|
55 static final int NETCONN_MAPCHANGE_MAZE_SIZE = 4; |
|
56 static final int NETCONN_MAPCHANGE_TEMPLATE = 5; |
|
57 static final int NETCONN_MAPCHANGE_THEME = 6; |
|
58 static final int NETCONN_MAPCHANGE_SEED = 7; |
|
59 |
|
60 static final int GAME_END_FINISHED = 0; |
|
61 static final int GAME_END_INTERRUPTED = 1; |
|
62 static final int GAME_END_HALTED = 2; |
|
63 static final int GAME_END_ERROR = 3; |
|
64 |
|
65 static final int HEDGEHOGS_PER_TEAM = 8; |
|
66 |
|
67 public static class NetconnPtr extends PointerType { } |
|
68 public static class MapconnPtr extends PointerType { } |
|
69 public static class GameconnPtr extends PointerType { } |
|
70 public static class MetaschemePtr extends PointerType { } |
|
71 |
|
72 public static class RoomArrayPtr extends PointerType { |
|
73 /** |
|
74 * Returns the (native-owned) rooms in this list |
|
75 */ |
|
76 public RoomlistRoom[] getRooms(int count) { |
|
77 Pointer ptr = getPointer(); |
|
78 if(ptr == null) { |
|
79 return new RoomlistRoom[0]; |
|
80 } |
|
81 Pointer[] untypedPtrs = ptr.getPointerArray(0, count); |
|
82 RoomlistRoom[] result = new RoomlistRoom[count]; |
|
83 for(int i=0; i<count; i++) { |
|
84 result[i] = RoomPtr.deref(untypedPtrs[i]); |
|
85 } |
|
86 return result; |
|
87 } |
|
88 } |
|
89 |
|
90 public static class RoomPtr extends PointerType { |
|
91 public RoomPtr() { super(); } |
|
92 public RoomPtr(Pointer ptr) { super(ptr); } |
|
93 |
|
94 public RoomlistRoom deref() { |
|
95 return deref(getPointer()); |
|
96 } |
|
97 |
|
98 public static RoomlistRoom deref(Pointer p) { |
|
99 RoomStruct r = new RoomStruct(p); |
|
100 r.read(); |
|
101 return new RoomlistRoom(r.name, r.map, r.scheme, r.weapons, r.owner, r.playerCount, r.teamCount, r.inProgress); |
|
102 } |
|
103 } |
|
104 |
|
105 public static class TeamPtr extends PointerType { |
|
106 public TeamInGame deref() { |
|
107 return deref(getPointer()); |
|
108 } |
|
109 |
|
110 public static TeamInGame deref(Pointer p) { |
|
111 TeamStruct ts = new TeamStruct(p); |
|
112 ts.read(); |
|
113 List<Hog> hogs = new ArrayList<Hog>(); |
|
114 for(int i=0; i<ts.hogs.length; i++) { |
|
115 HogStruct hog = ts.hogs[i]; |
|
116 hogs.add(new Hog(hog.name, hog.hat, hog.difficulty)); |
|
117 } |
|
118 Team team = new Team(ts.name, ts.grave, ts.flag, ts.voicepack, ts.fort, hogs); |
|
119 TeamIngameAttributes attrs = new TeamIngameAttributes(ts.ownerName, ts.colorIndex, ts.hogsInGame, ts.remoteDriven); |
|
120 return new TeamInGame(team, attrs); |
|
121 } |
|
122 |
|
123 public static TeamPtr createJavaOwned(Team t) { |
|
124 return createJavaOwned(new TeamInGame(t, null)); |
|
125 } |
|
126 |
|
127 public static TeamPtr createJavaOwned(TeamInGame ingameTeam) { |
|
128 TeamStruct ts = TeamStruct.from(ingameTeam.team, ingameTeam.ingameAttribs); |
|
129 ts.write(); |
|
130 TeamPtr result = new TeamPtr(); |
|
131 result.setPointer(ts.getPointer()); |
|
132 return result; |
|
133 } |
|
134 } |
|
135 |
|
136 public static class WeaponsetPtr extends PointerType { } |
|
137 public static class MapRecipePtr extends PointerType { } |
|
138 public static class SchemePtr extends PointerType { } |
|
139 public static class SchemelistPtr extends PointerType { |
|
140 private SchemelistStruct javaOwnedInstance; |
|
141 |
|
142 public List<Scheme> deref() { |
|
143 return deref(getPointer()); |
|
144 } |
|
145 |
|
146 public static List<Scheme> deref(Pointer p) { |
|
147 SchemelistStruct sls = new SchemelistStruct(p); |
|
148 sls.read(); |
|
149 return sls.toSchemeList(); |
|
150 } |
|
151 |
|
152 public static SchemelistPtr createJavaOwned(List<Scheme> schemes) { |
|
153 SchemelistPtr result = new SchemelistPtr(); |
|
154 result.javaOwnedInstance = new SchemelistStruct(); |
|
155 result.javaOwnedInstance.fillFrom(schemes); |
|
156 result.javaOwnedInstance.autoWrite(); |
|
157 result.setPointer(result.javaOwnedInstance.getPointer()); |
|
158 return result; |
|
159 } |
|
160 } |
|
161 |
|
162 public static class GameSetupPtr extends PointerType { |
|
163 public static GameSetupPtr createJavaOwned(GameConfig conf) { |
|
164 GameSetupStruct gss = GameSetupStruct.from(conf); |
|
165 gss.write(); |
|
166 GameSetupPtr result = new GameSetupPtr(); |
|
167 result.setPointer(gss.getPointer()); |
|
168 return result; |
|
169 } |
|
170 } |
|
171 |
|
172 static class HogStruct extends Structure { |
|
173 public static class ByVal extends HogStruct implements Structure.ByValue {} |
|
174 public static class ByRef extends HogStruct implements Structure.ByReference {} |
|
175 private static String[] FIELD_ORDER = new String[] {"name", "hat", "rounds", "kills", "deaths", "suicides", "difficulty", "initialHealth", "weaponset"}; |
|
176 |
|
177 public HogStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
178 public HogStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
179 |
|
180 public static HogStruct from(Hog hog) { |
|
181 HogStruct hs = new HogStruct(); |
|
182 hs.difficulty = hog.level; |
|
183 hs.hat = hog.hat; |
|
184 hs.name = hog.name; |
|
185 // TODO weaponset |
|
186 // TODO initialHealth |
|
187 return hs; |
|
188 } |
|
189 |
|
190 public String name; |
|
191 public String hat; |
|
192 |
|
193 public int rounds; |
|
194 public int kills; |
|
195 public int deaths; |
|
196 public int suicides; |
|
197 |
|
198 public int difficulty; |
|
199 |
|
200 public int initialHealth; |
|
201 public WeaponsetPtr weaponset; |
|
202 } |
|
203 |
|
204 static class TeamStruct extends Structure { |
|
205 public static class ByVal extends TeamStruct implements Structure.ByValue {} |
|
206 public static class ByRef extends TeamStruct implements Structure.ByReference {} |
|
207 private static String[] FIELD_ORDER = new String[] {"hogs", "name", "grave", "fort", "voicepack", "flag", "bindings", "bindingCount", "rounds", "wins", "campaignProgress", "colorIndex", "hogsInGame", "remoteDriven", "ownerName"}; |
|
208 |
|
209 public TeamStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
210 public TeamStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
211 |
|
212 public static TeamStruct from(Team team, TeamIngameAttributes attrs) { |
|
213 TeamStruct ts = new TeamStruct(); |
|
214 if(team != null) { |
|
215 ts.name = team.name; |
|
216 ts.grave = team.grave; |
|
217 ts.flag = team.flag; |
|
218 ts.voicepack = team.voice; |
|
219 ts.fort = team.fort; |
|
220 if(team.hogs.size() != HEDGEHOGS_PER_TEAM) { |
|
221 throw new IllegalArgumentException(); |
|
222 } |
|
223 for(int i=0; i<ts.hogs.length; i++) { |
|
224 ts.hogs[i] = HogStruct.from(team.hogs.get(i)); |
|
225 } |
|
226 } |
|
227 |
|
228 if(attrs != null) { |
|
229 ts.hogsInGame = attrs.hogCount; |
|
230 ts.ownerName = attrs.ownerName; |
|
231 ts.colorIndex = attrs.colorIndex; |
|
232 ts.remoteDriven = attrs.remoteDriven; |
|
233 } |
|
234 return ts; |
|
235 } |
|
236 |
|
237 public HogStruct[] hogs = new HogStruct[HEDGEHOGS_PER_TEAM]; |
|
238 public String name; |
|
239 public String grave; |
|
240 public String fort; |
|
241 public String voicepack; |
|
242 public String flag; |
|
243 |
|
244 public Pointer bindings; |
|
245 public int bindingCount; |
|
246 |
|
247 public int rounds; |
|
248 public int wins; |
|
249 public int campaignProgress; |
|
250 |
|
251 public int colorIndex; |
|
252 public int hogsInGame; |
|
253 public boolean remoteDriven; |
|
254 public String ownerName; |
|
255 } |
|
256 |
|
257 static class RoomStruct extends Structure { |
|
258 public static class ByVal extends RoomStruct implements Structure.ByValue {} |
|
259 public static class ByRef extends RoomStruct implements Structure.ByReference {} |
|
260 private static String[] FIELD_ORDER = new String[] {"inProgress", "name", "playerCount", "teamCount", "owner", "map", "scheme", "weapons"}; |
|
261 |
|
262 public RoomStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
263 public RoomStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
264 |
|
265 public boolean inProgress; |
|
266 public String name; |
|
267 public int playerCount; |
|
268 public int teamCount; |
|
269 public String owner; |
|
270 public String map; |
|
271 public String scheme; |
|
272 public String weapons; |
|
273 } |
|
274 |
|
275 static class MapRecipeStruct extends Structure { |
|
276 public static class ByVal extends MapRecipeStruct implements Structure.ByValue {} |
|
277 public static class ByRef extends MapRecipeStruct implements Structure.ByReference {} |
|
278 private static String[] FIELD_ORDER = new String[] {"_referenceCount", "mapgen", "name", "seed", "theme", "drawData", "drawDataSize", "templateFilter", "mazeSize"}; |
|
279 |
|
280 public MapRecipeStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
281 public MapRecipeStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
282 |
|
283 public int _referenceCount; |
|
284 public int mapgen; |
|
285 public String name; |
|
286 public String seed; |
|
287 public String theme; |
|
288 public Pointer drawData; |
|
289 public int drawDataSize; |
|
290 public int templateFilter; |
|
291 public int mazeSize; |
|
292 } |
|
293 |
|
294 static class MetaschemeSettingStruct extends Structure { |
|
295 public static class ByVal extends MetaschemeSettingStruct implements Structure.ByValue {} |
|
296 public static class ByRef extends MetaschemeSettingStruct implements Structure.ByReference {} |
|
297 private static String[] FIELD_ORDER = new String[] {"name", "engineCommand", "maxMeansInfinity", "times1000", "min", "max", "def"}; |
|
298 |
|
299 public MetaschemeSettingStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
300 public MetaschemeSettingStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
301 |
|
302 public void fillFrom(Setting setting) { |
|
303 name = setting.name; |
|
304 engineCommand = setting.engineCommand; |
|
305 maxMeansInfinity = setting.maxMeansInfinity; |
|
306 times1000 = setting.times1000; |
|
307 min = setting.min; |
|
308 max = setting.max; |
|
309 def = setting.def; |
|
310 } |
|
311 |
|
312 public MetaScheme.Setting toMetaSchemeSetting() { |
|
313 return new MetaScheme.Setting(name, engineCommand, maxMeansInfinity, times1000, min, max, def); |
|
314 } |
|
315 |
|
316 public String name; |
|
317 public String engineCommand; |
|
318 public boolean maxMeansInfinity; |
|
319 public boolean times1000; |
|
320 public int min; |
|
321 public int max; |
|
322 public int def; |
|
323 } |
|
324 |
|
325 static class MetaschemeModStruct extends Structure { |
|
326 public static class ByVal extends MetaschemeModStruct implements Structure.ByValue {} |
|
327 public static class ByRef extends MetaschemeModStruct implements Structure.ByReference {} |
|
328 private static String[] FIELD_ORDER = new String[] {"name", "bitmaskIndex"}; |
|
329 |
|
330 public MetaschemeModStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
331 public MetaschemeModStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
332 |
|
333 public void fillFrom(Mod mod) { |
|
334 name = mod.name; |
|
335 bitmaskIndex = mod.bitmaskIndex; |
|
336 } |
|
337 |
|
338 public MetaScheme.Mod toMetaSchemeMod() { |
|
339 return new MetaScheme.Mod(name, bitmaskIndex); |
|
340 } |
|
341 |
|
342 public String name; |
|
343 public int bitmaskIndex; |
|
344 |
|
345 } |
|
346 |
|
347 static class MetaschemeStruct extends Structure { |
|
348 public static class ByVal extends MetaschemeStruct implements Structure.ByValue {} |
|
349 public static class ByRef extends MetaschemeStruct implements Structure.ByReference {} |
|
350 |
|
351 private static String[] FIELD_ORDER = new String[] {"_referenceCount", "settingCount", "modCount", "settings", "mods"}; |
|
352 |
|
353 public MetaschemeStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
354 public MetaschemeStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
355 |
|
356 public void fillFrom(MetaScheme metascheme) { |
|
357 settingCount = metascheme.settings.size(); |
|
358 modCount = metascheme.mods.size(); |
|
359 |
|
360 settings = new MetaschemeSettingStruct.ByRef(); |
|
361 Structure[] settingStructs = settings.toArray(settingCount); |
|
362 mods = new MetaschemeModStruct.ByRef(); |
|
363 Structure[] modStructs = mods.toArray(modCount); |
|
364 |
|
365 for(int i=0; i<settingCount; i++) { |
|
366 MetaschemeSettingStruct mss = (MetaschemeSettingStruct)settingStructs[i]; |
|
367 mss.fillFrom(metascheme.settings.get(i)); |
|
368 } |
|
369 |
|
370 for(int i=0; i<modCount; i++) { |
|
371 MetaschemeModStruct mms = (MetaschemeModStruct)modStructs[i]; |
|
372 mms.fillFrom(metascheme.mods.get(i)); |
|
373 } |
|
374 } |
|
375 |
|
376 /** |
|
377 * Only use on native-owned structs! |
|
378 * Calling this method on a Java-owned struct could cause garbage collection of referenced |
|
379 * structures. |
|
380 */ |
|
381 public MetaScheme toMetaScheme() { |
|
382 List<MetaScheme.Setting> settingList = new ArrayList<MetaScheme.Setting>(settingCount); |
|
383 List<MetaScheme.Mod> modList = new ArrayList<MetaScheme.Mod>(modCount); |
|
384 |
|
385 Structure[] settingStructs = settings.toArray(settingCount); |
|
386 Structure[] modStructs = mods.toArray(modCount); |
|
387 |
|
388 for(int i=0; i<settingCount; i++) { |
|
389 MetaschemeSettingStruct mss = (MetaschemeSettingStruct)settingStructs[i]; |
|
390 settingList.add(mss.toMetaSchemeSetting()); |
|
391 } |
|
392 |
|
393 for(int i=0; i<modCount; i++) { |
|
394 MetaschemeModStruct mms = (MetaschemeModStruct)modStructs[i]; |
|
395 modList.add(mms.toMetaSchemeMod()); |
|
396 } |
|
397 |
|
398 return new MetaScheme(modList, settingList); |
|
399 } |
|
400 |
|
401 public int _referenceCount; |
|
402 public int settingCount; |
|
403 public int modCount; |
|
404 public MetaschemeSettingStruct.ByRef settings; |
|
405 public MetaschemeModStruct.ByRef mods; |
|
406 } |
|
407 |
|
408 static class SchemeStruct extends Structure { |
|
409 public static class ByVal extends SchemeStruct implements Structure.ByValue {} |
|
410 public static class ByRef extends SchemeStruct implements Structure.ByReference {} |
|
411 private static String[] FIELD_ORDER = new String[] {"_referenceCount", "meta", "name", "settings", "mod"}; |
|
412 |
|
413 public SchemeStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
414 public SchemeStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
415 |
|
416 public void fillFrom(Scheme scheme) { |
|
417 meta = new MetaschemeStruct.ByRef(); |
|
418 meta.fillFrom(scheme.metascheme); |
|
419 name = scheme.name; |
|
420 settings = new Memory(NATIVE_INT_SIZE * scheme.metascheme.settings.size()); |
|
421 for(int i=0; i<scheme.metascheme.settings.size(); i++) { |
|
422 Integer value = scheme.settings.get(scheme.metascheme.settings.get(i).name); |
|
423 settings.setInt(NATIVE_INT_SIZE*i, value); |
|
424 } |
|
425 mods = new Memory(NATIVE_BOOL_SIZE * scheme.metascheme.mods.size()); |
|
426 for(int i=0; i<scheme.metascheme.mods.size(); i++) { |
|
427 Boolean value = scheme.mods.get(scheme.metascheme.mods.get(i).name); |
|
428 mods.setByte(NATIVE_BOOL_SIZE*i, (byte)(value ? 1 : 0)); |
|
429 } |
|
430 } |
|
431 |
|
432 public Scheme toScheme() { |
|
433 MetaScheme metaScheme = meta.toMetaScheme(); |
|
434 Map<String, Integer> settingsMap = new HashMap<String, Integer>(); |
|
435 for(int i=0; i<metaScheme.settings.size(); i++) { |
|
436 settingsMap.put(metaScheme.settings.get(i).name, settings.getInt(NATIVE_INT_SIZE*i)); |
|
437 } |
|
438 Map<String, Boolean> modsMap = new HashMap<String, Boolean>(); |
|
439 for(int i=0; i<metaScheme.mods.size(); i++) { |
|
440 modsMap.put(metaScheme.mods.get(i).name, mods.getByte(i) != 0 ? Boolean.TRUE : Boolean.FALSE); |
|
441 } |
|
442 return new Scheme(metaScheme, name, settingsMap, modsMap); |
|
443 } |
|
444 |
|
445 public int _referenceCount; |
|
446 public MetaschemeStruct.ByRef meta; |
|
447 public String name; |
|
448 public Pointer settings; |
|
449 public Pointer mods; |
|
450 } |
|
451 |
|
452 /** |
|
453 * Represents a flib_scheme*, for use as part of a flib_scheme** |
|
454 */ |
|
455 static class SchemePointerByReference extends Structure implements Structure.ByReference { |
|
456 private static String[] FIELD_ORDER = new String[] {"scheme"}; |
|
457 |
|
458 public SchemePointerByReference() { super(); setFieldOrder(FIELD_ORDER); } |
|
459 public SchemePointerByReference(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
460 |
|
461 public SchemeStruct.ByRef scheme; |
|
462 } |
|
463 |
|
464 static class SchemelistStruct extends Structure { |
|
465 public static class ByVal extends SchemelistStruct implements Structure.ByValue {} |
|
466 public static class ByRef extends SchemelistStruct implements Structure.ByReference {} |
|
467 private static String[] FIELD_ORDER = new String[] {"schemeCount", "schemes"}; |
|
468 |
|
469 public SchemelistStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
470 public SchemelistStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
471 |
|
472 /** |
|
473 * Only use on native-owned structs! |
|
474 * Calling this method on a Java-owned struct could cause garbage collection of referenced |
|
475 * structures. |
|
476 */ |
|
477 public List<Scheme> toSchemeList() { |
|
478 if(schemeCount<=0) { |
|
479 return new ArrayList<Scheme>(); |
|
480 } else { |
|
481 List<Scheme> schemeList = new ArrayList<Scheme>(schemeCount); |
|
482 |
|
483 Structure[] schemePtrStructs = schemes.toArray(schemeCount); |
|
484 |
|
485 for(int i=0; i<schemeCount; i++) { |
|
486 SchemePointerByReference spbr2 = (SchemePointerByReference)schemePtrStructs[i]; |
|
487 schemeList.add(spbr2.scheme.toScheme()); |
|
488 } |
|
489 return schemeList; |
|
490 } |
|
491 } |
|
492 |
|
493 public void fillFrom(List<Scheme> schemeList) { |
|
494 schemeCount = schemeList.size(); |
|
495 schemes = new SchemePointerByReference(); |
|
496 Structure[] schemePtrStructs = schemes.toArray(schemeCount); |
|
497 |
|
498 for(int i=0; i<this.schemeCount; i++) { |
|
499 SchemePointerByReference spbr = (SchemePointerByReference)schemePtrStructs[i]; |
|
500 spbr.scheme = new SchemeStruct.ByRef(); |
|
501 spbr.scheme.fillFrom(schemeList.get(i)); |
|
502 } |
|
503 } |
|
504 |
|
505 public int schemeCount; |
|
506 public SchemePointerByReference schemes; |
|
507 } |
|
508 |
|
509 static class TeamlistStruct extends Structure { |
|
510 public static class ByVal extends TeamlistStruct implements Structure.ByValue {} |
|
511 public static class ByRef extends TeamlistStruct implements Structure.ByReference {} |
|
512 private static String[] FIELD_ORDER = new String[] {"teamCount", "teams"}; |
|
513 |
|
514 public TeamlistStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
515 public TeamlistStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
516 |
|
517 public int teamCount; |
|
518 public Pointer teams; |
|
519 } |
|
520 |
|
521 static class GameSetupStruct extends Structure { |
|
522 public static class ByVal extends GameSetupStruct implements Structure.ByValue {} |
|
523 public static class ByRef extends GameSetupStruct implements Structure.ByReference {} |
|
524 private static String[] FIELD_ORDER = new String[] {"script", "gamescheme", "map", "teamlist"}; |
|
525 |
|
526 public GameSetupStruct() { super(); setFieldOrder(FIELD_ORDER); } |
|
527 public GameSetupStruct(Pointer ptr) { super(ptr); setFieldOrder(FIELD_ORDER); } |
|
528 |
|
529 public static GameSetupStruct from(GameConfig conf) { |
|
530 GameSetupStruct gss = new GameSetupStruct(); |
|
531 gss.gamescheme = new SchemeStruct.ByRef(); |
|
532 gss.gamescheme.fillFrom(conf.scheme); |
|
533 gss.map = new MapRecipeStruct.ByRef(); |
|
534 // TODO gss.map.fillFrom(conf.map, conf.seed, conf.theme); |
|
535 gss.script = conf.style; |
|
536 gss.teamlist = new TeamlistStruct.ByRef(); |
|
537 // TODO gss.teamlist.fillFrom(conf.teams, conf.weapon); |
|
538 return gss; |
|
539 } |
|
540 |
|
541 public String script; |
|
542 public SchemeStruct.ByRef gamescheme; |
|
543 public MapRecipeStruct.ByRef map; |
|
544 public TeamlistStruct.ByRef teamlist; |
|
545 } |
|
546 |
|
547 public static interface VoidCallback extends Callback { |
|
548 void callback(Pointer context); |
|
549 } |
|
550 |
|
551 public static interface StrCallback extends Callback { |
|
552 void callback(Pointer context, String arg1); |
|
553 } |
|
554 |
|
555 public static interface IntCallback extends Callback { |
|
556 void callback(Pointer context, int arg1); |
|
557 } |
|
558 |
|
559 public static interface IntStrCallback extends Callback { |
|
560 void callback(Pointer context, int arg1, String arg2); |
|
561 } |
|
562 |
|
563 public static interface StrIntCallback extends Callback { |
|
564 void callback(Pointer context, String arg1, int arg2); |
|
565 } |
|
566 |
|
567 public static interface StrStrCallback extends Callback { |
|
568 void callback(Pointer context, String arg1, String arg2); |
|
569 } |
|
570 |
|
571 public static interface RoomCallback extends Callback { |
|
572 void callback(Pointer context, RoomPtr arg1); |
|
573 } |
|
574 |
|
575 public static interface RoomListCallback extends Callback { |
|
576 void callback(Pointer context, RoomArrayPtr arg1, int count); |
|
577 } |
|
578 |
|
579 public static interface StrRoomCallback extends Callback { |
|
580 void callback(Pointer context, String arg1, RoomPtr arg2); |
|
581 } |
|
582 |
|
583 public static interface BoolCallback extends Callback { |
|
584 void callback(Pointer context, boolean arg1); |
|
585 } |
|
586 |
|
587 public static interface StrBoolCallback extends Callback { |
|
588 void callback(Pointer context, String arg1, boolean arg2); |
|
589 } |
|
590 |
|
591 public static interface TeamCallback extends Callback { |
|
592 void callback(Pointer context, TeamPtr arg1); |
|
593 } |
|
594 |
|
595 public static interface BytesCallback extends Callback { |
|
596 void callback(Pointer context, Pointer buffer, NativeLong size); |
|
597 } |
|
598 |
|
599 public static interface BytesBoolCallback extends Callback { |
|
600 void callback(Pointer context, Pointer buffer, NativeLong size, boolean arg3); |
|
601 } |
|
602 |
|
603 public static interface SchemeCallback extends Callback { |
|
604 void callback(Pointer context, SchemePtr arg1); |
|
605 } |
|
606 |
|
607 public static interface MapIntCallback extends Callback { |
|
608 void callback(Pointer context, MapRecipePtr arg1, int arg2); |
|
609 } |
|
610 |
|
611 public static interface WeaponsetCallback extends Callback { |
|
612 void callback(Pointer context, WeaponsetPtr arg1); |
|
613 } |
|
614 |
|
615 public static interface MapimageCallback extends Callback { |
|
616 void callback(Pointer context, Pointer buffer, int hedgehogCount); |
|
617 } |
|
618 |
|
619 public static interface LogCallback extends Callback { |
|
620 void callback(int level, String logMessage); |
|
621 } |
|
622 |
|
623 int flib_init(); |
|
624 void flib_quit(); |
|
625 |
|
626 NetconnPtr flib_netconn_create(String playerName, MetaschemePtr meta, String dataDirPath, String host, int port); |
|
627 void flib_netconn_destroy(NetconnPtr conn); |
|
628 |
|
629 void flib_netconn_tick(NetconnPtr conn); |
|
630 boolean flib_netconn_is_chief(NetconnPtr conn); |
|
631 boolean flib_netconn_is_in_room_context(NetconnPtr conn); |
|
632 String flib_netconn_get_playername(NetconnPtr conn); |
|
633 GameSetupPtr flib_netconn_create_gamesetup(NetconnPtr conn); |
|
634 int flib_netconn_send_quit(NetconnPtr conn, String quitmsg); |
|
635 int flib_netconn_send_chat(NetconnPtr conn, String chat); |
|
636 int flib_netconn_send_teamchat(NetconnPtr conn, String msg); |
|
637 int flib_netconn_send_password(NetconnPtr conn, String passwd); |
|
638 int flib_netconn_send_nick(NetconnPtr conn, String nick); |
|
639 int flib_netconn_send_request_roomlist(NetconnPtr conn); |
|
640 int flib_netconn_send_joinRoom(NetconnPtr conn, String room); |
|
641 int flib_netconn_send_createRoom(NetconnPtr conn, String room); |
|
642 int flib_netconn_send_renameRoom(NetconnPtr conn, String roomName); |
|
643 int flib_netconn_send_leaveRoom(NetconnPtr conn, String msg); |
|
644 int flib_netconn_send_toggleReady(NetconnPtr conn); |
|
645 int flib_netconn_send_addTeam(NetconnPtr conn, TeamPtr team); |
|
646 int flib_netconn_send_removeTeam(NetconnPtr conn, String teamname); |
|
647 int flib_netconn_send_engineMessage(NetconnPtr conn, Buffer message, NativeLong size); // TODO check if NativeLong==size_t |
|
648 int flib_netconn_send_teamHogCount(NetconnPtr conn, String teamname, int hogcount); |
|
649 int flib_netconn_send_teamColor(NetconnPtr conn, String teamname, int colorIndex); |
|
650 int flib_netconn_send_weaponset(NetconnPtr conn, WeaponsetPtr weaponset); |
|
651 int flib_netconn_send_map(NetconnPtr conn, MapRecipePtr map); |
|
652 int flib_netconn_send_mapName(NetconnPtr conn, String mapName); |
|
653 int flib_netconn_send_mapGen(NetconnPtr conn, int mapGen); |
|
654 int flib_netconn_send_mapTemplate(NetconnPtr conn, int templateFilter); |
|
655 int flib_netconn_send_mapMazeSize(NetconnPtr conn, int mazeSize); |
|
656 int flib_netconn_send_mapSeed(NetconnPtr conn, String seed); |
|
657 int flib_netconn_send_mapTheme(NetconnPtr conn, String theme); |
|
658 int flib_netconn_send_mapDrawdata(NetconnPtr conn, Buffer drawData, NativeLong size); |
|
659 int flib_netconn_send_script(NetconnPtr conn, String scriptName); |
|
660 int flib_netconn_send_scheme(NetconnPtr conn, SchemePtr scheme); |
|
661 int flib_netconn_send_roundfinished(NetconnPtr conn, boolean withoutError); |
|
662 int flib_netconn_send_ban(NetconnPtr conn, String playerName); |
|
663 int flib_netconn_send_kick(NetconnPtr conn, String playerName); |
|
664 int flib_netconn_send_playerInfo(NetconnPtr conn, String playerName); |
|
665 int flib_netconn_send_playerFollow(NetconnPtr conn, String playerName); |
|
666 int flib_netconn_send_startGame(NetconnPtr conn); |
|
667 int flib_netconn_send_toggleRestrictJoins(NetconnPtr conn); |
|
668 int flib_netconn_send_toggleRestrictTeams(NetconnPtr conn); |
|
669 int flib_netconn_send_clearAccountsCache(NetconnPtr conn); |
|
670 int flib_netconn_send_setServerVar(NetconnPtr conn, String name, String value); |
|
671 int flib_netconn_send_getServerVars(NetconnPtr conn); |
|
672 |
|
673 void flib_netconn_onMessage(NetconnPtr conn, IntStrCallback callback, Pointer context); |
|
674 void flib_netconn_onChat(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
675 void flib_netconn_onConnected(NetconnPtr conn, VoidCallback callback, Pointer context); |
|
676 void flib_netconn_onDisconnected(NetconnPtr conn, IntStrCallback callback, Pointer context); |
|
677 void flib_netconn_onRoomlist(NetconnPtr conn, RoomListCallback callback, Pointer context); |
|
678 void flib_netconn_onRoomAdd(NetconnPtr conn, RoomCallback callback, Pointer context); |
|
679 void flib_netconn_onRoomDelete(NetconnPtr conn, StrCallback callback, Pointer context); |
|
680 void flib_netconn_onRoomUpdate(NetconnPtr conn, StrRoomCallback callback, Pointer context); |
|
681 void flib_netconn_onLobbyJoin(NetconnPtr conn, StrCallback callback, Pointer context); |
|
682 void flib_netconn_onLobbyLeave(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
683 void flib_netconn_onNickTaken(NetconnPtr conn, StrCallback callback, Pointer context); |
|
684 void flib_netconn_onPasswordRequest(NetconnPtr conn, StrCallback callback, Pointer context); |
|
685 void flib_netconn_onEnterRoom(NetconnPtr conn, BoolCallback callback, Pointer context); |
|
686 void flib_netconn_onRoomChiefStatus(NetconnPtr conn, BoolCallback callback, Pointer context); |
|
687 void flib_netconn_onReadyState(NetconnPtr conn, StrBoolCallback callback, Pointer context); |
|
688 void flib_netconn_onLeaveRoom(NetconnPtr conn, IntStrCallback callback, Pointer context); |
|
689 void flib_netconn_onTeamAdd(NetconnPtr conn, TeamCallback callback, Pointer context); |
|
690 void flib_netconn_onTeamDelete(NetconnPtr conn, StrCallback callback, Pointer context); |
|
691 void flib_netconn_onRoomJoin(NetconnPtr conn, StrCallback callback, Pointer context); |
|
692 void flib_netconn_onRoomLeave(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
693 void flib_netconn_onRunGame(NetconnPtr conn, VoidCallback callback, Pointer context); |
|
694 void flib_netconn_onTeamAccepted(NetconnPtr conn, StrCallback callback, Pointer context); |
|
695 void flib_netconn_onHogCountChanged(NetconnPtr conn, StrIntCallback callback, Pointer context); |
|
696 void flib_netconn_onTeamColorChanged(NetconnPtr conn, StrIntCallback callback, Pointer context); |
|
697 void flib_netconn_onEngineMessage(NetconnPtr conn, BytesCallback callback, Pointer context); |
|
698 void flib_netconn_onCfgScheme(NetconnPtr conn, SchemeCallback callback, Pointer context); |
|
699 void flib_netconn_onMapChanged(NetconnPtr conn, MapIntCallback callback, Pointer context); |
|
700 void flib_netconn_onScriptChanged(NetconnPtr conn, StrCallback callback, Pointer context); |
|
701 void flib_netconn_onWeaponsetChanged(NetconnPtr conn, WeaponsetCallback callback, Pointer context); |
|
702 void flib_netconn_onAdminAccess(NetconnPtr conn, VoidCallback callback, Pointer context); |
|
703 void flib_netconn_onServerVar(NetconnPtr conn, StrStrCallback callback, Pointer context); |
|
704 |
|
705 // Gameconn |
|
706 GameconnPtr flib_gameconn_create(String playerName, GameSetupPtr setup, boolean netgame); |
|
707 GameconnPtr flib_gameconn_create_playdemo(Buffer demo, NativeLong size); |
|
708 GameconnPtr flib_gameconn_create_loadgame(String playerName, Buffer save, NativeLong size); |
|
709 GameconnPtr flib_gameconn_create_campaign(String playerName, String seed, String script); |
|
710 |
|
711 void flib_gameconn_destroy(GameconnPtr conn); |
|
712 int flib_gameconn_getport(GameconnPtr conn); |
|
713 void flib_gameconn_tick(GameconnPtr conn); |
|
714 |
|
715 int flib_gameconn_send_enginemsg(GameconnPtr conn, Buffer data, NativeLong len); |
|
716 int flib_gameconn_send_textmsg(GameconnPtr conn, int msgtype, String msg); |
|
717 int flib_gameconn_send_chatmsg(GameconnPtr conn, String playername, String msg); |
|
718 |
|
719 void flib_gameconn_onConnect(GameconnPtr conn, VoidCallback callback, Pointer context); |
|
720 void flib_gameconn_onDisconnect(GameconnPtr conn, IntCallback callback, Pointer context); |
|
721 void flib_gameconn_onErrorMessage(GameconnPtr conn, StrCallback callback, Pointer context); |
|
722 void flib_gameconn_onChat(GameconnPtr conn, StrBoolCallback callback, Pointer context); |
|
723 void flib_gameconn_onGameRecorded(GameconnPtr conn, BytesBoolCallback callback, Pointer context); |
|
724 void flib_gameconn_onEngineMessage(GameconnPtr conn, BytesCallback callback, Pointer context); |
|
725 |
|
726 // MapConn |
|
727 MapconnPtr flib_mapconn_create(MapRecipePtr mapdesc); |
|
728 void flib_mapconn_destroy(MapconnPtr conn); |
|
729 int flib_mapconn_getport(MapconnPtr conn); |
|
730 void flib_mapconn_onSuccess(MapconnPtr conn, MapimageCallback callback, Pointer context); |
|
731 void flib_mapconn_onFailure(MapconnPtr conn, StrCallback callback, Pointer context); |
|
732 void flib_mapconn_tick(MapconnPtr conn); |
|
733 |
|
734 // GameSetup |
|
735 void flib_gamesetup_destroy(GameSetupPtr gamesetup); |
|
736 GameSetupPtr flib_gamesetup_copy(GameSetupPtr gamesetup); |
|
737 |
|
738 // MapRecipe |
|
739 public static final int MAPGEN_REGULAR = 0; |
|
740 public static final int MAPGEN_MAZE = 1; |
|
741 public static final int MAPGEN_DRAWN = 2; |
|
742 public static final int MAPGEN_NAMED = 3; |
|
743 |
|
744 public static final int TEMPLATEFILTER_ALL = 0; |
|
745 public static final int TEMPLATEFILTER_SMALL = 1; |
|
746 public static final int TEMPLATEFILTER_MEDIUM = 2; |
|
747 public static final int TEMPLATEFILTER_LARGE = 3; |
|
748 public static final int TEMPLATEFILTER_CAVERN = 4; |
|
749 public static final int TEMPLATEFILTER_WACKY = 5; |
|
750 |
|
751 public static final int MAZE_SIZE_SMALL_TUNNELS = 0; |
|
752 public static final int MAZE_SIZE_MEDIUM_TUNNELS = 1; |
|
753 public static final int MAZE_SIZE_LARGE_TUNNELS = 2; |
|
754 public static final int MAZE_SIZE_SMALL_ISLANDS = 3; |
|
755 public static final int MAZE_SIZE_MEDIUM_ISLANDS = 4; |
|
756 public static final int MAZE_SIZE_LARGE_ISLANDS = 5; |
|
757 |
|
758 MapRecipePtr flib_map_create_regular(String seed, String theme, int templateFilter); |
|
759 MapRecipePtr flib_map_create_maze(String seed, String theme, int mazeSize); |
|
760 MapRecipePtr flib_map_create_named(String seed, String name); |
|
761 MapRecipePtr flib_map_create_drawn(String seed, String theme, Buffer drawData, NativeLong drawDataSize); |
|
762 MapRecipePtr flib_map_copy(MapRecipePtr map); |
|
763 MapRecipePtr flib_map_retain(MapRecipePtr map); |
|
764 void flib_map_release(MapRecipePtr map); |
|
765 |
|
766 // Metascheme |
|
767 MetaschemePtr flib_metascheme_from_ini(String filename); |
|
768 MetaschemePtr flib_metascheme_retain(MetaschemePtr metainfo); |
|
769 void flib_metascheme_release(MetaschemePtr metainfo); |
|
770 |
|
771 // Scheme lists |
|
772 SchemelistPtr flib_schemelist_from_ini(MetaschemePtr meta, String filename); |
|
773 int flib_schemelist_to_ini(String filename, SchemelistPtr list); |
|
774 void flib_schemelist_destroy(SchemelistPtr list); |
|
775 |
|
776 // Team |
|
777 void flib_team_destroy(TeamPtr team); |
|
778 TeamPtr flib_team_from_ini(String filename); |
|
779 int flib_team_to_ini(String filename, TeamPtr team); |
|
780 void flib_team_set_weaponset(TeamPtr team, WeaponsetPtr set); |
|
781 void flib_team_set_health(TeamPtr team, int health); |
|
782 |
|
783 // Logging |
|
784 public static final int FLIB_LOGLEVEL_ALL = -100; |
|
785 public static final int FLIB_LOGLEVEL_DEBUG = -1; |
|
786 public static final int FLIB_LOGLEVEL_INFO = 0; |
|
787 public static final int FLIB_LOGLEVEL_WARNING = 1; |
|
788 public static final int FLIB_LOGLEVEL_ERROR = 2; |
|
789 public static final int FLIB_LOGLEVEL_NONE = 100; |
|
790 |
|
791 void flib_log_setLevel(int level); |
|
792 void flib_log_setCallback(LogCallback callback); |
|
793 } |