@@ -2,7 +2,7 @@ import { Cracker } from "@/@types";
2
2
import { AchievementFile } from "@/@types/achievements/types" ;
3
3
import { app } from "electron" ;
4
4
import { existsSync , readdirSync } from "node:fs" ;
5
- import { join } from "node:path" ;
5
+ import { join , resolve } from "node:path" ;
6
6
import { logger } from "../logging" ;
7
7
8
8
type PathType =
@@ -20,13 +20,22 @@ interface FilePath {
20
20
21
21
class AchievementFileLocator {
22
22
private static isWindows = process . platform === "win32" ;
23
- private static user = ! this . isWindows
24
- ? app . getPath ( "home" ) . split ( "/" ) . pop ( )
25
- : undefined ;
23
+ private static user = this . isWindows
24
+ ? undefined
25
+ : app . getPath ( "home" ) . split ( "/" ) . pop ( ) ;
26
26
27
27
private static winePrefix =
28
28
process . env . WINEPREFIX || join ( app . getPath ( "home" ) , ".wine" ) ;
29
29
30
+ private static validateWinePrefix ( ) : void {
31
+ if ( ! existsSync ( this . winePrefix ) ) {
32
+ logger . log (
33
+ "warn" ,
34
+ `Wine prefix not found at ${ this . winePrefix } . Check WINEPREFIX environment variable.`
35
+ ) ;
36
+ }
37
+ }
38
+
30
39
private static readonly crackerPaths : Readonly < Record < Cracker , FilePath [ ] > > =
31
40
Object . freeze ( {
32
41
codex : [
@@ -253,17 +262,6 @@ class AchievementFileLocator {
253
262
] ,
254
263
} ) ;
255
264
256
- /**
257
- * Allows updating the wine prefix dynamically.
258
- * @param newPrefix The new custom Wine prefix path.
259
- */
260
- static setWinePrefix ( newPrefix : string ) : void {
261
- if ( ! existsSync ( newPrefix ) ) {
262
- throw new Error ( `Specified Wine prefix does not exist: ${ newPrefix } ` ) ;
263
- }
264
- this . winePrefix = newPrefix ;
265
- }
266
-
267
265
private static getSystemPath ( type : PathType ) : string {
268
266
const basePaths = {
269
267
appData : this . isWindows
@@ -272,7 +270,7 @@ class AchievementFileLocator {
272
270
this . winePrefix ,
273
271
"drive_c" ,
274
272
"users" ,
275
- this . user || "" ,
273
+ this . user || "unknown " ,
276
274
"AppData" ,
277
275
"Roaming"
278
276
) ,
@@ -282,7 +280,7 @@ class AchievementFileLocator {
282
280
this . winePrefix ,
283
281
"drive_c" ,
284
282
"users" ,
285
- this . user || "" ,
283
+ this . user || "unknown " ,
286
284
"Documents"
287
285
) ,
288
286
publicDocuments : this . isWindows
@@ -294,7 +292,7 @@ class AchievementFileLocator {
294
292
this . winePrefix ,
295
293
"drive_c" ,
296
294
"users" ,
297
- this . user || "" ,
295
+ this . user || "unknown " ,
298
296
"AppData" ,
299
297
"Local"
300
298
) ,
@@ -304,37 +302,19 @@ class AchievementFileLocator {
304
302
winePrefix : this . winePrefix ,
305
303
} ;
306
304
307
- return (
308
- basePaths [ type ] ||
309
- ( ( ) => {
310
- throw new Error ( `Unknown path type: ${ type } ` ) ;
311
- } ) ( )
312
- ) ;
313
- }
314
-
315
- static getCrackerPath ( cracker : Cracker ) : FilePath [ ] {
316
- return this . crackerPaths [ cracker ] || [ ] ;
317
- }
318
-
319
- private static replacePlaceholders (
320
- path : string ,
321
- gameStoreId : string
322
- ) : string {
323
- if ( ! gameStoreId ) {
324
- throw new Error ( "Invalid gameStoreId provided" ) ;
305
+ const path = basePaths [ type ] ;
306
+ if ( ! path ) {
307
+ throw new Error ( `Unknown path type: ${ type } ` ) ;
325
308
}
326
- return path . replace ( / < g a m e _ s t o r e _ i d > / g , gameStoreId ) ;
309
+ return resolve ( path ) ;
327
310
}
328
311
329
- private static buildFilePath (
330
- folderPath : string ,
331
- fileLocations : string [ ] ,
332
- gameStoreId : string
333
- ) : string {
334
- const mappedLocations = fileLocations . map ( ( location ) =>
335
- this . replacePlaceholders ( location , gameStoreId )
336
- ) ;
337
- return join ( folderPath , ...mappedLocations ) ;
312
+ static setWinePrefix ( newPrefix : string ) : void {
313
+ if ( ! existsSync ( newPrefix ) ) {
314
+ throw new Error ( `Specified Wine prefix does not exist: ${ newPrefix } ` ) ;
315
+ }
316
+ this . winePrefix = newPrefix ;
317
+ this . validateWinePrefix ( ) ;
338
318
}
339
319
340
320
static findAllAchievementFiles (
@@ -343,27 +323,43 @@ class AchievementFileLocator {
343
323
if ( winePrefix ) this . setWinePrefix ( winePrefix ) ;
344
324
345
325
const gameAchievementFiles = new Map < string , AchievementFile [ ] > ( ) ;
346
- console . log ( `Searching for achievement files` ) ;
326
+
327
+ logger . log (
328
+ "info" ,
329
+ `Searching for achievement files for platform: ${ process . platform } `
330
+ ) ;
347
331
348
332
for ( const [ cracker , paths ] of Object . entries ( this . crackerPaths ) as [
349
333
Cracker ,
350
334
FilePath [ ] ,
351
335
] [ ] ) {
352
336
paths . forEach (
353
337
( { achievement_folder_location, achievement_file_location } ) => {
354
- if ( ! existsSync ( achievement_folder_location ) ) return ;
355
-
356
- console . log ( `${ achievement_folder_location } ` ) ;
338
+ if ( ! existsSync ( achievement_folder_location ) ) {
339
+ logger . log (
340
+ "debug" ,
341
+ `Folder not found: ${ achievement_folder_location } `
342
+ ) ;
343
+ return ;
344
+ }
357
345
346
+ logger . log (
347
+ "debug" ,
348
+ `Processing folder: ${ achievement_folder_location } `
349
+ ) ;
358
350
const gameStoreIds = readdirSync ( achievement_folder_location ) ;
351
+
359
352
gameStoreIds . forEach ( ( gameStoreId ) => {
360
353
const filePath = this . buildFilePath (
361
354
achievement_folder_location ,
362
355
achievement_file_location ,
363
356
gameStoreId
364
357
) ;
365
358
366
- if ( ! existsSync ( filePath ) ) return ;
359
+ if ( ! existsSync ( filePath ) ) {
360
+ logger . log ( "debug" , `File not found: ${ filePath } ` ) ;
361
+ return ;
362
+ }
367
363
368
364
const achievementFile : AchievementFile = {
369
365
cracker,
@@ -383,6 +379,17 @@ class AchievementFileLocator {
383
379
return gameAchievementFiles ;
384
380
}
385
381
382
+ private static buildFilePath (
383
+ folderPath : string ,
384
+ fileLocations : string [ ] ,
385
+ gameStoreId : string
386
+ ) : string {
387
+ const mappedLocations = fileLocations . map ( ( location ) =>
388
+ location . replace ( / < g a m e _ s t o r e _ i d > / g, gameStoreId )
389
+ ) ;
390
+ return join ( folderPath , ...mappedLocations ) ;
391
+ }
392
+
386
393
static findAchievementFiles (
387
394
gameStoreId : string ,
388
395
winePrefix ?: string | null
0 commit comments