import { app } from 'electron' ; import fs from 'fs' ; import { join } from 'path' ; import { parseOldLogsHandler } from 'root/app/old-log-handler' ; import { settingsStore } from 'root/app/settings-store/settings_store' ; import { error } from 'root/lib/logger' ; import { isMac } from 'root/lib/utils' ; export function locateMtgaDir ( checkPath ) { let pth = '' ; if ( checkPath !== undefined && fs . existsSync ( checkPath ) ) { pth = checkPath ; } else { const MtgaPathLocator = [ ] ; if ( isMac ( ) ) { MtgaPathLocator . push ( [ app . getPath ( 'home' ) , 'Library' , 'Application Support' , 'com.wizards.mtga' ] ) ; } else { const progFiles = process . env [ 'ProgramFiles' ] ; if ( progFiles === undefined ) { return false ; } MtgaPathLocator . push ( [ progFiles , 'Wizards of the Coast' , 'MTGA' , 'MTGA_Data' ] ) ; MtgaPathLocator . push ( [ progFiles , 'Epic Games' , 'MagicTheGathering' , 'MTGA_Data' ] ) ; } let pathFound = false ; MtgaPathLocator . forEach ( ( possiblePathElements ) => { if ( pathFound ) { return ; } const testPth = join ( ... possiblePathElements ) ; if ( fs . existsSync ( testPth ) ) { pth = testPth ; pathFound = true ; } } ) ; } let result = false ; const settings = settingsStore . get ( ) ; try { if ( fs . existsSync ( join ( pth , 'Logs' , 'Logs' ) ) ) { result = true ; settings . mtgaPath = pth ; } else if ( fs . existsSync ( join ( pth , 'MTGA_Data' , 'Logs' , 'Logs' ) ) ) { result = true ; settings . mtgaPath = join ( pth , 'MTGA_Data' ) ; } else { result = false ; settings . mtgaPath = undefined ; } } catch ( e ) { result = false ; settings . mtgaPath = undefined ; } settingsStore . save ( ) ; return result ; } interface MostRecentDate { date : number | undefined ; fileId : string | undefined ; logPath : string | undefined ; } export function locateMostRecentDate ( ) { const mtgaPath = settingsStore . get ( ) . mtgaPath ; if ( mtgaPath === undefined ) { return { date : undefined , fileId : undefined , logPath : undefined } ; } let logDate ; let fileId ; let logPath ; const pth = join ( mtgaPath , ... [ 'Logs' , 'Logs' ] ) ; try { const files = fs . readdirSync ( pth ) ; files . forEach ( ( file ) => { logPath = join ( pth , file ) ; const ctime = fs . statSync ( logPath ) . ctime ; if ( logDate === undefined || logDate < ctime ) { logDate = ctime ; fileId = file ; } } ) ; } catch ( e ) { error ( 'Error reading files in logs folder' , e ) ; } return { date : logDate ?. getTime ( ) , fileId , logPath } ; } export function getOldLogs ( ) { const mtgaPath = settingsStore . get ( ) . mtgaPath ; if ( mtgaPath === undefined ) { return undefined ; } const pth = join ( mtgaPath , ... [ 'Logs' , 'Logs' ] ) ; const files = [ ] ; try { fs . readdirSync ( pth ) . filter ( ( file ) => file . includes ( 'UTC_Log' ) && file . includes ( '.log' ) ) . forEach ( ( file ) => { files . push ( join ( mtgaPath , ... [ 'Logs' , 'Logs' ] , file ) ) ; } ) ; } catch ( e ) { error ( 'Error reading files in logs folder' , e ) ; } return files ; } export function ShadowLogParse ( ) { const logs = getOldLogs ( ) ; if ( logs === undefined ) { return ; } parseOldLogsHandler ( logs , 0 , 0 , true ) ; }