summaryrefslogtreecommitdiff
path: root/NET/worlds/console/LanguageManager.java
blob: c8fe07295d4433daddbb86289de0c05246d683af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package NET.worlds.console;

import NET.worlds.core.Std;
import NET.worlds.network.ProgressDialog;
import NET.worlds.network.URL;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class LanguageManager implements Runnable {
   private String m_Name;
   private String m_FinishName;
   private int m_fSize;

   public static void handle(String name, int fSize, String finishName) {
      LanguageManager lm = new LanguageManager(name, fSize, finishName);
      Thread t = new Thread(lm);
      t.setDaemon(true);
      t.start();
   }

   public LanguageManager(String name, int fSize, String finishName) {
      this.m_Name = name;
      this.m_fSize = fSize;
      this.m_FinishName = finishName;
   }

   @Override
   public void run() {
      String fName = this.m_Name.substring(this.m_Name.lastIndexOf(47) + 1, this.m_Name.length());
      Console.println(Console.message("Downloading-update") + fName);
      Vector<String> names = new Vector<String>();
      names.addElement(fName);
      Vector<URL> urls = new Vector<URL>();
      urls.addElement(URL.make(this.m_Name));
      int now = Std.getFastTime();
      ProgressDialog progD = new ProgressDialog(this.m_fSize);

      try {
         System.out.println("ProgressDialog start, now = " + now);
         System.out.println("fName = " + fName + ", name = " + this.m_Name);
         if (!progD.loadFiles(names, urls)) {
            System.out.println("Can't load " + fName);
            return;
         }

         now = Std.getFastTime();
         System.out.println("ProgressDialog end, now = " + now);
      } catch (IOException var7) {
         System.out.println("Exception " + var7.toString() + " loading file " + fName);
      }

      this.finishDownload(this.m_FinishName);
   }

   public synchronized boolean finishDownload(String localName) {
      String fType = localName.substring(localName.lastIndexOf(46), localName.length());
      System.out.println("Finished Downloading " + localName);
      if (fType.equals(".EXE")) {
         if (tryToRun(localName)) {
            System.out.println(Console.message("Loading2") + localName);
         } else {
            Console.println(Console.message("Loading2") + localName + " " + Console.message("failed"));
         }
      } else if (fType.equals(".zip")) {
         ZipFile langZip;
         try {
            langZip = new ZipFile(localName);
         } catch (IOException var12) {
            System.out.println("Error opening language file " + localName);
            this.notify();
            return false;
         }

         System.out.println("Expanding language file.");
         Enumeration<? extends ZipEntry> enums = langZip.entries();

         while (enums.hasMoreElements()) {
            ZipEntry ze = enums.nextElement();
            String filename = ze.getName();
            String langFile = filename;
            String[] validExtensions = new String[]{".properties", ".jpg", ".bmp", ".gif"};
            boolean validFile = false;

            for (int idx = 0; idx < validExtensions.length; idx++) {
               if (langFile.toString().endsWith(validExtensions[idx])) {
                  validFile = true;
                  break;
               }
            }

            if (validFile) {
               this.CopyLangFile(langZip, ze, langFile);
               System.out.println(langFile);
            }
         }

         try {
            langZip.close();
         } catch (IOException var11) {
         }
      }

      this.notify();
      new OkCancelDialog(Console.getFrame(), null, Console.message("Alert"), null, Console.message("OK"), Console.message("Change-exit"), true);
      return false;
   }

   private void CopyLangFile(ZipFile langZip, ZipEntry ze, String langFile) {
      InputStream is;
      try {
         is = langZip.getInputStream(ze);
      } catch (IOException var11) {
         return;
      }

      FileOutputStream os;
      try {
         os = new FileOutputStream(langFile);
      } catch (IOException var10) {
         try {
            is.close();
         } catch (IOException var8) {
         }

         return;
      }

      byte[] buffer = new byte[1024];

      while (true) {
         try {
            int bytesRead = is.read(buffer);
            if (bytesRead == -1) {
               break;
            }

            os.write(buffer, 0, bytesRead);
         } catch (IOException var12) {
            break;
         }
      }

      try {
         is.close();
         os.close();
      } catch (IOException var9) {
      }
   }

   public static boolean tryToRun(String s) {
      try {
         Runtime.getRuntime().exec(s);
         return true;
      } catch (IOException var2) {
         return false;
      }
   }
}