From 2dddeee60dddcdf6d77f52b392ae00d9de165cce Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sun, 21 Jan 2018 19:05:00 -0500 Subject: [PATCH] v0.6.3: improved error handling in SPD-classes --- .../java/com/watabou/glscripts/Script.java | 3 +- .../com/watabou/gltextures/TextureCache.java | 3 +- .../src/main/java/com/watabou/noosa/Game.java | 9 +++ .../main/java/com/watabou/noosa/Group.java | 2 +- .../java/com/watabou/noosa/audio/Music.java | 1 + .../main/java/com/watabou/utils/Bundle.java | 76 ++++++++----------- .../ShatteredPixelDungeon.java | 13 +--- 7 files changed, 48 insertions(+), 59 deletions(-) diff --git a/SPD-classes/src/main/java/com/watabou/glscripts/Script.java b/SPD-classes/src/main/java/com/watabou/glscripts/Script.java index 0f4bc0303..1deea0c9b 100644 --- a/SPD-classes/src/main/java/com/watabou/glscripts/Script.java +++ b/SPD-classes/src/main/java/com/watabou/glscripts/Script.java @@ -23,6 +23,7 @@ package com.watabou.glscripts; import com.watabou.glwrap.Program; import com.watabou.glwrap.Shader; +import com.watabou.noosa.Game; import java.util.HashMap; @@ -44,7 +45,7 @@ public class Script extends Program { try { script = c.newInstance(); } catch (Exception e) { - e.printStackTrace(); + Game.reportException(e); } all.put( c, script ); } diff --git a/SPD-classes/src/main/java/com/watabou/gltextures/TextureCache.java b/SPD-classes/src/main/java/com/watabou/gltextures/TextureCache.java index 14c620ee2..cba664a27 100644 --- a/SPD-classes/src/main/java/com/watabou/gltextures/TextureCache.java +++ b/SPD-classes/src/main/java/com/watabou/gltextures/TextureCache.java @@ -26,6 +26,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import com.watabou.glwrap.Texture; +import com.watabou.noosa.Game; import java.util.HashMap; @@ -157,7 +158,7 @@ public class TextureCache { } } catch (Exception e) { - e.printStackTrace(); + Game.reportException(e); return null; } diff --git a/SPD-classes/src/main/java/com/watabou/noosa/Game.java b/SPD-classes/src/main/java/com/watabou/noosa/Game.java index 358de099d..0f5075a4c 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/Game.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/Game.java @@ -32,6 +32,7 @@ import android.os.Bundle; import android.os.SystemClock; import android.os.Vibrator; import android.util.DisplayMetrics; +import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder; @@ -404,6 +405,14 @@ public class Game extends Activity implements GLSurfaceView.Renderer, View.OnTou Camera.updateAll(); } + public static void reportException( Throwable tr ) { + if (instance != null) instance.logException(tr); + } + + protected void logException( Throwable tr ){ + Log.e("GAME", Log.getStackTraceString(tr)); + } + public static void vibrate( int milliseconds ) { ((Vibrator)instance.getSystemService( VIBRATOR_SERVICE )).vibrate( milliseconds ); } diff --git a/SPD-classes/src/main/java/com/watabou/noosa/Group.java b/SPD-classes/src/main/java/com/watabou/noosa/Group.java index 5cbc7afc4..5d792ef3d 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/Group.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/Group.java @@ -191,7 +191,7 @@ public class Group extends Gizmo { try { return add( c.newInstance() ); } catch (Exception e) { - e.printStackTrace(); + Game.reportException(e); } } diff --git a/SPD-classes/src/main/java/com/watabou/noosa/audio/Music.java b/SPD-classes/src/main/java/com/watabou/noosa/audio/Music.java index ff9f19dcb..9025c2075 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/audio/Music.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/audio/Music.java @@ -72,6 +72,7 @@ public enum Music implements MediaPlayer.OnPreparedListener, MediaPlayer.OnError } catch (Exception e) { + Game.reportException(e); player = null; } diff --git a/SPD-classes/src/main/java/com/watabou/utils/Bundle.java b/SPD-classes/src/main/java/com/watabou/utils/Bundle.java index 06d6f0cb0..c5935a69b 100644 --- a/SPD-classes/src/main/java/com/watabou/utils/Bundle.java +++ b/SPD-classes/src/main/java/com/watabou/utils/Bundle.java @@ -21,6 +21,8 @@ package com.watabou.utils; +import com.watabou.noosa.Game; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -99,7 +101,7 @@ public class Bundle { Class cl = Class.forName( clName ); return cl; } catch (ClassNotFoundException e) { - reportException(e); + Game.reportException(e); return null; } } @@ -127,13 +129,13 @@ public class Bundle { return null; } } catch (ClassNotFoundException e ) { - reportException(e); + Game.reportException(e); return null; } catch (InstantiationException e ) { - reportException(e); + Game.reportException(e); return null; } catch (IllegalAccessException e ) { - reportException(e); + Game.reportException(e); return null; } } @@ -146,7 +148,7 @@ public class Bundle { try { return Enum.valueOf( enumClass, data.getString( key ) ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); return enumClass.getEnumConstants()[0]; } } @@ -161,7 +163,7 @@ public class Bundle { } return result; } catch (JSONException e) { - reportException(e); + Game.reportException(e); return null; } } @@ -176,7 +178,7 @@ public class Bundle { } return result; } catch (JSONException e) { - reportException(e); + Game.reportException(e); return null; } } @@ -191,7 +193,7 @@ public class Bundle { } return result; } catch (JSONException e) { - reportException(e); + Game.reportException(e); return null; } } @@ -206,7 +208,7 @@ public class Bundle { } return result; } catch (JSONException e) { - reportException(e); + Game.reportException(e); return null; } } @@ -225,13 +227,13 @@ public class Bundle { Class cl = Class.forName( clName ); result[i] = cl; } catch (ClassNotFoundException e) { - reportException(e); + Game.reportException(e); result[i] = null; } } return result; } catch (JSONException e) { - reportException(e); + Game.reportException(e); return null; } } @@ -247,7 +249,7 @@ public class Bundle { if (O != null) list.add( O ); } } catch (JSONException e) { - reportException(e); + Game.reportException(e); } return list; @@ -257,7 +259,7 @@ public class Bundle { try { data.put( key, value ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -265,7 +267,7 @@ public class Bundle { try { data.put( key, value ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -273,7 +275,7 @@ public class Bundle { try { data.put( key, value ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -281,7 +283,7 @@ public class Bundle { try { data.put( key, value ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -289,7 +291,7 @@ public class Bundle { try { data.put( key, value ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -297,7 +299,7 @@ public class Bundle { try { data.put( key, value ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -305,7 +307,7 @@ public class Bundle { try { data.put( key, bundle.data ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -317,7 +319,7 @@ public class Bundle { object.storeInBundle( bundle ); data.put( key, bundle.data ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } } @@ -327,7 +329,7 @@ public class Bundle { try { data.put( key, value.name() ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } } @@ -340,7 +342,7 @@ public class Bundle { } data.put( key, jsonArray ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -352,7 +354,7 @@ public class Bundle { } data.put( key, jsonArray ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -364,7 +366,7 @@ public class Bundle { } data.put( key, jsonArray ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -376,7 +378,7 @@ public class Bundle { } data.put( key, jsonArray ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -388,7 +390,7 @@ public class Bundle { } data.put( key, jsonArray ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -410,7 +412,7 @@ public class Bundle { try { data.put( key, array ); } catch (JSONException e) { - reportException(e); + Game.reportException(e); } } @@ -439,7 +441,7 @@ public class Bundle { return new Bundle( json ); } catch (Exception e) { - reportException(e); + Game.reportException(e); throw new IOException(); } } @@ -459,7 +461,7 @@ public class Bundle { return true; } catch (IOException e) { - reportException(e); + Game.reportException(e); return false; } } @@ -467,19 +469,5 @@ public class Bundle { public static void addAlias( Class cl, String alias ) { aliases.put( alias, cl.getName() ); } - - //This may be set in order to have bundles report exceptions - //...Yes it would be far cleaner to have the bundling methods throw exceptions - //But that would require too much code-changing right now. - public static BundleExceptionCallback exceptionReporter; - - private static void reportException(Throwable t){ - if (exceptionReporter != null){ - exceptionReporter.call(t); - } - } - - public static abstract class BundleExceptionCallback { - public abstract void call(Throwable t); - } + } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java index 384441465..196f9ef70 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java @@ -118,14 +118,6 @@ public class ShatteredPixelDungeon extends Game { com.shatteredpixel.shatteredpixeldungeon.actors.buffs.BlobImmunity.class, "com.shatteredpixel.shatteredpixeldungeon.actors.buffs.GasesImmunity" ); - com.watabou.utils.Bundle.exceptionReporter = - new com.watabou.utils.Bundle.BundleExceptionCallback() { - @Override - public void call(Throwable t) { - ShatteredPixelDungeon.reportException(t); - } - }; - } @SuppressWarnings("deprecation") @@ -323,8 +315,5 @@ public class ShatteredPixelDungeon extends Game { } } - - public static void reportException( Throwable tr ) { - Log.e("PD", Log.getStackTraceString(tr)); - } + } \ No newline at end of file