This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfilewriter.ts
114 lines (104 loc) · 4.18 KB
/
filewriter.ts
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
import Main from "../main";
import fs from "fs";
import ProgressBar from "electron-progressbar";
import jszip from "jszip";
import path from "path";
import log from "electron-log";
import filemanager from "./file_manager";
const loadingTextStyle = {
color: "ghostwhite"
};
class FileWriter
{
/**
* Extract a ZIP file to the target directory.
* @param targetPath Path to write all contents
* @param data ZIP file as a buffer
* @param currentModName Current mod name for this operation.
* @returns If this was succcessful.
*/
public static async ExtractZip(targetPath: string, data: Buffer, currentModName: string): Promise<boolean> {
const fileListObject = await filemanager.GetFileList(currentModName);
let active = true;
const progressBar = new ProgressBar({
indeterminate: false,
text: "Extracting data",
detail: "Starting data extraction...",
abortOnError: true,
closeOnComplete: false,
browserWindow: {
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
parent: Main.mainWindow,
modal: true,
title: "Extracting files...",
backgroundColor: "#2b2826",
closable: true
},
style: {
text: loadingTextStyle,
detail: loadingTextStyle,
value: loadingTextStyle
},
maxValue: 1
});
progressBar.on("completed", () => {
progressBar.detail = "Extraction completed. Exiting...";
});
//Create the target directory if it doesnt exist somehow.
if(!fs.existsSync(targetPath)){
fs.mkdirSync(targetPath, {recursive: true});
}
progressBar.on("aborted", () => {
active = false;
throw new Error("Extraction aborted by the user. You will need to restart the installation process to install this mod.");
});
const zip = await jszip.loadAsync(data.buffer);
const allFiles = Object.values(zip.files);
let filesWritten = 0;
for(let i = 0; i < allFiles.length; i++){
if (!active) {
return false;
}
const file = allFiles[i];
const fullPath = path.join(targetPath, file.name);
if(file.dir){
//Make missing directories syncronously as they MUST exist before we write.
fs.mkdirSync(fullPath, {recursive: true});
log.log("ExtractZip: Wrote directory: " + fullPath);
}
else {
const data = await zip.file(file.name).async("uint8array");
//Check and make missing folder paths anyway as we cannot guarantee the order of the elements given from JSZip.
//Check for the directory and write the file synchronously, otherwise missing path errors can happen. Do NOT change.
const folderPathOnly = path.dirname(fullPath);
if(!fs.existsSync(folderPathOnly)){
fs.mkdirSync(folderPathOnly, {recursive: true});
}
fs.writeFileSync(fullPath, data);
//Add file that we wrote to the file list
if (!fileListObject.files.includes(fullPath)) {
fileListObject.files.push(fullPath);
}
filesWritten++;
log.log(`ExtractZip: Wrote file: ${fullPath} (${i}/${allFiles.length})`);
try {
progressBar.detail = `Wrote ${file.name}. Total Files Written: ${filesWritten}.`;
progressBar.value = i / allFiles.length;
}
catch(e){
log.error("Error when trying to set progressbar data: " + e.toString());
}
}
}
active = false;
progressBar.setCompleted();
progressBar.close();
filemanager.SaveFileList(fileListObject, currentModName);
log.log("Update mod file list successfully.");
return true;
}
}
export default FileWriter;