Skip to content

Commit

Permalink
Merge pull request #204 from MaaAssistantArknights/feat/component-dir…
Browse files Browse the repository at this point in the history
…-customize

feat: 组件卸载和组件安装位置移动(实验性功能)
  • Loading branch information
ChingCdesu authored Sep 21, 2023
2 parents 0c47834 + d161c7a commit 436285a
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 151 deletions.
3 changes: 2 additions & 1 deletion packages/main/componentManager/components/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CoreInstaller from '@main/componentManager/installers/core'
import path from 'path'
import type { Component } from '@type/componentManager'
import { infoPathOf } from '../utils/update'
import { getComponentBaseDir } from '../utils/path'

export const getComponentCore = async (): Promise<Component> => {
const coreLoader = new CoreLoader()
Expand All @@ -16,7 +17,7 @@ export const getComponentCore = async (): Promise<Component> => {
installer,
}

const installed = fs.existsSync(path.join(coreLoader.libPath, 'core_version'))
const installed = fs.existsSync(path.join(getComponentBaseDir(), 'core', 'core_version'))
if (installed) {
componentCore.status = 'not-compatible'
}
Expand Down
4 changes: 2 additions & 2 deletions packages/main/componentManager/installers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export default class CoreInstaller extends InstallerBase {

const getRelease = createGetRelease(
[
'https://gh.cirno.xyz/api.github.com/repos/MaaAssistantArknights/MaaAssistantArknights/releases',
'https://api.github.com/repos/MaaAssistantArknights/MaaAssistantArknights/releases',
'https://gh.cirno.xyz/api.github.com/repos/MaaAssistantArknights/MaaRelease/releases',
'https://api.github.com/repos/MaaAssistantArknights/MaaRelease/releases',
],
this.componentType
)
Expand Down
56 changes: 56 additions & 0 deletions packages/main/componentManager/utils/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Storage from '@main/storageManager'
import { getAppBaseDir } from '@main/utils/path'
import { getComponentAdb } from '../components/adb'
import { getComponentCore } from '../components/core'
import fs from 'fs'
import path from 'path'
import CoreLoader from '@main/coreLoader'
import _ from 'lodash'
import logger from '@main/utils/logger'

export function getComponentBaseDir() {
const storage = new Storage()
const componentDir = storage.get('setting.componentDir') as string

return _.isEmpty(componentDir) ? getAppBaseDir() : componentDir
}

export async function moveComponentBaseDir(dir: string): Promise<boolean> {
let withError = false
const coreLoader = new CoreLoader()
const coreStatus = coreLoader.loadStatus
if (coreStatus) {
coreLoader.dispose()
}

const sourceBaseDir = getComponentBaseDir()
const adbComponent = await getComponentAdb()
const coreComponent = await getComponentCore()
const adbSourceDir = path.join(sourceBaseDir, adbComponent.installer!.componentDir)
const coreSourceDir = path.join(sourceBaseDir, coreComponent.installer!.componentDir)

const adbTargetDir = path.join(dir, adbComponent.installer!.componentDir)
const coreTargetDir = path.join(dir, coreComponent.installer!.componentDir)

fs.mkdirSync(dir, { recursive: true })

if (fs.existsSync(adbSourceDir)) {
await fs.promises.cp(adbSourceDir, adbTargetDir, { recursive: true })
try {
await fs.promises.rm(adbSourceDir, { recursive: true })
} catch (e) {
withError = true
logger.error(e)
}
}
if (fs.existsSync(coreSourceDir)) {
await fs.promises.cp(coreSourceDir, coreTargetDir, { recursive: true })
try {
await fs.promises.rm(coreSourceDir, { recursive: true })
} catch (e) {
withError = true
logger.error(e)
}
}
return withError
}
4 changes: 2 additions & 2 deletions packages/main/componentManager/utils/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import path from 'path'
import logger from '@main/utils/logger'
import type { ReleaseObject, SourceMirror, UpdateStatus } from '../types'
import type { ComponentType } from '@type/componentManager'
import { getAppBaseDir } from '@main/utils/path'
import { getComponentBaseDir } from './path'

export function infoPathOf(componentDir: string) {
const dir = path.join(getAppBaseDir(), componentDir)
const dir = path.join(getComponentBaseDir(), componentDir)
return {
installRoot: dir,
currentVersion: path.join(dir, 'version'),
Expand Down
43 changes: 12 additions & 31 deletions packages/main/coreLoader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { TouchMode } from '@type/misc'
import { InstanceOptionKey } from '@type/misc'
import { extractFile } from '@main/utils/extract'
import type { ResourceType } from '@type/game'
import { getComponentBaseDir } from '@main/componentManager/utils/path'

const storage = new Storage()

Expand Down Expand Up @@ -67,9 +68,6 @@ class CoreLoader {
}

private DLib!: ffi.DynamicLibrary
private static libPath: string
private static readonly libPathKey = 'libPath'
private static readonly defaultLibPath = path.join(getAppBaseDir(), 'core')
private static loadStatus: boolean // core加载状态
public MeoAsstLib!: any
private readonly DepLibs: DynamicLibrary[] = []
Expand All @@ -79,16 +77,6 @@ class CoreLoader {
constructor() {
// 在构造函数中创建core存储文件夹
CoreLoader.loadStatus = false
CoreLoader.libPath = storage.get(CoreLoader.libPathKey) as string
if (!_.isString(CoreLoader.libPath) || !existsSync(CoreLoader.libPath)) {
logger.error(`Update resource folder: ${CoreLoader.libPath} --> ${CoreLoader.defaultLibPath}`)
CoreLoader.libPath = CoreLoader.defaultLibPath
if (!existsSync(CoreLoader.libPath)) mkdirSync(CoreLoader.libPath)
}
if (path.isAbsolute(CoreLoader.libPath)) {
CoreLoader.libPath = path.resolve(CoreLoader.libPath)
storage.set(CoreLoader.libPathKey, CoreLoader.libPath)
}
}

/**
Expand All @@ -109,13 +97,6 @@ class CoreLoader {
return CoreLoader.loadStatus
}

/**
* @description 返回core所在目录
*/
public get libPath(): string {
return CoreLoader.libPath
}

/**
* @description 释放core
*/
Expand Down Expand Up @@ -150,10 +131,10 @@ class CoreLoader {
try {
CoreLoader.loadStatus = true
this.dependences[process.platform].forEach(lib => {
this.DepLibs.push(ffi.DynamicLibrary(path.join(this.libPath, lib)))
this.DepLibs.push(ffi.DynamicLibrary(path.join(getComponentBaseDir(), 'core', lib)))
})
this.DLib = ffi.DynamicLibrary(
path.join(this.libPath, this.libName[process.platform]),
path.join(getComponentBaseDir(), 'core', this.libName[process.platform]),
ffi.RTLD_NOW
)
this.MeoAsstLib = {
Expand Down Expand Up @@ -327,13 +308,13 @@ class CoreLoader {
* @param path? 未指定就用libPath
* @returns
*/
public LoadResource(path: string = this.libPath): Boolean {
if (!existsSync(path)) {
public LoadResource(corePath: string = path.join(getComponentBaseDir(), 'core')): Boolean {
if (!existsSync(corePath)) {
// cache文件夹可能不存在
logger.warn(`[LoadResource] path not exists ${path}`)
return false
}
return this.MeoAsstLib.AsstLoadResource(path ?? this.libPath)
return this.MeoAsstLib.AsstLoadResource(corePath)
}

/**
Expand Down Expand Up @@ -538,7 +519,7 @@ class CoreLoader {
}

public UpdateTaskJson(type: ResourceType, data: string): void {
const dirPath = path.join(getAppBaseDir(), 'core', 'cache', type, 'resource')
const dirPath = path.join(getComponentBaseDir(), 'core', 'cache', type, 'resource')
if (!existsSync(dirPath)) {
logger.info(`Create dir ${dirPath}`)
mkdirSync(dirPath, { recursive: true })
Expand All @@ -550,16 +531,16 @@ class CoreLoader {

public async Upgrade(): Promise<void> {
logger.info('Start upgrade core')
const currentVersionFile = path.join(getAppBaseDir(), 'core', 'version')
const currentVersionFile = path.join(getComponentBaseDir(), 'core', 'version')
const currentVersion = existsSync(currentVersionFile)
? readFileSync(currentVersionFile, 'utf-8')
: null
const upgradeVersionFile = path.join(getAppBaseDir(), 'core', 'upgradable')
const upgradeVersionFile = path.join(getComponentBaseDir(), 'core', 'upgradable')
const upgradeVersion = existsSync(upgradeVersionFile)
? readFileSync(upgradeVersionFile, 'utf-8')
: null
if (currentVersion && upgradeVersion && currentVersion !== upgradeVersion) {
const upgradeFilePath = path.join(getAppBaseDir(), 'core', 'upgrade')
const upgradeFilePath = path.join(getComponentBaseDir(), 'core', 'upgrade')
const upgradeFileName = existsSync(upgradeFilePath)
? readFileSync(upgradeFilePath, 'utf-8')
: null
Expand All @@ -570,10 +551,10 @@ class CoreLoader {
)
unlinkSync(upgradeFilePath) // 提前删除, 防止因为压缩包损坏导致重复尝试更新
const compressedFile = path.join(getAppBaseDir(), 'download', upgradeFileName)
const dist = path.join(getAppBaseDir(), 'core')
const dist = path.join(getComponentBaseDir(), 'core')
if (existsSync(compressedFile)) {
// 升级前删除cache文件夹
const cacheDir = path.join(getAppBaseDir(), 'core', 'cache')
const cacheDir = path.join(getComponentBaseDir(), 'core', 'cache')
if (existsSync(cacheDir)) {
rmdirSync(cacheDir, { recursive: true })
}
Expand Down
4 changes: 2 additions & 2 deletions packages/main/deviceDetector/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { getPlatform } from '@main/utils/os'
import { $, $$ } from '@main/utils/shell'
import fs from 'fs'
import logger from '@main/utils/logger'
import { getComponentBaseDir } from '@main/componentManager/utils/path'

const executableSuffix = getPlatform() === 'windows' ? '.exe' : ''

export const defaultAdbPath = path.join(
app.getPath('appData'),
app.getName(),
getComponentBaseDir(),
'platform-tools',
`adb${executableSuffix}`
)
Expand Down
25 changes: 25 additions & 0 deletions packages/main/hooks/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import path from 'path'
import { app } from 'electron'
import { manager } from '@main/utils/logger'
import { getAppBaseDir } from '@main/utils/path'
import type { ComponentType } from '@type/componentManager'
import { getComponentBaseDir } from '@main/componentManager/utils/path'
import { getComponentAdb } from '@main/componentManager/components/adb'
import { getComponentCore } from '@main/componentManager/components/core'
import CoreLoader from '@main/coreLoader'

export default function useClearHooks(): void {
globalThis.main.Util = {
Expand Down Expand Up @@ -58,5 +63,25 @@ export default function useClearHooks(): void {
fs.writeFileSync(path.join(app.getPath('temp'), 'clearConfigToken'), '1')
app.quit()
},
async removeComponent(component: ComponentType) {
const sourceBaseDir = getComponentBaseDir()

if (component === 'Android Platform Tools') {
const adbComponent = await getComponentAdb()
const adbSourceDir = path.join(sourceBaseDir, adbComponent.installer!.componentDir)

fs.rmSync(adbSourceDir, { recursive: true })
} else if (component === 'Maa Core') {
const coreLoader = new CoreLoader()
const coreStatus = coreLoader.loadStatus
if (coreStatus) {
coreLoader.dispose()
}
const coreComponent = await getComponentCore()
const coreSourceDir = path.join(sourceBaseDir, coreComponent.installer!.componentDir)

fs.rmSync(coreSourceDir, { recursive: true })
}
},
}
}
2 changes: 2 additions & 0 deletions packages/main/hooks/path.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { openFolder, openExternal, saveTempJson } from '@main/utils/path'
import { moveComponentBaseDir } from '@main/componentManager/utils/path'

export default function usePathHooks(): void {
globalThis.main.Util = {
openFolder,
openExternal,
saveTempJson,
moveComponentBaseDir,
}
}
2 changes: 1 addition & 1 deletion packages/main/hooks/shutdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function shutdownMAA(): Promise<void> {
}

async function shutdownComputer(): Promise<void> {
logger.silly('Shutdown MAA')
logger.silly('Shutdown Computer')
const platform = getPlatform()

if (platform === 'windows') {
Expand Down
5 changes: 3 additions & 2 deletions packages/main/utils/path.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getComponentBaseDir } from '@main/componentManager/utils/path'
import { MD5 } from 'crypto-js'
import { app, shell } from 'electron'
import fs from 'fs'
Expand All @@ -8,11 +9,11 @@ export const getAppBaseDir = (): string => path.join(app.getPath('appData'), app
export const openFolder = (type: 'core' | 'ui-log' | 'core-log'): void => {
const baseAppDir = getAppBaseDir()
if (type === 'core') {
shell.openPath(path.join(baseAppDir, 'core'))
shell.openPath(path.join(getComponentBaseDir(), 'core'))
} else if (type === 'ui-log') {
shell.openPath(path.join(baseAppDir, 'logs'))
} else if (type === 'core-log') {
shell.openPath(path.join(baseAppDir, 'core', 'debug'))
shell.openPath(path.join(getComponentBaseDir(), 'core', 'debug'))
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/renderer/src/assets/icons/folder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/renderer/src/assets/icons/info.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 436285a

Please sign in to comment.