· 10 years ago · Jan 28, 2016, 11:21 AM
1package fr.ketlark77.main;
2
3import java.awt.AlphaComposite;
4import java.awt.Color;
5import java.awt.Desktop;
6import java.awt.Font;
7import java.awt.FontFormatException;
8import java.awt.Graphics;
9import java.awt.Graphics2D;
10import java.awt.Image;
11import java.awt.Point;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14import java.awt.event.KeyEvent;
15import java.awt.event.KeyListener;
16import java.awt.event.MouseAdapter;
17import java.awt.event.MouseEvent;
18import java.awt.event.MouseMotionAdapter;
19import java.io.BufferedReader;
20import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.FileOutputStream;
25import java.io.FileReader;
26import java.io.FileWriter;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.InputStreamReader;
30import java.net.HttpURLConnection;
31import java.net.InetAddress;
32import java.net.MalformedURLException;
33import java.net.NetworkInterface;
34import java.net.SocketException;
35import java.net.URI;
36import java.net.URL;
37import java.net.UnknownHostException;
38import java.util.Random;
39
40import javax.crypto.Cipher;
41import javax.crypto.CipherInputStream;
42import javax.crypto.CipherOutputStream;
43import javax.crypto.SecretKey;
44import javax.crypto.SecretKeyFactory;
45import javax.crypto.spec.PBEKeySpec;
46import javax.crypto.spec.PBEParameterSpec;
47import javax.imageio.ImageIO;
48import javax.swing.Icon;
49import javax.swing.ImageIcon;
50import javax.swing.JCheckBox;
51import javax.swing.JLabel;
52import javax.swing.JOptionPane;
53import javax.swing.JPanel;
54import javax.swing.JPasswordField;
55import javax.swing.JTextField;
56import javax.swing.SwingConstants;
57import javax.swing.Timer;
58
59import fr.theshark34.supdate.BarAPI;
60import fr.theshark34.supdate.SUpdate;
61import fr.theshark34.supdate.application.integrated.FileDeleter;
62import fr.theshark34.supdate.exception.BadServerResponseException;
63import fr.theshark34.supdate.exception.BadServerVersionException;
64import fr.theshark34.supdate.exception.ServerDisabledException;
65import fr.theshark34.supdate.exception.ServerMissingSomethingException;
66
67public class Panel
68 extends JPanel
69 implements ActionListener, KeyListener
70{
71 private Point initialClick;
72 public static final long serialVersionUID = 1L;
73 public Image bg;
74 public Image field;
75 public JTextField user = new JTextField();
76 boolean showError;
77 String errorMSG;
78 public JPasswordField password = new JPasswordField();
79
80 public static String ram_value = "512M";
81
82 public ImgButton play = new ImgButton(this, 350, 420, 130, 42, new String[] {
83 "/images/play_1.png",
84 "/images/play.png" });
85
86 public ImgButton youtube = new ImgButton(this, 25, 25, 47, 47, new String[] {
87 "/images/youtube_1.png",
88 "/images/youtube.png" });
89
90 public ImgButton teamspeak = new ImgButton(this, 85, 25, 47, 47, new String[] {
91 "/images/teamspeak_1.png",
92 "/images/teamspeak.png" });
93
94 public ImgButton quit = new ImgButton(this, 790, 25, 47, 47, new String[] {
95 "/images/close_1.png",
96 "/images/close.png" });
97
98 public ImgButton reduce = new ImgButton(this, 730, 25, 47, 47, new String[] {
99 "/images/reduce_1.png",
100 "/images/reduce.png" });
101
102
103 public static StyledBar bar;
104 public String displayLoad;
105 public static JLabel state;
106
107 public Panel p;
108 public static Home home;
109 public ThreadUpdate thread = new ThreadUpdate(this);
110
111 //Ajout Vharion
112
113 public Icon checked = new ImgButtonCheckbox();
114 public Icon unchecked = new ImgButtonCheckbox();
115
116 public JCheckBox checkbox = new JCheckBox("", unchecked);
117
118 private Options options;
119
120 public static ConsoleTab console;
121
122 private Font font = null;
123 private InputStream inputstream = null;
124
125
126 public Panel(final Home home) throws FontFormatException, IOException
127 {
128
129 addMouseListener(new MouseAdapter()
130 {
131 public void mousePressed(MouseEvent e)
132 {
133 Panel.this.initialClick = e.getPoint();
134 Panel.this.getComponentAt(Panel.this.initialClick);
135 }
136 });
137 addMouseMotionListener(new MouseMotionAdapter()
138 {
139 public void mouseDragged(MouseEvent e)
140 {
141 int thisX = home.getLocation().x;
142 int thisY = home.getLocation().y;
143
144
145 int xMoved = thisX + e.getX() - (thisX + Panel.this.initialClick.x);
146 int yMoved = thisY + e.getY() - (thisY + Panel.this.initialClick.y);
147
148
149 int X = thisX + xMoved;
150 int Y = thisY + yMoved;
151 home.setLocation(X, Y);
152 }
153 });
154 Panel.home = home;
155
156 try
157 {
158 this.bg = ImageIO.read(Panel.class.getResource("/images/background.png"));
159 }
160 catch (IOException e)
161 {
162 e.printStackTrace();
163 }
164 setBounds(0, 0, 850, 500);
165
166 this.inputstream = getClass().getResourceAsStream("police.ttf");
167 this.font = Font.createFont(0, this.inputstream).deriveFont(0, 21.0F);
168
169 setLayout(null);
170 this.play.addActionListener(this);
171 this.youtube.addActionListener(this);
172 this.teamspeak.addActionListener(this);
173
174 this.quit.addActionListener(this);
175 this.reduce.addActionListener(this);
176
177 this.user.addKeyListener(this);
178 this.user.setFocusTraversalKeysEnabled(true);
179
180 this.user.setBorder(null);
181 this.user.setFont(new Font("Tahoma", 1, 19));
182 this.user.setBackground(new Color(0, 0, 0, 0));
183 this.user.setBounds(295, 358, 240, 25);
184 this.user.setForeground(Color.BLACK);
185 this.user.setHorizontalAlignment(0);
186
187 Panel.state = new JLabel("", SwingConstants.CENTER);
188 Panel.state.setBounds(500, 530, 300, 70);
189 Panel.state.setOpaque(false);
190 Panel.state.setFont(new Font("arial", 0, 11));
191 Panel.state.setForeground(Color.WHITE);
192
193 add(Panel.state);
194
195 Integer[] pos = {0, 555};
196
197 Panel.bar = new StyledBar(1282);
198 Panel.bar.setBounds(pos[0], pos[1], 0, 21);
199 Panel.bar.setDoubleBuffered(true);
200 Panel.bar.setOpaque(false);
201 ImageIcon imgM = new ImageIcon(Panel.class.getResource("/images/progress.png"));
202 Panel.bar.setIcon(imgM);
203
204 add(Panel.bar);
205
206 add(this.user);
207 add(this.password);
208 add(this.play);
209 add(this.youtube);
210 add(this.teamspeak);
211 add(this.quit);
212 add(this.reduce);
213
214 add(this.checkbox);
215
216 setBackground(new Color(0, 150, 0, 0));
217 readLastLogin();
218 readOptions();
219 setVisible(true);
220
221 timer_in.start();
222
223 if(console == null)
224 {
225 console = new ConsoleTab("Eryzia Launcher - Console");
226 console.setVisible(false);
227 console.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
228 console.println("|| Infos Système ||");
229 console.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
230 console.println("");
231 console.println(new StringBuilder().append("OS Name == '").append(System.getProperty("os.name")).append("'").toString());
232 console.println(new StringBuilder().append("OS Version == '").append(System.getProperty("os.version")).append("'").toString());
233 console.println(new StringBuilder().append("OS Arch == '").append(System.getProperty("os.arch")).append("'").toString());
234 console.println(new StringBuilder().append("Java Version == '").append(System.getProperty("java.version")).append("'").toString());
235 console.println(new StringBuilder().append("Java Vendor == '").append(System.getProperty("java.vendor")).append("'").toString());
236 console.println(new StringBuilder().append("Java Arch == '").append(System.getProperty("sun.arch.data.model")).append("'").toString());
237 console.println("");
238 console.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
239 console.println("");
240 console.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
241 console.println("");
242 }
243
244 }
245
246
247 public void paintComponent(Graphics g)
248 {
249 ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha_in));
250 g.setColor(Color.WHITE);
251 g.drawImage(this.bg, 0, 0, 850, 500, this);
252 // g.drawImage(this.field, 20, 45, 169, 90, this);
253
254 if (this.showError)
255 {
256 g.setColor(Color.red);
257 g.drawString(this.errorMSG, 220, 125);
258 }
259 /* else if (this.displayString != null)
260 {
261 g.setColor(Color.black);
262 g.drawString(this.displayString, 220, 125);
263 }*/
264 }
265
266 private float alpha_in = 0F;
267
268 final Timer timer_in = new Timer(10, new ActionListener() {
269 public void actionPerformed(ActionEvent e) {
270
271 alpha_in += 0.01F;
272 if (alpha_in >= 1)
273 {
274 alpha_in = 1;
275 timer_in.stop();
276 }
277 repaint();
278 }
279 });
280
281 public void actionPerformed(ActionEvent e)
282 {
283 if (e.getSource() == this.play) {
284 play();
285
286 }
287
288 else if (e.getSource() == this.quit)
289 {
290 System.exit(0);
291 }
292
293 else if (e.getSource() == this.reduce)
294 {
295 home.setState(1);;
296 }
297
298
299 else if (e.getSource() == this.teamspeak)
300 {
301 try
302 {
303 URI u = URI.create("ts3server://eryziats.cloudcraft.fr");
304 Desktop.getDesktop().browse(u);
305 } catch (IOException ex) {
306 ex.printStackTrace();
307 }
308 }
309 }
310
311 public void refresh()
312 {
313 Panel.home.repaint();
314 }
315
316
317 void play()
318 {
319 if(this.user.getText().length() > 0)
320 {
321 saveLastLogin();
322
323 this.user.setEnabled(false);
324 this.password.setEnabled(false);
325
326 Thread t = new Thread() {
327 public void run() {
328
329 SUpdate su = new SUpdate("http://eryzia.franceserv.fr/launcher/", Util.getWorkingDirectory());
330 su.addApplication(new FileDeleter());
331 su.getServerRequester().setRewriteEnabled(true);
332
333 state.setText("Recherche de fichiers à mettre à jour ...");
334 Panel.console.println("Recherche de fichiers à mettre à jour ...");
335
336 Thread t = new Thread() {
337 @Override
338 public void run() {
339 while (!this.isInterrupted()) {
340 float percent = (float) ((double) BarAPI.getNumberOfTotalDownloadedBytes() / (double) BarAPI.getNumberOfTotalBytesToDownload());
341 bar.setPourcent(percent);
342
343 if (BarAPI.getNumberOfTotalBytesToDownload() != 0) {
344 state.setText(String.valueOf(BarAPI.getNumberOfTotalDownloadedBytes()/ 1024 + " / " + BarAPI.getNumberOfTotalBytesToDownload() / 1024 + " Ko"));
345 }
346 }
347 }
348 };
349 t.start();
350
351 try {
352 su.start();
353 Panel.console.println("Téléchargement des fichiers en cours ...");
354 } catch (BadServerResponseException | ServerDisabledException
355 | BadServerVersionException
356 | ServerMissingSomethingException | IOException e) {
357 e.printStackTrace();
358 }
359
360 while(true)
361 {
362 new LaunchGame(user.getText(), p);
363 boolean launchSuccess = LaunchGame.launchGame(user.getText());
364
365 if (launchSuccess) {
366 if (console.isVisible()) {
367 state.setText("Jeu lancé");
368 boolean keepRunning = true;
369
370 while (keepRunning) {
371 try {
372
373 Thread.sleep(1000);
374
375 } catch (Exception e) {
376 keepRunning = false;
377 }
378 }
379
380 } else {
381 state.setText("Jeu lancé");
382 System.exit(0);
383 }
384
385
386
387 } else {
388 state.setText("Le lancement du jeu a échoué, veuillez réessayer en mettant à jour");
389 }
390 }
391
392 }
393 };
394 t.start();
395
396
397 }
398 else
399 {
400 JOptionPane.showMessageDialog(this, "Veuillez rentrez votre utilisateur", "Identifiants manquants", 0);
401 }
402
403 }
404
405 public void keyPressed(KeyEvent e)
406 {
407 if (e.getKeyCode() == 10) {
408 play();
409 }
410 }
411
412 public void keyReleased(KeyEvent e) {}
413
414 public void keyTyped(KeyEvent arg0) {}
415
416 private void readLastLogin()
417 {
418 File lastLogin = new File(Util.getWorkingDirectory() +
419 File.separator + "lastlogin");
420 if (!lastLogin.exists()) {
421 try
422 {
423 lastLogin.createNewFile();
424 }
425 catch (IOException e)
426 {
427 e.printStackTrace();
428 }
429 }
430 try
431 {
432 Cipher ciph = openCipher(2);
433 DataInputStream dis = new DataInputStream(
434 new CipherInputStream(new FileInputStream(lastLogin), ciph));
435
436 this.user.setText(dis.readUTF());
437 this.password.setText(dis.readUTF());
438 this.checkbox.setSelected(this.password.getPassword().length > 0);
439
440 dis.close();
441 }
442 catch (Exception e)
443 {
444 System.out.println("Failed to read lastLogin file");
445 e.printStackTrace();
446 }
447 }
448
449 private void readOptions()
450 {
451 File last = new File(Util.getWorkingDirectory(), "options_launch.txt");
452 if (last.exists())
453 {
454 BufferedReader br = null;
455 try
456 {
457 br = new BufferedReader(new FileReader(last));
458 }
459 catch (Exception e)
460 {
461 e.printStackTrace();
462 }
463 try
464 {
465 String resp = br.readLine();
466
467 Panel.ram_value = resp;
468
469 }
470 catch (Exception e)
471 {
472 e.printStackTrace();
473 try
474 {
475 br.close();
476 }
477 catch (IOException e1)
478 {
479 e1.printStackTrace();
480 }
481 }
482 finally
483 {
484 try
485 {
486 br.close();
487 }
488 catch (IOException e)
489 {
490 e.printStackTrace();
491 }
492 }
493 }
494 else
495 {
496 try {
497 last.createNewFile();
498 FileWriter ffw = new FileWriter(last);
499 ffw.write("512M");
500 ffw.close();
501
502 } catch (IOException e) {
503 e.printStackTrace();
504 }
505 }
506 }
507
508
509 private void saveLastLogin()
510 {
511 try
512 {
513 File lastLogin = new File(Util.getWorkingDirectory() +
514 File.separator + "lastlogin");
515
516 Cipher ciph = openCipher(1);
517 DataOutputStream dos = new DataOutputStream(new CipherOutputStream(new FileOutputStream(lastLogin), ciph));
518
519 dos.writeUTF(this.user.getText());
520 dos.writeUTF(this.checkbox.isSelected() ? new String(this.password.getText()) : "");
521 dos.close();
522 }
523 catch (Exception e)
524 {
525 System.out.println("Failed to save lastLogin file");
526 e.printStackTrace();
527 }
528 }
529
530
531 private Cipher openCipher(int mode) throws Exception
532 {
533 Random rnd = new Random(43287234L);
534 byte[] data = new byte[8];
535
536 rnd.nextBytes(data);
537 PBEParameterSpec spec = new PBEParameterSpec(data, 5);
538 SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec("passwordEryzia".toCharArray()));
539
540 Cipher ret = Cipher.getInstance("PBEWithMD5AndDES");
541 ret.init(mode, key, spec);
542 return ret;
543 }
544
545 public Options getOptions()
546 {
547 return this.options;
548 }
549
550 public static String getHTML(String urlToRead)
551 {
552 String result = "";
553 try {
554 URL url = new URL(urlToRead);
555 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
556 conn.setRequestMethod("GET");
557 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
558 String line;
559 while ((line = rd.readLine()) != null) {
560 result = result + line;
561 }
562 rd.close();
563 } catch (Exception e) {
564 System.out.println("error while reading");
565 return "";
566 }
567 return result;
568 }
569}