Skip to content

Commit cbbbfaa

Browse files
committed
chore: move updater to its own folder and create utils file
1 parent 798724a commit cbbbfaa

File tree

3 files changed

+135
-129
lines changed

3 files changed

+135
-129
lines changed

src/backend/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ import {
151151
checkWineBeforeLaunch,
152152
runWineCommandOnGame
153153
} from './utils/compatibility_layers'
154-
import { isClientUpdating } from 'backend/updater'
154+
import { isClientUpdating } from 'backend/updater/updater'
155155

156156
/*
157157
* INSERT OTHER IPC HANDLERS HERE

src/backend/updater.ts src/backend/updater/updater.ts

+12-128
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import { dialog, shell } from 'electron'
1+
import { app, dialog, shell } from 'electron'
22
import { autoUpdater } from 'electron-updater'
33
import { t } from 'i18next'
44

5-
import { configStore, icon, isLinux, isMac } from './constants'
6-
import { logError, logInfo, LogPrefix } from './logger/logger'
5+
import { configStore, icon, isLinux } from '../constants'
6+
import { logError, logInfo, LogPrefix } from '../logger/logger'
77
import { captureException } from '@sentry/electron'
8-
import { getFileSize } from './utils'
8+
import { getFileSize } from '../utils'
99
import { ClientUpdateStatuses } from '@hyperplay/utils'
10-
import { trackEvent } from './metrics/metrics'
11-
import { homedir } from 'os'
12-
import { join } from 'path'
13-
import { rm } from 'fs/promises'
10+
import { trackEvent } from '../metrics/metrics'
11+
import { getErrorMessage, removeCachedUpdatesFolder } from './utils'
1412
// to test auto update on windows locally make sure you added the option verifyUpdateCodeSignature: false
1513
// under build.win in electron-builder.yml and also change the app version to an old one there
1614

@@ -23,11 +21,12 @@ autoUpdater.autoInstallOnAppQuit = true
2321

2422
let isAppUpdating = false
2523
let hasUpdated = false
24+
2625
let updateAttempts = 0
2726
const MAX_UPDATE_ATTEMPTS = 10
27+
// check for updates every 3 hours
28+
const checkUpdateInterval = 3 * 1000 * 60 * 60
2829

29-
// check for updates every hour
30-
const checkUpdateInterval = 1 * 1000 * 60 * 60
3130
setInterval(async () => {
3231
if (shouldCheckForUpdates && !hasUpdated && !isAppUpdating) {
3332
logInfo('Checking for client updates...', LogPrefix.AutoUpdater)
@@ -115,17 +114,18 @@ autoUpdater.on('update-downloaded', async () => {
115114

116115
autoUpdater.on('error', async (error) => {
117116
isAppUpdating = false
117+
const isNewVersion = newVersion !== app.getVersion()
118118

119119
// To avoid false positives, we should not show the error dialog if the app has already updated successfully
120-
if (hasUpdated || !newVersion) {
120+
if (hasUpdated || !isNewVersion) {
121121
return
122122
}
123123

124124
const errorMessage = getErrorMessage(error.message)
125125
logError(`Error updating HyperPlay: ${errorMessage}`, LogPrefix.AutoUpdater)
126126

127127
// will remove cached updates when it fails to avoid corrupted updates
128-
if (updateAttempts > 2) {
128+
if (updateAttempts > 3) {
129129
await removeCachedUpdatesFolder()
130130
}
131131

@@ -184,119 +184,3 @@ export function isClientUpdating(): ClientUpdateStatuses {
184184
}
185185
return isAppUpdating ? 'updating' : 'idle'
186186
}
187-
188-
async function removeCachedUpdatesFolder() {
189-
// remove hyperplay-updates folder from cache directory
190-
// on macOS: /Users/<username>/Library/Caches/hyperplay-updates
191-
// on Windows: C:\Users\<username>\AppData\Local\hyperplay-updates
192-
const macOSPath = join(homedir(), 'Library', 'Caches', 'hyperplay-updates')
193-
const windowsPath = join(homedir(), 'AppData', 'Local', 'hyperplay-updates')
194-
195-
try {
196-
await rm(isMac ? macOSPath : windowsPath, { recursive: true })
197-
} catch (error) {
198-
logError(
199-
`Error removing cached updates folder: ${error}`,
200-
LogPrefix.AutoUpdater
201-
)
202-
}
203-
}
204-
205-
const commonDownloadErrors: Record<string, () => string> = {
206-
ERR_NETWORK_CHANGED: () =>
207-
t(
208-
'box.error.update.networkChanged',
209-
'Network changed. Please check your internet connection.'
210-
),
211-
ERR_INTERNET_DISCONNECTED: () =>
212-
t(
213-
'box.error.update.internetDisconnected',
214-
'Internet disconnected. Please check your internet connection.'
215-
),
216-
ERR_CONNECTION_RESET: () =>
217-
t(
218-
'box.error.update.connectionReset',
219-
'Connection reset. Please check your internet connection.'
220-
),
221-
ERR_CONNECTION_CLOSED: () =>
222-
t(
223-
'box.error.update.connectionClosed',
224-
'Connection closed. Please check your internet connection.'
225-
),
226-
ERR_CONNECTION_TIMED_OUT: () =>
227-
t(
228-
'box.error.update.connectionTimedOut',
229-
'Connection timed out. Please check your internet connection.'
230-
),
231-
ERR_NAME_NOT_RESOLVED: () =>
232-
t(
233-
'box.error.update.nameNotResolved',
234-
'Name not resolved. Please check your internet connection.'
235-
),
236-
ERR_CONNECTION_REFUSED: () =>
237-
t(
238-
'box.error.update.connectionRefused',
239-
'Connection refused. Please check your internet connection.'
240-
),
241-
ERR_SSL_PROTOCOL_ERROR: () =>
242-
t(
243-
'box.error.update.sslProtocolError',
244-
'SSL protocol error. Please check your system time and date or open a ticket with HyperPlay support.'
245-
),
246-
ERR_CERT_AUTHORITY_INVALID: () =>
247-
t(
248-
'box.error.update.certAuthorityInvalid',
249-
'Certificate authority invalid. Please check your system time and date or open a ticket with HyperPlay support.'
250-
),
251-
ERR_NETWORK_ACCESS_DENIED: () =>
252-
t(
253-
'box.error.update.networkAccessDenied',
254-
'Network access denied. Please check your internet connection.'
255-
),
256-
ERR_PROXY_CONNECTION_FAILED: () =>
257-
t(
258-
'box.error.update.proxyConnectionFailed',
259-
'Proxy connection failed. Please check your proxy settings.'
260-
),
261-
ERR_CONNECTION_ABORTED: () =>
262-
t(
263-
'box.error.update.connectionAborted',
264-
'Connection aborted. Please check your internet connection.'
265-
),
266-
ERR_ADDRESS_UNREACHABLE: () =>
267-
t(
268-
'box.error.update.addressUnreachable',
269-
'Address unreachable. Please check your internet connection.'
270-
),
271-
ERR_CERT_DATE_INVALID: () =>
272-
t(
273-
'box.error.update.certDateInvalid',
274-
'Certificate date invalid. Please check your system time and date or open a ticket with HyperPlay support.'
275-
),
276-
ERR_HTTP2_SERVER_REFUSED_STREAM: () =>
277-
t(
278-
'box.error.update.http2ServerRefusedStream',
279-
'HTTP2 server refused stream. Please check your internet connection.'
280-
),
281-
ERR_EMPTY_RESPONSE: () =>
282-
t(
283-
'box.error.update.emptyResponse',
284-
'Empty response. Please check your internet connection.'
285-
),
286-
ERR_FAILED: () =>
287-
t(
288-
'box.error.update.failed',
289-
'Download Failed. Please check your internet connection.'
290-
)
291-
}
292-
293-
function getErrorMessage(error: string): string {
294-
const trimmedError = error.replace('net::', '').trim()
295-
if (
296-
Object.prototype.hasOwnProperty.call(commonDownloadErrors, trimmedError)
297-
) {
298-
return commonDownloadErrors[trimmedError]()
299-
} else {
300-
return error
301-
}
302-
}

src/backend/updater/utils.ts

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { isMac } from 'backend/constants'
2+
import { logError, LogPrefix } from 'backend/logger/logger'
3+
import { rm } from 'fs/promises'
4+
import { t } from 'i18next'
5+
import { homedir } from 'os'
6+
import { join } from 'path'
7+
8+
export async function removeCachedUpdatesFolder() {
9+
// remove hyperplay-updates folder from cache directory
10+
// on macOS: /Users/<username>/Library/Caches/hyperplay-updates
11+
// on Windows: C:\Users\<username>\AppData\Local\hyperplay-updates
12+
const macOSPath = join(homedir(), 'Library', 'Caches', 'hyperplay-updates')
13+
const windowsPath = join(homedir(), 'AppData', 'Local', 'hyperplay-updates')
14+
15+
try {
16+
await rm(isMac ? macOSPath : windowsPath, { recursive: true })
17+
} catch (error) {
18+
logError(
19+
`Error removing cached updates folder: ${error}`,
20+
LogPrefix.AutoUpdater
21+
)
22+
}
23+
}
24+
25+
const commonDownloadErrors: Record<string, () => string> = {
26+
ERR_NETWORK_CHANGED: () =>
27+
t(
28+
'box.error.update.networkChanged',
29+
'Network changed. Please check your internet connection.'
30+
),
31+
ERR_INTERNET_DISCONNECTED: () =>
32+
t(
33+
'box.error.update.internetDisconnected',
34+
'Internet disconnected. Please check your internet connection.'
35+
),
36+
ERR_CONNECTION_RESET: () =>
37+
t(
38+
'box.error.update.connectionReset',
39+
'Connection reset. Please check your internet connection.'
40+
),
41+
ERR_CONNECTION_CLOSED: () =>
42+
t(
43+
'box.error.update.connectionClosed',
44+
'Connection closed. Please check your internet connection.'
45+
),
46+
ERR_CONNECTION_TIMED_OUT: () =>
47+
t(
48+
'box.error.update.connectionTimedOut',
49+
'Connection timed out. Please check your internet connection.'
50+
),
51+
ERR_NAME_NOT_RESOLVED: () =>
52+
t(
53+
'box.error.update.nameNotResolved',
54+
'Name not resolved. Please check your internet connection.'
55+
),
56+
ERR_CONNECTION_REFUSED: () =>
57+
t(
58+
'box.error.update.connectionRefused',
59+
'Connection refused. Please check your internet connection.'
60+
),
61+
ERR_SSL_PROTOCOL_ERROR: () =>
62+
t(
63+
'box.error.update.sslProtocolError',
64+
'SSL protocol error. Please check your system time and date or open a ticket with HyperPlay support.'
65+
),
66+
ERR_CERT_AUTHORITY_INVALID: () =>
67+
t(
68+
'box.error.update.certAuthorityInvalid',
69+
'Certificate authority invalid. Please check your system time and date or open a ticket with HyperPlay support.'
70+
),
71+
ERR_NETWORK_ACCESS_DENIED: () =>
72+
t(
73+
'box.error.update.networkAccessDenied',
74+
'Network access denied. Please check your internet connection.'
75+
),
76+
ERR_PROXY_CONNECTION_FAILED: () =>
77+
t(
78+
'box.error.update.proxyConnectionFailed',
79+
'Proxy connection failed. Please check your proxy settings.'
80+
),
81+
ERR_CONNECTION_ABORTED: () =>
82+
t(
83+
'box.error.update.connectionAborted',
84+
'Connection aborted. Please check your internet connection.'
85+
),
86+
ERR_ADDRESS_UNREACHABLE: () =>
87+
t(
88+
'box.error.update.addressUnreachable',
89+
'Address unreachable. Please check your internet connection.'
90+
),
91+
ERR_CERT_DATE_INVALID: () =>
92+
t(
93+
'box.error.update.certDateInvalid',
94+
'Certificate date invalid. Please check your system time and date or open a ticket with HyperPlay support.'
95+
),
96+
ERR_HTTP2_SERVER_REFUSED_STREAM: () =>
97+
t(
98+
'box.error.update.http2ServerRefusedStream',
99+
'HTTP2 server refused stream. Please check your internet connection.'
100+
),
101+
ERR_EMPTY_RESPONSE: () =>
102+
t(
103+
'box.error.update.emptyResponse',
104+
'Empty response. Please check your internet connection.'
105+
),
106+
ERR_FAILED: () =>
107+
t(
108+
'box.error.update.failed',
109+
'Download Failed. Please check your internet connection.'
110+
)
111+
}
112+
113+
export function getErrorMessage(error: string): string {
114+
const trimmedError = error.replace('net::', '').trim()
115+
if (
116+
Object.prototype.hasOwnProperty.call(commonDownloadErrors, trimmedError)
117+
) {
118+
return commonDownloadErrors[trimmedError]()
119+
} else {
120+
return error
121+
}
122+
}

0 commit comments

Comments
 (0)