import * as chmodr from "chmodr" ; import { createWriteStream , writeFileSync } from "fs" ; import { mkdirsSync , existsSync } from "fs-extra" ; import { join as joinPath , sep as pathSep } from "path" ; import Config from "../utils/Config" ; import unzip from "../archive/Unzipper" ; import InstallManager from "./InstallManager" ; import Logger from "../utils/Logger" ; import ModManagerHookInjector from "./ModManagerHookInjector" ; export default class InstallCreator { param folderName param installName param globalSave param injectScript public static createInstall ( folderName , installName ? , globalSave ? , injectScript ? ) { return new Promise ( ( ff , rj ) => { let unarchiving = false ; Logger . info ( "Install Creator" , "Creating install folder in " + folderName ) ; const canonicalPath = joinPath ( Config . readConfigValue ( "installFolder" ) , "installs" , folderName ) ; try { if ( existsSync ( canonicalPath ) ) { unarchiving = true ; } else { if ( ! installName ) { rj ( "No install name provided" ) ; return ; } mkdirsSync ( joinPath ( canonicalPath , "appdata" ) ) ; if ( process . platform === "win32" ) { mkdirsSync ( joinPath ( canonicalPath , "appdata" , "RenPy" ) ) ; } else if ( process . platform === "darwin" ) { mkdirsSync ( joinPath ( canonicalPath , "appdata" , "Library" , "RenPy" ) ) ; } else { mkdirsSync ( joinPath ( canonicalPath , "appdata" , ".renpy" ) ) ; } } mkdirsSync ( joinPath ( canonicalPath , "install" ) ) ; const zip = unzip ( joinPath ( Config . readConfigValue ( "installFolder" ) , "ddlc.zip" ) ) ; zip . on ( "file" , ( file ) => { const pathParts = file . path . split ( "/" ) ; pathParts . shift ( ) ; if ( pathParts . length == 0 ) return ; if ( process . platform === "darwin" ) { pathParts . shift ( ) ; if ( pathParts . length < 1 ) return ; } const fileName = pathParts . pop ( ) ; mkdirsSync ( joinPath ( canonicalPath , "install" , pathParts . join ( pathSep ) ) ) ; file . openStream ( ( err , stream ) => { if ( err ) { rj ( err ) ; } stream . pipe ( createWriteStream ( joinPath ( canonicalPath , "install" , pathParts . join ( pathSep ) , fileName ) ) ) ; } ) ; } ) ; zip . on ( "close" , ( ) => { function finish ( ) { Logger . info ( "Install Creator" , "Game installation completed" ) ; if ( unarchiving ) { InstallManager . updateInstallDataValue ( folderName , "archived" , false ) ; } else { writeFileSync ( joinPath ( canonicalPath , "install.json" ) , JSON . stringify ( { globalSave , mod : null , name : installName , playTime : 0 , category : "" , archived : false , firstRun : true , modded : false , singletonError : false } ) ) ; } if ( process . platform !== "win32" ) { chmodr ( joinPath ( canonicalPath , "install" ) , 0o774 , ( ) => { ff ( ) ; } ) ; } else { ff ( ) ; } } if ( injectScript ) { ModManagerHookInjector . injectScript ( folderName ) . finally ( finish ) ; } else { finish ( ) ; } } ) ; } catch ( e ) { rj ( e ) ; } } ) ; } }