diff --git a/.gitattributes b/.gitattributes index 8c1459ee88d..f26a019792f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -161,8 +161,10 @@ src/forge/MyButton.java svneol=native#text/plain src/forge/MyObservable.java svneol=native#text/plain src/forge/MyRandom.java svneol=native#text/plain src/forge/NameChanger.java svneol=native#text/plain +src/forge/NewDeckIO.java svneol=native#text/plain src/forge/NewIO.java svneol=native#text/plain src/forge/NonScrollingJEditorPane.java -text svneol=native#text/plain +src/forge/OldDeckIO.java svneol=native#text/plain src/forge/Phase.java svneol=native#text/plain src/forge/PhaseObserver.java svneol=native#text/plain src/forge/PicturePanel.java svneol=native#text/plain @@ -198,7 +200,6 @@ src/forge/Time.java svneol=native#text/plain src/forge/UndoCommand.java svneol=native#text/plain src/forge/Wait.java svneol=native#text/plain src/forge/WinLose.java svneol=native#text/plain -src/forge/_ObjectInputStream.java svneol=native#text/plain src/forge/error/ErrorViewer.java svneol=native#text/plain src/forge/error/ExceptionHandler.java -text svneol=native#text/plain src/forge/gui/ForgeAction.java svneol=native#text/plain diff --git a/res/all-decks2 b/res/all-decks2 index 44d66798b8b..1afb80e468c 100644 Binary files a/res/all-decks2 and b/res/all-decks2 differ diff --git a/res/main.properties b/res/main.properties index a788f955cc6..263187a2a15 100644 --- a/res/main.properties +++ b/res/main.properties @@ -5,6 +5,7 @@ tokens--file=AllTokens.txt decks--file=all-decks2 booster-decks--file=booster-decks +decks-dir--file=decks card-pictures--file=card-pictures.txt cards--file=cards.txt diff --git a/src/Deck.java b/src/Deck.java index 417ce7be5c0..3109dd5fdc6 100644 --- a/src/Deck.java +++ b/src/Deck.java @@ -1,4 +1,3 @@ -import java.io.ObjectStreamException; import java.io.Serializable; import java.util.ArrayList; @@ -25,20 +24,13 @@ public class Deck implements Serializable { private boolean isSealed; private boolean isDraft; - private ArrayList main = new ArrayList(); - private ArrayList sideboard = new ArrayList(); + private ArrayList main; + private ArrayList sideboard; //very important, do NOT change this - private String name = ""; + private String name; - private Object readResolve() throws ObjectStreamException { - System.out.println("resolving obsolete Deck"); - forge.Deck d = new forge.Deck(deckType); - d.setName(name); - for(String s:main) - d.addMain(s); - for(String s:sideboard) - d.addSideboard(s); - return d; + public forge.Deck migrate() { + return new forge.Deck(deckType, main, sideboard, name); } } diff --git a/src/QuestData_State.java b/src/QuestData_State.java index 7de2c519a94..0a5be654a4b 100644 --- a/src/QuestData_State.java +++ b/src/QuestData_State.java @@ -28,7 +28,7 @@ public class QuestData_State implements Serializable { @SuppressWarnings("unchecked") private Object readResolve() throws ObjectStreamException { - System.out.println("resolving obsolete QuestData_State"); +// System.out.println("resolving obsolete QuestData_State"); //removing generic types to make cast between Deck and forge.Deck //i'm using Deck in this class because it may be somewhere in the serialization stream //it is irrelevant, because readResolve replaces Deck by forge.Deck, and since generics diff --git a/src/forge/Constant.java b/src/forge/Constant.java index 93eff79ecbf..3987aac6f71 100644 --- a/src/forge/Constant.java +++ b/src/forge/Constant.java @@ -1,118 +1,124 @@ + package forge; + + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import javax.swing.ImageIcon; -public interface Constant -{ - public static final String ProgramName = "MTG Forge - 10/2009 - http://mtgrares.blogspot.com"; - //used to pass information between the GUI screens - public interface Runtime - { - public static final Deck[] HumanDeck = new Deck[1]; - public static final Deck[] ComputerDeck = new Deck[1]; - public static final String[] GameType = new String[1]; - public static final WinLose WinLose = new WinLose(); - public static final boolean[] smooth = new boolean[1]; +public interface Constant { + public static final String ProgramName = "MTG Forge - 10/2009 - http://mtgrares.blogspot.com"; - public static final int[] width = new int[1]; - public static final int[] height = new int[1]; + //used to pass information between the GUI screens + public interface Runtime { + public static final Deck[] HumanDeck = new Deck[1]; + public static final Deck[] ComputerDeck = new Deck[1]; + public static final String[] GameType = new String[1]; + public static final WinLose WinLose = new WinLose(); + public static final boolean[] smooth = new boolean[1]; + + public static final int[] width = new int[1]; + public static final int[] height = new int[1]; + + } - } - public interface GameType - { - public static final String Constructed = "constructed"; - public static final String Sealed = "sealed"; - public static final String Draft = "draft"; - } - public interface IO - { - //TODO: probably should read this from a file, or set from GUI + public interface GameType { + public static final String Constructed = "constructed"; + public static final String Sealed = "sealed"; + public static final String Draft = "draft"; + public static final List GameTypes = Collections.unmodifiableList(Arrays.asList(Constructed, + Sealed, Draft)); + } + + public interface IO { + //TODO: probably should read this from a file, or set from GUI // public static final String baseDir = "c:\\Tim\\game testing\\"; - - public static final String deckFile = "all-decks2"; - public static final String boosterDeckFile = "booster-decks"; - - public static final String imageBaseDir = "pics"; - public static final String cardFile = "cards.txt"; - - public static final ImageIcon upIcon = new ImageIcon ("up.gif"); - public static final ImageIcon downIcon = new ImageIcon ("down.gif"); - public static final ImageIcon leftIcon = new ImageIcon ("left.gif"); - public static final ImageIcon rightIcon = new ImageIcon("right.gif"); - } - public interface Ability - { - public static final String Triggered = "Triggered"; - public static final String Activated = "Activated"; - } - public interface Phase - { - public static final String Untap = "Untap"; - public static final String Upkeep = "Upkeep"; - public static final String Draw = "Draw"; - public static final String Main1 = "Main1"; - //public static final String Begin_Combat = "Beginning of Combat"; - public static final String Combat_Before_Declare_Attackers_InstantAbility = "Before Attack Phase - Play Instants and Abilities"; - public static final String Combat_Declare_Attackers_InstantAbility = "Declare Attackers - Play Instants and Abilities"; - public static final String Combat_Declare_Attackers = "Declare Attackers"; - public static final String Combat_Declare_Blockers = "Declare Blockers"; - public static final String Combat_Declare_Blockers_InstantAbility = "Declare Blockers - Play Instants and Abilities"; - public static final String Combat_Damage = "Combat Damage"; - public static final String Combat_FirstStrikeDamage = "First Strike Damage"; - //public static final String End_Combat = "End of Combat"; - public static final String Main2 = "Main2"; - public static final String At_End_Of_Turn = "At End of Turn"; - public static final String End_Of_Turn = "End of Turn"; - public static final String Until_End_Of_Turn = "Until End of Turn"; - public static final String Cleanup = "Cleanup"; - } - public interface Zone - { - public static final String Hand = "Hand"; - public static final String Library = "Library"; - public static final String Graveyard = "Graveyard"; - public static final String Play = "Play"; - public static final String Removed_From_Play = "Removed from play"; - //public static final String Stack = "Stack"; - } - public interface Player - { - public static final String Human = "Human"; - public static final String Computer = "Computer"; - } + + public static final String deckFile = "all-decks2"; + public static final String boosterDeckFile = "booster-decks"; + + public static final String imageBaseDir = "pics"; + public static final String cardFile = "cards.txt"; + + public static final ImageIcon upIcon = new ImageIcon("up.gif"); + public static final ImageIcon downIcon = new ImageIcon("down.gif"); + public static final ImageIcon leftIcon = new ImageIcon("left.gif"); + public static final ImageIcon rightIcon = new ImageIcon("right.gif"); + } + + public interface Ability { + public static final String Triggered = "Triggered"; + public static final String Activated = "Activated"; + } + + public interface Phase { + public static final String Untap = "Untap"; + public static final String Upkeep = "Upkeep"; + public static final String Draw = "Draw"; + public static final String Main1 = "Main1"; + //public static final String Begin_Combat = "Beginning of Combat"; + public static final String Combat_Before_Declare_Attackers_InstantAbility = "Before Attack Phase - Play Instants and Abilities"; + public static final String Combat_Declare_Attackers_InstantAbility = "Declare Attackers - Play Instants and Abilities"; + public static final String Combat_Declare_Attackers = "Declare Attackers"; + public static final String Combat_Declare_Blockers = "Declare Blockers"; + public static final String Combat_Declare_Blockers_InstantAbility = "Declare Blockers - Play Instants and Abilities"; + public static final String Combat_Damage = "Combat Damage"; + public static final String Combat_FirstStrikeDamage = "First Strike Damage"; + //public static final String End_Combat = "End of Combat"; + public static final String Main2 = "Main2"; + public static final String At_End_Of_Turn = "At End of Turn"; + public static final String End_Of_Turn = "End of Turn"; + public static final String Until_End_Of_Turn = "Until End of Turn"; + public static final String Cleanup = "Cleanup"; + } + + public interface Zone { + public static final String Hand = "Hand"; + public static final String Library = "Library"; + public static final String Graveyard = "Graveyard"; + public static final String Play = "Play"; + public static final String Removed_From_Play = "Removed from play"; + //public static final String Stack = "Stack"; + } + + public interface Player { + public static final String Human = "Human"; + public static final String Computer = "Computer"; + } + /* public interface CardType { - public static final String Artifact = "Artifact"; - public static final String Creature = "Creature"; - public static final String Enchantment = "Enchantment"; - public static final String Aura = "Aura"; - public static final String Instant = "Instant"; - public static final String Land = "Land"; - public static final String Legendary = "Legendary"; - public static final String Sorcery = "Sorcery"; - public static final String Basic = "Basic"; + public static final String Artifact = "Artifact"; + public static final String Creature = "Creature"; + public static final String Enchantment = "Enchantment"; + public static final String Aura = "Aura"; + public static final String Instant = "Instant"; + public static final String Land = "Land"; + public static final String Legendary = "Legendary"; + public static final String Sorcery = "Sorcery"; + public static final String Basic = "Basic"; } */ - public interface Color - { - public static final String Black = "black"; - public static final String Blue = "blue"; - public static final String Green = "green"; - public static final String Red = "red"; - public static final String White = "white"; - - public static final String Colorless = "colorless"; - //color order "wubrg" - public static final String Colors[] = - {White, Blue, Black, Red, Green, Colorless}; - - public static final String onlyColors[] = {White, Blue, Black, Red, Green}; - - public static final String Snow = "snow"; - public static final String ManaColors[] = - {White, Blue, Black, Red, Green, Colorless, Snow}; - } + public interface Color { + public static final String Black = "black"; + public static final String Blue = "blue"; + public static final String Green = "green"; + public static final String Red = "red"; + public static final String White = "white"; + + public static final String Colorless = "colorless"; + //color order "wubrg" + public static final String Colors[] = {White, Blue, Black, Red, Green, Colorless}; + + public static final String onlyColors[] = {White, Blue, Black, Red, Green}; + + public static final String Snow = "snow"; + public static final String ManaColors[] = {White, Blue, Black, Red, Green, Colorless, Snow}; + } }//Constant /* diff --git a/src/forge/Deck.java b/src/forge/Deck.java index 0e8d58a2184..138f1868f36 100644 --- a/src/forge/Deck.java +++ b/src/forge/Deck.java @@ -1,31 +1,45 @@ + package forge; +import static java.util.Collections.*; - -import java.util.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; public class Deck implements java.io.Serializable { private static final long serialVersionUID = -2188987217361601903L; //gameType is from Constant.GameType, like Constant.GameType.Regular - private String deckType; + private String name, comment, deckType; - private boolean isRegular; - private boolean isSealed; - private boolean isDraft; - - private ArrayList main = new ArrayList(); - private ArrayList sideboard = new ArrayList(); - - //very important, do NOT change this - private String name = ""; + private List main, sideboard, mainView, sideboardView; //gameType is from Constant.GameType, like Constant.GameType.Regular public Deck(String gameType) { - deckType = gameType; setDeckType(gameType); + name = ""; + main = new ArrayList(); + sideboard = new ArrayList(); + mainView = unmodifiableList(main); + sideboardView = unmodifiableList(sideboard); + } + + public Deck(String deckType, List main, List sideboard, String name) { + this.deckType = deckType; + this.main = main; + this.sideboard = main; + this.name = name; + } + + public List getMain() { + return mainView; + } + + public List getSideboard() { + return sideboardView; } public String getDeckType() { @@ -33,14 +47,14 @@ public class Deck implements java.io.Serializable { } //can only call this method ONCE - private final void setDeckType(String deckType) { - if(isRegular || isSealed || isDraft) throw new RuntimeException( + private void setDeckType(String deckType) { + if(this.deckType != null) throw new IllegalStateException( "Deck : setDeckType() error, deck type has already been set"); - if(deckType.equals(Constant.GameType.Constructed)) isRegular = true; - else if(deckType.equals(Constant.GameType.Sealed)) isSealed = true; - else if(deckType.equals(Constant.GameType.Draft)) isDraft = true; - else throw new RuntimeException("Deck : setDeckType() error, invalid deck type - " + deckType); + if(!Constant.GameType.GameTypes.contains(deckType)) throw new RuntimeException( + "Deck : setDeckType() error, invalid deck type - " + deckType); + + this.deckType = deckType; } public void setName(String s) { @@ -51,6 +65,14 @@ public class Deck implements java.io.Serializable { return name; }//may return null + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } + public void addMain(String cardName) { main.add(cardName); } @@ -84,21 +106,22 @@ public class Deck implements java.io.Serializable { } public boolean isDraft() { - return isDraft; + return deckType.equals(Constant.GameType.Draft); } public boolean isSealed() { - return isSealed; + return deckType.equals(Constant.GameType.Sealed); } public boolean isRegular() { - return isRegular; + return deckType.equals(Constant.GameType.Constructed); } public int hashcode() { return getName().hashCode(); } + @Override public String toString() { return getName(); } diff --git a/src/forge/DeckIO.java b/src/forge/DeckIO.java index 817f743a076..9bcbcc73838 100644 --- a/src/forge/DeckIO.java +++ b/src/forge/DeckIO.java @@ -1,202 +1,45 @@ +/** + * DeckIO.java + * + * Created on 01.11.2009 + */ package forge; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.Map; -import forge.error.ErrorViewer; - -//reads and write Deck objects -public class DeckIO { - private final File file; - private ArrayList deckList = new ArrayList(); +/** + * The class DeckIO. + * + * @version V0.0 01.11.2009 + * @author Clemens Koza + */ +public interface DeckIO { - //key is String of the humans deck - //data is Deck[] size of 8 - //humans deck is Deck[0] - private Map boosterMap = new HashMap(); + public boolean isUnique(String deckName); - public static void main(String[] args) { - Deck[] deck = new Deck[8]; - for(int i = 0; i < deck.length; i++) { - deck[i] = new Deck(Constant.GameType.Draft); - deck[i].setName("" + i); - } - - deck[0].addMain("Lantern Kami"); - - //DeckIO io = new DeckIO("booster-decks"); -// io.writeBoosterDeck(deck); -// io.close(); - -// io.deleteBoosterDeck("0"); -// System.out.println(io.getBoosterDecks().keySet().size()); -// io.close(); - } + public boolean isUniqueDraft(String deckName); - public DeckIO(String filename) { - this(new File(filename)); - } + public boolean hasName(String deckName); - public DeckIO(File file) { - this.file = file; - - readFile(); - } + public Deck readDeck(String deckName); - public boolean isUnique(String deckName) { - Deck d; - for(int i = 0; i < deckList.size(); i++) { - d = deckList.get(i); - if(d.getName().equals(deckName)) return false; - } - return true; - } + public void writeDeck(Deck deck); - public boolean isUniqueDraft(String deckName) { - ArrayList key = new ArrayList(boosterMap.keySet()); - - for(int i = 0; i < key.size(); i++) { - if(key.get(i).equals(deckName)) return false; - } - return true; - } + public void deleteDeck(String deckName); - public boolean hasName(String deckName) { - ArrayList string = new ArrayList(); - - for(int i = 0; i < deckList.size(); i++) - string.add(deckList.get(i).toString()); - - Iterator it = boosterMap.keySet().iterator(); - while(it.hasNext()) - string.add(it.next().toString()); - - return string.contains(deckName); - } + public Deck[] readBoosterDeck(String deckName); - public Deck readDeck(String deckName) { - return deckList.get(findDeckIndex(deckName)); - } + public void writeBoosterDeck(Deck[] deck);//writeBoosterDeck() - private int findDeckIndex(String deckName) { - int n = -1; - for(int i = 0; i < deckList.size(); i++) - if((deckList.get(i)).getName().equals(deckName)) n = i; - - if(n == -1) throw new RuntimeException("DeckIO : findDeckIndex() error, deck name not found - " + deckName); - - return n; - } + public void deleteBoosterDeck(String deckName); - public void writeDeck(Deck deck) { - if(deck.getDeckType().equals(Constant.GameType.Draft)) throw new RuntimeException( - "DeckIO : writeDeck() error, deck type is Draft"); - - deckList.add(deck); - } + public Deck[] getDecks(); - public void deleteDeck(String deckName) { - deckList.remove(findDeckIndex(deckName)); - } + public Map getBoosterDecks(); - public Deck[] readBoosterDeck(String deckName) { - if(!boosterMap.containsKey(deckName)) throw new RuntimeException( - "DeckIO : readBoosterDeck() error, deck name not found - " + deckName); - - return boosterMap.get(deckName); - } + public void close(); - public void writeBoosterDeck(Deck[] deck) { - checkBoosterDeck(deck); - - boosterMap.put(deck[0].toString(), deck); - }//writeBoosterDeck() - - public void deleteBoosterDeck(String deckName) { - if(!boosterMap.containsKey(deckName)) throw new RuntimeException( - "DeckIO : deleteBoosterDeck() error, deck name not found - " + deckName); - - boosterMap.remove(deckName); - } - - private void checkBoosterDeck(Deck[] deck) { - if(deck == null || deck.length != 8 || deck[0].getName().equals("") - || (!deck[0].getDeckType().equals(Constant.GameType.Draft))) { - throw new RuntimeException("DeckIO : checkBoosterDeck() error, invalid deck"); - } -// for(int i = 0; i < deck.length; i++) -// if(deck[i].getName().equals("")) -// throw new RuntimeException("DeckIO : checkBoosterDeck() error, deck does not have name - " +deck[i].getName()); - }//checkBoosterDeck() - - - public Deck[] getDecks() { - Deck[] out = new Deck[deckList.size()]; - deckList.toArray(out); - return out; - } - - public Map getBoosterDecks() { - return new HashMap(boosterMap); - } - - public void close() { - writeFile(); - } - - @SuppressWarnings("unchecked") - // ArrayList and Map have unchecked casts - private void readFile() { - try { -//~ - // Shouldn't ever get here, but just in case... - if(file == null) { - return; - } -//~ - if(!file.exists()) return; - - ObjectInputStream in = new _ObjectInputStream(new FileInputStream(file)); - - deckList = (ArrayList) in.readObject(); - boosterMap = (Map) in.readObject(); - - in.close(); - } catch(Exception ex) { - ErrorViewer.showError(ex); - throw new RuntimeException("DeckIO : read() error, " + ex); - } - } - - private void writeFile() { - try { -//~ - if(file == null) { - return; - } -//~ - if(!file.exists()) file.createNewFile(); - - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); - - out.writeObject(deckList); - out.writeObject(boosterMap); - - out.flush(); - out.close(); - } catch(Exception ex) { - ErrorViewer.showError(ex); - throw new RuntimeException("DeckIO : write() error, " + ex.getMessage()); - } - } -}//DeckIO +} diff --git a/src/forge/Gui_BoosterDraft.java b/src/forge/Gui_BoosterDraft.java index 97af615b38c..771dec033da 100644 --- a/src/forge/Gui_BoosterDraft.java +++ b/src/forge/Gui_BoosterDraft.java @@ -1,4 +1,7 @@ + package forge; + + import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; @@ -37,7 +40,6 @@ import forge.properties.ForgeProps; import forge.properties.NewConstants; - public class Gui_BoosterDraft extends JFrame implements CardDetail, NewConstants, NewConstants.LANG.Gui_BoosterDraft { private static final long serialVersionUID = -6055633915602448260L; @@ -450,7 +452,7 @@ public class Gui_BoosterDraft extends JFrame implements CardDetail, NewConstants private void refreshGui() { Deck deck = Constant.Runtime.HumanDeck[0]; if(deck == null) //this is just a patch, i know - deck = new Deck(Constant.Runtime.GameType[0]); + deck = new Deck(Constant.Runtime.GameType[0]); allCardModel.clear(); deckModel.clear(); @@ -461,8 +463,7 @@ public class Gui_BoosterDraft extends JFrame implements CardDetail, NewConstants c = AllZone.CardFactory.getCard(deck.getMain(i), Constant.Player.Human); //add rarity to card if this is a sealed card pool - if(!Constant.Runtime.GameType[0].equals(Constant.GameType.Constructed)) - c.setRarity(pack.getRarity(c.getName()));; + if(!Constant.Runtime.GameType[0].equals(Constant.GameType.Constructed)) c.setRarity(pack.getRarity(c.getName()));; deckModel.addCard(c); }//for @@ -510,7 +511,7 @@ public class Gui_BoosterDraft extends JFrame implements CardDetail, NewConstants //pick card int n = allCardTable.getSelectedRow(); if(n == -1) //is valid selection? - return; + return; Card c = allCardModel.rowToCard(n); @@ -582,7 +583,8 @@ public class Gui_BoosterDraft extends JFrame implements CardDetail, NewConstants Deck[] all = { human, computer[0], computer[1], computer[2], computer[3], computer[4], computer[5], computer[6]}; - DeckIO deckIO = new DeckIO(ForgeProps.getFile(BOOSTER_DECKS)); +// DeckIO deckIO = new OldDeckIO(ForgeProps.getFile(BOOSTER_DECKS)); + DeckIO deckIO = new NewDeckIO(ForgeProps.getFile(NEW_DECKS)); deckIO.writeBoosterDeck(all); //write file @@ -592,4 +594,4 @@ public class Gui_BoosterDraft extends JFrame implements CardDetail, NewConstants dispose(); new Gui_NewGame(); }/*saveDraft()*/ -} \ No newline at end of file +} diff --git a/src/forge/Gui_DeckEditor_Menu.java b/src/forge/Gui_DeckEditor_Menu.java index e4b530176ed..31f14f137a7 100644 --- a/src/forge/Gui_DeckEditor_Menu.java +++ b/src/forge/Gui_DeckEditor_Menu.java @@ -51,8 +51,11 @@ public class Gui_DeckEditor_Menu extends JMenuBar implements NewConstants { private final boolean debugPrint = false; - private final DeckIO deckIO = new DeckIO(ForgeProps.getFile(DECKS)); - private final DeckIO boosterDeckIO = new DeckIO(ForgeProps.getFile(BOOSTER_DECKS)); +// private final DeckIO deckIO = new OldDeckIO(ForgeProps.getFile(DECKS)); +// private final DeckIO boosterDeckIO = new OldDeckIO(ForgeProps.getFile(BOOSTER_DECKS)); + private final DeckIO deckIO = new NewDeckIO(ForgeProps.getFile(NEW_DECKS)); + //with the new IO, there's no reason to use different instances + private final DeckIO boosterDeckIO = deckIO; private boolean isDeckSaved; private String currentDeckName; @@ -329,7 +332,7 @@ public class Gui_DeckEditor_Menu extends JMenuBar implements NewConstants { Object check = null; try { - ObjectInputStream in = new _ObjectInputStream(new FileInputStream(file)); + ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); check = in.readObject(); in.close(); diff --git a/src/forge/Gui_NewGame.java b/src/forge/Gui_NewGame.java index e1abee290af..dc6765dbe90 100644 --- a/src/forge/Gui_NewGame.java +++ b/src/forge/Gui_NewGame.java @@ -1,5 +1,7 @@ + package forge; + import java.awt.Color; import java.awt.Component; import java.awt.Dimension; @@ -47,32 +49,35 @@ import forge.properties.NewConstants.LANG.Gui_NewGame.MENU_BAR.OPTIONS; public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LANG.Gui_NewGame { private static final long serialVersionUID = -2437047615019135648L; - private final DeckIO deckIO = new DeckIO(ForgeProps.getFile(DECKS)); - private final DeckIO boosterDeckIO = new DeckIO(ForgeProps.getFile(BOOSTER_DECKS)); +// private final DeckIO deckIO = new OldDeckIO(ForgeProps.getFile(DECKS)); +// private final DeckIO boosterDeckIO = new OldDeckIO(ForgeProps.getFile(BOOSTER_DECKS)); + private final DeckIO deckIO = new NewDeckIO(ForgeProps.getFile(NEW_DECKS)); + //with the new IO, there's no reason to use different instances + private final DeckIO boosterDeckIO = deckIO; private ArrayList allDecks; private static Gui_DeckEditor editor; - private JLabel titleLabel = new JLabel(); - private JLabel jLabel2 = new JLabel(); - private JLabel jLabel3 = new JLabel(); - private JComboBox humanComboBox = new JComboBox(); - private JComboBox computerComboBox = new JComboBox(); - private JButton deckEditorButton = new JButton(); - private JButton startButton = new JButton(); - private ButtonGroup buttonGroup1 = new ButtonGroup(); - private JRadioButton sealedRadioButton = new JRadioButton(); - private JRadioButton singleRadioButton = new JRadioButton(); - private JPanel jPanel2 = new JPanel(); - private Border border1; - private TitledBorder titledBorder1; - private JRadioButton draftRadioButton = new JRadioButton(); - private JPanel jPanel1 = new JPanel(); - private Border border2; + private JLabel titleLabel = new JLabel(); + private JLabel jLabel2 = new JLabel(); + private JLabel jLabel3 = new JLabel(); + private JComboBox humanComboBox = new JComboBox(); + private JComboBox computerComboBox = new JComboBox(); + private JButton deckEditorButton = new JButton(); + private JButton startButton = new JButton(); + private ButtonGroup buttonGroup1 = new ButtonGroup(); + private JRadioButton sealedRadioButton = new JRadioButton(); + private JRadioButton singleRadioButton = new JRadioButton(); + private JPanel jPanel2 = new JPanel(); + private Border border1; + private TitledBorder titledBorder1; + private JRadioButton draftRadioButton = new JRadioButton(); + private JPanel jPanel1 = new JPanel(); + private Border border2; @SuppressWarnings("unused") // titledBorder2 - private TitledBorder titledBorder2; - private static JCheckBox newGuiCheckBox = new JCheckBox("", true); - private static JCheckBox smoothLandCheckBox = new JCheckBox("", true); + private TitledBorder titledBorder2; + private static JCheckBox newGuiCheckBox = new JCheckBox("", true); + private static JCheckBox smoothLandCheckBox = new JCheckBox("", true); // GenerateConstructedDeck.get2Colors() and GenerateSealedDeck.get2Colors() // use these two variables @@ -81,7 +86,7 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA public static JCheckBoxMenuItem removeArtifacts = new JCheckBoxMenuItem( ForgeProps.getLocalized(MENU_BAR.OPTIONS.GENERATE.REMOVE_ARTIFACTS)); - private JButton questButton = new JButton(); + private JButton questButton = new JButton(); private Action LOOK_AND_FEEL_ACTION = new LookAndFeelAction(this); private Action DOWNLOAD_ACTION = new DownloadAction(); @@ -90,7 +95,7 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA public static void main(String[] args) { ExceptionHandler.registerErrorHandling(); - try { + try { Object[] o = UIManager.getInstalledLookAndFeels(); if(o.length > 3) { final Color background = new Color(204, 204, 204); @@ -102,7 +107,7 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA "Dialog.background", "OptionPane.background", "ScrollBar.background"}; for(int i = 0; i < properties.length; i++) { //UIManager.put(properties, background); - UIManager.put(properties[i], background); + UIManager.put(properties[i], background); } } } catch(Exception ex) { @@ -402,19 +407,15 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA if(humanGenerate) { if(constructed) Constant.Runtime.HumanDeck[0] = generateConstructedDeck(); else if(sealed) Constant.Runtime.HumanDeck[0] = generateSealedDeck(); - } - else if (humanGenerateMulti3) { - if(constructed) Constant.Runtime.HumanDeck[0] = generateConstructed3ColorDeck(); - } - else if (humanGenerateMulti5) { - if(constructed) Constant.Runtime.HumanDeck[0] = generateConstructed5ColorDeck(); - } - else if(humanRandom) { + } else if(humanGenerateMulti3) { + if(constructed) Constant.Runtime.HumanDeck[0] = generateConstructed3ColorDeck(); + } else if(humanGenerateMulti5) { + if(constructed) Constant.Runtime.HumanDeck[0] = generateConstructed5ColorDeck(); + } else if(humanRandom) { Constant.Runtime.HumanDeck[0] = getRandomDeck(getDecks(format)); JOptionPane.showMessageDialog(null, String.format("You are using deck: %s", Constant.Runtime.HumanDeck[0].getName()), "Deck Name", JOptionPane.INFORMATION_MESSAGE); - } - else { + } else { Constant.Runtime.HumanDeck[0] = deckIO.readDeck(human); } @@ -427,14 +428,11 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA if(computerGenerate) { if(constructed) Constant.Runtime.ComputerDeck[0] = generateConstructedDeck(); else if(sealed) Constant.Runtime.ComputerDeck[0] = generateSealedDeck(); - } - else if (computerGenerateMulti3) { - if(constructed) Constant.Runtime.ComputerDeck[0] = generateConstructed3ColorDeck(); - } - else if (computerGenerateMulti5) { - if(constructed) Constant.Runtime.ComputerDeck[0] = generateConstructed5ColorDeck(); - } - else if(computerRandom) { + } else if(computerGenerateMulti3) { + if(constructed) Constant.Runtime.ComputerDeck[0] = generateConstructed3ColorDeck(); + } else if(computerGenerateMulti5) { + if(constructed) Constant.Runtime.ComputerDeck[0] = generateConstructed5ColorDeck(); + } else if(computerRandom) { Constant.Runtime.ComputerDeck[0] = getRandomDeck(getDecks(format)); JOptionPane.showMessageDialog(null, String.format("The computer is using deck: %s", Constant.Runtime.ComputerDeck[0].getName()), "Deck Name", JOptionPane.INFORMATION_MESSAGE); @@ -479,7 +477,7 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA } private Deck generateConstructed3ColorDeck() { - GenerateConstructedMultiColorDeck gen = new GenerateConstructedMultiColorDeck(); + GenerateConstructedMultiColorDeck gen = new GenerateConstructedMultiColorDeck(); CardList name = gen.generate3ColorDeck(); Deck deck = new Deck(Constant.GameType.Constructed); @@ -489,7 +487,7 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA } private Deck generateConstructed5ColorDeck() { - GenerateConstructedMultiColorDeck gen = new GenerateConstructedMultiColorDeck(); + GenerateConstructedMultiColorDeck gen = new GenerateConstructedMultiColorDeck(); CardList name = gen.generate5ColorDeck(); Deck deck = new Deck(Constant.GameType.Constructed); @@ -580,9 +578,9 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA }/* draftRadioButton_actionPerformed() */ public static class LookAndFeelAction extends AbstractAction { - - private static final long serialVersionUID = -4447498333866711215L; - private Component c; + + private static final long serialVersionUID = -4447498333866711215L; + private Component c; public LookAndFeelAction(Component c) { super(ForgeProps.getLocalized(MENU_BAR.MENU.LF)); @@ -607,29 +605,29 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA } public static class DownloadAction extends AbstractAction { - - private static final long serialVersionUID = 6564425021778307101L; - - public DownloadAction() { + + private static final long serialVersionUID = 6564425021778307101L; + + public DownloadAction() { super(ForgeProps.getLocalized(MENU_BAR.MENU.DOWNLOAD)); } - + public void actionPerformed(ActionEvent e) { Gui_DownloadPictures.startDownload(null); } } public static class CardSizesAction extends AbstractAction { - - private static final long serialVersionUID = -2900235618450319571L; - private String[] keys = {"Tiny", "Small", "Medium", "Large"}; - private int[] widths = {42, 63, 70, 93}; - private int[] heights = {59, 88, 98, 130}; + + private static final long serialVersionUID = -2900235618450319571L; + private String[] keys = {"Tiny", "Small", "Medium", "Large"}; + private int[] widths = {42, 63, 70, 93}; + private int[] heights = {59, 88, 98, 130}; public CardSizesAction() { super(ForgeProps.getLocalized(MENU_BAR.MENU.CARD_SIZES)); } - + public void actionPerformed(ActionEvent e) { ListChooser ch = new ListChooser("Choose one", "Choose a new card size", 0, 1, keys); if(ch.show()) try { @@ -644,10 +642,10 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA } public static class AboutAction extends AbstractAction { - - private static final long serialVersionUID = 5492173304463396871L; - - public AboutAction() { + + private static final long serialVersionUID = 5492173304463396871L; + + public AboutAction() { super(ForgeProps.getLocalized(MENU_BAR.MENU.ABOUT)); } @@ -668,4 +666,4 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA JOptionPane.showMessageDialog(null, area, "About", JOptionPane.INFORMATION_MESSAGE); } } -} \ No newline at end of file +} diff --git a/src/forge/Gui_Quest_DeckEditor_Menu.java b/src/forge/Gui_Quest_DeckEditor_Menu.java index 42947644b67..af8528b9777 100644 --- a/src/forge/Gui_Quest_DeckEditor_Menu.java +++ b/src/forge/Gui_Quest_DeckEditor_Menu.java @@ -264,7 +264,7 @@ public class Gui_Quest_DeckEditor_Menu extends JMenuBar { Object check = null; try { - ObjectInputStream in = new _ObjectInputStream(new FileInputStream(file)); + ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); check = in.readObject(); in.close(); diff --git a/src/forge/IO.java b/src/forge/IO.java index cc808b821f6..a3e762e9b43 100644 --- a/src/forge/IO.java +++ b/src/forge/IO.java @@ -39,7 +39,7 @@ public class IO { // Map has unchecked cast private void readMap() { try { - ObjectInputStream in = new _ObjectInputStream(new FileInputStream(file)); + ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); map = (Map) in.readObject(); in.close(); } catch(Exception ex) { diff --git a/src/forge/NewDeckIO.java b/src/forge/NewDeckIO.java new file mode 100755 index 00000000000..89879716bdf --- /dev/null +++ b/src/forge/NewDeckIO.java @@ -0,0 +1,327 @@ + +package forge; + + +import static java.lang.Integer.*; +import static java.lang.String.*; +import static java.util.Arrays.*; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import forge.error.ErrorViewer; + + +//reads and write Deck objects +public class NewDeckIO implements DeckIO { + private static FilenameFilter dck = new FilenameFilter() { + public boolean accept(File dir, String name) { + return name.endsWith(".dck"); + } + }; + private static FilenameFilter bdk = new FilenameFilter() { + public boolean accept(File dir, String name) { + return name.endsWith(".bdk"); + } + }; + + private File dir; + List deckList; + Map boosterMap; + + public NewDeckIO(String fileName) { + this(new File(fileName)); + } + + public NewDeckIO(File dir) { + try { + this.dir = dir; + + if(dir.isFile()) { + throw new IOException("Not a directory"); + } else { + dir.mkdirs(); + if(!dir.isDirectory()) throw new IOException("Directory can't be created"); + deckList = new ArrayList(); + boosterMap = new HashMap(); + readFile(); + } + } catch(IOException ex) { + ErrorViewer.showError(ex); + throw new RuntimeException("DeckIO : write() error, " + ex.getMessage()); + } + } + + public boolean isUnique(String deckName) { + Deck d; + for(int i = 0; i < deckList.size(); i++) { + d = deckList.get(i); + if(d.getName().equals(deckName)) return false; + } + return true; + } + + public boolean isUniqueDraft(String deckName) { + ArrayList key = new ArrayList(boosterMap.keySet()); + + for(int i = 0; i < key.size(); i++) { + if(key.get(i).equals(deckName)) return false; + } + return true; + } + + public boolean hasName(String deckName) { + ArrayList string = new ArrayList(); + + for(int i = 0; i < deckList.size(); i++) + string.add(deckList.get(i).toString()); + + Iterator it = boosterMap.keySet().iterator(); + while(it.hasNext()) + string.add(it.next().toString()); + + return string.contains(deckName); + } + + public Deck readDeck(String deckName) { + return deckList.get(findDeckIndex(deckName)); + } + + private int findDeckIndex(String deckName) { + int n = -1; + for(int i = 0; i < deckList.size(); i++) + if((deckList.get(i)).getName().equals(deckName)) n = i; + + if(n == -1) throw new RuntimeException("DeckIO : findDeckIndex() error, deck name not found - " + deckName); + + return n; + } + + public void writeDeck(Deck deck) { + if(deck.getDeckType().equals(Constant.GameType.Draft)) throw new RuntimeException( + "DeckIO : writeDeck() error, deck type is Draft"); + + deckList.add(deck); + } + + public void deleteDeck(String deckName) { + deckList.remove(findDeckIndex(deckName)); + } + + public Deck[] readBoosterDeck(String deckName) { + if(!boosterMap.containsKey(deckName)) throw new RuntimeException( + "DeckIO : readBoosterDeck() error, deck name not found - " + deckName); + + return boosterMap.get(deckName); + } + + public void writeBoosterDeck(Deck[] deck) { + checkBoosterDeck(deck); + + boosterMap.put(deck[0].toString(), deck); + }//writeBoosterDeck() + + public void deleteBoosterDeck(String deckName) { + if(!boosterMap.containsKey(deckName)) throw new RuntimeException( + "DeckIO : deleteBoosterDeck() error, deck name not found - " + deckName); + + boosterMap.remove(deckName); + } + + private void checkBoosterDeck(Deck[] deck) { + if(deck == null || deck.length != 8 || deck[0].getName().equals("") + || (!deck[0].getDeckType().equals(Constant.GameType.Draft))) { + throw new RuntimeException("DeckIO : checkBoosterDeck() error, invalid deck"); + } +// for(int i = 0; i < deck.length; i++) +// if(deck[i].getName().equals("")) +// throw new RuntimeException("DeckIO : checkBoosterDeck() error, deck does not have name - " +deck[i].getName()); + }//checkBoosterDeck() + + + public Deck[] getDecks() { + Deck[] out = new Deck[deckList.size()]; + deckList.toArray(out); + return out; + } + + public Map getBoosterDecks() { + return new HashMap(boosterMap); + } + + public void close() { + writeFile(); + } + + public void readFile() { + try { + deckList.clear(); + + File[] files; + files = dir.listFiles(dck); + for(File file:files) { + BufferedReader in = new BufferedReader(new FileReader(file)); + deckList.add(read(in)); + in.close(); + } + + boosterMap.clear(); + files = dir.listFiles(bdk); + for(File file:files) { + Deck[] d = new Deck[8]; + for(int i = 0; i < d.length; i++) { + BufferedReader in = new BufferedReader(new FileReader(new File(file, i + ".dck"))); + d[i] = read(in); + in.close(); + } + boosterMap.put(d[0].getName(), d); + } + } catch(IOException ex) { + ErrorViewer.showError(ex); + throw new RuntimeException("DeckIO : read() error, " + ex.getMessage()); + } + } + + private Deck read(BufferedReader in) throws IOException { + String line; + + //read name + String name = in.readLine(); + if(name == null) throw new IOException("Unexpected end of file"); + + //read comments + String comment = null; + while((line = in.readLine()) != null && !line.equals("[general]")) { + if(comment == null) comment = line; + else comment += "\n" + line; + } + + //read deck type + String deckType = in.readLine(); + if(deckType == null) throw new IOException("Unexpected end of file"); + + Deck d = new Deck(deckType); + d.setName(name); + d.setComment(comment); + + //go to [main] + while((line = in.readLine()) != null && !line.equals("[main]")) + System.err.println("unexpected line: " + line); + if(line == null) throw new IOException("Unexpected end of file"); + + Pattern p = Pattern.compile("\\s*((\\d+)\\s+)?(.*?)\\s*"); + + //read main deck + while((line = in.readLine()) != null && !line.equals("[sideboard]")) { + Matcher m = p.matcher(line); + if(!m.matches()) throw new IOException("unexpected line: " + line); + String s = m.group(2); + int count = s == null? 1:parseInt(s); + + for(int i = 0; i < count; i++) { + d.addMain(m.group(3)); + } + } + if(line == null) throw new IOException("Unexpected end of file"); + + //read sideboard + while((line = in.readLine()) != null && line.length() != 0) { + Matcher m = p.matcher(line); + if(!m.matches()) throw new IOException("unexpected line: " + line); + String s = m.group(2); + int count = s == null? 1:parseInt(s); + for(int i = 0; i < count; i++) { + d.addSideboard(m.group(3)); + } + } + + return d; + } + + private String deriveFileName(String deckName) { + //skips all but the listed characters + return deckName.replaceAll("[^-_$#@.{[()]} a-zA-Z0-9]", ""); + } + + public void writeFile() { + try { + //store the files that do exist + List files = new ArrayList(); + files.addAll(asList(dir.listFiles(dck))); + + //save the files and remove them from the list + for(Deck deck:deckList) { + File f = new File(dir, deriveFileName(deck.getName()) + ".dck"); + files.remove(f); + BufferedWriter out = new BufferedWriter(new FileWriter(f)); + write(deck, out); + out.close(); + } + //delete the files that were not written out: the decks that were deleted + for(File file:files) + file.delete(); + + //store the files that do exist + files.clear(); + files.addAll(asList(dir.listFiles(bdk))); + + //save the files and remove them from the list + for(Entry e:boosterMap.entrySet()) { + File f = new File(dir, deriveFileName(e.getValue()[0].getName()) + ".bdk"); + f.mkdir(); + for(int i = 0; i < e.getValue().length; i++) { + BufferedWriter out = new BufferedWriter(new FileWriter(new File(f, i + ".dck"))); + write(e.getValue()[i], out); + out.close(); + } + } + //delete the files that were not written out: the decks that were deleted + for(File file:files) { + for(int i = 0; i < 8; i++) + new File(file, i + ".dck").delete(); + file.delete(); + } + } catch(IOException ex) { + ErrorViewer.showError(ex); + throw new RuntimeException("DeckIO : write() error, " + ex.getMessage()); + } + } + + private void write(Deck d, BufferedWriter out) throws IOException { + out.write(format("%s%n", d.getName())); + if(d.getComment() != null) out.write(format("%s%n", d.getComment())); + out.write(format("%s%n", "[general]")); + out.write(format("%s%n", d.getDeckType())); + out.write(format("%s%n", "[main]")); + for(Entry e:count(d.getMain()).entrySet()) { + out.write(format("%d %s%n", e.getValue(), e.getKey())); + } + out.write(format("%s%n", "[sideboard]")); + for(Entry e:count(d.getSideboard()).entrySet()) { + out.write(format("%d %s%n", e.getValue(), e.getKey())); + } + } + + private Map count(List src) { + Map result = new HashMap(); + for(String s:src) { + Integer dstValue = result.get(s); + if(dstValue == null) result.put(s, 1); + else result.put(s, dstValue.intValue() + 1); + } + return result; + } +} diff --git a/src/forge/NewIO.java b/src/forge/NewIO.java index 5f3cd3a3b19..35b3072fe01 100644 --- a/src/forge/NewIO.java +++ b/src/forge/NewIO.java @@ -66,7 +66,7 @@ public class NewIO { private ObjectInputStream getReader() { try { - return new _ObjectInputStream(new FileInputStream(file)); + return new ObjectInputStream(new FileInputStream(file)); } catch(IOException ex) { ErrorViewer.showError(ex); throw new RuntimeException("IO : getReader() - error - " + ex); diff --git a/src/forge/OldDeckIO.java b/src/forge/OldDeckIO.java new file mode 100644 index 00000000000..73e74bc551f --- /dev/null +++ b/src/forge/OldDeckIO.java @@ -0,0 +1,202 @@ + +package forge; + + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import forge.error.ErrorViewer; + + +//reads and write Deck objects +public class OldDeckIO implements DeckIO { + private final File file; + private ArrayList deckList = new ArrayList(); + + //key is String of the humans deck + //data is Deck[] size of 8 + //humans deck is Deck[0] + private Map boosterMap = new HashMap(); + + public static void main(String[] args) { + Deck[] deck = new Deck[8]; + for(int i = 0; i < deck.length; i++) { + deck[i] = new Deck(Constant.GameType.Draft); + deck[i].setName("" + i); + } + + deck[0].addMain("Lantern Kami"); + + //DeckIO io = new DeckIO("booster-decks"); +// io.writeBoosterDeck(deck); +// io.close(); + +// io.deleteBoosterDeck("0"); +// System.out.println(io.getBoosterDecks().keySet().size()); +// io.close(); + } + + public OldDeckIO(String filename) { + this(new File(filename)); + } + + public OldDeckIO(File file) { + this.file = file; + + readFile(); + } + + public boolean isUnique(String deckName) { + Deck d; + for(int i = 0; i < deckList.size(); i++) { + d = deckList.get(i); + if(d.getName().equals(deckName)) return false; + } + return true; + } + + public boolean isUniqueDraft(String deckName) { + ArrayList key = new ArrayList(boosterMap.keySet()); + + for(int i = 0; i < key.size(); i++) { + if(key.get(i).equals(deckName)) return false; + } + return true; + } + + public boolean hasName(String deckName) { + ArrayList string = new ArrayList(); + + for(int i = 0; i < deckList.size(); i++) + string.add(deckList.get(i).toString()); + + Iterator it = boosterMap.keySet().iterator(); + while(it.hasNext()) + string.add(it.next().toString()); + + return string.contains(deckName); + } + + public Deck readDeck(String deckName) { + return deckList.get(findDeckIndex(deckName)); + } + + private int findDeckIndex(String deckName) { + int n = -1; + for(int i = 0; i < deckList.size(); i++) + if((deckList.get(i)).getName().equals(deckName)) n = i; + + if(n == -1) throw new RuntimeException("DeckIO : findDeckIndex() error, deck name not found - " + deckName); + + return n; + } + + public void writeDeck(Deck deck) { + if(deck.getDeckType().equals(Constant.GameType.Draft)) throw new RuntimeException( + "DeckIO : writeDeck() error, deck type is Draft"); + + deckList.add(deck); + } + + public void deleteDeck(String deckName) { + deckList.remove(findDeckIndex(deckName)); + } + + public Deck[] readBoosterDeck(String deckName) { + if(!boosterMap.containsKey(deckName)) throw new RuntimeException( + "DeckIO : readBoosterDeck() error, deck name not found - " + deckName); + + return boosterMap.get(deckName); + } + + public void writeBoosterDeck(Deck[] deck) { + checkBoosterDeck(deck); + + boosterMap.put(deck[0].toString(), deck); + }//writeBoosterDeck() + + public void deleteBoosterDeck(String deckName) { + if(!boosterMap.containsKey(deckName)) throw new RuntimeException( + "DeckIO : deleteBoosterDeck() error, deck name not found - " + deckName); + + boosterMap.remove(deckName); + } + + private void checkBoosterDeck(Deck[] deck) { + if(deck == null || deck.length != 8 || deck[0].getName().equals("") + || (!deck[0].getDeckType().equals(Constant.GameType.Draft))) { + throw new RuntimeException("DeckIO : checkBoosterDeck() error, invalid deck"); + } +// for(int i = 0; i < deck.length; i++) +// if(deck[i].getName().equals("")) +// throw new RuntimeException("DeckIO : checkBoosterDeck() error, deck does not have name - " +deck[i].getName()); + }//checkBoosterDeck() + + + public Deck[] getDecks() { + Deck[] out = new Deck[deckList.size()]; + deckList.toArray(out); + return out; + } + + public Map getBoosterDecks() { + return new HashMap(boosterMap); + } + + public void close() { + writeFile(); + } + + @SuppressWarnings("unchecked") + // ArrayList and Map have unchecked casts + private void readFile() { + try { +//~ + // Shouldn't ever get here, but just in case... + if(file == null) { + return; + } +//~ + if(!file.exists()) return; + + ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); + + deckList = (ArrayList) in.readObject(); + boosterMap = (Map) in.readObject(); + + in.close(); + } catch(Exception ex) { + ErrorViewer.showError(ex); + throw new RuntimeException("DeckIO : read() error, " + ex); + } + } + + private void writeFile() { + try { +//~ + if(file == null) { + return; + } +//~ + if(!file.exists()) file.createNewFile(); + + ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); + + out.writeObject(deckList); + out.writeObject(boosterMap); + + out.flush(); + out.close(); + } catch(Exception ex) { + ErrorViewer.showError(ex); + throw new RuntimeException("DeckIO : write() error, " + ex.getMessage()); + } + } +}//DeckIO diff --git a/src/forge/QuestData.java b/src/forge/QuestData.java index 74106eae00d..6a79243afeb 100644 --- a/src/forge/QuestData.java +++ b/src/forge/QuestData.java @@ -195,7 +195,7 @@ public class QuestData implements NewConstants { static public QuestData loadData() { try { //read file "questData" - ObjectInputStream in = new _ObjectInputStream(new FileInputStream(ForgeProps.getFile(QUEST.DATA))); + ObjectInputStream in = new ObjectInputStream(new FileInputStream(ForgeProps.getFile(QUEST.DATA))); Object o = in.readObject(); in.close(); diff --git a/src/forge/_ObjectInputStream.java b/src/forge/_ObjectInputStream.java deleted file mode 100644 index 711294d54e6..00000000000 --- a/src/forge/_ObjectInputStream.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * _ObjectInputStream.java - * - * Created on 29.10.2009 - */ - -package forge; - - -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; - - -/** - * The class _ObjectInputStream. - * - * @version V0.0 29.10.2009 - * @author Clemens Koza - */ -public class _ObjectInputStream extends ObjectInputStream { - public _ObjectInputStream() throws IOException { - super(); - enableResolveObject(true); - } - - public _ObjectInputStream(InputStream is) throws IOException { - super(is); - enableResolveObject(true); - } - - @Override - protected Object resolveObject(Object obj) throws IOException { - Class c = obj.getClass().getComponentType(); - if(c != null && "Deck".equals(c.getName())) { - return new Deck[((Object[]) obj).length]; - } else return obj; - } -} diff --git a/src/forge/properties/NewConstants.java b/src/forge/properties/NewConstants.java index e9eddb81f94..e6106d3e240 100644 --- a/src/forge/properties/NewConstants.java +++ b/src/forge/properties/NewConstants.java @@ -22,6 +22,7 @@ public interface NewConstants { public static final String DECKS = "decks"; public static final String BOOSTER_DECKS = "booster-decks"; + public static final String NEW_DECKS = "decks-dir"; public static final String TOKENS = "tokens"; public static final String CARD_PICTURES = "card-pictures";