import { isElectron } from '@lib/isElectron' ; import { copyFileSync , createWriteStream , existsSync , writeFile } from 'fs' ; import path , { parse } from 'path' ; import { Json2SavTransform , SaveGame } from 'satisfactory-json' ; import { Writable } from 'stream' ; import streamSaver from 'streamsaver' ; streamSaver . mitm = '/streamsaver.html' ; export function saveFileToFilesystem ( saveGame , path , asJson , callback ) { transformFile ( saveGame , callback , path , asJson ) ; } class FileWriter extends Writable { private writer : any ; constructor ( writer ) { super ( ) ; this . writer = writer ; } _write ( chunk , encoding , callback ) { this . writer . write ( chunk ) . then ( ( err ) => { callback ( ) ; } ) ; } _final ( callback ) { this . writer . close ( ) ; callback ( ) ; } } function transformFile ( saveGame , callback , path , asJson ) { if ( isElectron ( ) ) { saveDesktop ( path , saveGame , callback , asJson ) ; } else { saveWeb ( path , saveGame , callback , asJson ) ; } } function saveDesktop ( filePath , saveGame , callback , asJson ) { if ( existsSync ( filePath ) ) { const timestamp = new Date ( ) . toISOString ( ) . replace ( 'T' , '_' ) . replace ( 'Z' , '' ) . replace ( /:/g , '_' ) ; const parsedPath = parse ( filePath ) ; copyFileSync ( filePath , path . join ( parsedPath . dir , parsedPath . base + '_' + timestamp + '.bak' ) ) ; } if ( asJson ) { writeFile ( filePath , JSON . stringify ( saveGame ) , { encoding : 'binary' } , err => { if ( err ) { callback ( err , undefined , undefined ) ; return ; } callback ( undefined , undefined , true ) ; } ) ; } else { console . log ( 'desktop' ) ; const json2sav = new Json2SavTransform ( ) ; const outStream = createWriteStream ( filePath ) ; json2sav . on ( 'error' , ( error ) => { callback ( error , undefined , undefined ) ; } ) . on ( 'progress' , progress => { callback ( undefined , progress , undefined ) ; } ) . pipe ( outStream ) . on ( 'finish' , ( ) => { callback ( undefined , undefined , true ) ; } ) . on ( 'error' , ( error ) => { callback ( error , undefined , undefined ) ; } ) ; json2sav . write ( saveGame ) ; json2sav . end ( ) ; } } function saveWeb ( path , saveGame , callback , asJson ) { if ( asJson ) { const fileStream = streamSaver . createWriteStream ( path ) ; const writer = fileStream . getWriter ( ) ; window . onunload = ( ) => writer . abort ( ) ; writer . write ( Buffer . from ( JSON . stringify ( saveGame ) ) ) . then ( ( ) => { writer . close ( ) ; callback ( undefined , undefined , true ) ; } ) ; } else { const transform = new Json2SavTransform ( ) ; const fileStream = streamSaver . createWriteStream ( path ) ; const writer = fileStream . getWriter ( ) ; window . onunload = ( ) => writer . abort ( ) ; transform . pipe ( new FileWriter ( writer ) ) . on ( 'finish' , ( ) => { callback ( undefined , undefined , true ) ; } ) ; transform . on ( 'progress' , progress => { callback ( undefined , progress , undefined ) ; } ) ; transform . on ( 'error' , error => { writer . abort ( ) ; callback ( error , undefined , undefined ) ; } ) ; transform . write ( saveGame ) ; transform . end ( ) ; } }