summaryrefslogtreecommitdiff
path: root/src/pw/cloudef/rpg/Launcher.java
blob: 626ac14faeee402d6e63ba77dfa510ac6f875076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 == nullthrow 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();
   }
}