Spaces:
				
			
			
	
			
			
					
		Running
		
			on 
			
			CPU Upgrade
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
			on 
			
			CPU Upgrade
	File size: 7,240 Bytes
			
			19c8b95  | 
								1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180  | 
								"use strict"
window.dragDropModelInstallation = (eType, event) => {
    if (["dragenter", "dragover"].includes(eType)) {
        left.style.background = "#5b5b5b"
        left.style.color = "white"
    }
    if (["dragleave", "drop"].includes(eType)) {
        left.style.background = "rgba(0,0,0,0)"
        left.style.color = "white"
    }
    event.preventDefault()
    event.stopPropagation()
    const dataLines = []
    if (eType=="drop") {
        const dataTransfer = event.dataTransfer
        const files = Array.from(dataTransfer.files)
        const modelGroups = {} // Group up all files by their base name (for if loose files are given)
        files.forEach(file => {
            const baseName = file.name.split(".")[0]
            if (!modelGroups[baseName]) {
                modelGroups[baseName] = []
            }
            modelGroups[baseName].push(file.path)
        })
        const modelGroupsComplete = []
        const modelGroupsNotComplete = []
        Object.keys(modelGroups).forEach(key => {
            if (modelGroups[key][0].split(".").at(-1)!="zip") {
                const fileExtensions = modelGroups[key].map(filePath => filePath.split(".").at(-1))
                if (fileExtensions.includes("json") && fileExtensions.includes("pt")) {
                    modelGroupsComplete.push(key)
                } else {
                    modelGroupsNotComplete.push(key)
                }
            } else {
                modelGroupsComplete.push(key)
            }
        })
        if (modelGroupsNotComplete.length) {
            return window.errorModal(window.i18n.MODEL_INSTALL_DRAGDROP_INCOMPLETE.replace("_1", modelGroupsNotComplete.join(", ")))
        }
        const modelsInstalledSuccessfully = []
        const modelsFailedInstallation = []
        let lastGameInstalledOkFor = undefined
        const handleZip = (files, key) => {
            return new Promise(resolve => {
                let installedOk = false
                let game = undefined
                try {
                    if (fs.existsSync(`${window.path}/downloads`)) {
                        fs.readdirSync(`${window.path}/downloads`).forEach(fileName => {
                            fs.unlinkSync(`${window.path}/downloads/${fileName}`)
                        })
                    } else {
                        fs.mkdirSync(`${window.path}/downloads`)
                    }
                    window.unzipFileTo(files[0], `${window.path}/downloads`).then(() => {
                        const allFiles = fs.readdirSync(`${window.path}/downloads`)
                        const jsonFiles = allFiles.filter(fname => fname.endsWith(".json"))
                        jsonFiles.forEach(jsonFile => {
                            const jsonData = JSON.parse(fs.readFileSync(`${window.path}/downloads/${jsonFile}`))
                            game = jsonData.games[0].gameId
                            const voiceId = jsonData.games[0].voiceId
                            const modelsFolder = window.userSettings[`modelspath_${game}`]
                            const allFilesForThisModel = allFiles.filter(fname => fname.includes(voiceId))
                            allFilesForThisModel.forEach(fname => {
                                fs.copyFileSync(`${window.path}/downloads/${fname}`, `${modelsFolder}/${fname}`)
                            })
                            installedOk = true
                        })
                        resolve([game, key, installedOk])
                    })
                } catch (e) {
                    resolve([game, key, false])
                }
            })
        }
        const handleLoose = (files, key) => {
            let game = undefined
            return new Promise(resolve => {
                try {
                    const jsonFile = files.find(fname => fname.endsWith(".json"))
                    const parentFolder = jsonFile.replaceAll("\\", "/").split("/").reverse().slice(1, 100000).reverse().join("/")
                    // const jsonData = JSON.parse(fs.readFileSync(`${parentFolder}/${jsonFile}`))
                    const jsonData = JSON.parse(fs.readFileSync(`${jsonFile}`))
                    const game = jsonData.games[0].gameId
                    const voiceId = jsonData.games[0].voiceId
                    const modelsFolder = window.userSettings[`modelspath_${game}`]
                    const allFilesForThisModel = fs.readdirSync(parentFolder).filter(fname => fname.includes(voiceId))
                    allFilesForThisModel.forEach(fname => {
                        fs.copyFileSync(`${parentFolder}/${fname}`, `${modelsFolder}/${fname}`)
                        // fs.copyFileSync(`${fname}`, `${modelsFolder}/${fname}`)
                    })
                    // lastGameInstalledOkFor = game
                    // modelsInstalledSuccessfully.push(key)
                    resolve([game, key, true])
                } catch (e) {
                    resolve([game, key, false])
                }
            })
        }
        const installPromises = []
        modelGroupsComplete.forEach(key => {
            try {
                const files = modelGroups[key]
                if (files[0].endsWith(".zip")) {
                    installPromises.push(handleZip(files, key))
                } else {
                    installPromises.push(handleLoose(files, key))
                }
            } catch (e) {
                console.log(e)
                window.appLogger.log(e)
                modelsFailedInstallation.push(key)
            }
        })
        Promise.all(installPromises).then(responses => {
            responses.forEach(([game, key, installedOk]) => {
                if (installedOk) {
                    lastGameInstalledOkFor = game
                    modelsInstalledSuccessfully.push(key)
                } else {
                    modelsFailedInstallation.push(key)
                }
            })
            let outputMessage = ""
            if (modelsInstalledSuccessfully.length) {
                outputMessage += window.i18n.MODEL_INSTALL_DRAGDROP_SUCCESS.replace("_1", modelsInstalledSuccessfully.length)
            }
            if (modelsFailedInstallation.length) {
                outputMessage += window.i18n.MODEL_INSTALL_DRAGDROP_FAILED.replace("_1", modelsFailedInstallation.length).replace("_2", modelsFailedInstallation.join(", "))
            }
            window.infoModal(outputMessage)
            if (lastGameInstalledOkFor) {
                window.changeGame(window.gameAssets[lastGameInstalledOkFor])
            }
            window.displayAllModels(true)
            window.loadAllModels(true).then(() => {
                changeGame(window.currentGame)
            })
        })
    }
}
left.addEventListener("dragenter", event => window.dragDropModelInstallation("dragenter", event), false)
left.addEventListener("dragleave", event => window.dragDropModelInstallation("dragleave", event), false)
left.addEventListener("dragover", event => window.dragDropModelInstallation("dragover", event), false)
left.addEventListener("drop", event => window.dragDropModelInstallation("drop", event), false) |