Skip to content

Commit

Permalink
fix: fix window reload crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Wurielle committed Jan 18, 2025
1 parent 3cc579e commit c5b39a9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
35 changes: 19 additions & 16 deletions apps/app/src/electron/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,25 @@ const App = () => {
const createWindows = () =>
app
.whenReady()
.then(() => ElectronWindowManager.registerInstance(
'messenger',
createMessengerWindow,
))
.then(() => ElectronWindowManager.registerInstance(
'overlay',
createOverlayWindow,
))
.then(() => ElectronWindowManager.registerInstance(
'speech-worker',
createSpeechWorkerWindow,
))
.then(() => ElectronWindowManager.registerInstance(
'messenger-game-overlay',
createMessengerGameOverlayWindow,
))
.then(async () => Promise.all([
ElectronWindowManager.registerInstance(
'messenger',
createMessengerWindow,
),
ElectronWindowManager.registerInstance(
'overlay',
createOverlayWindow,
),
ElectronWindowManager.registerInstance(
'speech-worker',
createSpeechWorkerWindow,
),
ElectronWindowManager.registerInstance(
'messenger-game-overlay',
createMessengerGameOverlayWindow,
),
]))


const registerElectronPinia = () => {
createApp(h({})).use(
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/electron/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { bridge } from '@packages/electron-bridger'
import electronDialog from '@/modules/electron-dialog'
import electronFilesystem from '@/modules/electron-filesystem'
import electronMessengerWindow from '@/teams/messenger/modules/electron-messenger-window'
import electronOverlayWindow from '@/teams/overlay/modules/electron-overlay-window'
import electronSpeechWorkerWindow from '@/teams/speech-worker/modules/electron-speech-worker-window'
import electronKeybinding from '@/modules/electron-keybinding'
import electronDisplay from '@/modules/electron-display'
import electronResources from '@/modules/electron-resources'
import electronSi from '@/modules/electron-si'
import electronTranslation from '@/modules/electron-translation'
import electronOverlayWindow from '@/teams/overlay/modules/electron-overlay-window'

export const bridgeModules = () =>
bridge.register([
Expand Down
10 changes: 6 additions & 4 deletions apps/app/src/electron/game-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { mouse, Point } from '@nut-tree-fork/nut-js'
import path from 'path'
import { EXTERNALS_DIR } from '@/electron/utils.ts'
import { fork } from 'child_process'
import { Window } from 'win-control'
import { Deferred } from '@packages/toolbox'
import { useGameOverlayStore } from '@/features/game-overlay/store'
import { onWatcherCleanup, watch } from 'vue'
Expand All @@ -30,6 +29,7 @@ type ProcessEvent = {
const ready = Deferred()

class GameOverlay {
public WinControl: any = null
public Overlay: any = null
public hookedProcesses: ProcessInfo[] = []
public intercepting = false
Expand Down Expand Up @@ -86,7 +86,7 @@ class GameOverlay {
if (focusWin) {
focusWin.blurWebView()
focusWin.focusOnWebView()
const { top, left, right, bottom } = Window.getByPid(
const { top, left, right, bottom } = this.WinControl.getByPid(
payload.pid,
).getDimensions()
const width = right-left
Expand Down Expand Up @@ -236,7 +236,7 @@ class GameOverlay {
let timeout: ReturnType<typeof setTimeout>
let interval: ReturnType<typeof setInterval>
interval = setInterval(() => {
const foregroundWindow = Window.getForeground()
const foregroundWindow = this.WinControl.getForeground()
if (pid === foregroundWindow?.getPid()) {
clearInterval(interval)
clearTimeout(timeout)
Expand Down Expand Up @@ -266,7 +266,9 @@ class GameOverlay {
public start() {
const databasesStore = useDatabasesStore()
const gameOverlayStore = useGameOverlayStore()
return Promise.all([import('@packages/electron-game-overlay'), gameOverlayStore.$whenReady()]).then(([Overlay]) => {
/* Importing win-control in preload breaks reload so we import it dynamically on start instead */
return Promise.all([import('@packages/electron-game-overlay'), import('win-control'), gameOverlayStore.$whenReady()]).then(([Overlay, WinControl]) => {
this.WinControl = WinControl
this.Overlay = Overlay.default
this.scaleFactor = screen.getDisplayNearestPoint({
x: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { useHitboxesStore } from '@/modules/vue-hitboxes/hitboxes.store'
import { Deferred } from '@packages/toolbox'
import ffi from 'ffi-napi'
import { getNativeWindowHandleInt } from '@/utils/electron-window'
import { Window } from 'win-control'
import gameOverlay from '@/electron/game-overlay.ts'

export const ElectronMessengerWindow = () => {
Expand All @@ -21,6 +20,7 @@ export const ElectronMessengerWindow = () => {
// let lastKeypressTime = 0
// const doubleKeypressDelta = 500
let registeredWindow: BrowserWindow | null = null
let WinControl: any | null = null
let hitboxesStore: ReturnType<typeof useHitboxesStore> | undefined
let settingsStore: ReturnType<typeof useSettingsStore> | undefined
let messengerStore: ReturnType<typeof useMessengerStore> | undefined
Expand Down Expand Up @@ -199,7 +199,7 @@ export const ElectronMessengerWindow = () => {
}

const toggleWindow = throttle((context: 'mouse' | 'keyboard') => {
const foregroundWindowPid = Window.getForeground()?.getPid()
const foregroundWindowPid = WinControl?.getForeground()?.getPid()
const hookedProcess = gameOverlay.hookedProcesses.find((process) => process.pid === foregroundWindowPid)
if (hookedProcess && !gameOverlay.intercepting) {
gameOverlay.startIntercept()
Expand Down Expand Up @@ -293,6 +293,7 @@ export const ElectronMessengerWindow = () => {
setDisplay(localSettingsStore.display)
})
ready.resolve(window)
import('win-control').then((module) => WinControl = module)
}

isReady().then(() => {
Expand Down

0 comments on commit c5b39a9

Please sign in to comment.