001package net.minecraft.world.storage;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.io.File;
006import java.io.FileInputStream;
007import java.io.FileOutputStream;
008import java.util.ArrayList;
009import java.util.List;
010import net.minecraft.client.AnvilConverterException;
011import net.minecraft.nbt.CompressedStreamTools;
012import net.minecraft.nbt.NBTTagCompound;
013import net.minecraft.util.IProgressUpdate;
014
015public class SaveFormatOld implements ISaveFormat
016{
017    /**
018     * Reference to the File object representing the directory for the world saves
019     */
020    protected final File savesDirectory;
021
022    public SaveFormatOld(File par1File)
023    {
024        if (!par1File.exists())
025        {
026            par1File.mkdirs();
027        }
028
029        this.savesDirectory = par1File;
030    }
031
032    @SideOnly(Side.CLIENT)
033    public List getSaveList() throws AnvilConverterException
034    {
035        ArrayList arraylist = new ArrayList();
036
037        for (int i = 0; i < 5; ++i)
038        {
039            String s = "World" + (i + 1);
040            WorldInfo worldinfo = this.getWorldInfo(s);
041
042            if (worldinfo != null)
043            {
044                arraylist.add(new SaveFormatComparator(s, "", worldinfo.getLastTimePlayed(), worldinfo.getSizeOnDisk(), worldinfo.getGameType(), false, worldinfo.isHardcoreModeEnabled(), worldinfo.areCommandsAllowed()));
045            }
046        }
047
048        return arraylist;
049    }
050
051    public void flushCache() {}
052
053    /**
054     * gets the world info
055     */
056    public WorldInfo getWorldInfo(String par1Str)
057    {
058        File file1 = new File(this.savesDirectory, par1Str);
059
060        if (!file1.exists())
061        {
062            return null;
063        }
064        else
065        {
066            File file2 = new File(file1, "level.dat");
067            NBTTagCompound nbttagcompound;
068            NBTTagCompound nbttagcompound1;
069
070            if (file2.exists())
071            {
072                try
073                {
074                    nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(file2));
075                    nbttagcompound1 = nbttagcompound.getCompoundTag("Data");
076                    return new WorldInfo(nbttagcompound1);
077                }
078                catch (Exception exception)
079                {
080                    exception.printStackTrace();
081                }
082            }
083
084            file2 = new File(file1, "level.dat_old");
085
086            if (file2.exists())
087            {
088                try
089                {
090                    nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(file2));
091                    nbttagcompound1 = nbttagcompound.getCompoundTag("Data");
092                    return new WorldInfo(nbttagcompound1);
093                }
094                catch (Exception exception1)
095                {
096                    exception1.printStackTrace();
097                }
098            }
099
100            return null;
101        }
102    }
103
104    @SideOnly(Side.CLIENT)
105
106    /**
107     * @args: Takes two arguments - first the name of the directory containing the world and second the new name for
108     * that world. @desc: Renames the world by storing the new name in level.dat. It does *not* rename the directory
109     * containing the world data.
110     */
111    public void renameWorld(String par1Str, String par2Str)
112    {
113        File file1 = new File(this.savesDirectory, par1Str);
114
115        if (file1.exists())
116        {
117            File file2 = new File(file1, "level.dat");
118
119            if (file2.exists())
120            {
121                try
122                {
123                    NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(file2));
124                    NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("Data");
125                    nbttagcompound1.setString("LevelName", par2Str);
126                    CompressedStreamTools.writeCompressed(nbttagcompound, new FileOutputStream(file2));
127                }
128                catch (Exception exception)
129                {
130                    exception.printStackTrace();
131                }
132            }
133        }
134    }
135
136    /**
137     * @args: Takes one argument - the name of the directory of the world to delete. @desc: Delete the world by deleting
138     * the associated directory recursively.
139     */
140    public boolean deleteWorldDirectory(String par1Str)
141    {
142        File file1 = new File(this.savesDirectory, par1Str);
143
144        if (!file1.exists())
145        {
146            return true;
147        }
148        else
149        {
150            System.out.println("Deleting level " + par1Str);
151
152            for (int i = 1; i <= 5; ++i)
153            {
154                System.out.println("Attempt " + i + "...");
155
156                if (deleteFiles(file1.listFiles()))
157                {
158                    break;
159                }
160
161                System.out.println("Unsuccessful in deleting contents.");
162
163                if (i < 5)
164                {
165                    try
166                    {
167                        Thread.sleep(500L);
168                    }
169                    catch (InterruptedException interruptedexception)
170                    {
171                        ;
172                    }
173                }
174            }
175
176            return file1.delete();
177        }
178    }
179
180    /**
181     * @args: Takes one argument - the list of files and directories to delete. @desc: Deletes the files and directory
182     * listed in the list recursively.
183     */
184    protected static boolean deleteFiles(File[] par0ArrayOfFile)
185    {
186        for (int i = 0; i < par0ArrayOfFile.length; ++i)
187        {
188            File file1 = par0ArrayOfFile[i];
189            System.out.println("Deleting " + file1);
190
191            if (file1.isDirectory() && !deleteFiles(file1.listFiles()))
192            {
193                System.out.println("Couldn\'t delete directory " + file1);
194                return false;
195            }
196
197            if (!file1.delete())
198            {
199                System.out.println("Couldn\'t delete file " + file1);
200                return false;
201            }
202        }
203
204        return true;
205    }
206
207    /**
208     * Returns back a loader for the specified save directory
209     */
210    public ISaveHandler getSaveLoader(String par1Str, boolean par2)
211    {
212        return new SaveHandler(this.savesDirectory, par1Str, par2);
213    }
214
215    /**
216     * Checks if the save directory uses the old map format
217     */
218    public boolean isOldMapFormat(String par1Str)
219    {
220        return false;
221    }
222
223    /**
224     * Converts the specified map to the new map format. Args: worldName, loadingScreen
225     */
226    public boolean convertMapFormat(String par1Str, IProgressUpdate par2IProgressUpdate)
227    {
228        return false;
229    }
230
231    @SideOnly(Side.CLIENT)
232
233    /**
234     * Return whether the given world can be loaded.
235     */
236    public boolean canLoadWorld(String par1Str)
237    {
238        File file1 = new File(this.savesDirectory, par1Str);
239        return file1.isDirectory();
240    }
241}