From: <ev...@us...> - 2010-07-25 17:08:43
|
Revision: 1357 http://rails.svn.sourceforge.net/rails/?rev=1357&view=rev Author: evos Date: 2010-07-25 17:08:37 +0000 (Sun, 25 Jul 2010) Log Message: ----------- Executed actions are no longer saved as a List<PossibleAction> but as a containerless sequence of PossibleAction objects. This allows postponing object assignment during deserialization until all previous actions have been processed, so that the game always is in the same state as during playing the game. This should fix bugs attributable to assignment to objects that do not exist at the start of the game. Modified Paths: -------------- trunk/18xx/rails/game/Game.java trunk/18xx/rails/game/GameManager.java trunk/18xx/rails/game/GameManagerI.java trunk/18xx/rails/util/ListAndFixSavedFiles.java Modified: trunk/18xx/rails/game/Game.java =================================================================== --- trunk/18xx/rails/game/Game.java 2010-07-24 15:47:32 UTC (rev 1356) +++ trunk/18xx/rails/game/Game.java 2010-07-25 17:08:37 UTC (rev 1357) @@ -94,7 +94,7 @@ // set special properties and token static variables SpecialProperty.init(); Token.init(); - + // Have the ComponentManager work through the other rails.game files componentManager.finishPreparation(); @@ -163,7 +163,7 @@ "No TileManager XML element found in file " + GAME_XML_FILE); } - + revenueManager = (RevenueManager) componentManager.findComponent("RevenueManager"); // revenueManager is optional so far @@ -189,7 +189,7 @@ bank.finishConfiguration(gameManager); stockMarket.finishConfiguration(gameManager); tileManager.finishConfiguration(gameManager); - if (revenueManager != null) + if (revenueManager != null) revenueManager.finishConfiguration(gameManager); } catch (Exception e) { String message = @@ -251,24 +251,59 @@ throw new ConfigurationException("Error in setting up " + name); } - List<PossibleAction> executedActions = - (List<PossibleAction>) ois.readObject(); - ois.close(); - log.debug("Number of loaded actions: " + executedActions.size()); - String startError = game.start(); if (startError != null) { DisplayBuffer.add(startError); return null; } + GameManagerI gameManager = game.getGameManager(); + int numberOfActions = 0; log.debug("Starting to execute loaded actions"); - if (!game.getGameManager().processOnReload(executedActions)) { - log.error ("Load interrupted"); - DisplayBuffer.add(LocalText.getText("LoadInterrupted")); + while (true) { // Single-pass loop. + Object firstActionObject; + try { + firstActionObject = ois.readObject(); + } catch (EOFException e) { + // Allow saved file at start of game (with no actions). + break; + } + if (firstActionObject instanceof List) { + // Old-style: one List of PossibleActions + List<PossibleAction> executedActions = + (List<PossibleAction>) firstActionObject; + numberOfActions = executedActions.size(); + for (PossibleAction action : executedActions) { + if (!gameManager.processOnReload(action)) { + log.error ("Load interrupted"); + DisplayBuffer.add(LocalText.getText("LoadInterrupted")); + break; + } + } + + } else { + // New style: separate PossibleActionsObjects, since Rails 1.3.1 + PossibleAction action = (PossibleAction) firstActionObject; + while (true) { + numberOfActions++; + if (!gameManager.processOnReload(action)) { + log.error ("Load interrupted"); + DisplayBuffer.add(LocalText.getText("LoadInterrupted")); + break; + } + try { + action = (PossibleAction) ois.readObject(); + } catch (EOFException e) { + break; + } + } + } + break; } + ois.close(); + game.getGameManager().finishLoading(); return game; } catch (Exception e) { Modified: trunk/18xx/rails/game/GameManager.java =================================================================== --- trunk/18xx/rails/game/GameManager.java 2010-07-24 15:47:32 UTC (rev 1356) +++ trunk/18xx/rails/game/GameManager.java 2010-07-25 17:08:37 UTC (rev 1357) @@ -891,64 +891,60 @@ /* (non-Javadoc) * @see rails.game.GameManagerI#processOnReload(java.util.List) */ - public boolean processOnReload(List<PossibleAction> actions) throws Exception { + public boolean processOnReload(PossibleAction action) throws Exception { - for (PossibleAction action : actions) { + DisplayBuffer.clear(); + // TEMPORARY FIX TO ALLOW OLD 1856 SAVED FILES TO BE PROCESSED + if (gameName.equals("1856") + && possibleActions.contains(RepayLoans.class) + && (!possibleActions.contains(action.getClass()) + || (action.getClass() == NullAction.class + && ((NullAction)action).getMode() != NullAction.DONE))) { + // Insert "Done" + log.debug("Action DONE inserted"); + getCurrentRound().process(new NullAction (NullAction.DONE)); + possibleActions.clear(); + getCurrentRound().setPossibleActions(); + if (!isGameOver()) setCorrectionActions(); + } - DisplayBuffer.clear(); - // TEMPORARY FIX TO ALLOW OLD 1856 SAVED FILES TO BE PROCESSED - if (gameName.equals("1856") - && possibleActions.contains(RepayLoans.class) - && (!possibleActions.contains(action.getClass()) - || (action.getClass() == NullAction.class - && ((NullAction)action).getMode() != NullAction.DONE))) { - // Insert "Done" - log.debug("Action DONE inserted"); - getCurrentRound().process(new NullAction (NullAction.DONE)); - possibleActions.clear(); - getCurrentRound().setPossibleActions(); - if (!isGameOver()) setCorrectionActions(); + try { + log.debug("Action ("+action.getPlayerName()+"): " + action); + if (!processCorrectionActions(action) && !getCurrentRound().process(action)) { + String msg = "Player "+action.getPlayerName()+"\'s action \"" + +action.toString()+"\"\n in "+getCurrentRound().getRoundName() + +" is considered invalid by the game engine"; + log.error(msg); + DisplayBuffer.add(msg); + if (moveStack.isOpen()) moveStack.finish(); + return false; } + possibleActions.clear(); + getCurrentRound().setPossibleActions(); - try { - log.debug("Action ("+action.getPlayerName()+"): " + action); - if (!processCorrectionActions(action) && !getCurrentRound().process(action)) { - String msg = "Player "+action.getPlayerName()+"\'s action \"" - +action.toString()+"\"\n in "+getCurrentRound().getRoundName() - +" is considered invalid by the game engine"; - log.error(msg); - DisplayBuffer.add(msg); - if (moveStack.isOpen()) moveStack.finish(); - return false; - } - possibleActions.clear(); - getCurrentRound().setPossibleActions(); + // Log possible actions (normally this is outcommented) + String playerName = getCurrentPlayer().getName(); + for (PossibleAction a : possibleActions.getList()) { + log.debug(playerName+" may: "+a.toString()); + } - // Log possible actions (normally this is outcommented) - String playerName = getCurrentPlayer().getName(); - for (PossibleAction a : possibleActions.getList()) { - log.debug(playerName+" may: "+a.toString()); - } + if (!isGameOver()) setCorrectionActions(); - if (!isGameOver()) setCorrectionActions(); + } catch (Exception e) { + log.error("Error while reprocessing " + action.toString(), e); + throw new Exception("Reload failure", e); + } + new AddToList<PossibleAction>(executedActions, action, "ExecutedActions"); + if (moveStack.isOpen()) moveStack.finish(); - } catch (Exception e) { - log.error("Error while reprocessing " + action.toString(), e); - throw new Exception("Reload failure", e); - } - new AddToList<PossibleAction>(executedActions, action, - "ExecutedActions"); - if (moveStack.isOpen()) moveStack.finish(); + log.debug("Turn: "+getCurrentPlayer().getName()); + return true; + } - log.debug("Turn: "+getCurrentPlayer().getName()); - } + public void finishLoading () { - // DisplayBuffer.clear(); - // previous line removed to allow display of nextPlayerMessages guiHints.clearVisibilityHints(); - - return true; } /** recoverySave method @@ -1023,7 +1019,9 @@ oos.writeObject(gameName); oos.writeObject(gameOptions); oos.writeObject(playerNames); - oos.writeObject(executedActions); + for (PossibleAction action : executedActions) { + oos.writeObject(action); + } oos.close(); result = true; Modified: trunk/18xx/rails/game/GameManagerI.java =================================================================== --- trunk/18xx/rails/game/GameManagerI.java 2010-07-24 15:47:32 UTC (rev 1356) +++ trunk/18xx/rails/game/GameManagerI.java 2010-07-25 17:08:37 UTC (rev 1357) @@ -55,9 +55,11 @@ */ public abstract boolean process(PossibleAction action); - public abstract boolean processOnReload(List<PossibleAction> actions) + public abstract boolean processOnReload(PossibleAction action) throws Exception; + public void finishLoading (); + public abstract void finishShareSellingRound(); public abstract void finishTreasuryShareRound(); Modified: trunk/18xx/rails/util/ListAndFixSavedFiles.java =================================================================== --- trunk/18xx/rails/util/ListAndFixSavedFiles.java 2010-07-24 15:47:32 UTC (rev 1356) +++ trunk/18xx/rails/util/ListAndFixSavedFiles.java 2010-07-25 17:08:37 UTC (rev 1357) @@ -29,12 +29,12 @@ private JMenuItem trimItem, deleteItem; private StringBuffer headerText = new StringBuffer(); - + private List<Object> savedObjects = new ArrayList<Object>(512); private List<PossibleAction> executedActions; - + private int vbarPos; - + private static String saveDirectory; private String filepath; @@ -44,13 +44,13 @@ * @param args */ public static void main(String[] args) { - + // intialize configuration Config.setConfigSelection(); // delayed setting of logger log = Logger.getLogger(ListAndFixSavedFiles.class.getPackage().getName()); - + saveDirectory = Config.get("save.directory"); System.out.println("Save directory = " + saveDirectory); @@ -142,11 +142,12 @@ setVisible(true); saveDirectory = Config.get("save.directory"); - + load(); } + @SuppressWarnings("unchecked") private void load() { JFileChooser jfc = new JFileChooser(); @@ -211,10 +212,26 @@ throw new ConfigurationException("Error in setting up " + name); } - executedActions = - (List<PossibleAction>) ois.readObject(); - savedObjects.add(executedActions); - + Object firstActionObject = ois.readObject(); + if (firstActionObject instanceof List) { + // Old-style: one List of PossibleActions + executedActions = + (List<PossibleAction>) firstActionObject; + savedObjects.add(executedActions); + } else { + // New style: separate PossibleActionsObjects, since Rails 1.3.1 + executedActions = new ArrayList<PossibleAction>(); + PossibleAction action = (PossibleAction) firstActionObject; + while (true) { + savedObjects.add (action); + executedActions.add(action); + try { + action = (PossibleAction) ois.readObject(); + } catch (EOFException e) { + break; + } + } + } setReportText(true); ois.close(); @@ -232,13 +249,13 @@ headerText.append("\n"); } } - + private void setReportText(boolean initial) { if (initial) vbarPos = -1; else vbarPos = this.vbar.getValue(); - + reportText.setText(headerText.toString()); // append actionText int i=0; @@ -248,8 +265,8 @@ } scrollDown(vbarPos); } - + public void scrollDown (int pos) { SwingUtilities.invokeLater(new Runnable() { public void run() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |