Skip to content

Commit

Permalink
特性: 游戏内多开
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Aug 21, 2019
1 parent f2b1fd5 commit 8330ea4
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 161 deletions.
1 change: 0 additions & 1 deletion assets/bin/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
<div id="mainWindowBox">
<webview
id="mainWindow"
src="./loading.html"
webpreferences="nativeWindowOpen=yes"
allowpopups
></webview>
Expand Down
21 changes: 10 additions & 11 deletions docs/IPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

下表列出了主进程接收到的请求:

| ID | 接收者 | 名称 | 参数 | 说明 |
| --- | ------------- | ----------------------- | -------- | ------------------------------------------------ |
| 01 | ManagerWindow | start-game || 启动游戏。 |
| 02 | ManagerWindow | start-tool | `id` | 启动 `id` 对应的工具。 |
| 03 | ManagerWindow | close-manager || 关闭 Manager 窗口。 |
| 04 | ManagerWindow | clear-cache || 清理缓存(`appDataDir/static/`|
| 05 | ManagerWindow | update-user-config | `config` | 更新主进程中的 `config` 并保存。 |
| 06 | GameWindow | main-loader-ready || 游戏宿主窗口已创建并初始化完毕,需要加载端口信息 |
| 07 | sandbox | sandbox-dirname-request || 返回当前的 `dirname`|
| 08 | sandbox | sandbox-appdata-request || 返回当前的 `appDataDir` |
| 09 | screenshot | save-screenshot | `buffer` | 对截屏进行保存。 |
| ID | 接收者 | 名称 | 参数 | 说明 |
| --- | ------------- | ----------------------- | -------- | -------------------------------- |
| 01 | ManagerWindow | start-game || 启动游戏。 |
| 02 | ManagerWindow | start-tool | `id` | 启动 `id` 对应的工具。 |
| 03 | ManagerWindow | close-manager || 关闭 Manager 窗口。 |
| 04 | ManagerWindow | clear-cache || 清理缓存(`appDataDir/static/`|
| 05 | ManagerWindow | update-user-config | `config` | 更新主进程中的 `config` 并保存。 |
| 06 | sandbox | sandbox-dirname-request || 返回当前的 `dirname`|
| 07 | sandbox | sandbox-appdata-request || 返回当前的 `appDataDir` |
| 08 | screenshot | save-screenshot | `buffer` | 对截屏进行保存。 |

### 通用请求

Expand Down
42 changes: 23 additions & 19 deletions src/bin/main/mainLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcRenderer, screen as electronScreen } from 'electron'
import { ipcRenderer } from 'electron'
import i18n from '../../i18n'
import Global from '../../manager/global'
import { MajsoulPlus } from '../../majsoul_plus'
Expand Down Expand Up @@ -43,21 +43,24 @@ function showScreenshotLabel(src: string) {
}, 8000)
}

