Skip to content

Commit

Permalink
Merge branch 'migrate-sendTo' into update-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Mar 6, 2024
2 parents 586be17 + 9554ab3 commit bb50b2a
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 228 deletions.
12 changes: 10 additions & 2 deletions app/src/ts/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './common/asar'
import { app, BrowserWindow, ipcMain, BrowserWindowConstructorOptions, Menu, MenuItem, globalShortcut } from 'electron'
import { app, BrowserWindow, ipcMain, BrowserWindowConstructorOptions, Menu, MenuItem, globalShortcut, MessageChannelMain } from 'electron'
import { join } from 'path'
import * as url from 'url'
import './common/get-path'
Expand All @@ -24,6 +24,8 @@ let mainWindow: BrowserWindow | null = null
let backWindow: BrowserWindow | null = null

function createWindow (): void {
const { port1, port2 } = new MessageChannelMain()

const browerWindowOptions: BrowserWindowConstructorOptions = {
width: 1296,
height: 863,
Expand All @@ -50,6 +52,7 @@ function createWindow (): void {
if (!mainWindow) return
mainWindow.show()
mainWindow.focus()
mainWindow.webContents.postMessage('port', null, [port1])
})

mainWindow.on('closed', function () {
Expand All @@ -75,6 +78,11 @@ function createWindow (): void {
}
})

backWindow.on('ready-to-show', function () {
if (!backWindow) return
backWindow.webContents.postMessage('port', null, [port2])
})

backWindow.on('closed', function () {
backWindow = null
})
Expand Down Expand Up @@ -127,7 +135,7 @@ async function main (): Promise<void> {
await app.whenReady()
registerGlobalShortcut()
setMenu()
ipc()
ipc(() => mainWindow)

ipcMain.on('flash', () => {
mainWindow && mainWindow.flashFrame(true)
Expand Down
13 changes: 9 additions & 4 deletions app/src/ts/main/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain, SaveDialogOptions, dialog, app, RelaunchOptions, OpenDialogOptions } from 'electron'
import { ipcMain, SaveDialogOptions, dialog, app, RelaunchOptions, OpenDialogOptions, type BrowserWindow } from 'electron'
import { getProxyAgent } from 'mishiro-core/util/proxy'
import got from 'got'
// import onManifestQuery from './on-manifest-query'
Expand Down Expand Up @@ -40,7 +40,7 @@ function registerIpcConfig (configurer: Configurer): void {
})
}

export default function ipc (): void {
export default function ipc (getMainWindow: () => (BrowserWindow | null)): void {
if (initialized) return

// let manifestData: any = {}
Expand All @@ -67,8 +67,13 @@ export default function ipc (): void {
// event.sender.send('readManifest', masterHash, resVer)
// })

ipcMain.on('openScoreWindow', function () {
openScoreWindow()
ipcMain.handle('openScoreWindow', function () {
openScoreWindow(() => {
const mainWindow = getMainWindow()
if (mainWindow) {
mainWindow.webContents.send('liveEnd')
}
})
})

ipcMain.handle('showSaveDialog', (_event, options: SaveDialogOptions) => {
Expand Down
5 changes: 4 additions & 1 deletion app/src/ts/main/open-score-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { error } from './log'

let win: BrowserWindow | null = null

export default function openScoreWindow (): void {
export default function openScoreWindow (onClose?: () => void): void {
if (win !== null) {
return
}
Expand Down Expand Up @@ -45,6 +45,9 @@ export default function openScoreWindow (): void {
}

win.on('close', () => {
if (typeof onClose === 'function') {
onClose()
}
win = null
})
}
163 changes: 69 additions & 94 deletions app/src/ts/renderer-back.ts
Original file line number Diff line number Diff line change
@@ -1,134 +1,109 @@
import './renderer/preload'
import { ipcRenderer } from 'electron'
import DB from './common/db'
import { batchDownload, batchStop, getBatchErrorList, setDownloaderProxy } from './renderer/back/batch-download'
import mainWindowId from './renderer/back/main-window-id'
// import mainWindowId from './renderer/back/main-window-id'
import readMaster from './renderer/back/on-master-read'

let manifest: DB | null = null
let master: DB | null = null

ipcRenderer.on('openManifestDatabase', async (event, callbackChannel: string, path: string) => {
if (manifest) {
event.sender.sendTo(mainWindowId, callbackChannel, null)
return
}
try {
manifest = await DB.open(path)
event.sender.sendTo(mainWindowId, callbackChannel, null)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message)
window.node.electron.ipcRenderer.on('port', e => {
const mainWindowPort = e.ports[0]
const portEvent = new window.node.events.EventEmitter()

mainWindowPort.onmessage = function (event) {
portEvent.emit('message', event)
}
})

ipcRenderer.on('openMasterDatabase', async (event, callbackChannel: string, path: string) => {
if (master) {
event.sender.sendTo(mainWindowId, callbackChannel, null)
return
function defineRemoteFunction (name: string, fn: (...args: any[]) => any): void {
portEvent.on('message', (event) => {
if (event.data.type === name) {
Promise.resolve(fn(...event.data.payload)).then(ret => {
mainWindowPort.postMessage({
id: event.data.id,
err: null,
data: ret
})
}).catch(err => {
mainWindowPort.postMessage({
id: event.data.id,
err: err.message,
data: undefined
})
})
}
})
}
try {

defineRemoteFunction('openManifestDatabase', async (path: string) => {
if (manifest) {
return
}
manifest = await DB.open(path)
})

defineRemoteFunction('openMasterDatabase', async (path: string) => {
if (master) {
return
}
master = await DB.open(path)
event.sender.sendTo(mainWindowId, callbackChannel, null)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message)
}
})
})

ipcRenderer.on('getMasterHash', async (event, callbackChannel: string) => {
try {
defineRemoteFunction('getMasterHash', async () => {
const masterHash = (await manifest!.find('manifests', ['name', 'hash'], { name: 'master.mdb' }))[0].hash as string
event.sender.sendTo(mainWindowId, callbackChannel, null, masterHash)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
return masterHash
})

ipcRenderer.on('readMasterData', async (event, callbackChannel: string, masterFile: string) => {
try {
defineRemoteFunction('readMasterData', async (masterFile: string) => {
master = await DB.open(masterFile)
const masterData = await readMaster(master, manifest!)
await master.close()
master = null
event.sender.sendTo(mainWindowId, callbackChannel, null, masterData)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
return masterData
})

ipcRenderer.on('getCardHash', async (event, callbackChannel: string, id: string | number) => {
try {
defineRemoteFunction('getCardHash', async (id: string | number) => {
const res = await manifest!.find('manifests', ['hash'], { name: `card_bg_${id}.unity3d` })
event.sender.sendTo(mainWindowId, callbackChannel, null, res[0].hash)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
return res[0].hash
})

ipcRenderer.on('getIconHash', async (event, callbackChannel: string, id: string | number) => {
try {
defineRemoteFunction('getIconHash', async (id: string | number) => {
const res = await manifest!.findOne('manifests', ['hash'], { name: `card_${id}_m.unity3d` })
event.sender.sendTo(mainWindowId, callbackChannel, null, res.hash)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
return res.hash
})

ipcRenderer.on('getEmblemHash', async (event, callbackChannel: string, id: string | number) => {
try {
defineRemoteFunction('getEmblemHash', async (id: string | number) => {
const res = await manifest!.findOne('manifests', ['hash'], { name: `emblem_${id}_l.unity3d` })
event.sender.sendTo(mainWindowId, callbackChannel, null, res.hash)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
return res.hash
})

ipcRenderer.on('searchResources', async (event, callbackChannel: string, queryString: string) => {
try {
defineRemoteFunction('searchResources', async (queryString: string) => {
const res = await manifest!.find<{ name: string, hash: string }>('manifests', ['name', 'hash', 'size'], { name: { $like: `%${queryString.trim()}%` } })
event.sender.sendTo(mainWindowId, callbackChannel, null, res)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
return res
})

let batchDownloading = false
let batchDownloading = false

ipcRenderer.on('startBatchDownload', async (event, callbackChannel: string) => {
try {
defineRemoteFunction('startBatchDownload', async () => {
batchDownloading = true
await batchDownload(manifest!)
event.sender.sendTo(mainWindowId, callbackChannel, null, batchDownloading)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
await batchDownload(mainWindowPort, manifest!)
return batchDownloading
})

ipcRenderer.on('stopBatchDownload', async (event, callbackChannel: string) => {
try {
defineRemoteFunction('stopBatchDownload', async () => {
await batchStop()
batchDownloading = false
event.sender.sendTo(mainWindowId, callbackChannel, null, batchDownloading)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, '')
}
})
return batchDownloading
})

ipcRenderer.on('getBatchErrorList', (event, callbackChannel: string) => {
try {
defineRemoteFunction('getBatchErrorList', () => {
const list = getBatchErrorList()
event.sender.sendTo(mainWindowId, callbackChannel, null, list)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message, null)
}
})
return list
})

ipcRenderer.on('setDownloaderProxy', (event, callbackChannel: string, proxy: string) => {
try {
defineRemoteFunction('setDownloaderProxy', (proxy: string) => {
setDownloaderProxy(proxy)
event.sender.sendTo(mainWindowId, callbackChannel, null)
} catch (err: any) {
event.sender.sendTo(mainWindowId, callbackChannel, err.message)
}
})
})

window.addEventListener('beforeunload', () => {
Expand Down
Loading

0 comments on commit bb50b2a

Please sign in to comment.