Hedgeroid: Fixed JNA mapping of size_t
authorMedo <smaxein@googlemail.com>
Sat, 18 Aug 2012 13:39:05 +0200
changeset 7558 983ff426f91e
parent 7556 4617e8ec0507
child 7560 e7ab89ab86f6
Hedgeroid: Fixed JNA mapping of size_t
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/GameConnection.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SettingsFragment.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/AndroidTypeMapper.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/Flib.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/Frontlib.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/FrontlibTypeMapper.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/NativeSizeT.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/ThreadedNetConnection.java
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/GameConnection.java	Sat Aug 18 13:21:37 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/GameConnection.java	Sat Aug 18 13:39:05 2012 +0200
@@ -10,6 +10,7 @@
 import org.hedgewars.hedgeroid.frontlib.Frontlib.StrBoolCallback;
 import org.hedgewars.hedgeroid.frontlib.Frontlib.StrCallback;
 import org.hedgewars.hedgeroid.frontlib.Frontlib.VoidCallback;
+import org.hedgewars.hedgeroid.frontlib.NativeSizeT;
 import org.hedgewars.hedgeroid.netplay.GameMessageListener;
 import org.hedgewars.hedgeroid.netplay.Netplay;
 import org.hedgewars.hedgeroid.util.TickHandler;
@@ -20,7 +21,6 @@
 import android.util.Log;
 
 import com.sun.jna.Memory;
-import com.sun.jna.NativeLong;
 import com.sun.jna.Pointer;
 
 public final class GameConnection {
@@ -162,7 +162,7 @@
 	
 	// runs on the IPCThread
 	private final BytesCallback engineMessageCb = new BytesCallback() {
-		public void callback(Pointer context, Pointer buffer, NativeLong size) {
+		public void callback(Pointer context, Pointer buffer, NativeSizeT size) {
 			netplay.sendEngineMessage(buffer.getByteArray(0, size.intValue()));
 		}
 	};
@@ -197,10 +197,9 @@
 				public void run() {
 					Memory mem = new Memory(em.length);
 					mem.write(0, em, 0, em.length);
-					Flib.INSTANCE.flib_gameconn_send_enginemsg(conn, mem, new NativeLong(em.length));
+					Flib.INSTANCE.flib_gameconn_send_enginemsg(conn, mem, NativeSizeT.valueOf(em.length));
 				}
 			});
