-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
223 lines (191 loc) · 8.03 KB
/
Copy pathrenderer.js
File metadata and controls
223 lines (191 loc) · 8.03 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
const { ipcRenderer } = require('electron');
const fs = require('fs').promises;
const path = require('path');
let selectedDirectory = null; // Store the selected directory
function updateProgress(fileIndex, totalFiles) {
const percent = (fileIndex / totalFiles) * 100;
document.getElementById('total-progress').style.width = `${percent}%`;
document.getElementById('total-status').textContent = `${fileIndex}/${totalFiles} files`;
}
function formatSpeed(bytesPerSecond) {
if (bytesPerSecond > 1024 * 1024) {
return `${(bytesPerSecond / (1024 * 1024)).toFixed(2)} MB/s`;
} else if (bytesPerSecond > 1024) {
return `${(bytesPerSecond / 1024).toFixed(2)} KB/s`;
}
return `${bytesPerSecond.toFixed(2)} B/s`;
}
function updateDirectoryDisplay() {
const dirElement = document.getElementById('current-directory');
dirElement.textContent = selectedDirectory || 'Not selected';
}
function updateReadyStatus() {
const statusElement = document.getElementById('status');
if (!selectedDirectory) {
statusElement.textContent = 'Please select installation directory';
return false;
}
statusElement.textContent = 'Ready to launch';
return true;
}
// Update the document ready listener
document.addEventListener('DOMContentLoaded', async () => {
await loadSavedDirectory();
updateReadyStatus();
updateDirectoryDisplay();
});
async function selectInstallLocation() {
try {
const result = await ipcRenderer.invoke('select-directory');
if (result) {
selectedDirectory = result;
await ipcRenderer.invoke('save-install-dir', result);
updateReadyStatus();
updateDirectoryDisplay();
await updateConfig(); // Automatically update config when directory is selected
await checkFiles(); // Automatically check and download files
}
} catch (error) {
document.getElementById('status').textContent = `Error selecting directory: ${error.message}`;
}
}
async function loadSavedDirectory() {
try {
const savedDir = await ipcRenderer.invoke('get-install-dir');
if (savedDir && await fs.access(savedDir).then(() => true).catch(() => false)) {
selectedDirectory = savedDir;
updateReadyStatus();
updateDirectoryDisplay();
}
} catch (error) {
console.error('Error loading saved directory:', error);
}
}
async function checkFiles() {
if (!selectedDirectory) {
document.getElementById('status').textContent = 'Please select an installation directory first';
return;
}
try {
document.getElementById('status').textContent = 'Checking files...';
document.getElementById('file-progress').style.width = '0%';
document.getElementById('total-progress').style.width = '0%';
const requiredFiles = await ipcRenderer.invoke('load-required-files');
let completedFiles = 0;
document.getElementById('total-status').textContent = `0/${requiredFiles.length} files`;
for (const fileInfo of requiredFiles) {
const filePath = path.join(selectedDirectory, fileInfo.name);
try {
const stats = await fs.stat(filePath);
if (fileInfo.size === 0 && fileInfo.md5 === "") {
document.getElementById('status').textContent = `Skipping check for ${fileInfo.name}`;
} else if (stats.size !== fileInfo.size) {
await downloadFile(fileInfo, filePath);
} else {
const fileMd5 = await ipcRenderer.invoke('check-md5', filePath);
if (fileMd5 !== fileInfo.md5) {
await downloadFile(fileInfo, filePath);
}
}
} catch (error) {
await downloadFile(fileInfo, filePath);
}
completedFiles++;
updateProgress(completedFiles, requiredFiles.length);
}
document.getElementById('status').textContent = 'All files are up to date!';
document.getElementById('download-speed').textContent = '';
} catch (error) {
document.getElementById('status').textContent = `Error: ${error.message}`;
}
}
async function launchGame() {
if (!selectedDirectory) {
document.getElementById('status').textContent = 'Please select an installation directory first';
return;
}
try {
const exePath = path.join(selectedDirectory, 'SWGEmu.exe');
document.getElementById('status').textContent = 'Launching game...';
await ipcRenderer.invoke('launch-game', exePath);
document.getElementById('status').textContent = 'Game launched!';
} catch (error) {
document.getElementById('status').textContent = `Error launching game: ${error.message}`;
}
}
async function downloadFile(fileInfo, destination) {
document.getElementById('status').textContent = `Downloading ${fileInfo.name}...`;
document.getElementById('file-progress').style.width = '0%';
try {
// Skip MD5 check if size is 0 and MD5 is empty
const skipChecks = fileInfo.size === 0 && fileInfo.md5 === "";
const result = await ipcRenderer.invoke('download-file', {
url: fileInfo.url,
destination: destination,
expectedMd5: fileInfo.md5,
skipChecks: skipChecks
});
if (result === 'kept-with-mismatch') {
document.getElementById('status').textContent = `Kept ${fileInfo.name} despite MD5 mismatch`;
}
} catch (error) {
if (error.message.startsWith('MD5_MISMATCH:')) {
return downloadFile(fileInfo, destination);
}
throw error;
}
ipcRenderer.on('download-progress', (event, { percent, speed }) => {
const progressBar = document.getElementById('file-progress');
progressBar.style.width = `${percent}%`;
progressBar.classList.add('active');
document.getElementById('download-speed').textContent = formatSpeed(speed);
});
}
async function launchSettings() {
if (!selectedDirectory) {
document.getElementById('status').textContent = 'Please select an installation directory first';
return;
}
try {
const setupPath = path.join(selectedDirectory, 'SWGEmu_Setup.exe');
document.getElementById('status').textContent = 'Launching settings...';
await ipcRenderer.invoke('launch-admin', setupPath);
document.getElementById('status').textContent = 'Settings launched!';
} catch (error) {
document.getElementById('status').textContent = `Error launching settings: ${error.message}`;
}
}
async function updateConfig() {
if (!selectedDirectory) {
document.getElementById('status').textContent = 'Please select an installation directory first';
return;
}
try {
const config = `[ClientGame]\nloginServerAddress0=62.146.229.237\nloginServerPort0=44453`;
await ipcRenderer.invoke('update-config', {
directory: selectedDirectory,
content: config
});
document.getElementById('status').textContent = 'Server configuration updated successfully!';
} catch (error) {
document.getElementById('status').textContent = `Error updating config: ${error.message}`;
}
}
function removeActiveProgress() {
document.getElementById('file-progress').classList.remove('active');
}
function toggleConfig() {
const configPanel = document.getElementById('config-panel');
if (!configPanel) {
console.error('Config panel element not found');
return;
}
configPanel.classList.toggle('visible');
// Ensure input fields are enabled when panel is visible
const ipInput = document.getElementById('serverIp');
const portInput = document.getElementById('serverPort');
if (configPanel.classList.contains('visible')) {
if (ipInput) ipInput.removeAttribute('disabled');
if (portInput) portInput.removeAttribute('disabled');
}
}