Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: add proxy support for auto-updater via env vars

esengine62ae562fde56
diff --git a/desktop/src-tauri/Cargo.lock b/desktop/src-tauri/Cargo.lock
index 87f7c3b73..106582757 100644
--- a/desktop/src-tauri/Cargo.lock
+++ b/desktop/src-tauri/Cargo.lock
@@ -794,6 +794,7 @@ dependencies = [
"tauri-plugin-updater",
"tiny_http",
"tokio",
+ "url",
"urlencoding",
"zip 2.4.2",
]
diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml
index cc19cc581..55f084904 100644
--- a/desktop/src-tauri/Cargo.toml
+++ b/desktop/src-tauri/Cargo.toml
@@ -26,6 +26,7 @@ tiny_http = "0.12"
open = "5"
urlencoding = "2"
zip = "2"
+url = "2"
[profile.release]
panic = "abort"
diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rs
index 07a6a3030..65573e396 100644
--- a/desktop/src-tauri/src/lib.rs
+++ b/desktop/src-tauri/src/lib.rs
@@ -9,6 +9,7 @@ use std::path::PathBuf;
use std::process::Stdio;
use std::sync::Mutex;
use tauri::{AppHandle, Emitter, Manager, State};
+use tauri_plugin_updater::UpdaterExt;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
@@ -208,6 +209,58 @@ async fn execute_command(
})
}
+// =============================================================================
+// Update (proxy-aware)
+// =============================================================================
+
+fn get_proxy_url() -> Option<url::Url> {
+ ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy"]
+ .iter()
+ .find_map(|key| std::env::var(key).ok())
+ .and_then(|s| url::Url::parse(&s).ok())
+}
+
+#[derive(serde::Serialize)]
+struct UpdateInfo {
+ version: String,
+ body: Option<String>,
+}
+
+#[tauri::command]
+async fn check_update(app: AppHandle) -> Result<Option<UpdateInfo>, String> {
+ let mut builder = app.updater_builder();
+ if let Some(proxy) = get_proxy_url() {
+ builder = builder.proxy(proxy);
+ }
+ let updater = builder.build().map_err(|e| e.to_string())?;
+ match updater.check().await {
+ Ok(Some(update)) => Ok(Some(UpdateInfo {
+ version: update.version.clone(),
+ body: update.body.clone(),
+ })),
+ Ok(None) => Ok(None),
+ Err(e) => Err(e.to_string()),
+ }
+}
+
+#[tauri::command]
+async fn install_update(app: AppHandle) -> Result<(), String> {
+ let mut builder = app.updater_builder();
+ if let Some(proxy) = get_proxy_url() {
+ builder = builder.proxy(proxy);
+ }
+ let updater = builder.build().map_err(|e| e.to_string())?;
+ let update = updater
+ .check()
+ .await
+ .map_err(|e| e.to_string())?
+ .ok_or_else(|| "No update available".to_string())?;
+ update
+ .download_and_install(|_, _| {}, || {})
+ .await
+ .map_err(|e| e.to_string())
+}
+
// =============================================================================
// Entry Point
// =============================================================================
@@ -233,6 +286,8 @@ pub fn run() {
unzip_to_directory,
execute_command,
get_embedded_asset,
+ check_update,
+ install_update,
])
.on_window_event(|window, event| {
if let tauri::WindowEvent::Destroyed = event {
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index 39294f70a..80e6d0ac6 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -10,7 +10,6 @@ import { TauriPlatformAdapter } from './TauriPlatformAdapter';
import { nativeFS, nativeShell } from './native-fs';
import { invoke } from '@tauri-apps/api/core';
import { getVersion } from '@tauri-apps/api/app';
-import { check } from '@tauri-apps/plugin-updater';
import { relaunch } from '@tauri-apps/plugin-process';
import type { App, ESEngineModule } from 'esengine';
@@ -142,7 +141,7 @@ async function checkForUpdate(manual = false): Promise<void> {
}
try {
- const update = await check();
+ const update = await invoke<{ version: string; body: string | null } | null>('check_update');
if (!update) {
if (tid) {
updateToast(tid, { type: 'success', title: 'You\'re up to date', message: 'No updates available.' });
@@ -164,7 +163,7 @@ async function checkForUpdate(manual = false): Promise<void> {
onClick: async () => {
const dlTid = showProgressToast('Downloading update...');
try {
- await update.downloadAndInstall();
+ await invoke('install_update');
updateToast(dlTid, { type: 'success', title: 'Update complete', message: 'Restarting...' });
setTimeout(() => relaunch(), 1000);
} catch (e) {