From: oskar.wiksten Date: Tue, 22 May 2012 20:55:51 +0000 (+0000) Subject: Removed lots of unncessary logging. X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=ea5fb3d7912259fd8c305401e93f7d9650d902b1;p=users%2Fmchehab%2Fandors-trail.git Removed lots of unncessary logging. Added additional safeguards to the code that loads savegames, to make sure that worlds are loaded correctly. Now only one thread may load the world at a time, even if a new LoadingActivity is created, for example by rotating the device. git-svn-id: https://andors-trail.googlecode.com/svn/trunk@250 08aca716-68be-ccc6-4d58-36f5abd142ac --- diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/WorldSetup.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/WorldSetup.java index b4fa7ad..72acf57 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/WorldSetup.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/WorldSetup.java @@ -18,7 +18,9 @@ public final class WorldSetup { private final WeakReference androidContext; private boolean isResourcesInitialized = false; private boolean isInitializingResources = false; - private WeakReference listener; + private WeakReference onResourcesLoadedListener; + private WeakReference onSceneLoadedListener; + private Object sceneLoaderId; public boolean createNewCharacter = false; public int loadFromSlot = Savegames.SLOT_QUICKSAVE; @@ -30,6 +32,17 @@ public final class WorldSetup { this.world = world; this.androidContext = new WeakReference(androidContext); } + + public void setOnResourcesLoadedListener(OnResourcesLoadedListener listener) { + synchronized (this) { + onResourcesLoadedListener = null; + if (isResourcesInitialized) { + if (listener != null) listener.onResourcesLoaded(); + return; + } + onResourcesLoadedListener = new WeakReference(listener); + } + } public void startResourceLoader(final Resources r, final AndorsTrailPreferences preferences) { if (isResourcesInitialized) return; @@ -52,52 +65,71 @@ public final class WorldSetup { synchronized (WorldSetup.this) { isResourcesInitialized = true; isInitializingResources = false; - if (listener == null) return; // sceneloader will be fired by next caller. + + if (onResourcesLoadedListener == null) return; + WorldSetup.OnResourcesLoadedListener listener = onResourcesLoadedListener.get(); + onResourcesLoadedListener = null; + if (listener == null) return; + listener.onResourcesLoaded(); } - startSceneLoader(); } }).execute(); } public void startCharacterSetup(final OnSceneLoadedListener listener) { synchronized (WorldSetup.this) { - this.listener = new WeakReference(listener); - if (!isResourcesInitialized) return; // sceneloader will be fired by the resourceloader. + this.onSceneLoadedListener = new WeakReference(listener); } startSceneLoader(); } + public void removeOnSceneLoadedListener(final OnSceneLoadedListener listener) { + synchronized (WorldSetup.this) { + if (this.onSceneLoadedListener == null) return; + if (this.onSceneLoadedListener.get() == listener) this.onSceneLoadedListener = null; + } + } + private final Object onlyOneThreadAtATimeMayLoadSavegames = new Object(); private void startSceneLoader() { isSceneReady = false; + final Object thisLoaderId = new Object(); + synchronized (WorldSetup.this) { + sceneLoaderId = thisLoaderId; + } + (new AsyncTask() { @Override protected Void doInBackground(Void... arg0) { - if (world.model != null) world.reset(); - if (createNewCharacter) { - createNewWorld(); - loadResult = Savegames.LOAD_RESULT_SUCCESS; - } else { - loadResult = continueWorld(); + synchronized (onlyOneThreadAtATimeMayLoadSavegames) { + if (world.model != null) world.reset(); + if (createNewCharacter) { + createNewWorld(); + loadResult = Savegames.LOAD_RESULT_SUCCESS; + } else { + loadResult = continueWorld(); + } + createNewCharacter = false; } - createNewCharacter = false; return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - isSceneReady = true; - OnSceneLoadedListener o; synchronized (WorldSetup.this) { - if (listener == null) return; - o = listener.get(); - listener = null; - } - if (o == null) return; - if (loadResult == Savegames.LOAD_RESULT_SUCCESS) { - o.onSceneLoaded(); - } else { - o.onSceneLoadFailed(loadResult); + if (sceneLoaderId != thisLoaderId) return; // Some other thread has started after we started. + isSceneReady = true; + + if (onSceneLoadedListener == null) return; + OnSceneLoadedListener o = onSceneLoadedListener.get(); + onSceneLoadedListener = null; + if (o == null) return; + + if (loadResult == Savegames.LOAD_RESULT_SUCCESS) { + o.onSceneLoaded(); + } else { + o.onSceneLoadFailed(loadResult); + } } } }).execute(); @@ -106,7 +138,7 @@ public final class WorldSetup { private int continueWorld() { Context ctx = androidContext.get(); int result = Savegames.loadWorld(world, ctx, loadFromSlot); - if (result == Savegames.LOAD_RESULT_SUCCESS) { + if (result == Savegames.LOAD_RESULT_SUCCESS) { MovementController.cacheCurrentMapData(ctx.getResources(), world, world.model.currentMap); } return result; @@ -125,4 +157,7 @@ public final class WorldSetup { void onSceneLoaded(); void onSceneLoadFailed(int loadResult); } + public interface OnResourcesLoadedListener { + void onResourcesLoaded(); + } } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/LoadingActivity.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/LoadingActivity.java index 0df588c..abd411f 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/LoadingActivity.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/LoadingActivity.java @@ -12,31 +12,46 @@ import android.os.Bundle; import com.gpl.rpg.AndorsTrail.AndorsTrailApplication; import com.gpl.rpg.AndorsTrail.R; import com.gpl.rpg.AndorsTrail.Savegames; +import com.gpl.rpg.AndorsTrail.WorldSetup; +import com.gpl.rpg.AndorsTrail.WorldSetup.OnResourcesLoadedListener; import com.gpl.rpg.AndorsTrail.WorldSetup.OnSceneLoadedListener; -import com.gpl.rpg.AndorsTrail.util.L; -public final class LoadingActivity extends Activity implements OnSceneLoadedListener { +public final class LoadingActivity extends Activity implements OnResourcesLoadedListener, OnSceneLoadedListener { private static final int DIALOG_LOADING = 1; private static final int DIALOG_LOADING_FAILED = 2; private static final int DIALOG_LOADING_WRONGVERSION = 3; - + private WorldSetup setup; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndorsTrailApplication app = AndorsTrailApplication.getApplicationFromActivity(this); AndorsTrailApplication.setWindowParameters(this, app.preferences); - - L.log("LoadingActivity::onCreate"); - - showDialog(DIALOG_LOADING); - app.setup.startCharacterSetup(this); + this.setup = app.setup; } + @Override + public void onResume() { + super.onResume(); + showDialog(DIALOG_LOADING); + setup.setOnResourcesLoadedListener(this); + } + + @Override + public void onPause() { + super.onPause(); + setup.setOnResourcesLoadedListener(null); + setup.removeOnSceneLoadedListener(this); + } + + @Override + public void onResourcesLoaded() { + setup.startCharacterSetup(this); + } + @Override public void onSceneLoaded() { - L.log("LoadingActivity::onSceneLoaded"); - removeDialog(DIALOG_LOADING); startActivity(new Intent(this, MainActivity.class)); this.finish(); @@ -44,8 +59,6 @@ public final class LoadingActivity extends Activity implements OnSceneLoadedList @Override public void onSceneLoadFailed(int loadResult) { - L.log("LoadingActivity::onSceneLoadFailed"); - removeDialog(DIALOG_LOADING); if (loadResult == Savegames.LOAD_RESULT_FUTURE_VERSION) { showDialog(DIALOG_LOADING_WRONGVERSION); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java index 5e53ab7..badc06f 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java @@ -15,7 +15,6 @@ import com.gpl.rpg.AndorsTrail.model.actor.Monster; import com.gpl.rpg.AndorsTrail.model.actor.Player; import com.gpl.rpg.AndorsTrail.model.item.ItemContainer.ItemEntry; import com.gpl.rpg.AndorsTrail.util.Coord; -import com.gpl.rpg.AndorsTrail.util.L; import com.gpl.rpg.AndorsTrail.view.CombatView; import com.gpl.rpg.AndorsTrail.view.MainView; import com.gpl.rpg.AndorsTrail.view.VirtualDpadView; @@ -72,8 +71,7 @@ public final class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - L.log("onCreate"); - + AndorsTrailApplication app = AndorsTrailApplication.getApplicationFromActivity(this); if (!app.isInitialized()) { finish(); return; } this.world = app.world; @@ -164,7 +162,6 @@ public final class MainActivity extends Activity { @Override protected void onPause() { super.onPause(); - L.log("onPause"); view.gameRoundController.pause(); view.movementController.stopMovement(); @@ -174,7 +171,6 @@ public final class MainActivity extends Activity { @Override protected void onResume() { super.onResume(); - L.log("onResume"); if (!AndorsTrailApplication.getApplicationFromActivity(this).setup.isSceneReady) return; view.gameRoundController.resume(); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java index 9dc1399..72660fc 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java @@ -268,15 +268,11 @@ public final class MovementController implements TimedMessageTask.Callback { } public static void cacheCurrentMapData(final Resources res, final WorldContext world, final PredefinedMap nextMap) { - L.log("-> cacheCurrentMapData"); - long start = System.currentTimeMillis(); LayeredTileMap mapTiles = TMXMapTranslator.readLayeredTileMap(res, world.tileManager.tileCache, nextMap); TileCollection cachedTiles = world.tileManager.loadTilesFor(nextMap, mapTiles, world, res); world.model.currentTileMap = mapTiles; world.tileManager.currentMapTiles = cachedTiles; world.tileManager.cacheAdjacentMaps(res, world, nextMap); - long duration = System.currentTimeMillis() - start; - L.log(" <- cacheCurrentMapData " + duration + "ms"); } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/tiles/TileCache.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/tiles/TileCache.java index a7dd230..9e3ee59 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/tiles/TileCache.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/tiles/TileCache.java @@ -7,8 +7,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map.Entry; -import com.gpl.rpg.AndorsTrail.AndorsTrailApplication; -import com.gpl.rpg.AndorsTrail.util.L; import com.gpl.rpg.AndorsTrail.util.LruCache; import android.content.res.Resources; @@ -65,7 +63,6 @@ public final class TileCache { public TileCollection loadTilesFor(Collection iconIDs, Resources r) { return loadTilesFor(iconIDs, r, null); } public TileCollection loadTilesFor(Collection iconIDs, Resources r, TileCollection result) { - if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) L.log("TileCache::loadTilesFor({" + iconIDs.size() + " items})"); int maxTileID = 0; HashMap> tilesToLoadPerSourceFile = new HashMap>(); for(int tileID : iconIDs) { @@ -93,9 +90,6 @@ public final class TileCache { if (bitmap == null) { if (cutter == null) { - if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) { - L.log("Loading tiles from tileset " + e.getKey().tilesetName); - } if (!hasLoadedTiles) cleanQueue(); cutter = new TileCutter(e.getKey(), r); hasLoadedTiles = true; @@ -120,9 +114,6 @@ public final class TileCache { Bitmap bitmap = cache.get(tileID); if (bitmap != null) return bitmap; - if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) { - L.log("Loading single tile from tileset " + tile.tileset.tilesetName); - } TileCutter cutter = new TileCutter(tile.tileset, r); Bitmap result = cutter.createTile(tile.localID); cutter.recycle(); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java index 26e8e91..87e8941 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java @@ -18,7 +18,6 @@ import com.gpl.rpg.AndorsTrail.resource.tiles.TileCollection; import com.gpl.rpg.AndorsTrail.resource.tiles.TileManager; import com.gpl.rpg.AndorsTrail.util.Coord; import com.gpl.rpg.AndorsTrail.util.CoordRect; -import com.gpl.rpg.AndorsTrail.util.L; import com.gpl.rpg.AndorsTrail.util.Size; import android.content.Context; @@ -100,12 +99,8 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (w <= 0 || h <= 0) return; - L.log("surfaceChanged " + w + ", " + h); - this.scale = world.tileManager.scale; this.scaledTileSize = world.tileManager.viewTileSize; - L.log("scale=" + scale); - L.log("scaledTileSize=" + scaledTileSize); screenSizeTileCount = new Size( (int) Math.floor(w / scaledTileSize) @@ -132,7 +127,6 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac @Override public void surfaceDestroyed(SurfaceHolder holder) { hasSurface = false; - L.log("surfaceDestroyed"); } @Override