@@ -44,6 +44,7 @@ const path = require("path");
4444const os = require ( "os" ) ;
4545const { exec } = require ( "child_process" ) ;
4646const fs = require ( "fs-extra" ) ;
47+ const fsPromises = require ( "fs/promises" ) ;
4748const Seven = require ( "node-7z" ) ;
4849const sevenBin = require ( "7zip-bin" ) ;
4950const which = require ( "which" ) ;
@@ -3293,6 +3294,8 @@ class ElectronLoader {
32933294 const gameDetails = this . #getGameDetails( profile . gameId ) ;
32943295 // Substitute variables for profile
32953296 const gameActionCmd = template ( gameAction . actionScript ) ( { ...profile , gameDetails } ) ;
3297+
3298+ log . info ( "Running game action: " , gameActionCmd ) ;
32963299
32973300 // Run the action
32983301 try {
@@ -3415,9 +3418,14 @@ class ElectronLoader {
34153418
34163419 if ( fs . existsSync ( destFilePath ) ) {
34173420 if ( extFilesList ?. includes ( modFile ) ) {
3418- // Backup original external file to temp directory for deploy and override
3419- fs . moveSync ( destFilePath , path . join ( extFilesBackupDir , modFile ) ) ;
3420- remove ( extFilesList , extFile => extFile === modFile ) ;
3421+ if ( ! shouldCopy ) {
3422+ log . warn ( "Original file to backup is a directory, this should not happen." ) ;
3423+ } else {
3424+ // Backup original external file to temp directory for deploy and override
3425+ fs . moveSync ( destFilePath , path . join ( extFilesBackupDir , modFile ) ) ;
3426+ remove ( extFilesList , extFile => extFile === modFile ) ;
3427+ }
3428+
34213429 } else {
34223430 // Don't override deployed files
34233431 shouldCopy = false ;
@@ -3753,8 +3761,13 @@ class ElectronLoader {
37533761 // Recursively remove empty parent directories
37543762 let existingDir = path . dirname ( fullExistingPath ) ;
37553763 while ( existingDir !== profile . gameInstallation . modDir && fs . existsSync ( existingDir ) && fs . readdirSync ( existingDir ) . length === 0 ) {
3756- fs . rmdirSync ( existingDir ) ;
3757- existingDir = path . dirname ( existingDir ) ;
3764+ try {
3765+ fs . rmdirSync ( existingDir ) ;
3766+ existingDir = path . dirname ( existingDir ) ;
3767+ } catch ( error ) {
3768+ log . error ( "Failed to remove dir" , existingDir , error ) ;
3769+ break ;
3770+ }
37583771 }
37593772 } ) ;
37603773
@@ -3776,25 +3789,38 @@ class ElectronLoader {
37763789
37773790 // Restore original external files, if any were moved
37783791 for ( const extFilesBackupDir of extFilesBackupDirs ) {
3779- if ( fs . existsSync ( extFilesBackupDir ) ) {
3780- const backupTransfers = fs . readdirSync ( extFilesBackupDir ) . map ( ( backupFile ) => {
3781- const backupSrc = path . join ( extFilesBackupDir , backupFile ) ;
3782- const backupDest = path . join ( path . dirname ( extFilesBackupDir ) , backupFile ) ;
3792+ if ( await fs . exists ( extFilesBackupDir ) ) {
3793+ const backupEntries = await fs . readdir ( extFilesBackupDir ) ;
37833794
3784- if ( fs . existsSync ( backupDest ) ) {
3785- fs . removeSync ( backupDest ) ;
3786- }
3795+ for ( const backupEntry of backupEntries ) {
3796+ const backupSrc = path . join ( extFilesBackupDir , backupEntry ) ;
3797+ const backupSrcIsDir = ( await fs . lstat ( backupSrc ) ) . isDirectory ( ) ;
3798+ const backupDest = path . join ( path . dirname ( extFilesBackupDir ) , backupEntry ) ;
3799+ const backupDestExists = await fs . exists ( backupDest ) ;
3800+ const backupDestIsDir = backupDestExists && ( await fs . lstat ( backupDest ) ) . isDirectory ( ) ;
3801+
3802+ if ( backupDestIsDir ) {
3803+ if ( backupSrcIsDir ) {
3804+ log . info ( "Merging restored game file backups with existing directory." ) ;
37873805
3788- // Use hardlinks for faster file restoration in link mode
3789- if ( profile . modLinkMode && ! fs . lstatSync ( backupSrc ) . isDirectory ( ) ) {
3790- // TODO - Recursively do this when encountering directories
3791- return fs . link ( backupSrc , backupDest ) ;
3806+ await fsPromises . cp ( backupSrc , backupDest , { recursive : true } ) ;
3807+ } else {
3808+ throw new Error ( "Backup file is not a directory but write dest is. This should not happen." ) ;
3809+ }
37923810 } else {
3793- return fs . copy ( backupSrc , backupDest ) ;
3794- }
3795- } ) ;
3811+ if ( backupDestExists ) {
3812+ await fs . remove ( backupDest ) ;
3813+ }
37963814
3797- await Promise . all ( backupTransfers ) ;
3815+ if ( profile . modLinkMode && ! backupSrcIsDir ) {
3816+ // Use hardlinks for faster file restoration in link mode
3817+ // TODO - Recursively do this when encountering directories
3818+ await fs . link ( backupSrc , backupDest ) ;
3819+ } else {
3820+ await fs . copy ( backupSrc , backupDest ) ;
3821+ }
3822+ }
3823+ }
37983824 }
37993825 }
38003826
0 commit comments