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