Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.jme3.gde.core.errorreport.ExceptionUtils;
import com.jme3.gde.core.scene.SceneApplication;
import com.jme3.gde.materialdefinition.fileStructure.MatDefBlock;
import com.jme3.gde.materialdefinition.fileStructure.MatDefParseException;
import com.jme3.gde.materialdefinition.fileStructure.TechniqueBlock;
import com.jme3.gde.materialdefinition.fileStructure.UberStatement;
import com.jme3.gde.materialdefinition.fileStructure.leaves.LeafStatement;
Expand All @@ -60,6 +61,8 @@
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.cookies.EditorCookie;
import org.openide.explorer.ExplorerManager;
import org.openide.filesystems.FileLock;
Expand Down Expand Up @@ -91,6 +94,7 @@ public class EditableMatDefFile {
private Lookup lookup;
private boolean loaded = false;
private boolean dirty = false;
private MatDefParseException parseError = null;
private final MatStructChangeListener changeListener = new MatStructChangeListener(this);

public EditableMatDefFile(Lookup lookup) {
Expand All @@ -101,6 +105,7 @@ public EditableMatDefFile(Lookup lookup) {

public final void load(Lookup lookup) {
loaded = false;
parseError = null;
this.matDefFile = obj.getPrimaryFile();
this.assetManager = lookup.lookup(ProjectAssetManager.class);
this.glsl100 = new Glsl100ShaderGenerator(assetManager);
Expand Down Expand Up @@ -133,6 +138,13 @@ public final void load(Lookup lookup) {
assetManager.deleteFromCache(matDefKey);
materialDef = (MaterialDef) assetManager.loadAsset(assetManager.getRelativeAssetPath(matDefFile.getPath()));
}
} catch (MatDefParseException ex) {
// The SDK's own parser hit a statement it cannot make sense of.
// Keep whatever was parsed so far and notify the user with a
// specific message instead of showing the exception report.
parseError = ex;
Logger.getLogger(EditableMatDefFile.class.getName()).log(Level.WARNING, ex.getMessage());
notifyParseError(ex);
} catch (Exception ex) {
Throwable t = ex.getCause();

Expand Down Expand Up @@ -167,18 +179,40 @@ public final void load(Lookup lookup) {
}
}
}
if (materialDef != null && !matParseError) {
if(currentTechnique == null){
currentTechnique = matDefStructure.getTechniques().get(0);
}
registerListener(matDefStructure);
if ((materialDef != null || parseError != null) && !matParseError) {
if (matDefStructure != null && !matDefStructure.getTechniques().isEmpty()) {
if (currentTechnique == null) {
currentTechnique = matDefStructure.getTechniques().get(0);
}
registerListener(matDefStructure);

obj.getLookupContents().add(matDefStructure);
updateLookupWithMaterialData(obj);
obj.getLookupContents().add(matDefStructure);
}
if (materialDef != null) {
updateLookupWithMaterialData(obj);
}
loaded = true;
}
}

/**
* @return the parse error encountered while loading the j3md, or null if
* the file parsed cleanly (or was not loaded yet).
*/
public MatDefParseException getParseError() {
return parseError;
}

private void notifyParseError(MatDefParseException ex) {
String fileName = matDefFile != null ? matDefFile.getNameExt() : "material definition";
StringBuilder msg = new StringBuilder();
msg.append("Could not fully parse ").append(fileName).append(": ").append(ex.getMessage());
msg.append("\n\nThe visual editor may be incomplete or broken. ");
msg.append("Please fix the file in the text editor.");
DialogDisplayer.getDefault().notifyLater(
new NotifyDescriptor.Message(msg.toString(), NotifyDescriptor.WARNING_MESSAGE));
}

void registerListener(Statement sta) {
if (sta instanceof UberStatement) {
((UberStatement) sta).addPropertyChangeListener(WeakListeners.propertyChange(changeListener, ((UberStatement) sta)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.jme3.gde.materialdefinition.editor.ShaderNodePanel.NodeType;
import com.jme3.gde.materialdefinition.editor.util.MatDefEditorUtil;
import com.jme3.gde.materialdefinition.fileStructure.MatDefBlock;
import com.jme3.gde.materialdefinition.fileStructure.MatDefParseException;
import com.jme3.gde.materialdefinition.fileStructure.ShaderNodeBlock;
import com.jme3.gde.materialdefinition.fileStructure.TechniqueBlock;
import com.jme3.gde.materialdefinition.fileStructure.leaves.InputMappingBlock;
Expand Down Expand Up @@ -80,6 +81,7 @@
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.UndoRedo;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
Expand Down Expand Up @@ -138,6 +140,17 @@ public MatDefEditorlElement(final Lookup lkp) {
}

private void initDiagram(Lookup lkp) throws NumberFormatException {
MatDefParseException parseError = obj.getEditableFile().getParseError();
if (parseError != null) {
String msg = "The material definition could not be fully parsed: " + parseError.getMessage()
+ "\n\nThe diagram below may be incomplete. Please fix the file in the text editor.";
DialogDisplayer.getDefault().notifyLater(
new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE));
}
initDiagramInternal(lkp);
}

private void initDiagramInternal(Lookup lkp) throws NumberFormatException {
/* Note: I don't know if the following could also be done by calling
* {@link ShaderNodeDiagram#addNodesFromDefs()} etc
*/
Expand All @@ -150,6 +163,10 @@ private void initDiagram(Lookup lkp) throws NumberFormatException {
ProjectAssetManager manager = obj.getLookup().lookup(ProjectAssetManager.class);
final MatDefBlock matDef = obj.getLookup().lookup(MatDefBlock.class);
TechniqueBlock technique = getTechnique();
if (technique == null) {
// Partial parse: no technique could be built, nothing to draw.
return;
}

diagram1.setCurrentTechniqueName(technique.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public MatDefBlock(Statement sta) {
addStatement(new UnsupportedStatement(statement));
}
}
String[] s = line.split("\\s");
String[] s = line.trim().split("\\s+");
if (s.length < 2) {
throw new MatDefParseException("MaterialDef statement is missing a name, expected \"MaterialDef MyMaterial\"", getLineNumber(), getLine());
}
name = s[1];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2009-2026 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.gde.materialdefinition.fileStructure;

/**
* Thrown when the SDK's own parser cannot make sense of a statement in a
* material definition (j3md) file. Carries the offending line number and
* content so the user can be told exactly what is wrong instead of being
* confronted with a generic exception report.
*
* @author Nehon
*/
public class MatDefParseException extends RuntimeException {

private final int lineNumber;
private final String line;

public MatDefParseException(String message, int lineNumber, String line) {
super(message);
this.lineNumber = lineNumber;
this.line = line;
}

public MatDefParseException(String message, int lineNumber, String line, Throwable cause) {
super(message, cause);
this.lineNumber = lineNumber;
this.line = line;
}

public int getLineNumber() {
return lineNumber;
}

public String getLine() {
return line;
}

@Override
public String getMessage() {
StringBuilder sb = new StringBuilder(super.getMessage());
if (lineNumber > 0) {
sb.append(" (line ").append(lineNumber);
if (line != null && !line.isBlank()) {
sb.append(": \"").append(line.trim()).append("\"");
}
sb.append(")");
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ protected ShaderNodeBlock(int lineNumber, String line) {

public ShaderNodeBlock(Statement sta) {
this(sta.getLineNumber(), sta.getLine());
String[] s = line.split("\\s");
String[] s = line.trim().split("\\s+");
if (s.length < 2) {
throw new MatDefParseException("ShaderNode statement is missing a name, expected \"ShaderNode MyNode\"", getLineNumber(), getLine());
}
name = s[1];
for (Statement statement : sta.getContents()) {
if (statement.getLine().trim().startsWith("Definition")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TechniqueBlock(Statement sta) {
addStatement(new UnsupportedStatement(statement));
}
}
String[] s = line.split("\\s");
String[] s = line.trim().split("\\s+");
if (s.length == 1) {
name = "Default";
} else {
Expand Down Expand Up @@ -107,6 +107,10 @@ protected WorldParametersBlock getWorldParameters() {
return getBlock(WorldParametersBlock.class);
}

public DefinesBlock getDefines() {
return getBlock(DefinesBlock.class);
}

public List<WorldParamBlock> getWorldParams() {
WorldParametersBlock block = getWorldParameters();
if (block != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
*/
package com.jme3.gde.materialdefinition.fileStructure.leaves;

import com.jme3.gde.materialdefinition.fileStructure.MatDefParseException;
import com.jme3.util.blockparser.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
Expand Down Expand Up @@ -46,12 +45,10 @@ public void setCondition(String condition) {
}

private void parse(Statement sta) {
try {
String[] split = sta.getLine().split(":");
condition = split[1].trim();

} catch (ArrayIndexOutOfBoundsException e) {
Logger.getLogger(ConditionBlock.class.getName()).log(Level.WARNING, "Parsing error line {0} : {1}", new Object[]{sta.getLineNumber(), sta.getLine()});
String[] split = sta.getLine().split(":");
if (split.length < 2 || split[1].trim().isEmpty()) {
throw new MatDefParseException("Condition statement is missing its condition, expected \"Condition : SomeParam > 0.5\"", sta.getLineNumber(), sta.getLine());
}
condition = split[1].trim();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.jme3.gde.materialdefinition.fileStructure.leaves;

import com.jme3.gde.materialdefinition.fileStructure.MatDefParseException;
import com.jme3.util.blockparser.Statement;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Handles one row of Defines in a MaterialDef, ie ALBEDOMAP : AlbedoMap
Expand Down Expand Up @@ -30,7 +33,7 @@ public DefineBlock(String name, String define) {
}

private void updateLine() {
this.line = String.format("%s : %s", name, define);
this.line = String.format("%s : %s", name, define == null ? "" : define);
}

public String getName() {
Expand All @@ -52,8 +55,35 @@ public void setDefine(String define) {
}

private void parse(Statement sta) {
String[] values = sta.getLine().split(":");
String content = sta.getLine();
int commentIndex = content.indexOf("//");
String comment = null;
if (commentIndex != -1) {
comment = content.substring(commentIndex + 2).trim();
content = content.substring(0, commentIndex);
}
String[] values = content.split(":", 2);
name = values[0].trim();
if (name.isEmpty()) {
throw new MatDefParseException("Define has no name, expected \"DEFINE_NAME : MaterialParam\"", sta.getLineNumber(), sta.getLine());
}
if (values.length < 2 || values[1].trim().isEmpty()) {
// Older j3md files (and some hand written ones) contain defines
// without a mapped material parameter, e.g. "HAS_COLORMAP // comment".
// The engine treats these as defines that are always set, so we
// keep the line but leave the define unmapped.
define = null;
if (comment != null && !comment.isEmpty()) {
Logger.getLogger(DefineBlock.class.getName()).log(Level.WARNING,
"Define \"{0}\" at line {1} has no material parameter mapping (missing \": ParamName\"), it will always be set. Comment: {2}",
new Object[]{name, sta.getLineNumber(), comment});
} else {
Logger.getLogger(DefineBlock.class.getName()).log(Level.WARNING,
"Define \"{0}\" at line {1} has no material parameter mapping (missing \": ParamName\"), it will always be set.",
new Object[]{name, sta.getLineNumber()});
}
return;
}
define = values[1].trim();
}

Expand All @@ -70,7 +100,7 @@ public boolean equals(Object obj) {
if (!(obj instanceof DefineBlock)) {
return false;
}
return ((DefineBlock) obj).getName().equals(name) && ((DefineBlock) obj).getDefine().equals(define);
return ((DefineBlock) obj).getName().equals(name) && Objects.equals(((DefineBlock) obj).getDefine(), define);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
*/
package com.jme3.gde.materialdefinition.fileStructure.leaves;

import com.jme3.gde.materialdefinition.fileStructure.MatDefParseException;
import com.jme3.util.blockparser.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
Expand Down Expand Up @@ -57,16 +56,13 @@ public void setPath(String path) {
}

private void parse(Statement sta) {
try {
String[] split = sta.getLine().split(":");
if (split.length > 2) {
path = split[2].trim();
}
name = split[1].trim();


} catch (ArrayIndexOutOfBoundsException e) {
Logger.getLogger(DefinitionBlock.class.getName()).log(Level.WARNING, "Parsing error line {0} : {1}", new Object[]{sta.getLineNumber(), sta.getLine()});
String[] split = sta.getLine().split(":");
if (split.length > 2) {
path = split[2].trim();
}
if (split.length < 2 || split[1].trim().isEmpty()) {
throw new MatDefParseException("Shader node definition is missing a name, expected \"Definition : Name : path/to/definition.j3sn\"", sta.getLineNumber(), sta.getLine());
}
name = split[1].trim();
}
}
Loading
Loading