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