forked from PojavLauncherTeam/PojavLauncher
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathAsyncAssetManager.java
More file actions
172 lines (150 loc) · 7.68 KB
/
Copy pathAsyncAssetManager.java
File metadata and controls
172 lines (150 loc) · 7.68 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package net.kdt.pojavlaunch.tasks;
import static net.kdt.pojavlaunch.Architecture.archAsString;
import static net.kdt.pojavlaunch.Architecture.archAsStringAndroid;
import static net.kdt.pojavlaunch.Architecture.getDeviceArchitecture;
import static net.kdt.pojavlaunch.PojavApplication.sExecutorService;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import com.kdt.mcgui.ProgressLayout;
import net.kdt.pojavlaunch.Architecture;
import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.multirt.MultiRTUtils;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class AsyncAssetManager {
private AsyncAssetManager(){}
/**
* Attempt to install the java 8 runtime, if necessary
* @param am App context
*/
public static void unpackRuntime(AssetManager am) {
/* Check if JRE is included */
String rt_version = null;
String current_rt_version = MultiRTUtils.readInternalRuntimeVersion("Internal");
try {
rt_version = Tools.read(am.open("components/jre/version"));
} catch (IOException e) {
Log.e("JREAuto", "JRE was not included on this APK.", e);
}
String exactJREName = MultiRTUtils.getExactJreName(8);
if(current_rt_version == null && exactJREName != null && !exactJREName.equals("Internal")/*this clause is for when the internal runtime is goofed*/) return;
if(rt_version == null) return;
if(rt_version.equals(current_rt_version)) return;
// Install the runtime in an async manner, hope for the best
String finalRt_version = rt_version;
sExecutorService.execute(() -> {
try {
MultiRTUtils.installRuntimeNamedBinpack(
am.open("components/jre/universal.tar.xz"),
am.open("components/jre/bin-" + archAsString(Tools.DEVICE_ARCHITECTURE) + ".tar.xz"),
"Internal", finalRt_version);
MultiRTUtils.postPrepare("Internal");
}catch (IOException e) {
Log.e("JREAuto", "Internal JRE unpack failed", e);
}
});
}
/** Unpack single files, with no regard to version tracking */
public static void unpackSingleFiles(Context ctx){
ProgressLayout.setProgress(ProgressLayout.EXTRACT_SINGLE_FILES, 0);
sExecutorService.execute(() -> {
try {
Tools.copyAssetFile(ctx, "options.txt", Tools.DIR_GAME_NEW, false);
Tools.copyAssetFile(ctx, "default.json", Tools.CTRLMAP_PATH, false);
Tools.copyAssetFile(ctx, "launcher_profiles.json", Tools.DIR_GAME_NEW, false);
Tools.copyAssetFile(ctx,"resolv.conf",Tools.DIR_DATA, false);
} catch (IOException e) {
Log.e("AsyncAssetManager", "Failed to unpack critical components !");
}
ProgressLayout.clearProgress(ProgressLayout.EXTRACT_SINGLE_FILES);
});
}
public static void unpackComponents(Context ctx){
ProgressLayout.setProgress(ProgressLayout.EXTRACT_COMPONENTS, 0);
sExecutorService.execute(() -> {
try {
unpackComponent(ctx, "caciocavallo", false);
unpackComponent(ctx, "caciocavallo17", false);
// Since the Java module system doesn't allow multiple JARs to declare the same module,
// we repack them to a single file here
unpackLwjglNatives(ctx);
unpackComponent(ctx, "lwjgl3/3.3.3", false);
unpackComponent(ctx, "lwjgl3/3.4.1", false);
unpackComponent(ctx, "security", true);
unpackComponent(ctx, "arc_dns_injector", true);
unpackComponent(ctx, "methods_injector_agent", true);
unpackComponent(ctx, "forge_installer", true);
} catch (IOException e) {
Log.e("AsyncAssetManager", "Failed to unpack components !",e );
}
ProgressLayout.clearProgress(ProgressLayout.EXTRACT_COMPONENTS);
});
}
// Piggybacks off of the java modules extracting later to use their version files for update checks
// This is indeed prone to breaking.
private static void unpackLwjglNatives(Context ctx) throws IOException {
AssetManager am = ctx.getAssets();
String rootDir = Tools.DIR_DATA;
String sArch = archAsStringAndroid(getDeviceArchitecture());
String[] lwjglVersions = {"3.3.3", "3.4.1"};
for (String lwjglVer : lwjglVersions) {
File versionFile = new File(Tools.DIR_GAME_HOME + String.format("/lwjgl3/%s/version", lwjglVer));
InputStream is = am.open("components/lwjgl3/" + lwjglVer + "/version");
String pathToLwjglNatives = String.format("lwjgl-%s-natives/", lwjglVer) + sArch;
boolean shouldUpdate = true;
if (versionFile.exists()) {
FileInputStream fis = new FileInputStream(versionFile);
String release1 = Tools.read(is);
String release2 = Tools.read(fis);
if (release1.equals(release2))
shouldUpdate = false;
}
if (shouldUpdate) {
Log.i("UnpackLwjgl", lwjglVer + " was installed manually, or does not exist, unpacking new...");
String[] fileList = am.list("components/" + pathToLwjglNatives);
for (String fileName : fileList) {
Tools.copyAssetFile(ctx, "components/" + pathToLwjglNatives + "/" + fileName, rootDir + "/" + pathToLwjglNatives, true);
}
} else {
Log.i("UnpackLwjgl", lwjglVer + " is up-to-date with the launcher, continuing...");
}
}
}
private static void unpackComponent(Context ctx, String component, boolean privateDirectory) throws IOException {
AssetManager am = ctx.getAssets();
String rootDir = privateDirectory ? Tools.DIR_DATA : Tools.DIR_GAME_HOME;
File versionFile = new File(rootDir + "/" + component + "/version");
InputStream is = am.open("components/" + component + "/version");
if(!versionFile.exists()) {
if (versionFile.getParentFile().exists() && versionFile.getParentFile().isDirectory()) {
FileUtils.deleteDirectory(versionFile.getParentFile());
}
versionFile.getParentFile().mkdir();
Log.i("UnpackPrep", component + ": Pack was installed manually, or does not exist, unpacking new...");
String[] fileList = am.list("components/" + component);
for(String s : fileList) {
Tools.copyAssetFile(ctx, "components/" + component + "/" + s, rootDir + "/" + component, true);
}
} else {
FileInputStream fis = new FileInputStream(versionFile);
String release1 = Tools.read(is);
String release2 = Tools.read(fis);
if (!release1.equals(release2)) {
if (versionFile.getParentFile().exists() && versionFile.getParentFile().isDirectory()) {
FileUtils.deleteDirectory(versionFile.getParentFile());
}
versionFile.getParentFile().mkdir();
String[] fileList = am.list("components/" + component);
for (String fileName : fileList) {
Tools.copyAssetFile(ctx, "components/" + component + "/" + fileName, rootDir + "/" + component, true);
}
} else {
Log.i("UnpackPrep", component + ": Pack is up-to-date with the launcher, continuing...");
}
}
}
}