    001package net.minecraft.stats;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.BufferedReader;
    006import java.io.File;
    007import java.io.FileReader;
    008import java.io.FileWriter;
    009import java.io.IOException;
    010import java.io.PrintWriter;
    011import java.util.Map;
    012import net.minecraft.util.Session;
    013
    014@SideOnly(Side.CLIENT)
    015public class StatsSyncher
    016{
    017    private volatile boolean isBusy = false;
    018    private volatile Map field_77430_b = null;
    019    private volatile Map field_77431_c = null;
    020
    021    /**
    022     * The StatFileWriter object, presumably used to write to the statistics files
    023     */
    024    private StatFileWriter statFileWriter;
    025
    026    /** A file named 'stats_' [lower case username] '_unsent.dat' */
    027    private File unsentDataFile;
    028
    029    /** A file named 'stats_' [lower case username] '.dat' */
    030    private File dataFile;
    031
    032    /** A file named 'stats_' [lower case username] '_unsent.tmp' */
    033    private File unsentTempFile;
    034
    035    /** A file named 'stats_' [lower case username] '.tmp' */
    036    private File tempFile;
    037
    038    /** A file named 'stats_' [lower case username] '_unsent.old' */
    039    private File unsentOldFile;
    040
    041    /** A file named 'stats_' [lower case username] '.old' */
    042    private File oldFile;
    043
    044    /** The Session object */
    045    private Session theSession;
    046    private int field_77433_l = 0;
    047    private int field_77434_m = 0;
    048
    049    public StatsSyncher(Session par1Session, StatFileWriter par2StatFileWriter, File par3File)
    050    {
    051        this.unsentDataFile = new File(par3File, "stats_" + par1Session.username.toLowerCase() + "_unsent.dat");
    052        this.dataFile = new File(par3File, "stats_" + par1Session.username.toLowerCase() + ".dat");
    053        this.unsentOldFile = new File(par3File, "stats_" + par1Session.username.toLowerCase() + "_unsent.old");
    054        this.oldFile = new File(par3File, "stats_" + par1Session.username.toLowerCase() + ".old");
    055        this.unsentTempFile = new File(par3File, "stats_" + par1Session.username.toLowerCase() + "_unsent.tmp");
    056        this.tempFile = new File(par3File, "stats_" + par1Session.username.toLowerCase() + ".tmp");
    057
    058        if (!par1Session.username.toLowerCase().equals(par1Session.username))
    059        {
    060            this.func_77412_a(par3File, "stats_" + par1Session.username + "_unsent.dat", this.unsentDataFile);
    061            this.func_77412_a(par3File, "stats_" + par1Session.username + ".dat", this.dataFile);
    062            this.func_77412_a(par3File, "stats_" + par1Session.username + "_unsent.old", this.unsentOldFile);
    063            this.func_77412_a(par3File, "stats_" + par1Session.username + ".old", this.oldFile);
    064            this.func_77412_a(par3File, "stats_" + par1Session.username + "_unsent.tmp", this.unsentTempFile);
    065            this.func_77412_a(par3File, "stats_" + par1Session.username + ".tmp", this.tempFile);
    066        }
    067
    068        this.statFileWriter = par2StatFileWriter;
    069        this.theSession = par1Session;
    070
    071        if (this.unsentDataFile.exists())
    072        {
    073            par2StatFileWriter.writeStats(this.func_77417_a(this.unsentDataFile, this.unsentTempFile, this.unsentOldFile));
    074        }
    075
    076        this.beginReceiveStats();
    077    }
    078
    079    private void func_77412_a(File par1File, String par2Str, File par3File)
    080    {
    081        File file3 = new File(par1File, par2Str);
    082
    083        if (file3.exists() && !file3.isDirectory() && !par3File.exists())
    084        {
    085            file3.renameTo(par3File);
    086        }
    087    }
    088
    089    private Map func_77417_a(File par1File, File par2File, File par3File)
    090    {
    091        return par1File.exists() ? this.func_77413_a(par1File) : (par3File.exists() ? this.func_77413_a(par3File) : (par2File.exists() ? this.func_77413_a(par2File) : null));
    092    }
    093
    094    private Map func_77413_a(File par1File)
    095    {
    096        BufferedReader bufferedreader = null;
    097
    098        try
    099        {
    100            bufferedreader = new BufferedReader(new FileReader(par1File));
    101            String s = "";
    102            StringBuilder stringbuilder = new StringBuilder();
    103
    104            while ((s = bufferedreader.readLine()) != null)
    105            {
    106                stringbuilder.append(s);
    107            }
    108
    109            Map map = StatFileWriter.func_77453_b(stringbuilder.toString());
    110            return map;
    111        }
    112        catch (Exception exception)
    113        {
    114            exception.printStackTrace();
    115        }
    116        finally
    117        {
    118            if (bufferedreader != null)
    119            {
    120                try
    121                {
    122                    bufferedreader.close();
    123                }
    124                catch (Exception exception1)
    125                {
    126                    exception1.printStackTrace();
    127                }
    128            }
    129        }
    130
    131        return null;
    132    }
    133
    134    private void func_77421_a(Map par1Map, File par2File, File par3File, File par4File) throws IOException
    135    {
    136        PrintWriter printwriter = new PrintWriter(new FileWriter(par3File, false));
    137
    138        try
    139        {
    140            printwriter.print(StatFileWriter.func_77441_a(this.theSession.username, "local", par1Map));
    141        }
    142        finally
    143        {
    144            printwriter.close();
    145        }
    146
    147        if (par4File.exists())
    148        {
    149            par4File.delete();
    150        }
    151
    152        if (par2File.exists())
    153        {
    154            par2File.renameTo(par4File);
    155        }
    156
    157        par3File.renameTo(par2File);
    158    }
    159
    160    /**
    161     * Attempts to begin receiving stats from the server. Will throw an IllegalStateException if the syncher is already
    162     * busy.
    163     */
    164    public void beginReceiveStats()
    165    {
    166        if (this.isBusy)
    167        {
    168            throw new IllegalStateException("Can\'t get stats from server while StatsSyncher is busy!");
    169        }
    170        else
    171        {
    172            this.field_77433_l = 100;
    173            this.isBusy = true;
    174            (new ThreadStatSyncherReceive(this)).start();
    175        }
    176    }
    177
    178    /**
    179     * Attempts to begin sending stats to the server. Will throw an IllegalStateException if the syncher is already
    180     * busy.
    181     */
    182    public void beginSendStats(Map par1Map)
    183    {
    184        if (this.isBusy)
    185        {
    186            throw new IllegalStateException("Can\'t save stats while StatsSyncher is busy!");
    187        }
    188        else
    189        {
    190            this.field_77433_l = 100;
    191            this.isBusy = true;
    192            (new ThreadStatSyncherSend(this, par1Map)).start();
    193        }
    194    }
    195
    196    public void syncStatsFileWithMap(Map par1Map)
    197    {
    198        int i = 30;
    199
    200        while (this.isBusy)
    201        {
    202            --i;
    203
    204            if (i <= 0)
    205            {
    206                break;
    207            }
    208
    209            try
    210            {
    211                Thread.sleep(100L);
    212            }
    213            catch (InterruptedException interruptedexception)
    214            {
    215                interruptedexception.printStackTrace();
    216            }
    217        }
    218
    219        this.isBusy = true;
    220
    221        try
    222        {
    223            this.func_77421_a(par1Map, this.unsentDataFile, this.unsentTempFile, this.unsentOldFile);
    224        }
    225        catch (Exception exception)
    226        {
    227            exception.printStackTrace();
    228        }
    229        finally
    230        {
    231            this.isBusy = false;
    232        }
    233    }
    234
    235    public boolean func_77425_c()
    236    {
    237        return this.field_77433_l <= 0 && !this.isBusy && this.field_77431_c == null;
    238    }
    239
    240    public void func_77422_e()
    241    {
    242        if (this.field_77433_l > 0)
    243        {
    244            --this.field_77433_l;
    245        }
    246
    247        if (this.field_77434_m > 0)
    248        {
    249            --this.field_77434_m;
    250        }
    251
    252        if (this.field_77431_c != null)
    253        {
    254            this.statFileWriter.func_77448_c(this.field_77431_c);
    255            this.field_77431_c = null;
    256        }
    257
    258        if (this.field_77430_b != null)
    259        {
    260            this.statFileWriter.func_77452_b(this.field_77430_b);
    261            this.field_77430_b = null;
    262        }
    263    }
    264
    265    static Map func_77419_a(StatsSyncher par0StatsSyncher)
    266    {
    267        return par0StatsSyncher.field_77430_b;
    268    }
    269
    270    static File func_77408_b(StatsSyncher par0StatsSyncher)
    271    {
    272        return par0StatsSyncher.dataFile;
    273    }
    274
    275    static File func_77407_c(StatsSyncher par0StatsSyncher)
    276    {
    277        return par0StatsSyncher.tempFile;
    278    }
    279
    280    static File func_77411_d(StatsSyncher par0StatsSyncher)
    281    {
    282        return par0StatsSyncher.oldFile;
    283    }
    284
    285    static void func_77414_a(StatsSyncher par0StatsSyncher, Map par1Map, File par2File, File par3File, File par4File) throws IOException
    286    {
    287        par0StatsSyncher.func_77421_a(par1Map, par2File, par3File, par4File);
    288    }
    289
    290    static Map func_77416_a(StatsSyncher par0StatsSyncher, Map par1Map)
    291    {
    292        return par0StatsSyncher.field_77430_b = par1Map;
    293    }
    294
    295    static Map func_77410_a(StatsSyncher par0StatsSyncher, File par1File, File par2File, File par3File)
    296    {
    297        return par0StatsSyncher.func_77417_a(par1File, par2File, par3File);
    298    }
    299
    300    static boolean setBusy(StatsSyncher par0StatsSyncher, boolean par1)
    301    {
    302        return par0StatsSyncher.isBusy = par1;
    303    }
    304
    305    static File getUnsentDataFile(StatsSyncher par0StatsSyncher)
    306    {
    307        return par0StatsSyncher.unsentDataFile;
    308    }
    309
    310    static File getUnsentTempFile(StatsSyncher par0StatsSyncher)
    311    {
    312        return par0StatsSyncher.unsentTempFile;
    313    }
    314
    315    static File getUnsentOldFile(StatsSyncher par0StatsSyncher)
    316    {
    317        return par0StatsSyncher.unsentOldFile;
    318    }
    319}