]> www.infradead.org Git - users/mchehab/andors-trail.git/commitdiff
Removed lots of unncessary logging.
authoroskar.wiksten <oskar.wiksten@08aca716-68be-ccc6-4d58-36f5abd142ac>
Tue, 22 May 2012 20:55:51 +0000 (20:55 +0000)
committeroskar.wiksten <oskar.wiksten@08aca716-68be-ccc6-4d58-36f5abd142ac>
Tue, 22 May 2012 20:55:51 +0000 (20:55 +0000)
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

AndorsTrail/src/com/gpl/rpg/AndorsTrail/WorldSetup.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/LoadingActivity.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/tiles/TileCache.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java

index b4fa7ad4aec2d081bed0cec592230f39dad09a05..72acf57acc7a0e74e0f01354a5ab738c417bb79b 100644 (file)
@@ -18,7 +18,9 @@ public final class WorldSetup {
        private final WeakReference<Context> androidContext;
        private boolean isResourcesInitialized = false;
        private boolean isInitializingResources = false;
-       private WeakReference<OnSceneLoadedListener> listener;
+       private WeakReference<OnResourcesLoadedListener> onResourcesLoadedListener;
+       private WeakReference<OnSceneLoadedListener> 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<Context>(androidContext);
        }
+
+       public void setOnResourcesLoadedListener(OnResourcesLoadedListener listener) {
+               synchronized (this) {
+                       onResourcesLoadedListener = null;
+                       if (isResourcesInitialized) {
+                               if (listener != null) listener.onResourcesLoaded();
+                               return;
+                       }
+                       onResourcesLoadedListener = new WeakReference<WorldSetup.OnResourcesLoadedListener>(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<OnSceneLoadedListener>(listener);
-                       if (!isResourcesInitialized) return; // sceneloader will be fired by the resourceloader.
+                       this.onSceneLoadedListener = new WeakReference<OnSceneLoadedListener>(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<Void, Void, Void>() {
                        @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();
+    }
 }
index 0df588cee7e91ba528904118630ee637b84beac5..abd411f5e287c485152bccc3ae97cfd407256889 100644 (file)
@@ -12,31 +12,46 @@ import android.os.Bundle;
 import com.gpl.rpg.AndorsTrail.AndorsTrailApplication;\r
 import com.gpl.rpg.AndorsTrail.R;\r
 import com.gpl.rpg.AndorsTrail.Savegames;\r
+import com.gpl.rpg.AndorsTrail.WorldSetup;\r
+import com.gpl.rpg.AndorsTrail.WorldSetup.OnResourcesLoadedListener;\r
 import com.gpl.rpg.AndorsTrail.WorldSetup.OnSceneLoadedListener;\r
-import com.gpl.rpg.AndorsTrail.util.L;\r
 \r
-public final class LoadingActivity extends Activity implements OnSceneLoadedListener {\r
+public final class LoadingActivity extends Activity implements OnResourcesLoadedListener, OnSceneLoadedListener {\r
 \r
     private static final int DIALOG_LOADING = 1;\r
     private static final int DIALOG_LOADING_FAILED = 2;\r
     private static final int DIALOG_LOADING_WRONGVERSION = 3;\r
-\r
+    private WorldSetup setup;\r
+    \r
        @Override\r
     public void onCreate(Bundle savedInstanceState) {\r
         super.onCreate(savedInstanceState);\r
         AndorsTrailApplication app = AndorsTrailApplication.getApplicationFromActivity(this);\r
         AndorsTrailApplication.setWindowParameters(this, app.preferences);\r
-        \r
-        L.log("LoadingActivity::onCreate");\r
-        \r
-        showDialog(DIALOG_LOADING);\r
-        app.setup.startCharacterSetup(this);\r
+        this.setup = app.setup;\r
     }\r
        \r
+       @Override\r
+    public void onResume() {\r
+               super.onResume();\r
+               showDialog(DIALOG_LOADING);\r
+        setup.setOnResourcesLoadedListener(this);\r
+       }\r
+       \r
+       @Override\r
+    public void onPause() {\r
+               super.onPause();\r
+               setup.setOnResourcesLoadedListener(null);\r
+               setup.removeOnSceneLoadedListener(this);\r
+       }\r
+       \r
+       @Override\r
+       public void onResourcesLoaded() {\r
+               setup.startCharacterSetup(this);\r
+       }\r
+       \r
        @Override\r
        public void onSceneLoaded() {\r
-       L.log("LoadingActivity::onSceneLoaded");\r
-        \r
        removeDialog(DIALOG_LOADING);\r
        startActivity(new Intent(this, MainActivity.class));\r
        this.finish();\r
@@ -44,8 +59,6 @@ public final class LoadingActivity extends Activity implements OnSceneLoadedList
        \r
        @Override\r
        public void onSceneLoadFailed(int loadResult) {\r
-       L.log("LoadingActivity::onSceneLoadFailed");\r
-        \r
        removeDialog(DIALOG_LOADING);\r
        if (loadResult == Savegames.LOAD_RESULT_FUTURE_VERSION) {\r
                showDialog(DIALOG_LOADING_WRONGVERSION);        \r
index 5e53ab7fd20c13685901244dc1268e759687a082..badc06f6a2f1ec0a1e606cec311c55488cbebed2 100644 (file)
@@ -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();
index 9dc1399c5494c7b7e2cd12066ef8d1598c62fecc..72660fc3b991b909586fa7cb0cc0b25796ed65a7 100644 (file)
@@ -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");
        }
        
        
index a7dd230e26785cfe19f99fb61bfd7b2732674097..9e3ee595dc608796673a3688addb708ba7ebe860 100644 (file)
@@ -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<Integer> iconIDs, Resources r) { return loadTilesFor(iconIDs, r, null); }
        public TileCollection loadTilesFor(Collection<Integer> iconIDs, Resources r, TileCollection result) {
-               if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) L.log("TileCache::loadTilesFor({" + iconIDs.size() + " items})");
                int maxTileID = 0;
                HashMap<ResourceFileTileset, SparseArray<ResourceFileTile>> tilesToLoadPerSourceFile = new HashMap<ResourceFileTileset, SparseArray<ResourceFileTile>>();
                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();
index 26e8e911d239d230f6b3772e66c057ef1cedce4a..87e89418d14f6d32ba604e6671d0a3338f2568ce 100644 (file)
@@ -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