ipcRenderer.on('take-screenshot', (event, scaleFactor: number) => {
if (webContents) {
webContents.capturePage(
{
x: 0,
y: 0,
width: Math.floor(mainWindow.clientWidth * scaleFactor),
height: Math.floor(mainWindow.clientHeight * scaleFactor)
},
image => {
ipcRenderer.send('save-screenshot', image.toPNG())
}
)
ipcRenderer.on(
'take-screenshot',
(event, index: number, scaleFactor: number) => {
if (webContents) {
webContents.capturePage(
{
x: 0,
y: 0,
width: Math.floor(mainWindow.clientWidth * scaleFactor),
height: Math.floor(mainWindow.clientHeight * scaleFactor)
},
image => {
ipcRenderer.send('save-screenshot', index, image.toPNG())
}
)
}
}
})
)

ipcRenderer.on('screenshot-saved', (event, filePath: string) => {
showScreenshotLabel('file://' + filePath)
Expand Down Expand Up @@ -112,7 +115,6 @@ mainWindow.addEventListener('dom-ready', () => {
if (!webContents) {
webContents = mainWindow.getWebContents()
webContents.setZoomFactor(1)
ipcRenderer.send('main-loader-ready')

webContents.on('will-navigate', (event, url) => {
if (isVanillaGameUrl(url)) {
Expand All @@ -136,11 +138,14 @@ mainWindow.addEventListener('dom-ready', () => {

ipcRenderer.on(
'load-url',
(event, url: string, port: number, http: boolean) => {
(event, url: string, port: number, http: boolean, partition?: string) => {
serverInfo = { url, port, http }
console.log('[Majsoul Plus] LoadURL', serverInfo)

mainWindow.loadURL(url)
if (partition) {
mainWindow.partition = partition
}
mainWindow.src = url
mainWindowBox.style.width = '100vw'
mainWindowBox.style.height = '100vh'
mainWindowBox.style.transform = 'none'
Expand All @@ -152,7 +157,6 @@ ipcRenderer.on('get-local-storage', () => {
'Object.entries(localStorage)',
false,
result => {
console.log(result)
ipcRenderer.send('save-local-storage', result)
}
)
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { app, ipcMain, dialog } from 'electron'
import { app, dialog, ipcMain } from 'electron'
import * as os from 'os'
import * as path from 'path'
import { UserConfigs } from './config'
import { LoadExtension } from './extension/extension'
import { Global, InitGlobal, Logger } from './global'
import i18n from './i18n'
import { LoadResourcePack } from './resourcepack/resourcepack'
import { LoadServer, ListenServer } from './server'
import { ListenServer, LoadServer } from './server'
import { LoadTool } from './tool/tool'
import bossKey from './utilities/bossKey'
import openFile from './utilities/openFile'
import sandbox from './utilities/sandbox'
import screenshot from './utilities/screenshot'
import { initGameWindow, GameWindow } from './windows/game'
import { initPlayer, AudioPlayer } from './windows/audioPlayer'
import { GameWindows, initGameWindow } from './windows/game'
import { initManagerWindow, ManagerWindow } from './windows/manager'
import { initToolManager } from './windows/tool'
import { initPlayer } from './windows/audioPlayer'
import i18n from './i18n'

// 初始化全局变量
InitGlobal()
Expand Down Expand Up @@ -107,7 +107,7 @@ const shouldQuit = app.makeSingleInstance((argv, directory) => {
} else {
// GameWindow Mode
if (argv.length > 2 + Number(process.env.NODE_ENV === 'development')) {
dialog.showMessageBox(GameWindow, {
dialog.showMessageBox(AudioPlayer, {
type: 'info',
title: i18n.text.main.programName(),
// TODO: i18n
Expand Down Expand Up @@ -144,6 +144,7 @@ app.on('ready', () => {
if (!process.env.SERVER_ONLY) {
// 初始化游戏窗口
initGameWindow()
GameWindows.newWindow()
} else {
// 通过 audioPlayer 窗口阻止程序退出
initPlayer()
Expand Down
29 changes: 12 additions & 17 deletions src/utilities/bossKey.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import Utility from './utility'
import { BrowserWindow, globalShortcut } from 'electron'
import { MajsoulPlus } from '../majsoul_plus'
import { GameWindowStatus, GameWindow } from '../windows/game'
import { ManagerWindow, ManagerWindowStatus } from '../windows/manager'
import { GameWindows } from '../windows/game'
import { ManagerWindow } from '../windows/manager'

function hideWindow(window: BrowserWindow, option: MajsoulPlus.WindowStatus) {
if (window) {
option.visible = window.isVisible()
option.muted = ManagerWindow.webContents.isAudioMuted()
function hideWindow(window: BrowserWindow) {
if (window && !window.isDestroyed()) {
window.hide()
window.webContents.setAudioMuted(true)
}
}

function showWindow(window: BrowserWindow, option: MajsoulPlus.WindowStatus) {
if (window) {
if (option.visible) {
window.show()
}
window.webContents.setAudioMuted(option.muted)
function showWindow(window: BrowserWindow) {
if (window && !window.isDestroyed()) {
window.show()
window.webContents.setAudioMuted(false)
}
}

Expand All @@ -34,12 +29,12 @@ class BossKey extends Utility {
globalShortcut.register('Alt+X', () => {
if (this.isActive) {
// 备份窗口信息 & 隐藏窗口
hideWindow(ManagerWindow, ManagerWindowStatus)
hideWindow(GameWindow, GameWindowStatus)
hideWindow(ManagerWindow)
GameWindows.forEach(window => hideWindow(window))
} else {
// 重新显示窗口
showWindow(ManagerWindow, ManagerWindowStatus)
showWindow(GameWindow, GameWindowStatus)
showWindow(ManagerWindow)
GameWindows.forEach(window => showWindow(window))
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions src/utilities/screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Utility from './utility'
import { ipcMain, app, clipboard, nativeImage } from 'electron'
import { app, clipboard, ipcMain, nativeImage } from 'electron'
import * as path from 'path'
import { writeFile } from '../utils'
import { GameWindow } from '../windows/game'
import { GameWindows } from '../windows/game'
import Utility from './utility'

class ScreenShot extends Utility {
constructor() {
Expand All @@ -11,7 +11,7 @@ class ScreenShot extends Utility {
}

protected execute() {
ipcMain.on('save-screenshot', (event, buf: Buffer) => {
ipcMain.on('save-screenshot', (event, index: number, buf: Buffer) => {
// 接收到的截图 Buffer
const buffer: Buffer = buf
// 由主进程进行保存
Expand All @@ -23,7 +23,7 @@ class ScreenShot extends Utility {
// 写入文件
writeFile(filePath, buffer).then(() => {
// 通知渲染进程(游戏宿主窗口)截图已保存,并由渲染进程弹窗
GameWindow.webContents.send('screenshot-saved', filePath)
GameWindows.get(index).webContents.send('screenshot-saved', filePath)
})
// 写入图像到剪切板
clipboard.writeImage(nativeImage.createFromBuffer(buffer))
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function mkdirsSync(dirname: string) {
export function getRemoteUrl(originalUrl: string): string {
return (
RemoteDomains[UserConfigs.userData.serverToPlay].domain +
originalUrl.replace(/^\/(0\/)?/g, '')
originalUrl.replace(/^\/\d\/?/g, '')
)
}

Expand Down
Loading

0 comments on commit 8330ea4

Please sign in to comment.