import { Injectable } from '@angular/core' ; import { ElectronService } from '../core/electron.service' ; import { AddSetterToObject } from '../core/util' ; import * as crypto from 'crypto' ; import { GlobalStatusService } from './globalStatus.service' ; import { ElMessageService } from 'element-angular' ; import { TranslateService } from '@ngx-translate/core' ; import { GameModel } from '../core/game.model' ; import { Size } from '../core/util' ; import * as Store from 'electron-store' ; const config = new Store ( ) ; export const ProxyRule = { off : 'direct://' , shimakazego : '127.0.0.1:8099' , acgp : '127.0.0.1:8123' , shadowsocks : 'socks5://127.0.0.1:1080' , custom : '' } ; export class Proxy { Type = 'off' ; Host = '' ; Port = '' ; Socks5 = false ; Enabled = false ; } export class Account { Name = '新账户' ; Username = '' ; Password = '' ; IsDefault = false ; } export class AccountList { Encrypted = false ; EncryptedList = '' ; List : Account [ ] = [ ] ; } export class GlobalSetting { public Proxy = new Proxy ( ) ; public SpeedUpKey = '' ; public UseSkillKey = '' ; public ScreenShotKey = '' ; public ReloadKey = '' ; public PauseKey = '' ; public BulletTimeKey = '' ; public Language = 'cn' ; public Zoom = 100 ; public Opacity = 100 ; public Mute = false ; public Lock = false ; public DefaultGame = new GameModel ( 'None' , new Size ( 640 , 960 ) , 'about:blank' ) ; public DataCollectPermit = true ; public DisableHardwareAcceleration = false ; } const needDispatch = [ 'Zoom' , 'Mute' , 'Lock' , 'Opacity' ] ; @ Injectable ( ) export class GlobalSettingService { public GlobalSetting = new GlobalSetting ( ) ; public AccountList = new AccountList ( ) ; constructor ( private electronService , private globalStatusService , private translateService , private message ) { if ( window . localStorage . getItem ( 'globalSetting' ) ) { try { this . GlobalSetting = Object . assign ( this . GlobalSetting , JSON . parse ( window . localStorage . getItem ( 'globalSetting' ) ) ) ; for ( let i = 0 ; i < needDispatch . length ; i ++ ) { const key = needDispatch [ i ] ; this . globalStatusService . GlobalStatusStore . Get ( key ) . Dispatch ( this . GlobalSetting [ key ] ) ; } this . globalStatusService . GlobalStatusStore . Get ( 'CurrentGame' ) . Dispatch ( this . GlobalSetting . DefaultGame ) ; } catch { } } this . GlobalSetting . DisableHardwareAcceleration = config . get ( 'disable-hardware-acceleration' , false ) as any ; if ( window . localStorage . getItem ( 'accountList' ) ) { try { this . AccountList = Object . assign ( this . AccountList , JSON . parse ( window . localStorage . getItem ( 'accountList' ) ) ) ; if ( ! this . AccountList . Encrypted ) { this . globalStatusService . GlobalStatusStore . Get ( 'AccountList' ) . Dispatch ( this . AccountList . List . map ( v => { return v . Username ; } ) ) ; const defaultAccount = this . AccountList . List . find ( v => v . IsDefault ) ; if ( defaultAccount ) { this . globalStatusService . GlobalStatusStore . Get ( 'SelectedAccount' ) . Dispatch ( defaultAccount . Username ) ; } } } catch { } } this . globalStatusService . GlobalStatusStore . Get ( 'AccountListPassword' ) . Subscribe ( v => { if ( this . AccountList . Encrypted ) { this . DecryptAccountList ( ) ; } else { this . translateService . get ( 'MESSAGE.SET-SUCCESS' ) . subscribe ( res => { this . message [ 'success' ] ( res ) ; } ) ; this . SaveAccountList ( ) ; } } ) ; for ( let i = 0 ; i < needDispatch . length ; i ++ ) { const key = needDispatch [ i ] ; this . globalStatusService . GlobalStatusStore . Get ( key ) . Subscribe ( v => { this . GlobalSetting [ key ] = v ; } ) ; } AddSetterToObject ( this . GlobalSetting , v => { this . saveConfigure ( ) ; } ) ; } public revertDisableHardwareAcceleration ( ) { this . GlobalSetting . DisableHardwareAcceleration = ! this . GlobalSetting . DisableHardwareAcceleration ; config . set ( 'disable-hardware-acceleration' , this . GlobalSetting . DisableHardwareAcceleration ) ; } private saveConfigure ( ) { window . localStorage . setItem ( 'globalSetting' , JSON . stringify ( this . GlobalSetting ) ) ; } public SaveAccountList ( ) { window . localStorage . setItem ( 'accountList' , JSON . stringify ( this . AccountList ) ) ; const password = this . globalStatusService . GlobalStatusStore . Get ( 'AccountListPassword' ) . Value ; if ( password && password !== '' ) { const tempAccountList = new AccountList ( ) ; tempAccountList . Encrypted = true ; const cipher = crypto . createCipher ( 'aes192' , password ) ; let encrypted = '' ; cipher . on ( 'readable' , ( ) => { const data = cipher . read ( ) ; if ( data ) { encrypted += ( < Buffer > data ) . toString ( 'hex' ) ; } } ) ; cipher . on ( 'end' , ( ) => { tempAccountList . EncryptedList = encrypted ; window . localStorage . setItem ( 'accountList' , JSON . stringify ( tempAccountList ) ) ; } ) ; cipher . write ( JSON . stringify ( this . AccountList . List ) ) ; cipher . end ( ) ; } else { const tempAccountList = new AccountList ( ) ; tempAccountList . Encrypted = false ; tempAccountList . List = this . AccountList . List ; window . localStorage . setItem ( 'accountList' , JSON . stringify ( tempAccountList ) ) ; } } Init ( ) { this . setProxy ( this . GlobalSetting . Proxy ) ; this . globalStatusService . GlobalStatusStore . Get ( 'Zoom' ) . Dispatch ( this . GlobalSetting . Zoom ) ; this . translateService . use ( this . GlobalSetting . Language ) ; } setProxy ( proxy ) { if ( proxy . Type === undefined ) { proxy . Type = proxy . Enabled ? 'custom' : 'off' ; } this . GlobalSetting . Proxy = proxy ; let proxyRule = ProxyRule [ proxy . Type ] ; if ( proxyRule === '' ) { proxyRule = ( proxy . Socks5 ? 'socks5://' : '' ) + `${ proxy . Host }:${ proxy . Port }` ; } if ( proxyRule === undefined ) { proxyRule = 'direct://' ; } this . electronService . ipcRenderer . send ( 'proxyStatusUpdate' , proxyRule ) ; } FindAccount ( username ) { return this . AccountList . List . find ( v => v . Username === username ) ; } DecryptAccountList ( ) { if ( this . AccountList . Encrypted ) { const password = this . globalStatusService . GlobalStatusStore . Get ( 'AccountListPassword' ) . Value ; if ( password !== '' && password ) { const decipher = crypto . createDecipher ( 'aes192' , password ) ; let decrypted = '' ; decipher . on ( 'readable' , ( ) => { const data = decipher . read ( ) ; if ( data ) { decrypted += ( < Buffer > data ) . toString ( 'utf-8' ) ; } } ) ; decipher . on ( 'end' , ( ) => { try { this . AccountList . List = JSON . parse ( decrypted ) ; this . globalStatusService . GlobalStatusStore . Get ( 'AccountList' ) . Dispatch ( this . AccountList . List . map ( v => { return v . Username ; } ) ) ; this . AccountList . Encrypted = false ; this . globalStatusService . GlobalStatusStore . Get ( 'AccountListPasswordError' ) . Dispatch ( false ) ; this . translateService . get ( 'MESSAGE.DECRYPT-SUCCESS' ) . subscribe ( res => { this . message [ 'success' ] ( res ) ; } ) ; } catch { } } ) ; decipher . on ( 'error' , ( ) => { this . translateService . get ( 'UTIL.PASSWORDERROR' ) . subscribe ( res => { this . message [ 'error' ] ( res ) ; } ) ; this . globalStatusService . GlobalStatusStore . Get ( 'AccountListPasswordError' ) . Dispatch ( true ) ; } ) ; decipher . write ( this . AccountList . EncryptedList , 'hex' ) ; decipher . end ( ) ; } } } ForceClearPassword ( ) { this . AccountList . Encrypted = false ; this . AccountList . EncryptedList = '' ; this . SaveAccountList ( ) ; } }