Determining if the game is on Android or PC

I am trying to implement a method to save the game state so that the user can restart a previous game. I’ve got the classes done that implement Savable and have the data being saved and read from them. The problem is that the location of the file is different for PC versions vs Android versions so I need to come up with a way in the game to determine which operating platform the game is being run on so that I can change the location of the file.



In Android, I think I need to use android.os.Environment.getExternalStorageDirectory().getAbsolutePath() to get the path to the SDCARD, where as on a PC I need to use System.getProperty(“user.home”);



Any advice would be appreciated.

JmeSystem.getPlataform()



[java]

public static Platform getPlatform() {

String os = System.getProperty("os.name").toLowerCase();

String arch = System.getProperty("os.arch").toLowerCase();

boolean is64 = is64Bit(arch);

if (os.contains("windows")) {

return is64 ? Platform.Windows64 : Platform.Windows32;

} else if (os.contains("linux") || os.contains("freebsd") || os.contains("sunos")) {

return is64 ? Platform.Linux64 : Platform.Linux32;

} else if (os.contains("mac os x") || os.contains("darwin")) {

if (arch.startsWith("ppc")) {

return is64 ? Platform.MacOSX_PPC64 : Platform.MacOSX_PPC32;

} else {

return is64 ? Platform.MacOSX64 : Platform.MacOSX32;

}

} else {

throw new UnsupportedOperationException("The specified platform: " + os + " is not supported.");

}

}

[/java]

Use java preferences for that, they are always stored in a location that is compatible with the OS you run on.

Thanks for the reference. @normen, not sure I understand what you mean. I have multiple j3o files to save. Arent preferences just for used to save variable settings?

Preferences can store any string, you can encode any binary data into a string using e.g. uuencode.

I didn’t realize that. Thanks.

This is what I ended up doing. Runs on both the PC and Android device:



[java]

Platform = JmeSystem.getPlatform().toString();

isAndroid = Platform.contains("Android");



logger.log(Level.INFO, "Platform: {0}", JmeSystem.getPlatform().toString());

logger.log(Level.INFO, "isAndroid: {0}", isAndroid);



if (isAndroid) {



try {

Class clazz = Class.forName("android.os.Environment");

Method meth = clazz.getDeclaredMethod("getExternalStorageDirectory");



Object tmpObj = meth.invoke(null, (Object[]) null);

if (tmpObj == null) {

logger.log(Level.INFO, "invoke returned null");

} else {

if (tmpObj instanceof File) {

strStorageRoot = ((File) tmpObj).getAbsolutePath();

logger.log(Level.INFO, "strStorageRoot: {0}", strStorageRoot);

} else {

logger.log(Level.INFO, "invoke returned non-File object: {0}", tmpObj.getClass().getName());

}

}



} catch (Exception e) {

System.out.println("ERROR: " + e.getLocalizedMessage());

}



if (strStorageRoot.length() < 1) {

strStorageRoot = "/mnt/sdcard";

}

strStorageRoot += "/Android/data/com.mycompany.mygame/files/";



} else {

strStorageRoot = System.getProperty("user.home") + "&quot;;

}



logger.log(Level.INFO, "strStorageRoot: {0}", strStorageRoot);

[/java]

1 Like

You could simplify it by creating a shared interface which you implement on each platform separately

Like OsSpecific would have saveGame(byte[] data) which would use the appropriate OS functions to write to the file

1 Like