package pw.cloudef.rpg; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.net.Uri; import android.util.Log; import android.system.Os; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.nio.charset.Charset; import java.util.zip.ZipFile; import java.util.zip.ZipEntry; import java.util.Enumeration; public class Launcher extends Activity { protected String gameId() { return getIntent().getExtras().getString("id"); } protected String zipPath() { return getApplicationContext().getCacheDir().toPath().toString() + "/" + gameId() + ".zip"; } protected String baseDir() { return getApplicationContext().getFilesDir().toPath().toString() + "/data/" + gameId(); } protected String rtpDir() { return getApplicationContext().getFilesDir().toPath().toString() + "/rtp"; } enum GameType { UNKNOWN, EASYRPG, MKXP, NODEWEB, }; protected Class getActivityForZip(File file) { ZipFile zipFile; try { zipFile = new ZipFile(file, java.util.zip.ZipFile.OPEN_READ, Charset.forName("Windows-31J")); } catch (Exception e) { Log.d("pw.cloudef.rpg", e.toString()); return null; } try { Enumeration zipEntries = zipFile.entries(); GameType type = GameType.UNKNOWN; while (zipEntries.hasMoreElements()) { String fname = ((ZipEntry)zipEntries.nextElement()).getName(); try { String base = fname.split(".+?/(?=[^/]+$)")[1]; String name = base.split("\\.(?=[^\\.]+$)")[0]; if (name.toUpperCase().equals("RPG_RT")) type = GameType.EASYRPG; else if (name.toUpperCase().equals("GAME")) type = GameType.MKXP; else if (base.toUpperCase().equals("RPG_CORE.JS")) type = GameType.NODEWEB; } catch (Exception e2) {} if (type == GameType.EASYRPG || type == GameType.NODEWEB) break; } switch (type) { case EASYRPG: return Easyrpg.class; case MKXP: return Mkxp.class; case NODEWEB: return Nodeweb.class; } } catch (Exception e) { Log.d("pw.cloudef.rpg", e.toString()); } try { zipFile.close(); } catch (Exception e) {} return null; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { InputStream input = getContentResolver().openInputStream(getIntent().getData()); File file = new File(zipPath()); OutputStream output = new FileOutputStream(file); byte[] buf = new byte[8 * 1024]; for (int read = 0; (read = input.read(buf)) != -1;) output.write(buf, 0, read); Class activity = getActivityForZip(file); if (activity == null) throw new Exception("Can't handle this game"); new File(baseDir()).mkdirs(); new File(rtpDir()).mkdirs(); try { Os.remove(baseDir() + "/game.zip"); } catch (Exception e2) {} Os.symlink(zipPath(), baseDir() + "/game.zip"); try { Os.remove(baseDir() + "/rtp"); } catch (Exception e2) {} Os.symlink(rtpDir(), baseDir() + "/rtp"); Intent intent = new Intent(this, activity); intent.putExtra("id", gameId()); startActivity(intent); finish(); } catch (Exception e) { Log.d("pw.cloudef.rpg", e.toString()); try { Os.remove(zipPath()); } catch (Exception e2) {} try { Os.remove(baseDir() + "/game.zip"); } catch (Exception e2) {} } finishAndRemoveTask(); } }