summaryrefslogtreecommitdiff
path: root/NET/worlds/core/ServerTableManager.java
blob: b7a525f2b33a57e5eb4cdd720a93a35f9ade8cdf (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package NET.worlds.core;

import NET.worlds.console.Gamma;
import NET.worlds.network.Cache;
import NET.worlds.network.CacheFile;
import NET.worlds.network.NetUpdate;
import NET.worlds.network.ProgressDialog;
import NET.worlds.network.URL;
import NET.worlds.scape.BGLoaded;
import NET.worlds.scape.BackgroundLoader;
import NET.worlds.scape.Room;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.StringReader;
import java.util.StringTokenizer;
import java.util.Vector;

public class ServerTableManager implements BGLoaded {
   private Hashtable<String, String[]> tables;
   private String tableFile;
   private int fileVersion = 0;
   public static ServerTableManager theTableManager = null;
   private static String tableStart = "private static String[] ";
   private static String version = "VERSION";

   public int getFileVersion() {
      return this.fileVersion;
   }

   public static ServerTableManager instance() {
      if (theTableManager == null) {
         theTableManager = new ServerTableManager();
      }

      return theTableManager;
   }

   public String[] getTable(String tableName) {
      Object table = this.tables.get(tableName);
      if (table == null) {
         System.out.println("Requested table " + tableName + " not found.");
      }

      return (String[])table;
   }

   private ServerTableManager() {
      this.tables = new Hashtable<String, String[]>();
      this.tableFile = IniFile.override().getIniString("ServerTableFile", "tables/tables.dat");
      if (IniFile.gamma().getIniInt("encryptTables", 0) == 1) {
         this.encryptTablesFile();
      }

      this.loadTableFile();
   }

   private boolean loadTableFile() {
      URL tableURL = URL.make(NetUpdate.getUpgradeServerURL() + this.tableFile);
      boolean syncLoad = IniFile.gamma().getIniInt("synchronousTableLoad", 1) == 1;
      if (Gamma.loadProgress != null) {
         Gamma.loadProgress.setMessage("Downloading global server information...");
         Gamma.loadProgress.advance();
      }

      if (syncLoad) {
         CacheFile cf = Cache.getFile(tableURL);
         cf.waitUntilLoaded();
         if (!cf.error()) {
            this.syncBackgroundLoad(cf.getLocalName(), null);
         }
      }

      boolean ok = this.parseFile();
      if (!syncLoad) {
         BackgroundLoader.get(this, tableURL);
      }

      return ok;
   }

   @Override
   public synchronized Object asyncBackgroundLoad(String localName, URL remoteURL) {
      return localName;
   }

   @Override
   public boolean syncBackgroundLoad(Object obj, URL remoteURL) {
      String localName = (String)obj;
      if (localName != null && new File(localName).exists()) {
         ProgressDialog.copyFile(localName, this.tableFile);
      }

      return false;
   }

   @Override
   public Room getBackgroundLoadRoom() {
      return null;
   }

   private boolean parseFile() {
      BufferedReader buf = null;

      try {
         RandomAccessFile fIn = new RandomAccessFile(this.tableFile, "r");
         byte[] encrypted = new byte[fIn.readInt()];
         fIn.readFully(encrypted);
         fIn.close();
         String decrypted = this.decrypt(encrypted);
         buf = new BufferedReader(new StringReader(decrypted));

         String line;
         while ((line = buf.readLine()) != null) {
            line = line.trim();
            if (!line.startsWith("//") && line.length() != 0) {
               if (line.startsWith(version)) {
                  StringTokenizer tok = new StringTokenizer(line);
                  String versionString = "";

                  while (tok.hasMoreTokens()) {
                     versionString = tok.nextToken();
                  }

                  if (versionString != "") {
                     this.fileVersion = Double.valueOf(versionString).intValue();
                  }
               }

               if (line.startsWith(tableStart)) {
                  String tableName = line.substring(tableStart.length());
                  StringTokenizer tok = new StringTokenizer(tableName);
                  if (tok.hasMoreTokens()) {
                     this.parseTable(buf, tok.nextToken().trim());
                  }
               }
            }
         }

         buf.close();
         return true;
      } catch (FileNotFoundException var8) {
         System.out.println(var8);
         return false;
      } catch (IOException var9) {
         System.out.println(var9);
         return false;
      }
   }

   private String stripQuotes(String in) {
      String out = new String(in);
      if (out.charAt(0) == '"') {
         out = out.substring(1);
      }

      if (out.charAt(out.length() - 1) == '"') {
         out = out.substring(0, out.length() - 1);
      }

      int idx;
      while ((idx = out.indexOf(34)) != -1) {
         out = out.substring(0, idx) + out.substring(idx + 1);
      }

      return out;
   }

   private void parseTable(BufferedReader buf, String tableName) throws IOException {
      String lastEntry = null;
      Vector<String> strings = new Vector<String>();

      String line;
      while ((line = buf.readLine()) != null) {
         line = line.trim();
         if (line.length() != 0 && !line.startsWith("//")) {
            if (line.startsWith("};")) {
               break;
            }

            StringTokenizer tok = new StringTokenizer(line, ",\n\r");

            while (tok.hasMoreTokens()) {
               String token = tok.nextToken().trim();
               if (token.startsWith("+ \"")) {
                  if (lastEntry != null) {
                     token = token.substring(2);
                     lastEntry = lastEntry + this.stripQuotes(token);
                     strings.removeElement(strings.lastElement());
                     strings.addElement(lastEntry);
                  }
               } else {
                  String entry = this.stripQuotes(token);
                  lastEntry = entry;
                  strings.addElement(entry);
               }
            }
         }
      }

      int numStrings = strings.size();
      String[] table = new String[numStrings];

      for (int i = 0; i < numStrings; i++) {
         table[i] = strings.elementAt(i);
      }

      System.out.println("Adding table " + tableName);
      this.tables.put(tableName, table);
   }

   private byte[] encrypt(String in) {
      byte[] inBytes;
      try {
         inBytes = in.getBytes("UTF8");
      } catch (Exception var6) {
         System.out.println("Error encoding to UTF8" + var6.toString());
         return null;
      }

      byte[] outBytes = new byte[inBytes.length];
      int len = inBytes.length;
      outBytes[0] = inBytes[0];

      for (int idx = 1; idx < len; idx++) {
         outBytes[idx] = (byte)(inBytes[idx] ^ outBytes[idx - 1]);
      }

      return outBytes;
   }

   private String decrypt(byte[] in) {
      byte[] outBytes = new byte[in.length];
      int len = in.length;
      outBytes[0] = in[0];

      for (int idx = 1; idx < len; idx++) {
         outBytes[idx] = (byte)(in[idx] ^ in[idx - 1]);
      }

      try {
         return new String(outBytes, "UTF8");
      } catch (Exception var6) {
         System.out.println("Error encoding UTF8 " + var6.toString());
         return null;
      }
   }

   private void encryptTablesFile() {
      try {
         System.out.println("Encoding tables file...");
         RandomAccessFile inFilth = new RandomAccessFile("..\\tables.txt", "r");
         String inStr = new String();

         while (inFilth.getFilePointer() < inFilth.length()) {
            String in = inFilth.readLine();
            System.out.println(in);
            inStr = inStr + in + "\r\n";
         }

         inFilth.close();
         byte[] encrypted = this.encrypt(inStr);
         RandomAccessFile out = new RandomAccessFile(this.tableFile, "rw");
         RandomAccessFile out2 = new RandomAccessFile("..\\tables.dat", "rw");
         out.writeInt(encrypted.length);
         out2.writeInt(encrypted.length);
         out.write(encrypted);
         out2.write(encrypted);
         out.close();
         out2.close();
         System.out.println("Tables file " + this.tableFile + " successfully written.");
      } catch (Exception var6) {
         System.out.println("Error encoding tables file: " + var6.toString());
      }
   }
}