import store from '@/store' ; import webapi from '@/services/webapi' ; import { GenericResponse , GenericResponseWithData } from '@/services/responses/genericResponse' ; import { ipcRenderer } from 'electron' ; import InstallBepinexArgs from '@/electronMain/models/installBepinexArgs' ; import ModListService from '@/services/modListService' ; import axios from 'axios' ; import { BEPINEX_REPO , NUCLEARPOWERED_ORG } from '@/consts' ; import { CheckModBuildUpdateResponse } from '@/services/responses/modResponse' ; import semver from 'semver' ; export default class UpdateService { static async checkModUpdates ( ) { const modMetadata = await ModListService . readModsFromFs ( ) ; const response = ( await webapi ( ) . post ( '/api/update/checkmodbuildupdates' , { gameVersion : store . state . gameInstallInfo . gameVersionPlatform . versionNumber , gamePlatform : store . state . gameInstallInfo . gameVersionPlatform . platform , guids : modMetadata . map ( m => m . id ) } ) ) . data ; if ( ! response . success ) return false ; const modUpdateResponses = ( response as GenericResponseWithData < CheckModBuildUpdateResponse [ ] > ) . data ; for ( const modUpdate of modUpdateResponses ) { const oldModMetadata = modMetadata . find ( mm => mm . id === modUpdate . guid ) ; if ( typeof oldModMetadata !== 'undefined' ) { if ( semver . gt ( modUpdate . modBuild . version , oldModMetadata . version ) ) { await ModListService . removeModByGuidVer ( oldModMetadata . id , oldModMetadata . version ) ; await ModListService . downloadModByUrl ( modUpdate . modBuild . downloadUrl , modUpdate . modBuild . fileName ) ; } } } } static async checkGithubReleaseUpdate ( org , repo , releaseId ) { const response = await axios . get ( `https://api.github.com/repos/${ org }/${ repo }/releases/latest` ) ; if ( response . status !== 200 ) return false ; return response . data . id > releaseId ; } static async updateBepinex ( ) { if ( ! store . state . bepinex . installed ) return false ; return await this . installBepinex ( ) ; } static async installBepinex ( ) { const updateExists = await this . checkGithubReleaseUpdate ( NUCLEARPOWERED_ORG , BEPINEX_REPO , store . state . bepinex . releaseId ) ; if ( ! updateExists ) return false ; const response = await axios . get ( `https://api.github.com/repos/${ NUCLEARPOWERED_ORG }/${ BEPINEX_REPO }/releases/latest` ) ; if ( response . status !== 200 ) return false ; ipcRenderer . send ( 'install-bepinex' , { location : store . state . gameInstallInfo . location , downloadUrl : response . data . assets [ 0 ] . browser_download_url } as InstallBepinexArgs ) ; return new Promise < boolean > ( ( resolve , reject ) => { ipcRenderer . once ( 'install-bepinex' , ( event , args ) => { if ( args ) { store . commit ( 'updateBepinexVersion' , response . data . id ) ; resolve ( args ) ; } reject ( new Error ( 'Could not install bepinex' ) ) ; } ) ; } ) ; } }