    001    package net.minecraft.src;
    002
    003    import java.io.DataInputStream;
    004    import java.io.DataOutputStream;
    005    import java.io.File;
    006    import java.io.FileInputStream;
    007    import java.io.FileOutputStream;
    008    import java.util.ArrayList;
    009    import java.util.HashMap;
    010    import java.util.Iterator;
    011    import java.util.List;
    012    import java.util.Map;
    013
    014    public class MapStorage
    015    {
    016        private ISaveHandler saveHandler;
    017
    018        /** Map of item data String id to loaded MapDataBases */
    019        private Map loadedDataMap = new HashMap();
    020
    021        /** List of loaded MapDataBases. */
    022        private List loadedDataList = new ArrayList();
    023
    024        /**
    025         * Map of MapDataBase id String prefixes ('map' etc) to max known unique Short id (the 0 part etc) for that prefix
    026         */
    027        private Map idCounts = new HashMap();
    028
    029        public MapStorage(ISaveHandler par1ISaveHandler)
    030        {
    031            this.saveHandler = par1ISaveHandler;
    032            this.loadIdCounts();
    033        }
    034
    035        /**
    036         * Loads an existing MapDataBase corresponding to the given String id from disk, instantiating the given Class, or
    037         * returns null if none such file exists. args: Class to instantiate, String dataid
    038         */
    039        public WorldSavedData loadData(Class par1Class, String par2Str)
    040        {
    041            WorldSavedData var3 = (WorldSavedData)this.loadedDataMap.get(par2Str);
    042
    043            if (var3 != null)
    044            {
    045                return var3;
    046            }
    047            else
    048            {
    049                if (this.saveHandler != null)
    050                {
    051                    try
    052                    {
    053                        File var4 = this.saveHandler.getMapFileFromName(par2Str);
    054
    055                        if (var4 != null && var4.exists())
    056                        {
    057                            try
    058                            {
    059                                var3 = (WorldSavedData)par1Class.getConstructor(new Class[] {String.class}).newInstance(new Object[] {par2Str});
    060                            }
    061                            catch (Exception var7)
    062                            {
    063                                throw new RuntimeException("Failed to instantiate " + par1Class.toString(), var7);
    064                            }
    065
    066                            FileInputStream var5 = new FileInputStream(var4);
    067                            NBTTagCompound var6 = CompressedStreamTools.readCompressed(var5);
    068                            var5.close();
    069                            var3.readFromNBT(var6.getCompoundTag("data"));
    070                        }
    071                    }
    072                    catch (Exception var8)
    073                    {
    074                        var8.printStackTrace();
    075                    }
    076                }
    077
    078                if (var3 != null)
    079                {
    080                    this.loadedDataMap.put(par2Str, var3);
    081                    this.loadedDataList.add(var3);
    082                }
    083
    084                return var3;
    085            }
    086        }
    087
    088        /**
    089         * Assigns the given String id to the given MapDataBase, removing any existing ones of the same id.
    090         */
    091        public void setData(String par1Str, WorldSavedData par2WorldSavedData)
    092        {
    093            if (par2WorldSavedData == null)
    094            {
    095                throw new RuntimeException("Can\'t set null data");
    096            }
    097            else
    098            {
    099                if (this.loadedDataMap.containsKey(par1Str))
    100                {
    101                    this.loadedDataList.remove(this.loadedDataMap.remove(par1Str));
    102                }
    103
    104                this.loadedDataMap.put(par1Str, par2WorldSavedData);
    105                this.loadedDataList.add(par2WorldSavedData);
    106            }
    107        }
    108
    109        /**
    110         * Saves all dirty loaded MapDataBases to disk.
    111         */
    112        public void saveAllData()
    113        {
    114            Iterator var1 = this.loadedDataList.iterator();
    115
    116            while (var1.hasNext())
    117            {
    118                WorldSavedData var2 = (WorldSavedData)var1.next();
    119
    120                if (var2.isDirty())
    121                {
    122                    this.saveData(var2);
    123                    var2.setDirty(false);
    124                }
    125            }
    126        }
    127
    128        /**
    129         * Saves the given MapDataBase to disk.
    130         */
    131        private void saveData(WorldSavedData par1WorldSavedData)
    132        {
    133            if (this.saveHandler != null)
    134            {
    135                try
    136                {
    137                    File var2 = this.saveHandler.getMapFileFromName(par1WorldSavedData.mapName);
    138
    139                    if (var2 != null)
    140                    {
    141                        NBTTagCompound var3 = new NBTTagCompound();
    142                        par1WorldSavedData.writeToNBT(var3);
    143                        NBTTagCompound var4 = new NBTTagCompound();
    144                        var4.setCompoundTag("data", var3);
    145                        FileOutputStream var5 = new FileOutputStream(var2);
    146                        CompressedStreamTools.writeCompressed(var4, var5);
    147                        var5.close();
    148                    }
    149                }
    150                catch (Exception var6)
    151                {
    152                    var6.printStackTrace();
    153                }
    154            }
    155        }
    156
    157        /**
    158         * Loads the idCounts Map from the 'idcounts' file.
    159         */
    160        private void loadIdCounts()
    161        {
    162            try
    163            {
    164                this.idCounts.clear();
    165
    166                if (this.saveHandler == null)
    167                {
    168                    return;
    169                }
    170
    171                File var1 = this.saveHandler.getMapFileFromName("idcounts");
    172
    173                if (var1 != null && var1.exists())
    174                {
    175                    DataInputStream var2 = new DataInputStream(new FileInputStream(var1));
    176                    NBTTagCompound var3 = CompressedStreamTools.read(var2);
    177                    var2.close();
    178                    Iterator var4 = var3.getTags().iterator();
    179
    180                    while (var4.hasNext())
    181                    {
    182                        NBTBase var5 = (NBTBase)var4.next();
    183
    184                        if (var5 instanceof NBTTagShort)
    185                        {
    186                            NBTTagShort var6 = (NBTTagShort)var5;
    187                            String var7 = var6.getName();
    188                            short var8 = var6.data;
    189                            this.idCounts.put(var7, Short.valueOf(var8));
    190                        }
    191                    }
    192                }
    193            }
    194            catch (Exception var9)
    195            {
    196                var9.printStackTrace();
    197            }
    198        }
    199
    200        /**
    201         * Returns an unique new data id for the given prefix and saves the idCounts map to the 'idcounts' file.
    202         */
    203        public int getUniqueDataId(String par1Str)
    204        {
    205            Short var2 = (Short)this.idCounts.get(par1Str);
    206
    207            if (var2 == null)
    208            {
    209                var2 = Short.valueOf((short)0);
    210            }
    211            else
    212            {
    213                var2 = Short.valueOf((short)(var2.shortValue() + 1));
    214            }
    215
    216            this.idCounts.put(par1Str, var2);
    217
    218            if (this.saveHandler == null)
    219            {
    220                return var2.shortValue();
    221            }
    222            else
    223            {
    224                try
    225                {
    226                    File var3 = this.saveHandler.getMapFileFromName("idcounts");
    227
    228                    if (var3 != null)
    229                    {
    230                        NBTTagCompound var4 = new NBTTagCompound();
    231                        Iterator var5 = this.idCounts.keySet().iterator();
    232
    233                        while (var5.hasNext())
    234                        {
    235                            String var6 = (String)var5.next();
    236                            short var7 = ((Short)this.idCounts.get(var6)).shortValue();
    237                            var4.setShort(var6, var7);
    238                        }
    239
    240                        DataOutputStream var9 = new DataOutputStream(new FileOutputStream(var3));
    241                        CompressedStreamTools.write(var4, var9);
    242                        var9.close();
    243                    }
    244                }
    245                catch (Exception var8)
    246                {
    247                    var8.printStackTrace();
    248                }
    249
    250                return var2.shortValue();
    251            }
    252        }
    253    }