-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaup-js.js
394 lines (338 loc) · 11.5 KB
/
uaup-js.js
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//////////////////// IMPORTING ////////////////////
//#region IMPORTING
const request = require('request');
const fs = require('fs');
const separator = require('path').sep;
//#endregion IMPORTING
//////////////////// GLOBAL VARIABLES ////////////////////
//#region GLOBAL VARIABLES
var app_library = (process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share")) + separator;
var git_api;
var new_version = "unknown";
var current_version = "unknown";
var dl_bar = null;
var dl_label = null;
const defaultStages = {
Checking: "Checking For Updates!",
Found: "Update Found!",
NotFound: "No Update Found.",
Downloading: "Downloading...",
Unzipping: "Installing...",
Cleaning: "Finalizing...",
Launch: "Launching..."
};
const defaultOptions = {
useGithub: true,
gitRepo: "unknown",
gitUsername: "unknown",
isGitRepoPrivate: false,
gitRepoToken: "uknown",
appName: "unknown",
appExecutableName: this.appName + "",
appDirectory: app_library + this.appName,
versionFile: this.appDirectory + `${separator}settings${separator}version.json`,
tempDirectory: this.appDirectory + `${separator}tmp"`,
progressBar: null,
label: null,
forceUpdate: false,
stageTitles: defaultStages
};
//#endregion GLOBAL VARIABLES
//////////////////// INITIALIZATION ////////////////////
//#region INITIALIZATION
/**
* Tests if the Required Fields are Filled out
* @param {defaultOptions} options
*/
function testOptions(options) {
//return !(options.appName === "unknown" || (options.useGithub && (options.gitUsername === "unknown" || options.gitRepo === "unknown")) || (options.isGitRepoPrivate && options.gitRepoToken === "unknown"));
return true
}
/**
* Updates Global Variables based on the Provided Options
* @param {defaultOptions} options
*/
function setOptions(options) {
// Sets the Default Values
options.useGithub = options.useGithub == null ? true : options.appDirectory;
options.forceUpdate = options.forceUpdate == null ? false : options.forceUpdate;
options.appDirectory = options.appDirectory == null ? app_library + options.appName : options.appDirectory;
options.appDirectory += separator;
console.log("App Directory: " + options.appDirectory, "App Name: " + options.appName, app_library);
options.versionFile = options.versionFile == null ? options.appDirectory + `${separator}settings${separator}version.json` : options.versionFile;
options.tempDirectory = options.tempDirectory == null ? options.appDirectory +separator+"tmp" : options.tempDirectory;
options.appExecutableName = options.appExecutableName == null ? options.appName : options.appExecutableName;
options.stageTitles = options.stageTitles == null ? defaultOptions.stageTitles : options.stageTitles;
// Sets the Elements (if used)
if (options.label !== null)
dl_label = options.label;
if (options.progressBar !== null)
dl_bar = options.progressBar;
if (options.useGithub)
setupGitProtocol(options);
return options;
}
/**
* Sets the Values for GitHub based on the Options Provieded.
* @param {defaultOptions} options
*/
function setupGitProtocol(options) {
options.gitRepo = options.gitRepo.toString().replace('/ /gi', '-');
git_api = `https://api.github.com/repos/${options.gitUsername}/${options.gitRepo}/releases/latest`;
}
/**
* Creates the Required Directories for the Application to Run
* @param {defaultOptions} options
*/
function createDirectories(options) {
if (!fs.existsSync(options.appDirectory))
fs.mkdirSync(options.appDirectory, { recursive: true });
if (!fs.existsSync(options.tempDirectory))
fs.mkdirSync(options.tempDirectory, { recursive: true });
if (!fs.existsSync(require('path').dirname(options.versionFile)))
fs.mkdirSync(require('path').dirname(options.versionFile), { recursive: true });
}
//#endregion INITIALIZATION
//////////////////// VERSIONING ////////////////////
//#region VERSIONING
/**
* Gets the Direct Download URL From the GitHub API.
* @param {defaultOptions} options
* @returns {*} The Direct Download URL
*/
async function GetUpdateURL(options) {
return fetch(git_api).then(response => response.json()).then(data => { json = data; }).catch(e => {
try {
// Electron
alert(`Something went wrong: ${e}`);
} catch {
// NodeJS
console.error(`Something went wrong: ${e}`);
return;
}
}).then(() => {
let zip;
for (i = 0; i < json['assets'].length; i++) {
if (json['assets'][i]['name'] === `${options.appName}.zip`) zip = json['assets'][i];
}
return zip['browser_download_url'];
});
}
/**
* Gets the current relase version from GitHub
*/
async function GetUpdateVersion() {
return fetch(git_api).then(response => response.json()).then(data => { json = data; }).catch(e => {
try {
// Electron
alert(`Something went wrong: ${e}`);
} catch {
// NodeJS
console.error(`Something went wrong: ${e}`);
return;
}
}).then(() => {
return json['tag_name'];
});
}
/**
* Gets the Current Version from the File System
* @param {defaultOptions} options
*/
function GetCurrentVersion(options) {
return JSON.parse(fs.readFileSync(options.versionFile, (err, data) => {
if (err) throw err;
}))['game_version'];
}
/**
* Updates the Version File to Reflect the New Version
* @param {defaultOptions} options
*/
function UpdateCurrentVersion(options) {
let version = {
game_version: new_version,
last_updated: Date.now().toString()
};
fs.writeFileSync(options.versionFile, JSON.stringify(version));
}
//#endregion VERSIONING
//////////////////// LOGGING ////////////////////
//#region LOGGING
/**
* Updates the Progressbar, if any, and logs the percentage to console.
* @param {number} rb - Bytes already Downloaded
* @param {number} tb - Total Bytes to Download
*/
function showProgress(rb, tb) {
console.log(`${Math.floor((rb * 100) / tb)}% | ${rb} bytes of ${tb} bytes`);
try {
if (dl_bar !== null)
dl_bar.setAttribute('value', (rb * 100) / tb);
} catch { }
}
/**
* Updates the Status Label, if any, and Logs the value.
* @param {string} value
*/
function updateHeader(value) {
console.log(value);
try {
if (dl_label !== null)
dl_label.innerHTML = value;
} catch { }
}
//#endregion LOGGING
//////////////////// STAGES ////////////////////
//#region STAGES
/**
* Updates the Application based on the Provided Options
* @param {defaultOptions} options
*/
async function Update(options = defaultOptions) {
console.log(defaultOptions.appDirectory, defaultOptions.versionFile, defaultOptions.tempDirectory);
if (testOptions(options)) {
options = setOptions(options);
createDirectories(options);
if (options.forceUpdate || await CheckForUpdates(options)) {
updateHeader(options.stageTitles.Found);
await sleep(1000);
let url = await GetUpdateURL(options);
Download(url, `${options.tempDirectory}${separator}${options.appName}.zip`, options);
UpdateCurrentVersion(options);
} else {
updateHeader(options.stageTitles.NotFound);
await sleep(1000);
LaunchApplication(options)
}
} else {
try {
// Electron
alert(`Please Fill out the Options Fully`);
window.close();
} catch {
// NodeJS
console.log(`Please Fill out the Options Fully`);
return;
}
}
}
/**
* Checks if there are any updates
* @param {defaultOptions} options
* @returns {*} true if an update is needed and false if not
*/
async function CheckForUpdates(options = defaultOptions) {
if (testOptions(options)) {
options = setOptions(options);
createDirectories(options);
updateHeader(options.stageTitles.Checking);
await sleep(1000);
new_version = await GetUpdateVersion();
if (fs.existsSync(options.versionFile)) {
current_version = GetCurrentVersion(options);
if (current_version == "unknown") {
console.error('Unable to Load Current Version... Trying again');
return true;
}
return current_version !== new_version;
} else {
console.warn("Couln't find the Version File, this usually means that there was no previous update.")
return true;
}
}
}
/**
*
* @param {string} url - Direct Download URL
* @param {string} path - Temp Download Directory ex: /path/to/file.zip
*/
function Download(url, path, options) {
updateHeader(options.stageTitles.Downloading)
let received_bytes = 0;
let total_bytes = 0;
var req = request(
{
method: 'GET',
uri: url
}
);
var out = fs.createWriteStream(path);
req.pipe(out);
req.on('response', data => {
total_bytes = parseInt(data.headers['content-length']);
});
req.on('data', chunk => {
received_bytes += chunk.length;
showProgress(received_bytes, total_bytes);
});
req.on('end', () => {
Install(options)
});
}
/**
* Unzips the Downloaded Archive to the Application Directory
* @param {defaultOptions} options
*/
function Install(options) {
updateHeader(options.stageTitles.Unzipping);
var AdmZip = require('adm-zip');
var zip = new AdmZip(`${options.tempDirectory}/${options.appName}.zip`);
zip.extractAllTo(options.appDirectory, true);
setTimeout(() => CleanUp(options), 2000);
}
/**
* Removes any Temp Directories and Files.
* @param {defaultOptions} options
*/
function CleanUp(options) {
updateHeader(options.stageTitles.Cleaning);
fs.rmdirSync(options.tempDirectory, { recursive: true, maxRetries: 3, retryDelay: 500 })
setTimeout(() => LaunchApplication(options), 2000);
}
/**
* Launches the Application
* @param {defaultOptions} options
*/
function LaunchApplication(options) {
let executablePath = require('path').join(options.appDirectory, options.appExecutableName);
if (fs.existsSync(executablePath)) {
updateHeader(options.stageTitles.Launch);
let child = require('child_process').exec;
if (process.platform == "linux"){
child(`chmod 755 "${executablePath}"`, (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(data.toString());
})
}
var execFile = require('child_process').spawn;
execFile(executablePath, [], {'detached':true});
} else {
console.error(`File Not Found: ${executablePath}`);
options.forceUpdate = true;
Update(options);
return;
}
setTimeout(() => {
try {
// Electron
window.close();
} catch (e) {
// NodeJS
process.exit(0);
}
}, 1000);
}
//#endregion STAGES
//////////////////// UTILITIES ////////////////////
//#region UTILITIES
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function GetAppLibrary() {
return app_library;
}
//#endregion
module.exports = { Update, CheckForUpdates, GetAppLibrary };