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.

fix: resolve CSP blocking, WASM loading and texture 404 in release build

esengine9c0b570bb149
diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml
index dd4379a96..88e648b2e 100644
--- a/desktop/src-tauri/Cargo.toml
+++ b/desktop/src-tauri/Cargo.toml
@@ -13,7 +13,7 @@ crate-type = ["lib", "cdylib", "staticlib"]
tauri-build = { version = "2", features = [] }
[dependencies]
-tauri = { version = "2", features = ["protocol-asset"] }
+tauri = { version = "2", features = ["protocol-asset", "devtools"] }
tauri-plugin-dialog = "2"
tauri-plugin-fs = { version = "2", features = ["watch"] }
tauri-plugin-shell = "2"
diff --git a/desktop/src-tauri/tauri.conf.json b/desktop/src-tauri/tauri.conf.json
index 31e47a6a8..2d9872444 100644
--- a/desktop/src-tauri/tauri.conf.json
+++ b/desktop/src-tauri/tauri.conf.json
@@ -23,7 +23,8 @@
}
],
"security": {
- "csp": "default-src 'self' ipc: http://ipc.localhost; script-src 'self' 'wasm-unsafe-eval' 'unsafe-eval' blob:; img-src 'self' asset: http://asset.localhost data:; style-src 'self' 'unsafe-inline'",
+ "csp": "default-src 'self' ipc: http://ipc.localhost; script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval' 'unsafe-eval' blob:; img-src 'self' asset: http://asset.localhost data:; style-src 'self' 'unsafe-inline'",
+ "dangerousDisableAssetCspModification": ["style-src"],
"assetProtocol": {
"enable": true,
"scope": ["*/**"]
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index 4040c532f..3ecd0be89 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -19,15 +19,24 @@ let wasmModule: ESEngineModule | null = null;
function loadESModule(url: string): Promise<any> {
return new Promise((resolve, reject) => {
const id = `__esm_${Date.now()}`;
+ const fullUrl = new URL(url, window.location.origin).href;
+ const code = `import m from '${fullUrl}'; window.${id}=m; window.dispatchEvent(new Event('${id}'));`;
+ const blob = new Blob([code], { type: 'application/javascript' });
+ const blobUrl = URL.createObjectURL(blob);
const script = document.createElement('script');
script.type = 'module';
- script.textContent = `import m from '${url}'; window.${id}=m; window.dispatchEvent(new Event('${id}'));`;
+ script.src = blobUrl;
window.addEventListener(id, () => {
resolve((window as any)[id]);
delete (window as any)[id];
script.remove();
+ URL.revokeObjectURL(blobUrl);
}, { once: true });
- script.onerror = reject;
+ script.onerror = (e) => {
+ script.remove();
+ URL.revokeObjectURL(blobUrl);
+ reject(e);
+ };
document.head.appendChild(script);
});
}
diff --git a/desktop/src/native-fs.ts b/desktop/src/native-fs.ts
index 5c395f9fb..1113c7614 100644
--- a/desktop/src/native-fs.ts
+++ b/desktop/src/native-fs.ts
@@ -171,7 +171,8 @@ export const nativeFS: NativeFS = {
async readFile(path: string) {
try {
return await readTextFile(path);
- } catch {
+ } catch (err) {
+ console.error(`[NativeFS] readFile failed: ${path}`, err);
return null;
}
},
@@ -179,7 +180,8 @@ export const nativeFS: NativeFS = {
async readBinaryFile(path: string) {
try {
return await readFile(path);
- } catch {
+ } catch (err) {
+ console.error(`[NativeFS] readBinaryFile failed: ${path}`, err);
return null;
}
},
diff --git a/editor/src/Editor.ts b/editor/src/Editor.ts
index a49e26a0b..cf4b1f8bc 100644
--- a/editor/src/Editor.ts
+++ b/editor/src/Editor.ts
@@ -110,6 +110,7 @@ export class Editor {
private previewService_: PreviewService | null = null;
private shortcutManager_: ShortcutManager;
private activeBottomPanelId_: string | null = 'content-browser';
+ private assetLibraryReady_: Promise<void> = Promise.resolve();
constructor(container: HTMLElement, options?: EditorOptions) {
this.container_ = container;
@@ -148,7 +149,7 @@ export class Editor {
if (this.projectPath_) {
this.setupEditorGlobals();
- this.initializeAssetLibrary();
+ this.assetLibraryReady_ = this.initializeAssetLibrary();
this.initializeAllScripts();
this.syncProjectSettings();
this.previewService_ = new PreviewService({ projectPath: this.projectPath_ });
@@ -304,6 +305,7 @@ export class Editor {
}
async loadScene(): Promise<void> {
+ await this.assetLibraryReady_;
const scene = await loadSceneFromFile();
if (scene) {
await getAssetLibrary().migrateScene(scene);
@@ -312,6 +314,7 @@ export class Editor {
}
async openSceneFromPath(scenePath: string): Promise<void> {
+ await this.assetLibraryReady_;
const scene = await loadSceneFromPath(scenePath);
if (scene) {
const migrated = await getAssetLibrary().migrateScene(scene);
@@ -463,10 +466,17 @@ export class Editor {
// =========================================================================
private async initializeAssetLibrary(): Promise<void> {
- if (!this.projectPath_) return;
+ console.log(`[Editor] initializeAssetLibrary: projectPath=${this.projectPath_}`);
+ if (!this.projectPath_) {
+ console.warn('[Editor] initializeAssetLibrary: no projectPath');
+ return;
+ }
const fs = getEditorContext().fs;
- if (!fs) return;
+ if (!fs) {
+ console.warn('[Editor] initializeAssetLibrary: no fs in context');
+ return;
+ }
const projectDir = this.projectPath_.replace(/[/\\][^/\\]+$/, '');
try {
diff --git a/editor/src/asset/AssetDatabase.ts b/editor/src/asset/AssetDatabase.ts
index ac3379bc0..e8b588e35 100644
--- a/editor/src/asset/AssetDatabase.ts
+++ b/editor/src/asset/AssetDatabase.ts
@@ -56,6 +56,7 @@ export class AssetDatabase {
private groupService_: AssetGroupService | null = null;
async initialize(projectDir: string, fs: NativeFS): Promise<void> {
+ console.log(`[AssetDatabase] initialize: projectDir="${projectDir}"`);
this.fs_ = fs;
this.projectDir_ = projectDir;
this.uuidToEntry_.clear();
@@ -68,9 +69,12 @@ export class AssetDatabase {
await this.groupService_.load();
const assetsDir = joinPath(projectDir, 'assets');
- if (await fs.exists(assetsDir)) {
+ const assetsDirExists = await fs.exists(assetsDir);
+ console.log(`[AssetDatabase] assetsDir="${assetsDir}", exists=${assetsDirExists}`);
+ if (assetsDirExists) {
await this.scanDirectory(assetsDir, 'assets');
}
+ console.log(`[AssetDatabase] initialized: ${this.uuidToEntry_.size} entries registered`);
}
// =========================================================================
@@ -78,7 +82,11 @@ export class AssetDatabase {
// =========================================================================
getPath(uuid: string): string | undefined {
- return this.uuidToEntry_.get(uuid)?.path;
+ const path = this.uuidToEntry_.get(uuid)?.path;
+ if (!path) {
+ console.warn(`[AssetDatabase] getPath: UUID "${uuid}" not found (${this.uuidToEntry_.size} entries registered)`);
+ }
+ return path;
}
getUuid(path: string): string | undefined {
diff --git a/editor/src/panels/SceneViewPanel.ts b/editor/src/panels/SceneViewPanel.ts
index 282760439..b323c8e0d 100644
--- a/editor/src/panels/SceneViewPanel.ts
+++ b/editor/src/panels/SceneViewPanel.ts
@@ -12,6 +12,7 @@ import { getPlatformAdapter } from '../platform/PlatformAdapter';
import { hasSlicing, parseTextureMetadata } from '../types/TextureMetadata';
import { getDefaultComponentData } from '../schemas/ComponentSchemas';
import { getGlobalPathResolver } from '../asset';
+import { isUUID, getAssetLibrary } from '../asset/AssetDatabase';
import { icons } from '../utils/icons';
import { EditorSceneRenderer } from '../renderer/EditorSceneRenderer';
import { quatToEuler, eulerToQuat, findCanvasWorldRect } from '../math/Transform';
@@ -315,29 +316,33 @@ export class SceneViewPanel {
private loadTexture(texturePath: string): HTMLImageElement | null {
if (!this.projectPath_ || !texturePath) return null;
- if (this.textureCache_.has(texturePath)) {
- return this.textureCache_.get(texturePath) ?? null;
+ const resolved = isUUID(texturePath)
+ ? (getAssetLibrary().getPath(texturePath) ?? texturePath)
+ : texturePath;
+
+ if (this.textureCache_.has(resolved)) {
+ return this.textureCache_.get(resolved) ?? null;
}
- if (this.loadingTextures_.has(texturePath)) {
+ if (this.loadingTextures_.has(resolved)) {
return null;
}
- this.loadingTextures_.add(texturePath);
+ this.loadingTextures_.add(resolved);
const projectDir = this.projectPath_.replace(/\\/g, '/').replace(/\/[^/]+$/, '');
- const fullPath = `${projectDir}/${texturePath}`;
+ const fullPath = `${projectDir}/${resolved}`;
const img = new Image();
img.onload = () => {
- this.loadingTextures_.delete(texturePath);
- this.textureCache_.set(texturePath, img);
+ this.loadingTextures_.delete(resolved);
+ this.textureCache_.set(resolved, img);
this.requestRender();
};
img.onerror = () => {
- this.loadingTextures_.delete(texturePath);
- this.textureCache_.set(texturePath, null);
+ this.loadingTextures_.delete(resolved);
+ this.textureCache_.set(resolved, null);
console.warn(`Failed to load texture: ${fullPath}`);
};
diff --git a/editor/src/panels/inspector/EntityInspector.ts b/editor/src/panels/inspector/EntityInspector.ts
index 580e4724b..e45b2474e 100644
--- a/editor/src/panels/inspector/EntityInspector.ts
+++ b/editor/src/panels/inspector/EntityInspector.ts
@@ -96,6 +96,9 @@ export function renderComponent(
editors: EditorInfo[]
): void {
const schema = getComponentSchema(component.type);
+ if (!schema) {
+ console.warn(`[Inspector] No schema found for component "${component.type}"`);
+ }
if (schema?.category === 'tag') {
renderTagComponent(container, entity, component, store);
return;
diff --git a/editor/src/property/PropertyEditor.ts b/editor/src/property/PropertyEditor.ts
index 015f19e02..7f8db7145 100644
--- a/editor/src/property/PropertyEditor.ts
+++ b/editor/src/property/PropertyEditor.ts
@@ -67,6 +67,9 @@ export function createPropertyEditor(
ctx: PropertyEditorContext
): PropertyEditorInstance | null {
const factory = editorRegistry.get(ctx.meta.type);
- if (!factory) return null;
+ if (!factory) {
+ console.warn(`[PropertyEditor] No editor registered for type "${ctx.meta.type}"`);
+ return null;
+ }
return factory(container, ctx);
}
diff --git a/editor/src/property/editorUtils.ts b/editor/src/property/editorUtils.ts
new file mode 100644
index 000000000..e0910794a
--- /dev/null
+++ b/editor/src/property/editorUtils.ts
@@ -0,0 +1,50 @@
+export function setupDragLabel(
+ label: HTMLElement,
+ input: HTMLInputElement,
+ onChange: (delta: number) => void,
+ step: number = 0.1
+): void {
+ let startX = 0;
+ let startValue = 0;
+ let isDragging = false;
+
+ const onMouseMove = (e: MouseEvent) => {
+ if (!isDragging) return;
+ const delta = (e.clientX - startX) * step;
+ const newValue = startValue + delta;
+ input.value = newValue.toFixed(2);
+ onChange(newValue);
+ };
+
+ const onMouseUp = () => {
+ isDragging = false;
+ document.removeEventListener('mousemove', onMouseMove);
+ document.removeEventListener('mouseup', onMouseUp);
+ document.body.style.cursor = '';
+ };
+
+ label.style.cursor = 'ew-resize';
+ label.addEventListener('mousedown', (e) => {
+ e.preventDefault();
+ isDragging = true;
+ startX = e.clientX;
+ startValue = parseFloat(input.value) || 0;
+ document.body.style.cursor = 'ew-resize';
+ document.addEventListener('mousemove', onMouseMove);
+ document.addEventListener('mouseup', onMouseUp);
+ });
+}
+
+export function colorToHex(color: { r: number; g: number; b: number; a: number }): string {
+ const r = Math.round(color.r * 255).toString(16).padStart(2, '0');
+ const g = Math.round(color.g * 255).toString(16).padStart(2, '0');
+ const b = Math.round(color.b * 255).toString(16).padStart(2, '0');
+ return `#${r}${g}${b}`;
+}
+
+export function hexToColor(hex: string): { r: number; g: number; b: number; a: number } {
+ const r = parseInt(hex.slice(1, 3), 16) / 255;
+ const g = parseInt(hex.slice(3, 5), 16) / 255;
+ const b = parseInt(hex.slice(5, 7), 16) / 255;
+ return { r, g, b, a: 1 };
+}
diff --git a/editor/src/property/editors.ts b/editor/src/property/editors.ts
index 865ed28b7..98eb9812f 100644
--- a/editor/src/property/editors.ts
+++ b/editor/src/property/editors.ts
@@ -14,7 +14,8 @@ import { getAssetLibrary, isUUID } from '../asset/AssetLibrary';
import type { NativeFS } from '../types/NativeFS';
import { createUIRectEditor } from './uiRectEditor';
import { createButtonTransitionEditor } from './buttonTransitionEditor';
-export { setupDragLabel, colorToHex, hexToColor } from './editorUtils';
+import { setupDragLabel, colorToHex, hexToColor } from './editorUtils';
+export { setupDragLabel, colorToHex, hexToColor };
// =============================================================================
// Number Editor
diff --git a/editor/src/property/uiRectEditor.ts b/editor/src/property/uiRectEditor.ts
index 8c58c5300..cb4b6f52d 100644
--- a/editor/src/property/uiRectEditor.ts
+++ b/editor/src/property/uiRectEditor.ts
@@ -4,7 +4,7 @@
*/
import type { PropertyEditorContext, PropertyEditorInstance } from './PropertyEditor';
-import { setupDragLabel } from './editors';
+import { setupDragLabel } from './editorUtils';
// =============================================================================
// Types