summaryrefslogtreecommitdiff
path: root/src/pw/cloudef/rpg/Launcher.java
diff options
context:
space:
mode:
authorJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-16 18:49:26 +0900
committerJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-30 00:39:06 +0900
commitfcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch)
tree64cb17de3f41a2b6fef2368028fbd00349946994 /src/pw/cloudef/rpg/Launcher.java
Fresh start
Diffstat (limited to 'src/pw/cloudef/rpg/Launcher.java')
-rw-r--r--src/pw/cloudef/rpg/Launcher.java104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/pw/cloudef/rpg/Launcher.java b/src/pw/cloudef/rpg/Launcher.java
new file mode 100644
index 0000000..626ac14
--- /dev/null
+++ b/src/pw/cloudef/rpg/Launcher.java
@@ -0,0 +1,104 @@
+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();
+ }
+}