import webapi from '@/services/webapi' ; import { GenericResponse , GenericResponseWithData } from '@/services/responses/genericResponse' ; import { ModBuildResponse , ModResponse } from './responses/modResponse' ; import store from '@/store' ; import { ipcRenderer } from 'electron' ; import ModMetadata from '@/electronMain/models/modMetadata' ; import { DownloadModArgs , RemoveModArgs } from '@/electronMain/models/modArgs' ; import ExtModMetadata from '@/models/extModViewModel' ; export default class ModListService { static async downloadModByUrl ( downloadUrl , fileName ) { ipcRenderer . send ( 'download-mod' , { location : store . state . gameInstallInfo . location , fileName , downloadUrl } as DownloadModArgs ) ; return new Promise < boolean > ( ( resolve , reject ) => { ipcRenderer . once ( 'download-mod' , ( event , args ) => { args ? resolve ( args ) : reject ( new Error ( `Couldn't download mod at: ${ downloadUrl }` ) ) ; } ) ; } ) ; } static async removeModByGuidVer ( guid , version ) { ipcRenderer . send ( 'remove-mod' , { location : store . state . gameInstallInfo . location , guid : guid , version } as RemoveModArgs ) ; return new Promise < boolean > ( ( resolve , reject ) => { ipcRenderer . once ( 'remove-mod' , ( event , args ) => { if ( args ) { resolve ( args ) ; } reject ( new Error ( `Couldn't remove mod with namespace ${ guid } and version ${ version }` ) ) ; } ) ; } ) ; } static async getModInfo ( guid ) { try { const response = ( await webapi ( ) . get ( `/api/mods/guid/${ guid }` ) ) . data ; if ( response . success ) { return ( response as GenericResponseWithData < ModResponse > ) . data ; } } catch ( e ) { console . log ( `Mod with guid: ${ guid } was not found on the online repos` ) ; } } static async getInfoForModMetadata ( m ) { const modResponse = await ModListService . getModInfo ( m . id ) ; if ( modResponse ) { return { ... m , imageUrl : modResponse . imageUrl , creator : modResponse . creator . username , description : modResponse . description } ; } else { return { ... m , imageUrl : '' , creator : m . authors , description : m . description } ; } } static async getModListPaginated ( page ) { try { const response = ( await webapi ( ) . get ( `/api/mods/page/${ page }` ) ) . data ; if ( response . success ) { return ( response as GenericResponseWithData < ModResponse [ ] > ) . data ; } } catch ( e ) { console . warn ( `Could not ${ this . getModListPaginated . toString ( ) }` ) ; } } static async searchModList ( search ) { try { const response = ( await webapi ( ) . get ( `/api/mods/search?query=${ encodeURIComponent ( search ) }` ) ) . data ; if ( response . success ) { return ( response as GenericResponseWithData < ModResponse [ ] > ) . data ; } } catch ( e ) { console . warn ( `Could not ${ this . searchModList . toString ( ) }` ) ; } } static async getModBuildsPaginatedFor ( modid , page ) { try { const response = ( await webapi ( ) . get ( `/api/modbuilds/${ modid }/page/${ page }` ) ) . data ; if ( response . success ) { return ( response as GenericResponseWithData < ModBuildResponse [ ] > ) . data ; } } catch ( e ) { console . warn ( `Could not ${ this . getModBuildsPaginatedFor . toString ( ) }` ) ; } } static async updateStarForMod ( id ) { try { const response = ( await webapi ( ) . post ( `/api/mods/${ id }/star` ) ) . data ; if ( response . success ) { return ( response as GenericResponseWithData < ModResponse > ) . data ; } } catch ( e ) { console . warn ( `Could not ${ this . updateStarForMod . toString ( ) }` ) ; } } static async readModsFromFs ( ) { ipcRenderer . send ( 'refresh-mods' , store . state . gameInstallInfo . location ) ; return new Promise < ModMetadata [ ] > ( ( resolve , reject ) => { ipcRenderer . once ( 'refresh-mods' , ( event , args ) => { if ( args ) { resolve ( args ) ; } reject ( new Error ( 'Unable to refresh mods' ) ) ; } ) ; } ) ; } static async startFileWatcher ( ) { ipcRenderer . send ( 'start-file-watcher' , store . state . gameInstallInfo . location ) ; return new Promise < boolean > ( ( resolve , reject ) => { ipcRenderer . once ( 'start-file-watcher' , ( event , args ) => { args ? resolve ( args ) : reject ( new Error ( `Couldn't start file watcher at ${ store . state . gameInstallInfo . location }` ) ) ; } ) ; } ) ; } static async stopFileWatcher ( ) { ipcRenderer . send ( 'stop-file-watcher' ) ; return new Promise < boolean > ( ( resolve , reject ) => { ipcRenderer . once ( 'stop-file-watcher' , ( event , args ) => { args ? resolve ( args ) : reject ( new Error ( `Couldn't stop file watcher at ${ store . state . gameInstallInfo . location }` ) ) ; } ) ; } ) ; } }