From c0b19b02eb7667cb5f9f33d18b83ef7bade96809 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 16 Nov 2014 17:14:40 -0700 Subject: [PATCH] deal with VerifyError with new 7u72 and bad tools, also convert line endings, continue work with Canvas embedding --- app/src/processing/app/Editor.java | 4 + .../app/contrib/ToolContribution.java | 314 +++++++++--------- core/src/processing/core/PGraphics.java | 17 +- core/src/processing/core/PGraphicsJava2D.java | 38 ++- core/src/processing/core/PSurfaceAWT.java | 26 +- 5 files changed, 211 insertions(+), 188 deletions(-) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 83ee34b03..b743c6e85 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -1113,6 +1113,10 @@ public abstract class Editor extends JFrame implements RunnerListener { // TODO Once the dust settles on 2.x, change this to Base.showError() // and open the Tools folder instead of showing System.err.println(). + } catch (VerifyError ve) { + System.err.println("\"" + tool.getMenuTitle() + "\" is not " + + "compatible with this version of Processing"); + } catch (NoSuchMethodError nsme) { System.err.println("\"" + tool.getMenuTitle() + "\" is not " + "compatible with this version of Processing"); diff --git a/app/src/processing/app/contrib/ToolContribution.java b/app/src/processing/app/contrib/ToolContribution.java index b84dbd89c..e336ed7d8 100644 --- a/app/src/processing/app/contrib/ToolContribution.java +++ b/app/src/processing/app/contrib/ToolContribution.java @@ -1,156 +1,158 @@ -/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ - -/* - Part of the Processing project - http://processing.org - - Copyright (c) 2013 The Processing Foundation - Copyright (c) 2011-12 Ben Fry and Casey Reas - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 - as published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc. - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ -package processing.app.contrib; - -import java.io.*; -import java.net.URLClassLoader; -//import java.net.*; -import java.util.*; - -import processing.app.Base; -//import processing.app.Base; -import processing.app.Editor; -import processing.app.tools.Tool; - - -public class ToolContribution extends LocalContribution implements Tool { - private Tool tool; - - - static public ToolContribution load(File folder) { - try { - return new ToolContribution(folder); - } catch (IgnorableException ig) { - Base.log(ig.getMessage()); - } catch (Error err) { - // Handles UnsupportedClassVersionError and others - err.printStackTrace(); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - - private ToolContribution(File folder) throws Exception { - super(folder); - - String className = initLoader(null); - if (className != null) { - Class toolClass = loader.loadClass(className); - tool = (Tool) toolClass.newInstance(); - } - } - - - /** - * Method to close the ClassLoader so that the archives are no longer "locked" and - * a tool can be removed without restart. - */ - public void clearClassLoader(Base base) { - try { - ((URLClassLoader) this.loader).close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - Iterator editorIter = base.getEditors().iterator(); - while (editorIter.hasNext()) { - Editor editor = editorIter.next(); - ArrayList contribTools = editor.contribTools; - for (ToolContribution toolContrib : contribTools) - if (toolContrib.getName().equals(this.name)) { - try { - ((URLClassLoader) toolContrib.loader).close(); - editor.contribTools.remove(toolContrib); - break; - } catch (IOException e) { - e.printStackTrace(); - } -// base.getActiveEditor().rebuildToolMenu(); - } - } - } - - -// static protected List discover(File folder) { -// File[] folders = listCandidates(folder, "tool"); -// if (folders == null) { -// return new ArrayList(); -// } else { -// return Arrays.asList(folders); -// } -// } - - - static public ArrayList loadAll(File toolsFolder) { - File[] list = ContributionType.TOOL.listCandidates(toolsFolder); - ArrayList outgoing = new ArrayList(); - // If toolsFolder does not exist or is inaccessible (stranger things have - // happened, and are reported as bugs) list will come back null. - if (list != null) { - for (File folder : list) { - try { - ToolContribution tc = load(folder); - if (tc != null) { - outgoing.add(tc); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - return outgoing; - } - - -// Editor editor; // used to send error messages - - public void init(Editor editor) { -// try { -// this.editor = editor; - tool.init(editor); -// } catch (NoSuchMethodError nsme) { -// editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing"); -// nsme.printStackTrace(); -// } - } - - - public void run() { -// try { - tool.run(); -// } catch (NoSuchMethodError nsme) { -// editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing"); -// nsme.printStackTrace(); -// } - } - - - public String getMenuTitle() { - return tool.getMenuTitle(); - } - - - public ContributionType getType() { - return ContributionType.TOOL; - } -} \ No newline at end of file +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2013 The Processing Foundation + Copyright (c) 2011-12 Ben Fry and Casey Reas + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 + as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +package processing.app.contrib; + +import java.io.*; +import java.net.URLClassLoader; +//import java.net.*; +import java.util.*; + +import processing.app.Base; +//import processing.app.Base; +import processing.app.Editor; +import processing.app.tools.Tool; + + +public class ToolContribution extends LocalContribution implements Tool { + private Tool tool; + + + static public ToolContribution load(File folder) { + try { + return new ToolContribution(folder); + + } catch (IgnorableException ig) { + Base.log(ig.getMessage()); + + } catch (VerifyError ve) { // incompatible + // avoid the excessive error spew that happens here + + } catch (Throwable e) { // unknown error + e.printStackTrace(); + } + return null; + } + + + private ToolContribution(File folder) throws Throwable { + super(folder); + + String className = initLoader(null); + if (className != null) { + Class toolClass = loader.loadClass(className); + tool = (Tool) toolClass.newInstance(); + } + } + + + /** + * Method to close the ClassLoader so that the archives are no longer "locked" and + * a tool can be removed without restart. + */ + public void clearClassLoader(Base base) { + try { + ((URLClassLoader) this.loader).close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + Iterator editorIter = base.getEditors().iterator(); + while (editorIter.hasNext()) { + Editor editor = editorIter.next(); + ArrayList contribTools = editor.contribTools; + for (ToolContribution toolContrib : contribTools) + if (toolContrib.getName().equals(this.name)) { + try { + ((URLClassLoader) toolContrib.loader).close(); + editor.contribTools.remove(toolContrib); + break; + } catch (IOException e) { + e.printStackTrace(); + } +// base.getActiveEditor().rebuildToolMenu(); + } + } + } + + +// static protected List discover(File folder) { +// File[] folders = listCandidates(folder, "tool"); +// if (folders == null) { +// return new ArrayList(); +// } else { +// return Arrays.asList(folders); +// } +// } + + + static public ArrayList loadAll(File toolsFolder) { + File[] list = ContributionType.TOOL.listCandidates(toolsFolder); + ArrayList outgoing = new ArrayList(); + // If toolsFolder does not exist or is inaccessible (stranger things have + // happened, and are reported as bugs) list will come back null. + if (list != null) { + for (File folder : list) { + try { + ToolContribution tc = load(folder); + if (tc != null) { + outgoing.add(tc); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return outgoing; + } + + +// Editor editor; // used to send error messages + + public void init(Editor editor) { +// try { +// this.editor = editor; + tool.init(editor); +// } catch (NoSuchMethodError nsme) { +// editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing"); +// nsme.printStackTrace(); +// } + } + + + public void run() { +// try { + tool.run(); +// } catch (NoSuchMethodError nsme) { +// editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing"); +// nsme.printStackTrace(); +// } + } + + + public String getMenuTitle() { + return tool.getMenuTitle(); + } + + + public ContributionType getType() { + return ContributionType.TOOL; + } +} diff --git a/core/src/processing/core/PGraphics.java b/core/src/processing/core/PGraphics.java index 8e8c129a2..e19d864e7 100644 --- a/core/src/processing/core/PGraphics.java +++ b/core/src/processing/core/PGraphics.java @@ -739,19 +739,20 @@ public class PGraphics extends PImage implements PConstants { public void setSize(int w, int h) { // ignore width = w; height = h; -// width1 = width - 1; -// height1 = height - 1; - allocate(); + pixelWidth = width * pixelFactor; + pixelHeight = height * pixelFactor; + +// allocate(); reapplySettings(); } - /** - * Allocate memory for this renderer. Generally will need to be implemented - * for all renderers. - */ - protected void allocate() { } +// /** +// * Allocate memory for this renderer. Generally will need to be implemented +// * for all renderers. +// */ +// protected void allocate() { } /** diff --git a/core/src/processing/core/PGraphicsJava2D.java b/core/src/processing/core/PGraphicsJava2D.java index 421b15cfb..7e9882fcf 100644 --- a/core/src/processing/core/PGraphicsJava2D.java +++ b/core/src/processing/core/PGraphicsJava2D.java @@ -120,26 +120,26 @@ public class PGraphicsJava2D extends PGraphics { //public void setPath(String path) - /** - * Called in response to a resize event, handles setting the - * new width and height internally, as well as re-allocating - * the pixel buffer for the new size. - * - * Note that this will nuke any cameraMode() settings. - */ - @Override - public void setSize(int iwidth, int iheight) { // ignore - width = iwidth; - height = iheight; - - allocate(); - reapplySettings(); - } +// /** +// * Called in response to a resize event, handles setting the +// * new width and height internally, as well as re-allocating +// * the pixel buffer for the new size. +// * +// * Note that this will nuke any cameraMode() settings. +// */ +// @Override +// public void setSize(int iwidth, int iheight) { // ignore +// width = iwidth; +// height = iheight; +// +// allocate(); +// reapplySettings(); +// } + /* @Override protected void allocate() { - /* // Tried this with RGB instead of ARGB for the primarySurface version, // but didn't see any performance difference (OS X 10.6, Java 6u24). // For 0196, also attempted RGB instead of ARGB, but that causes @@ -230,8 +230,8 @@ public class PGraphicsJava2D extends PGraphics { } g2 = (Graphics2D) image.getGraphics(); - */ } + */ //public void dispose() @@ -2626,9 +2626,11 @@ public class PGraphicsJava2D extends PGraphics { @Override public void updatePixels(int x, int y, int c, int d) { //if ((x == 0) && (y == 0) && (c == width) && (d == height)) { - if ((x != 0) || (y != 0) || (c != width) || (d != height)) { + System.err.format("%d %d %d %d .. w/h = %d %d .. pw/ph = %d %d %n", x, y, c, d, width, height, pixelWidth, pixelHeight); + if ((x != 0) || (y != 0) || (c != pixelWidth) || (d != pixelHeight)) { // Show a warning message, but continue anyway. showVariationWarning("updatePixels(x, y, w, h)"); +// new Exception().printStackTrace(System.out); } // updatePixels(); if (pixels != null) { diff --git a/core/src/processing/core/PSurfaceAWT.java b/core/src/processing/core/PSurfaceAWT.java index 6445aaf3f..b41f37407 100644 --- a/core/src/processing/core/PSurfaceAWT.java +++ b/core/src/processing/core/PSurfaceAWT.java @@ -164,7 +164,7 @@ public class PSurfaceAWT implements PSurface { newSize.width = getWidth(); newSize.height = getHeight(); if (oldSize.equals(newSize)) { - System.out.println("validate() return " + oldSize); +// System.out.println("validate() return " + oldSize); return; } else { // System.out.println("validate() render old=" + oldSize + " -> new=" + newSize); @@ -410,11 +410,10 @@ public class PSurfaceAWT implements PSurface { public Canvas initCanvas(PApplet sketch) { this.sketch = sketch; + // needed for getPreferredSize() et al sketchWidth = sketch.sketchWidth(); sketchHeight = sketch.sketchHeight(); - setSize(sketchWidth, sketchHeight); - return canvas; } @@ -751,8 +750,12 @@ public class PSurfaceAWT implements PSurface { public void startThread() { - thread = new AnimationThread(); - thread.start(); + if (thread == null) { + thread = new AnimationThread(); + thread.start(); + } else { + throw new IllegalStateException("Thread already started in PSurfaceAWT"); + } } @@ -819,6 +822,9 @@ public class PSurfaceAWT implements PSurface { //throw new RuntimeException("implement me, see readme.md"); sketchWidth = sketch.width = wide; sketchHeight = sketch.height = high; + + // sets internal variables for width/height/pixelWidth/pixelHeight + graphics.setSize(wide, high); } @@ -1357,6 +1363,14 @@ public class PSurfaceAWT implements PSurface { // animation thread yields to other running threads. final int NO_DELAYS_PER_YIELD = 15; + // If size un-initialized, might be a Canvas. Call setSize() here since + // we now have a parent object that this Canvas can use as a peer. + if (graphics.image == null) { +// System.out.format("it's null, sketchW/H already set to %d %d%n", sketchWidth, sketchHeight); + setSize(sketchWidth, sketchHeight); +// System.out.format(" but now, sketchW/H changed to %d %d%n", sketchWidth, sketchHeight); + } + // un-pause the sketch and get rolling sketch.start(); @@ -1370,7 +1384,7 @@ public class PSurfaceAWT implements PSurface { if (currentSize.width != sketchWidth || currentSize.height != sketchHeight) { //resizeRenderer(currentSize.width, currentSize.height); //System.err.format("need to resize from %s to %d, %d", currentSize, graphics.width, graphics.height); - System.err.format("need to resize from %s to %d, %d", currentSize, sketchWidth, sketchHeight); + System.err.format("need to resize from %s to %d, %d%n", currentSize, sketchWidth, sketchHeight); } // }