Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 41 additions & 28 deletions editor/src/tools/workers/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,52 @@ function extract(outputAbsolutePath: string) {

try {
const output = require(outputAbsolutePath) as any;
inspectorProperties = output.default?._VisibleInInspector ?? null;

try {
function createMock(recorder = {}) {
return new Proxy(function () {}, {
get(_target, prop) {
if (!(prop in recorder)) {
recorder[prop] = createMock({});
}
return recorder[prop];
},
set(_target, prop, value) {
recorder[prop] = value;
return true;
},
apply() {
return createMock({});
},
construct() {
return createMock({});
},
if (output.default) {
inspectorProperties = output.default?._VisibleInInspector ?? null;

try {
function createMock(recorder = {}) {
return new Proxy(function () {}, {
get(_target, prop) {
if (!(prop in recorder)) {
recorder[prop] = createMock({});
}
return recorder[prop];
},
set(_target, prop, value) {
recorder[prop] = value;
return true;
},
apply() {
return createMock({});
},
construct() {
return createMock({});
},
});
}

const mock = createMock();
const instance = new output.default(mock);

inspectorProperties?.forEach((value) => {
const defaultValue = instance[value.propertyKey];
value.defaultValue = defaultValue?.asArray?.() ?? defaultValue;
});
} catch (e) {
// Catch silently.
}
} else if (output.config) {
const keys = Object.keys(output.config);

const mock = createMock();
const instance = new output.default(mock);
inspectorProperties = keys.map((key) => {
const property = output.config[key];
property.propertyKey = key;
property.defaultValue = property.value?.asArray?.() ?? property.value;
delete property.value;

inspectorProperties?.forEach((value) => {
const defaultValue = instance[value.propertyKey];
value.defaultValue = defaultValue?.asArray?.() ?? defaultValue;
return property;
});
} catch (e) {
// Catch silently.
}
} catch (e) {
// Catch silently.
Expand Down
1 change: 1 addition & 0 deletions tools/src/decorators/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export function applyDecorators(scene: Scene, object: any, script: any, instance
break;
}
}
break;
}
}
});
Expand Down
2 changes: 2 additions & 0 deletions tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export * from "./decorators/inspector";
export * from "./decorators/events";
export * from "./decorators/sprite";

export * from "./loading/script/config";

export * from "./script";

export * from "./cinematic/parse";
Expand Down
5 changes: 5 additions & 0 deletions tools/src/loading/script/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { applyDecorators } from "../../decorators/apply";
import { isAnyParticleSystem, isNode, isScene } from "../../tools/guards";

import { ScriptMap } from "../loader";
import { applyConfig } from "./config";

/**
* @internal
Expand Down Expand Up @@ -42,6 +43,10 @@ export function _applyScriptsForObject(scene: Scene, object: any, scriptsMap: Sc
Object.assign(observers, decoratorsResult?.observers ?? {});
}

if (exports.config) {
applyConfig(scene, exports.config, script.values, rootUrl);
}

if (result.onStart) {
observers.onStartObserver = scene.onBeforeRenderObservable.addOnce(() => result.onStart!(object));
}
Expand Down
125 changes: 125 additions & 0 deletions tools/src/loading/script/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { Scene } from "@babylonjs/core/scene";
import { Material } from "@babylonjs/core/Materials/material";
import { Color3, Color4 } from "@babylonjs/core/Maths/math.color";
import { Texture } from "@babylonjs/core/Materials/Textures/texture";
import { Vector2, Vector3 } from "@babylonjs/core/Maths/math.vector";
import { NodeParticleSystemSet } from "@babylonjs/core/Particles/Node/nodeParticleSystemSet";

import { getSoundById } from "../../tools/sound";

import {
VisibleInInspectorDecoratorColor3Configuration,
VisibleInInspectorDecoratorColor4Configuration,
VisibleInInspectorDecoratorConfiguration,
VisibleInInspectorDecoratorEntityConfiguration,
VisibleInInspectorDecoratorNumberConfiguration,
VisibleInInspectorDecoratorStringConfiguration,
VisibleInInspectorDecoratorTextureConfiguration,
VisibleInInspectorDecoratorVector2Configuration,
VisibleInInspectorDecoratorVector3Configuration,
VisibleInspectorDecoratorAssetConfiguration,
} from "../../decorators/inspector";

import { scriptAssetsCache } from "./preload";

export type ScriptConfig = {
[key: string]: {
label: string;
value?: any;
configuration:
| VisibleInInspectorDecoratorConfiguration
| VisibleInInspectorDecoratorStringConfiguration
| VisibleInInspectorDecoratorNumberConfiguration
| VisibleInInspectorDecoratorVector2Configuration
| VisibleInInspectorDecoratorVector3Configuration
| VisibleInInspectorDecoratorColor3Configuration
| VisibleInInspectorDecoratorColor4Configuration
| VisibleInInspectorDecoratorEntityConfiguration
| VisibleInInspectorDecoratorTextureConfiguration
| VisibleInspectorDecoratorAssetConfiguration;
};
};

export function applyConfig(scene: Scene, instance: ScriptConfig, config: ScriptConfig, rootUrl: string) {
const keys = Object.keys(instance);
keys.forEach((key) => {
const exportedValue = config[key];
const originalValue = instance[key];

if (!exportedValue || !originalValue) {
return;
}

switch (originalValue.configuration.type) {
case "number":
case "boolean":
case "keymap":
case "string":
originalValue.value = exportedValue.value;
break;

case "vector2":
originalValue.value = Vector2.FromArray(exportedValue.value);
break;
case "vector3":
originalValue.value = Vector3.FromArray(exportedValue.value);
break;

case "color3":
originalValue.value = Color3.FromArray(exportedValue.value);
break;
case "color4":
originalValue.value = Color4.FromArray(exportedValue.value);
break;

case "entity":
const entityType = (originalValue.configuration as VisibleInInspectorDecoratorEntityConfiguration).entityType;
switch (entityType) {
case "node":
originalValue.value = scene.getNodeById(exportedValue.value) ?? null;
break;
case "animationGroup":
originalValue.value = scene.getAnimationGroupByName(exportedValue.value) ?? null;
break;
case "sound":
originalValue.value = getSoundById(exportedValue.value, scene);
break;
case "particleSystem":
originalValue.value = scene.particleSystems?.find((ps) => ps.id === exportedValue.value) ?? null;
break;
}
break;

case "texture":
if (exportedValue.value) {
originalValue.value = Texture.Parse(exportedValue.value, scene, rootUrl);
}
break;

case "asset":
if (exportedValue.value) {
const assetType = (originalValue.configuration as VisibleInspectorDecoratorAssetConfiguration).assetType;
const data = scriptAssetsCache.get(exportedValue.value);

switch (assetType) {
case "json":
case "gui":
case "scene":
case "navmesh":
originalValue.value = data;
break;

case "nodeParticleSystemSet":
const npss = NodeParticleSystemSet.Parse(data);
originalValue.value = npss;
break;

case "material":
originalValue.value = Material.Parse(data, scene, rootUrl);
break;
}
}
break;
}
});
}
7 changes: 7 additions & 0 deletions tools/src/script.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { ScriptConfig } from "./loading/script/config";

/**
* Defines the interface that can be implemented by scripts attached to nodes in the editor.
*/
export interface IScript {
/**
* Optional configuration object that can be used to configure the script.
*/
config?: ScriptConfig;

/**
* Method called when the script starts. This method is called only once.
*/
Expand Down