Files
cp-nix/patches/system-electron-resources.patch

150 lines
5.7 KiB
Diff

diff --git a/src/main/resolve/floatingWindow.ts b/src/main/resolve/floatingWindow.ts
index 49d1852..83fdb75 100644
--- a/src/main/resolve/floatingWindow.ts
+++ b/src/main/resolve/floatingWindow.ts
@@ -1,8 +1,8 @@
import { join } from 'path'
-import { is } from '@electron-toolkit/utils'
import { BrowserWindow, ipcMain } from 'electron'
import windowStateKeeper from 'electron-window-state'
import { getAppConfig, patchAppConfig } from '../config'
+import { isDevelopment } from '../utils/dirs'
import { floatingWindowLogger } from '../utils/logger'
import { applyTheme } from './theme'
import { buildContextMenu, showTrayIcon } from './tray'
@@ -86,11 +86,11 @@ async function createFloatingWindow(): Promise<void> {
// 加载页面
const url =
- is.dev && process.env['ELECTRON_RENDERER_URL']
+ isDevelopment() && process.env['ELECTRON_RENDERER_URL']
? `${process.env['ELECTRON_RENDERER_URL']}/floating.html`
: join(__dirname, '../renderer/floating.html')
- is.dev ? await floatingWindow.loadURL(url) : await floatingWindow.loadFile(url)
+ isDevelopment() ? await floatingWindow.loadURL(url) : await floatingWindow.loadFile(url)
} catch (error) {
logError('Failed to create floating window', error)
floatingWindow = null
diff --git a/src/main/utils/dirs.ts b/src/main/utils/dirs.ts
index 0771ca0..2c4170f 100644
--- a/src/main/utils/dirs.ts
+++ b/src/main/utils/dirs.ts
@@ -13,11 +13,15 @@ function portableDataDir(): string {
return path.join(exeDir(), 'data')
}
+export function isDevelopment(): boolean {
+ return is.dev && !app.getAppPath().endsWith('asar')
+}
+
// 本地 Electron.app 使用独立的应用名和数据目录,避免开发时访问正式版的
// `mihomo-party Safe Storage` Keychain item 或修改正式配置。已打包的 dev 预发行版
// 仍与正式版共享身份和数据,保持原有滚动升级路径。
export function configureAppPaths(): void {
- if (!app.isPackaged) {
+ if (isDevelopment()) {
app.setName('mihomo-party-dev')
app.setPath('userData', path.join(app.getPath('appData'), 'mihomo-party-dev'))
}
@@ -62,14 +66,13 @@ export function exePath(): string {
}
export function resourcesDir(): string {
- if (is.dev) {
+ const appPath = app.getAppPath()
+ if (appPath.endsWith('asar')) {
+ return path.dirname(appPath)
+ } else if (isDevelopment()) {
return path.join(__dirname, '../../extra')
} else {
- if (app.getAppPath().endsWith('asar')) {
- return process.resourcesPath
- } else {
- return path.join(app.getAppPath(), 'resources')
- }
+ return path.join(appPath, 'resources')
}
}
diff --git a/src/main/utils/icon.ts b/src/main/utils/icon.ts
index d3b32e1..b05fd69 100644
--- a/src/main/utils/icon.ts
+++ b/src/main/utils/icon.ts
@@ -9,6 +9,7 @@ import { app } from 'electron'
import { getControledMihomoConfig } from '../config'
import { DEFAULT_MIHOMO_PORTS } from '../../shared/appConfig'
import { windowsDefaultIcon, darwinDefaultIcon, otherDevicesIcon } from './defaultIcon'
+import { isDevelopment } from './dirs'
export function isIOSApp(appPath: string): boolean {
const appDir = appPath.endsWith('.app')
@@ -83,7 +84,7 @@ export function findBestAppPath(appPath: string): string | null {
}
async function getMacOSIconBuffer(appPath: string): Promise<Buffer> {
- if (!app.isPackaged) {
+ if (isDevelopment()) {
const { fileIconToBuffer } = await import('file-icon')
return Buffer.from(await fileIconToBuffer(appPath, { size: 512 }))
}
diff --git a/src/main/window.ts b/src/main/window.ts
index d40f89c..60529a3 100644
--- a/src/main/window.ts
+++ b/src/main/window.ts
@@ -1,12 +1,11 @@
import { join } from 'path'
import { readFileSync } from 'fs'
import { BrowserWindow, Menu, screen, shell } from 'electron'
-import { is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { getAppConfig } from './config'
import { quitWithoutCore } from './core/manager'
import { hideDockIcon, showDockIcon } from './resolve/tray'
-import { dataDir } from './utils/dirs'
+import { dataDir, isDevelopment } from './utils/dirs'
import { mainWindowLogger } from './utils/logger'
import { atomicWriteFileSync } from './utils/safeFile'
@@ -199,12 +198,12 @@ async function createWindowInternal(): Promise<void> {
autoQuitWithoutCoreMode
})
- if (is.dev) {
+ if (isDevelopment()) {
mainWindow.webContents.openDevTools()
}
const windowToLoad = mainWindow
- if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
+ if (isDevelopment() && process.env['ELECTRON_RENDERER_URL']) {
await windowToLoad.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
await windowToLoad.loadFile(join(__dirname, '../renderer/index.html'))
@@ -228,7 +227,7 @@ function setupWindowEvents(window: BrowserWindow, config: WindowConfig): void {
}
// 开发模式下始终显示窗口
- if (!silentStart || is.dev) {
+ if (!silentStart || isDevelopment()) {
clearQuitTimeout()
window.show()
window.focusOnWebView()
diff --git a/src/native/sysproxy/index.js b/src/native/sysproxy/index.js
index 147988f..11d0e55 100644
--- a/src/native/sysproxy/index.js
+++ b/src/native/sysproxy/index.js
@@ -71,6 +71,12 @@ function getResourcesPath() {
try {
const { app } = require('electron')
const appPath = app.getAppPath()
+ // A shared system Electron keeps process.resourcesPath in Electron's own
+ // store path. The directory containing app.asar is the app resource path.
+ if (appPath.endsWith('asar') && existsSync(join(dirname(appPath), 'sidecar'))) {
+ return dirname(appPath)
+ }
+
if (existsSync(join(appPath, 'extra', 'sidecar'))) {
return appPath
}