-			
 		}
 		
 		public void onChatMessage(final String nick, final String message) {
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SettingsFragment.java	Sat Aug 18 13:21:37 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SettingsFragment.java	Sat Aug 18 13:39:05 2012 +0200
@@ -30,6 +30,8 @@
 import android.widget.Toast;
 
 public class SettingsFragment extends Fragment implements RoomStateManager.Observer {
+	private static final String TAG = SettingsFragment.class.getSimpleName();
+	
 	private Spinner styleSpinner, schemeSpinner, weaponsetSpinner, themeSpinner;
 	private ImageView themeIcon;
 	
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/AndroidTypeMapper.java	Sat Aug 18 13:39:05 2012 +0200
@@ -0,0 +1,44 @@
+package org.hedgewars.hedgeroid.frontlib;
+
+import com.sun.jna.DefaultTypeMapper;
+import com.sun.jna.FromNativeContext;
+import com.sun.jna.ToNativeContext;
+import com.sun.jna.TypeConverter;
+import com.sun.jna.TypeMapper;
+
+class AndroidTypeMapper extends DefaultTypeMapper {
+	static final int NATIVE_INT_SIZE = 4;
+	static final int NATIVE_SIZE_T_SIZE = 4;
+	static final int NATIVE_BOOL_SIZE = 1;
+    public static final TypeMapper INSTANCE = new AndroidTypeMapper();
+    
+    protected AndroidTypeMapper() {
+        addTypeConverter(Boolean.class, new BooleanConverter());
+        addTypeConverter(NativeSizeT.class, new SizeTConverter());
+    }
+
+    private static final class BooleanConverter implements TypeConverter {
+    	public Class<Byte> nativeType() {
+    		return Byte.class;
+    	}
+    	public Object fromNative(Object value, FromNativeContext context) {
+    		return ((Byte)value).intValue() != 0 ? Boolean.TRUE : Boolean.FALSE;
+    	}
+    	public Object toNative(Object value, ToNativeContext context) {
+    		return Byte.valueOf((byte)(Boolean.TRUE.equals(value) ? 1 : 0));
+    	}
+    }
+    
+    private static final class SizeTConverter implements TypeConverter {
+    	public Class<Integer> nativeType() {
+    		return Integer.class;
+    	}
+    	public Object fromNative(Object value, FromNativeContext context) {
+    		return NativeSizeT.valueOf((Integer)value);
+    	}
+    	public Object toNative(Object value, ToNativeContext context) {
+    		return Integer.valueOf(value==null ? 0 : ((NativeSizeT)value).intValue());
+    	}
+    }
+}
+
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/Flib.java	Sat Aug 18 13:21:37 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/Flib.java	Sat Aug 18 13:39:05 2012 +0200
@@ -12,7 +12,7 @@
 		System.loadLibrary("SDL_net");
 		System.setProperty("jna.encoding", "UTF8"); // Ugly global setting, but it seems JNA doesn't allow setting this per-library... 
 	}
-	public static final Frontlib INSTANCE = (Frontlib)Native.loadLibrary("frontlib", Frontlib.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, FrontlibTypeMapper.INSTANCE));
+	public static final Frontlib INSTANCE = (Frontlib)Native.loadLibrary("frontlib", Frontlib.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, AndroidTypeMapper.INSTANCE));
 	
 	// Hook frontlib logging into Android logging
 	private static final Frontlib.LogCallback logCb = new Frontlib.LogCallback() {
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/Frontlib.java	Sat Aug 18 13:21:37 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/Frontlib.java	Sat Aug 18 13:39:05 2012 +0200
@@ -22,7 +22,6 @@
 import com.sun.jna.Callback;
 import com.sun.jna.Library;
 import com.sun.jna.Memory;
-import com.sun.jna.NativeLong;
 import com.sun.jna.Pointer;
 import com.sun.jna.PointerType;
 import com.sun.jna.Structure;
@@ -122,14 +121,10 @@
  * headers, the compiler will give you errors if the signatures don't match.
  */
 public interface Frontlib extends Library {
-	static final int NATIVE_INT_SIZE = 4;
-	static final int NATIVE_BOOL_SIZE = 1;
-	
 	public static class NetconnPtr extends PointerType { }
 	public static class MapconnPtr extends PointerType { }
 	public static class GameconnPtr extends PointerType { }
 	
-	// TODO avoid code duplication in the pointer types
 	public static class MetaschemePtr extends PointerType {
 		public MetaScheme deref() {
 			return deref(getPointer());
@@ -555,10 +550,10 @@
 			if(buf != null) {
 				drawData = new Memory(buf.length);
 				drawData.write(0, buf, 0, buf.length);
-				drawDataSize = new NativeLong(buf.length);
+				drawDataSize = NativeSizeT.valueOf(buf.length);
 			} else {
 				drawData = null;
-				drawDataSize = new NativeLong(0);
+				drawDataSize = NativeSizeT.valueOf(0);
 			}
 			templateFilter = map.templateFilter;
 			mazeSize = map.mazeSize;
@@ -578,7 +573,7 @@
 		public String seed;
 		public String theme;
 		public Pointer drawData;
-		public NativeLong drawDataSize;
+		public NativeSizeT drawDataSize;
 		public int templateFilter;
 		public int mazeSize;
 	}
@@ -687,15 +682,15 @@
 		public void fillFrom(Scheme scheme) {
 			MetaScheme meta = MetaScheme.INSTANCE;
 			name = scheme.name;
-			settings = new Memory(NATIVE_INT_SIZE * meta.settings.size());
+			settings = new Memory(AndroidTypeMapper.NATIVE_INT_SIZE * meta.settings.size());
 			for(int i=0; i<meta.settings.size(); i++) {
 				Integer value = scheme.settings.get(meta.settings.get(i).name);
-				settings.setInt(NATIVE_INT_SIZE*i, value);
+				settings.setInt(AndroidTypeMapper.NATIVE_INT_SIZE*i, value);
 			}
-			mods = new Memory(NATIVE_BOOL_SIZE * meta.mods.size());
+			mods = new Memory(AndroidTypeMapper.NATIVE_BOOL_SIZE * meta.mods.size());
 			for(int i=0; i<meta.mods.size(); i++) {
 				Boolean value = scheme.mods.get(meta.mods.get(i).name);
-				mods.setByte(NATIVE_BOOL_SIZE*i, (byte)(value ? 1 : 0));
+				mods.setByte(AndroidTypeMapper.NATIVE_BOOL_SIZE*i, (byte)(value ? 1 : 0));
 			}
 		}
 
@@ -703,7 +698,7 @@
 			Map<String, Integer> settingsMap = new HashMap<String, Integer>();
 			MetaScheme meta = MetaScheme.INSTANCE;
 			for(int i=0; i<meta.settings.size(); i++) {
-				settingsMap.put(meta.settings.get(i).name, settings.getInt(NATIVE_INT_SIZE*i));
+				settingsMap.put(meta.settings.get(i).name, settings.getInt(AndroidTypeMapper.NATIVE_INT_SIZE*i));
 			}
 			Map<String, Boolean> modsMap = new HashMap<String, Boolean>();
 			for(int i=0; i<meta.mods.size(); i++) {
@@ -934,11 +929,11 @@
 	}
 	
 	public static interface BytesCallback extends Callback {
-		void callback(Pointer context, Pointer buffer, NativeLong size);
+		void callback(Pointer context, Pointer buffer, NativeSizeT size);
 	}
 	
 	public static interface BytesBoolCallback extends Callback {
-		void callback(Pointer context, Pointer buffer, NativeLong size, boolean arg3);
+		void callback(Pointer context, Pointer buffer, NativeSizeT size, boolean arg3);
 	}
 	
 	public static interface SchemeCallback extends Callback {
@@ -1021,7 +1016,7 @@
 	int flib_netconn_send_toggleReady(NetconnPtr conn);
 	int flib_netconn_send_addTeam(NetconnPtr conn, TeamPtr team);
 	int flib_netconn_send_removeTeam(NetconnPtr conn, String teamname);
-	int flib_netconn_send_engineMessage(NetconnPtr conn, Pointer message, NativeLong size);
+	int flib_netconn_send_engineMessage(NetconnPtr conn, Pointer message, NativeSizeT size);
 	int flib_netconn_send_teamHogCount(NetconnPtr conn, String teamname, int hogcount);
 	int flib_netconn_send_teamColor(NetconnPtr conn, String teamname, int colorIndex);
 	int flib_netconn_send_weaponset(NetconnPtr conn, WeaponsetPtr weaponset);
@@ -1032,7 +1027,7 @@
 	int flib_netconn_send_mapMazeSize(NetconnPtr conn, int mazeSize);
 	int flib_netconn_send_mapSeed(NetconnPtr conn, String seed);
 	int flib_netconn_send_mapTheme(NetconnPtr conn, String theme);
-	int flib_netconn_send_mapDrawdata(NetconnPtr conn, Pointer drawData, NativeLong size);
+	int flib_netconn_send_mapDrawdata(NetconnPtr conn, Pointer drawData, NativeSizeT size);
 	int flib_netconn_send_script(NetconnPtr conn, String scriptName);
 	int flib_netconn_send_scheme(NetconnPtr conn, SchemePtr scheme);
 	int flib_netconn_send_roundfinished(NetconnPtr conn, boolean withoutError);
@@ -1086,15 +1081,15 @@
 	static final int GAME_END_ERROR = 3;
 	
 	GameconnPtr flib_gameconn_create(String playerName, GameSetupPtr setup, boolean netgame);
-	GameconnPtr flib_gameconn_create_playdemo(Buffer demo, NativeLong size);
-	GameconnPtr flib_gameconn_create_loadgame(String playerName, Buffer save, NativeLong size);
+	GameconnPtr flib_gameconn_create_playdemo(Buffer demo, NativeSizeT size);
+	GameconnPtr flib_gameconn_create_loadgame(String playerName, Buffer save, NativeSizeT size);
 	GameconnPtr flib_gameconn_create_campaign(String playerName, String seed, String script);
 
 	void flib_gameconn_destroy(GameconnPtr conn);
 	int flib_gameconn_getport(GameconnPtr conn);
 	void flib_gameconn_tick(GameconnPtr conn);
 
-	int flib_gameconn_send_enginemsg(GameconnPtr conn, Pointer data, NativeLong len);
+	int flib_gameconn_send_enginemsg(GameconnPtr conn, Pointer data, NativeSizeT len);
 	int flib_gameconn_send_textmsg(GameconnPtr conn, int msgtype, String msg);
 	int flib_gameconn_send_chatmsg(GameconnPtr conn, String playername, String msg);
 	int flib_gameconn_send_quit(GameconnPtr conn);
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/FrontlibTypeMapper.java	Sat Aug 18 13:21:37 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-package org.hedgewars.hedgeroid.frontlib;
-
-import com.sun.jna.DefaultTypeMapper;
-import com.sun.jna.FromNativeContext;
-import com.sun.jna.ToNativeContext;
-import com.sun.jna.TypeConverter;
-import com.sun.jna.TypeMapper;
-
-class FrontlibTypeMapper extends DefaultTypeMapper {
-    public static final TypeMapper INSTANCE = new FrontlibTypeMapper();
-    
-    protected FrontlibTypeMapper() {
-        addTypeConverter(Boolean.class, new BooleanConverter());
-    }
-}
-
-class BooleanConverter implements TypeConverter {
-    public Class<Byte> nativeType() {
-        return Byte.class;
-    }
-	public Object fromNative(Object value, FromNativeContext context) {
-		return ((Byte)value).intValue() != 0 ? Boolean.TRUE : Boolean.FALSE;
-	}
-	public Object toNative(Object value, ToNativeContext context) {
-		return Byte.valueOf((byte)(Boolean.TRUE.equals(value) ? 1 : 0));
-	}
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/frontlib/NativeSizeT.java	Sat Aug 18 13:39:05 2012 +0200
@@ -0,0 +1,39 @@
+package org.hedgewars.hedgeroid.frontlib;
+
+/**
+ * This class represents the native C type size_t. On Android, this type could be mapped with int,
+ * but we use a separate type to make it easier to adapt for other platforms if anyone wants to use
+ * the mappings elsewhere. 
+ */
+public final class NativeSizeT extends Number {
+	private static final long serialVersionUID = 1L;
+	private final long value;
+	
+	private NativeSizeT(long value) {
+		this.value = value;
+	}
+	
+	public static NativeSizeT valueOf(long l) {
+		return new NativeSizeT(l);
+	}
+	
+	@Override
+	public int intValue() {
+		return (int)value;
+	}
+	
+	@Override
+	public long longValue() {
+		return value;
+	}
+
+	@Override
+	public double doubleValue() {
+		return value;
+	}
+
+	@Override
+	public float floatValue() {
+		return value;
+	}
+}
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/ThreadedNetConnection.java	Sat Aug 18 13:21:37 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/ThreadedNetConnection.java	Sat Aug 18 13:39:05 2012 +0200
@@ -38,6 +38,7 @@
 import org.hedgewars.hedgeroid.frontlib.Frontlib.VoidCallback;
 import org.hedgewars.hedgeroid.frontlib.Frontlib.WeaponsetCallback;
 import org.hedgewars.hedgeroid.frontlib.Frontlib.WeaponsetPtr;
+import org.hedgewars.hedgeroid.frontlib.NativeSizeT;
 import org.hedgewars.hedgeroid.netplay.Netplay.FromNetHandler;
 import org.hedgewars.hedgeroid.netplay.Netplay.FromNetMsgType;
 import org.hedgewars.hedgeroid.util.FileUtils;
@@ -54,7 +55,6 @@
 import android.util.Pair;
 
 import com.sun.jna.Memory;
-import com.sun.jna.NativeLong;
 import com.sun.jna.Pointer;
 
 /**
@@ -312,7 +312,7 @@
 	};
 	
 	private final BytesCallback engineMessageCb = new BytesCallback() {
-		public void callback(Pointer context, Pointer buffer, NativeLong size) {
+		public void callback(Pointer context, Pointer buffer, NativeSizeT size) {
 			sendFromNet(MSG_ENGINE_MESSAGE, buffer.getByteArray(0, size.intValue()));
 		}
 	};
@@ -498,7 +498,7 @@
 				byte[] message = (byte[])msg.obj;
 				Memory mem = new Memory(message.length);
 				mem.write(0, message, 0, message.length);
-				FLIB.flib_netconn_send_engineMessage(conn, mem, new NativeLong(message.length));
+				FLIB.flib_netconn_send_engineMessage(conn, mem, NativeSizeT.valueOf(message.length));
 				break;
 			}
 			case MSG_SEND_ROUND_FINISHED: {
@@ -545,7 +545,7 @@
 				byte[] message = (byte[])msg.obj;
 				Memory mem = new Memory(message.length);
 				mem.write(0, message, 0, message.length);
-				FLIB.flib_netconn_send_mapDrawdata(conn, mem, new NativeLong(message.length));
+				FLIB.flib_netconn_send_mapDrawdata(conn, mem, NativeSizeT.valueOf(message.length));
 				break;
 			}
 			case MSG_SEND_GAMESTYLE: {