Skip to content

Commit

Permalink
fix: Correctly handle mod update event
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jan 2, 2024
1 parent 0771d97 commit a5d1e75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 0 additions & 2 deletions xmcl-electron-app/esbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import pluginRenderer from './plugins/esbuild.renderer.plugin'
import createSourcemapPlugin from './plugins/esbuild.sourcemap.plugin'
import pluginStatic from './plugins/esbuild.static.plugin'
import pluginWorker from './plugins/esbuild.worker.plugin'
import createFSPlugin from 'plugins/esbuild.fs.plugin'

const config = {
bundle: true,
Expand Down Expand Up @@ -46,7 +45,6 @@ const config = {
plugins: [
pluginRenderer(),
pluginStatic(),
createFSPlugin(),
createSourcemapPlugin(),
pluginPreload(path.resolve(__dirname, './preload')),
pluginVueDevtools(path.resolve(__dirname, '../extensions')),
Expand Down
4 changes: 2 additions & 2 deletions xmcl-runtime-api/src/services/InstanceModsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export class InstanceModsState {
const mods = [...this.mods]
for (const [r, a] of ops) {
if (a === InstanceModUpdatePayloadAction.Upsert) {
const index = mods.findIndex(m => m.path === r.path)
const index = mods.findIndex(m => m.path === r.path || m.hash === r.hash)
if (index === -1) {
mods.push(r)
} else if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-debugger
console.debug(`The mod ${r.path} is already in the list!`)
}
} else if (a === InstanceModUpdatePayloadAction.Remove) {
const index = mods.findIndex(m => m.path === r.path)
const index = mods.findIndex(m => m.path === r.path || m.hash === r.hash)
if (index !== -1) mods.splice(index, 1)
} else {
for (const update of r as PartialResourceHash[]) {
Expand Down
19 changes: 14 additions & 5 deletions xmcl-runtime/mod/InstanceModsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Inject, LauncherAppKey } from '~/app'
import { ResourceService } from '~/resource'
import { shouldIgnoreFile } from '~/resource/core/pathUtils'
import { AbstractService, ExposeServiceKey, ServiceStateManager } from '~/service'
import { AnyError } from '~/util/error'
import { AnyError, isSystemError } from '~/util/error'
import { LauncherApp } from '../app/LauncherApp'
import { AggregateExecutor } from '../util/aggregator'
import { linkWithTimeoutOrCopy, readdirIfPresent } from '../util/fs'
Expand Down Expand Up @@ -57,7 +57,11 @@ export class InstanceModsService extends AbstractService implements IInstanceMod
const state = new InstanceModsState()
const listener = this.resourceService as IResourceService
const onResourceUpdate = async (res: PartialResourceHash[]) => {
updateMod.push([res, InstanceModUpdatePayloadAction.Update])
if (res) {
updateMod.push([res, InstanceModUpdatePayloadAction.Update])
} else {
this.error(new AnyError('InstanceModUpdateError', 'Cannot update instance mods as the resource is empty'))
}
}

listener
Expand All @@ -69,17 +73,22 @@ export class InstanceModsService extends AbstractService implements IInstanceMod
const initializing = scan(basePath)
state.mods = await initializing

const processUpdate = async (filePath: string) => {
const processUpdate = async (filePath: string, retryLimit = 3) => {
try {
const [resource] = await this.resourceService.importResources([{ path: filePath, domain: ResourceDomain.Mods }], true)
if (resource && isModResource(resource)) {
this.log(`Instance mod add ${filePath}`)
updateMod.push([resource, InstanceModUpdatePayloadAction.Upsert])
} else {
this.warn(`Non mod resource added in /mods directory! ${filePath}`)
}
updateMod.push([resource, InstanceModUpdatePayloadAction.Upsert])
} catch (e) {
this.error(new AnyError('InstanceModAddError', `Fail to add instance mod ${filePath}`, { cause: e }))
if (isSystemError(e) && e.code === 'EMFILE' && retryLimit > 0) {
// Retry
setTimeout(() => processUpdate(filePath, retryLimit - 1), Math.random() * 2000)
} else {
this.error(new AnyError('InstanceModAddError', `Fail to add instance mod ${filePath}`, { cause: e }))
}
}
}

Expand Down

0 comments on commit a5d1e75

Please sign in to comment.