Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Inspector] Prevent crashing when a mesh's name is of the wrong type. #13893

Merged
merged 5 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/dev/core/src/scene.ts
Expand Up @@ -2358,6 +2358,10 @@ export class Scene extends AbstractScene implements IAnimatable, IClipPlanesHold
return;
}

if (typeof newMesh.name !== "string") {
Tools.Warn("Mesh's name is not of string type.");
}

this.meshes.push(newMesh);

newMesh._resyncLightSources();
Expand Down
Expand Up @@ -22,14 +22,22 @@ export class ParentPropertyGridComponent extends React.Component<IParentProperty
super(props);
}

private _getNameForSorting(node: any) {
const isNameAString = node.name && typeof node.name === "string";
return isNameAString ? node.name : "no name";
}

render() {
const node = this.props.node;
const scene = node.getScene();

const sortedNodes = scene
.getNodes()
.filter((n) => n !== node)
.sort((a, b) => (a.name || "no name").localeCompare(b.name || "no name"));
.map((n) => this._getNameForSorting(n))
.sort((aName, bName) => {
return aName.localeCompare(bName);
});

const nodeOptions = sortedNodes.map((m, i) => {
return {
Expand Down Expand Up @@ -57,7 +65,7 @@ export class ParentPropertyGridComponent extends React.Component<IParentProperty
noDirectUpdate={true}
onSelect={(value) => {
const nodeAsTransform = node as TransformNode;
if (value < 0) {
if (typeof value !== "number" || value < 0) {
if (nodeAsTransform.setParent) {
nodeAsTransform.setParent(null);
} else {
Expand Down
Expand Up @@ -39,14 +39,20 @@ export class MeshTreeItemComponent extends React.Component<IMeshTreeItemComponen
this.props.mesh.isVisible = newState;
}

// mesh.name can fail the type check when we're in javascript, so
// we can check to avoid crashing
private _getNameForLabel(): string {
return typeof this.props.mesh.name === "string" ? this.props.mesh.name : "no name";
}

render() {
const mesh = this.props.mesh;

const visibilityElement = this.state.isVisible ? <FontAwesomeIcon icon={faEye} /> : <FontAwesomeIcon icon={faEyeSlash} className="isNotActive" />;

return (
<div className="meshTools">
<TreeItemLabelComponent label={mesh.name} onClick={() => this.props.onClick()} icon={faCube} color="dodgerblue" />
<TreeItemLabelComponent label={this._getNameForLabel()} onClick={() => this.props.onClick()} icon={faCube} color="dodgerblue" />
<div
className={this.state.isBoundingBoxEnabled ? "bounding-box selected icon" : "bounding-box icon"}
onClick={() => this.showBoundingBox()}
Expand Down