-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path#modpacker.js
More file actions
315 lines (261 loc) · 7.95 KB
/
#modpacker.js
File metadata and controls
315 lines (261 loc) · 7.95 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
LIBRARY({
name: "#modpacker",
version: 1,
shared: false,
api: "PrefsWinAPI"
});
var Environment = android.os.Environment;
var Scanner = java.util.Scanner;
var File = java.io.File;
var StringBuilder = java.lang.StringBuilder;
var BufferedWriter = java.io.BufferedWriter;
var FileWriter = java.io.FileWriter;
var AlertDialog = android.app.AlertDialog;
var LinearLayout = android.widget.LinearLayout;
var TextView = android.widget.TextView;
var ProgressBar = android.widget.ProgressBar;
var BitmapFactory = android.graphics.BitmapFactory;
var BitmapDrawable = android.graphics.drawable.BitmapDrawable;
var version_file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/games/com.mojang/mods/versions.json";
var ctx = UI.getContext();
function inThread(func) {
(new java.lang.Thread({
run: function() {
try {
func();
} catch(e) {
log(e);
}
}
})).start();
}
function runAsUI(func){
ctx.runOnUiThread(new java.lang.Runnable({ run: function(){
try{
func();
}catch(e){
log(e);
}}
}));
}
var DownloadHandler = {
downloads: [],
addDownload: function(url, callbacks) {
var download = {
url: url,
status: "prepared",
isCancelled: function() {
return this.status == "stopped";
},
cancel: function() {
if (!this.isCancelled() && this.status != "finishing") {
this.status = "stopped";
if (this._cancel) {
this._cancel();
}
}
}
};
for (var name in callbacks) {
download["_" + name] = callbacks[name];
}
this.downloads.push(download);
inThread(function() {
if (!download.isCancelled()) {
download.status = "running";
if (download._start) {
download._start();
}
}
try {
var result = Network.downloadFile(url, {
message: function(msg) {
if (download._message && !download.isCancelled()) {
download._message(msg);
}
},
progress: function(p) {
if (download._progress && !download.isCancelled()) {
download._progress(p);
}
},
isCancelled: function() {
return download.isCancelled();
}
});
} catch(e) {
if (download._fail) {
download._fail(e);
}
}
if (!download.isCancelled()) {
download.status = "finishing";
if (result) {
if (download._complete) {
download._complete(result);
}
}
else {
if (download._fail) {
download._fail();
}
}
download.status = "stopped";
}
});
},
stop: function(url) {
for (var i in this.downloads) {
var download = this.downloads[i];
if (download.url == url) {
this.downloads.splice(i--);
download.cancel();
}
}
},
stopAll: function() {
for (var i in this.downloads) {
this.downloads[i].cancel();
}
this.downloads = [];
}
};
function getVersions(){
var file = new File(version_file);
if(!file.exists()){
return {};
}
var sc = new Scanner(file);
var builder = new StringBuilder();
while(sc.hasNextLine()){
builder.append(sc.nextLine());
}
try{
var str = builder.toString();
var obj = JSON.parse(str);
if(typeof obj === "object") return obj;
else return {};
} catch(e){
log(e);
return {};
}
}
function writeVersions(versions){
var writer = new BufferedWriter(new FileWriter(version_file));
writer.write(JSON.stringify(versions));
writer.close();
}
function getCurrentVersion(id){
return parseInt(Network.getURLContents("https://icmods.mineprogramming.org/api/version.php?id=" + id));
}
var text;
var status;
var progress;
var count;
function showUI(name, count, icon){
runAsUI(function(){
var builder = new AlertDialog.Builder(ctx);
builder.setTitle(name + " | #modpacker");
if(icon){
icon = BitmapFactory.decodeFile(icon);
builder.setIcon(new BitmapDrawable(icon));
}
var layout = new LinearLayout(ctx);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(15, 15, 15, 15);
text = new TextView(ctx);
text.setText("Downloading and installing mods 0/" + count);
layout.addView(text);
status = new TextView(ctx);
status.setText("Downloading...");
layout.addView(status);
progress = new ProgressBar(ctx, null, android.R.attr.progressBarStyleHorizontal);
progress.setMax(count);
layout.addView(progress);
builder.setView(layout);
builder.create().show();
});
}
function setStatus(s){
runAsUI(function(){
status.setText(s);
});
}
function addProgress(){
runAsUI(function(){
let p = progress.getProgress() + 1;
if(p >= progress.getMax()){
status.setText("Success! Restart Inner Core to proceed!");
}
progress.setProgress(p);
text.setText("Downloading and installing mods " + p + "/" + count);
});
}
function install(id, versions){
let url = "https://icmods.mineprogramming.org/api/download.php?id=" + id;
setStatus("Downloading...");
DownloadHandler.addDownload(url, {
message: function(msg) {
setStatus(msg);
},
complete: function(result) {
try {
var success = Prefs.installModFile(result, {
message: function(msg) {
}
}, {
run: function() {
setStatus("Installing...");
}
});
if(success) {
addProgress();
versions["" + id] = getCurrentVersion(id);
writeVersions(versions);
}
else {
setStatus("Failed to install mod!");
DownloadHandler.stopAll();
}
} catch(e) {
setStatus("Failed to install mod! " + e);
DownloadHandler.stopAll();
}
},
fail: function() {
setStatus("Failed to download mod!");
DownloadHandler.stopAll();
},
cancel: function() {
status("Failed to download mod!");
DownloadHandler.stopAll();
}
});
}
function installAll(name, ids, icon){
let versions = getVersions();
count = 0;
for(var i in ids){
let id = ids[i];
if(!versions["" + id]){
count++;
}
}
if(count > 0){
showUI(name, count, icon);
for(var i in ids){
let id = ids[i];
if(!versions["" + id]){
install(id, versions);
}
}
}
}
function restartInnerCore(){
}
var ModPack = {
install: function(data){
installAll(data.name, data.mods, data.icon);
}
}
EXPORT("ModPack", ModPack);