    001    package net.minecraft.stats;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.io.BufferedReader;
    006    import java.io.File;
    007    import java.io.FileReader;
    008    import java.io.FileWriter;
    009    import java.io.IOException;
    010    import java.io.PrintWriter;
    011    import java.util.Map;
    012    import net.minecraft.util.Session;
    013
    014    @SideOnly(Side.CLIENT)
    015    public 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 var4 = new File(par1File, par2Str);
    082
    083            if (var4.exists() && !var4.isDirectory() && !par3File.exists())
    084            {
    085                var4.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 var2 = null;
    097
    098            try
    099            {
    100                var2 = new BufferedReader(new FileReader(par1File));
    101                String var3 = "";
    102                StringBuilder var4 = new StringBuilder();
    103
    104                while ((var3 = var2.readLine()) != null)
    105                {
    106                    var4.append(var3);
    107                }
    108
    109                Map var5 = StatFileWriter.func_77453_b(var4.toString());
    110                return var5;
    111            }
    112            catch (Exception var15)
    113            {
    114                var15.printStackTrace();
    115            }
    116            finally
    117            {
    118                if (var2 != null)
    119                {
    120                    try
    121                    {
    122                        var2.close();
    123                    }
    124                    catch (Exception var14)
    125                    {
    126                        var14.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 var5 = new PrintWriter(new FileWriter(par3File, false));
    137
    138            try
    139            {
    140                var5.print(StatFileWriter.func_77441_a(this.theSession.username, "local", par1Map));
    141            }
    142            finally
    143            {
    144                var5.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 var2 = 30;
    199
    200            while (this.isBusy)
    201            {
    202                --var2;
    203
    204                if (var2 <= 0)
    205                {
    206                    break;
    207                }
    208
    209                try
    210                {
    211                    Thread.sleep(100L);
    212                }
    213                catch (InterruptedException var10)
    214                {
    215                    var10.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 var8)
    226            {
    227                var8.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    }