-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
134 lines (112 loc) · 3.6 KB
/
index.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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require("fs");
const Unlocker = require("./unlocker");
const GitHubUpdater = require("./updater");
const packageJsonPath = path.join(__dirname, 'package.json');
const packageData = require(packageJsonPath)
const updater = new GitHubUpdater(packageData.author, packageData.name);
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
resizable: false,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
win.loadFile('index.html');
}
const checkWeModPath = (root) => fs.existsSync(path.join(root, 'WeMod.exe')) &&
fs.existsSync(path.join(root, 'resources/app.asar'))
function log(message, type = 'info') {
BrowserWindow.getAllWindows().forEach(win => {
win.webContents.send('log', { message, type });
});
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
ipcMain.handle('select-file', async () => {
const result = await dialog.showOpenDialog({
properties: ["openDirectory"],
defaultPath: process.env.LOCALAPPDATA || path.join(process.env.HOME || process.env.USERPROFILE, 'AppData', 'Local')
});
if (!result.canceled && result.filePaths.length > 0) {
return {
filePath: result.filePaths[0],
fileName: path.basename(result.filePaths[0]),
valid: checkWeModPath(result.filePaths[0])
};
}
return null;
});
ipcMain.handle('apply-patch', async (event, path) => {
const unlocker = new Unlocker(path, (e) => log(e))
await unlocker.start()
})
ipcMain.handle('resolve-default-path', async () => {
const defaultDir = path.join(process.env.LOCALAPPDATA || path.join(process.env.HOME || process.env.USERPROFILE, 'AppData', 'Local'), 'WeMod');
if (!fs.existsSync(defaultDir)) {
return null;
}
const items = fs.readdirSync(defaultDir, {withFileTypes: true});
const appFolders = items
.filter(item => item.isDirectory() && /^app-\w+/.test(item.name))
.map(item => {
const folderPath = path.join(defaultDir, item.name);
const stats = fs.statSync(folderPath);
return {
name: item.name,
path: folderPath,
mtime: stats.mtime,
};
});
let appDir = null;
appFolders.sort((a, b) => b.mtime - a.mtime);
for (const folder of appFolders) {
if (checkWeModPath(folder.path)) {
appDir = folder.path;
break;
}
}
return appDir;
})
ipcMain.handle('start-patch', async (event, filePath) => {
try {
// stub
return {
success: true,
message: 'Patch completed successfully!'
};
} catch (error) {
return {
success: false,
message: error.message
};
}
});
ipcMain.handle("get-current-version", () => {
return packageData.version
})
ipcMain.handle("check-updates", async () => {
return await updater.checkForUpdates()
})
ipcMain.on('open-link', () => {
require('electron').shell.openExternal("/~https://github.com/k1tbyte/Wemod-Patcher")
})
ipcMain.on("apply-update", async (event, source) => {
try {
log("Downloading update ...")
const path = await updater.downloadUpdate(source)
log("Preparation")
updater.applyUpdate(path)
} catch (err) {
log(err, "error")
}
})