    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            for (int var1 = 0; var1 < this.loadedDataList.size(); ++var1)
    115            {
    116                WorldSavedData var2 = (WorldSavedData)this.loadedDataList.get(var1);
    117
    118                if (var2.isDirty())
    119                {
    120                    this.saveData(var2);
    121                    var2.setDirty(false);
    122                }
    123            }
    124        }
    125
    126        /**
    127         * Saves the given MapDataBase to disk.
    128         */
    129        private void saveData(WorldSavedData par1WorldSavedData)
    130        {
    131            if (this.saveHandler != null)
    132            {
    133                try
    134                {
    135                    File var2 = this.saveHandler.getMapFileFromName(par1WorldSavedData.mapName);
    136
    137                    if (var2 != null)
    138                    {
    139                        NBTTagCompound var3 = new NBTTagCompound();
    140                        par1WorldSavedData.writeToNBT(var3);
    141                        NBTTagCompound var4 = new NBTTagCompound();
    142                        var4.setCompoundTag("data", var3);
    143                        FileOutputStream var5 = new FileOutputStream(var2);
    144                        CompressedStreamTools.writeCompressed(var4, var5);
    145                        var5.close();
    146                    }
    147                }
    148                catch (Exception var6)
    149                {
    150                    var6.printStackTrace();
    151                }
    152            }
    153        }
    154
    155        /**
    156         * Loads the idCounts Map from the 'idcounts' file.
    157         */
    158        private void loadIdCounts()
    159        {
    160            try
    161            {
    162                this.idCounts.clear();
    163
    164                if (this.saveHandler == null)
    165                {
    166                    return;
    167                }
    168
    169                File var1 = this.saveHandler.getMapFileFromName("idcounts");
    170
    171                if (var1 != null && var1.exists())
    172                {
    173                    DataInputStream var2 = new DataInputStream(new FileInputStream(var1));
    174                    NBTTagCompound var3 = CompressedStreamTools.read(var2);
    175                    var2.close();
    176                    Iterator var4 = var3.getTags().iterator();
    177
    178                    while (var4.hasNext())
    179                    {
    180                        NBTBase var5 = (NBTBase)var4.next();
    181
    182                        if (var5 instanceof NBTTagShort)
    183                        {
    184                            NBTTagShort var6 = (NBTTagShort)var5;
    185                            String var7 = var6.getName();
    186                            short var8 = var6.data;
    187                            this.idCounts.put(var7, Short.valueOf(var8));
    188                        }
    189                    }
    190                }
    191            }
    192            catch (Exception var9)
    193            {
    194                var9.printStackTrace();
    195            }
    196        }
    197
    198        /**
    199         * Returns an unique new data id for the given prefix and saves the idCounts map to the 'idcounts' file.
    200         */
    201        public int getUniqueDataId(String par1Str)
    202        {
    203            Short var2 = (Short)this.idCounts.get(par1Str);
    204
    205            if (var2 == null)
    206            {
    207                var2 = Short.valueOf((short)0);
    208            }
    209            else
    210            {
    211                var2 = Short.valueOf((short)(var2.shortValue() + 1));
    212            }
    213
    214            this.idCounts.put(par1Str, var2);
    215
    216            if (this.saveHandler == null)
    217            {
    218                return var2.shortValue();
    219            }
    220            else
    221            {
    222                try
    223                {
    224                    File var3 = this.saveHandler.getMapFileFromName("idcounts");
    225
    226                    if (var3 != null)
    227                    {
    228                        NBTTagCompound var4 = new NBTTagCompound();
    229                        Iterator var5 = this.idCounts.keySet().iterator();
    230
    231                        while (var5.hasNext())
    232                        {
    233                            String var6 = (String)var5.next();
    234                            short var7 = ((Short)this.idCounts.get(var6)).shortValue();
    235                            var4.setShort(var6, var7);
    236                        }
    237
    238                        DataOutputStream var9 = new DataOutputStream(new FileOutputStream(var3));
    239                        CompressedStreamTools.write(var4, var9);
    240                        var9.close();
    241                    }
    242                }
    243                catch (Exception var8)
    244                {
    245                    var8.printStackTrace();
    246                }
    247
    248                return var2.shortValue();
    249            }
    250        }
    251    }