-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
218 lines (182 loc) · 5.77 KB
/
main.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
const { app, BrowserWindow, ipcMain, dialog, shell } = require("electron");
const fs = require("fs");
const path = require("path");
const { VPK, VPKCopy, VPatcher } = require("./tfvpktool/")
const os = require('os');
const storage = require('electron-json-storage');
function getTextEncoding(buf) {
var d = buf.subarray(0, 5);
return d[0] === 0xfe && d[1] === 0xff ? "utf16be" : d[0] === 0xff && d[1] === 0xfe ? "utf16le" : "utf8";
}
// Initialise + read custom.css
const customCSSPath = app.getPath('userData') + "\\custom.css";
if(!fs.existsSync(customCSSPath)) {
fs.writeFileSync(customCSSPath, "/* Add your own custom CSS to Harmony VPK Tool */\n\n")
}
const customCSS = fs.readFileSync(customCSSPath, "utf8");
var settings = storage.getSync('settings');
if(settings.threadCount == undefined || Number.isNaN(settings.threadCount)) {
settings.threadCount = Math.max(8, os.cpus().length);
}
if(settings.patchWavs == undefined || typeof settings.patchWavs != 'boolean') {
settings.patchWavs = true;
}
storage.set('settings', settings);
async function handleVPKOpen(event) {
const { canceled, filePaths } = await dialog.showOpenDialog({
title: "Open VPK File",
filters: [
{ name: 'VPK Archives', extensions: ['vpk'] },
],
properties: ['openFile']
})
if (canceled) {
return
} else {
return filePaths[0]
}
}
async function handleReadVPKTree(event, vpkPath) {
let vpk = new VPK(vpkPath);
vpk.readTree();
return vpk;
}
let copierCloseFunc = () => {};;
async function handleCopyFiles(event, vpkPath, files) {
const { canceled, filePaths } = await dialog.showOpenDialog({
title: "Select Output Folder",
properties: ['openDirectory']
})
if (canceled)
return false
let copier = new VPKCopy(vpkPath, settings.threadCount, settings.patchWavs);
copier.on("progress", p => {
mainWindow.webContents.send("copyProgress", p);
})
copier.copy(files, filePaths[0]).then(() => {
mainWindow.webContents.send("copyDone");
copierCloseFunc();
})
copierCloseFunc = () => {
copier.close();
copierCloseFunc = () => {};
}
return true;
}
async function handleCopyCancel(event) {
copierCloseFunc();
}
async function handleSelectPatchFile(event) {
const { canceled, filePaths } = await dialog.showOpenDialog({
title: "Select Replacement File",
properties: ['openFile']
})
if (canceled) {
return
} else {
return filePaths[0]
}
}
async function handleStartPatch(event, vpkPath, files) {
const { canceled, filePaths } = await dialog.showOpenDialog({
title: "Select Output Folder",
defaultPath: vpkPath.replaceAll("\\", "/").substring(0, vpkPath.lastIndexOf("/")+1),
properties: ['openDirectory']
})
if (canceled)
return false
let outDir = filePaths[0].replaceAll("\\", "/") + "/";
let patcher = new VPatcher(vpkPath);
if(outDir == path.dirname(vpkPath)+"/") {
if(fs.existsSync(vpkPath+".bak")) {
let bakIdx = 1;
while(fs.existsSync(vpkPath+".bak"+bakIdx))
bakIdx++;
fs.renameSync(vpkPath, vpkPath+".bak"+bakIdx);
} else {
fs.renameSync(vpkPath, vpkPath+".bak");
}
}
for(let file of files) {
await patcher.addFile(file.inPath, file.outPath);
}
await patcher.writeVPK(outDir + vpkPath.split("/").pop());
return true;
}
async function handlePreviewFile(event, vpkPath, filePath, isText) {
let vpk = new VPK(vpkPath);
vpk.readTree();
let buf = await vpk.readFile(filePath, true);
vpk.close();
let outPath = path.join(app.getPath("temp"), "vpkpreview", filePath).replaceAll("\\", "/");
await fs.promises.mkdir(outPath.substring(0, outPath.lastIndexOf("/")+1), { recursive: true });
if(isText) {
await fs.promises.writeFile(outPath, buf, getTextEncoding(buf));
return { buf: buf.toString(getTextEncoding(buf)), outPath };
} else {
await fs.promises.writeFile(outPath, buf);
return { buf, outPath };
}
}
async function handleBrowsePreviewFile(event, filePath) {
let tempPreviewPath = filePath.substring(0, filePath.lastIndexOf("/")+1);
shell.openPath(tempPreviewPath);
}
async function handleGetSettings(event) {
settings = storage.getSync('settings', settings);
return settings;
}
async function handleUpdateSettings(event, newSettings) {
Object.assign(settings, newSettings)
storage.set('settings', settings);
}
async function handleGetCustomCSS(event) {
return customCSS;
}
let mainWindow;
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
height: 640,
width: 960,
minHeight: 640,
minWidth: 960,
icon: __dirname + '/img/harmony.ico',
title: "Harmony VPK Tool",
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
show: false
});
mainWindow.setMenu(null);
if(app.commandLine.hasSwitch("dev-tools")) {
mainWindow.openDevTools();
}
mainWindow.on('ready-to-show', function() {
mainWindow.show();
mainWindow.focus();
});
mainWindow.loadFile(path.join(__dirname, "index.html"));
}
app.on("ready", () => {
ipcMain.handle('openVPK', handleVPKOpen)
ipcMain.handle('readVPKTree', handleReadVPKTree)
ipcMain.handle('copyFiles', handleCopyFiles)
ipcMain.handle('copyCancel', handleCopyCancel)
ipcMain.handle('selectPatchFile', handleSelectPatchFile)
ipcMain.handle('startPatch', handleStartPatch)
ipcMain.handle('previewFile', handlePreviewFile)
ipcMain.handle('browsePreviewFile', handleBrowsePreviewFile)
ipcMain.handle('getSettings', handleGetSettings)
ipcMain.handle('updateSettings', handleUpdateSettings)
ipcMain.handle('getCustomCSS', handleGetCustomCSS)
createWindow();
app.on("activate", function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});