From e1e781bb2135ef78592226f1a3eaba4925702f1f Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 3 May 2021 16:38:41 -0700 Subject: :star: --- NET/worlds/core/ServerTableManager.java | 336 ++++++++++++++++++++++++++++++++ 1 file changed, 336 insertions(+) create mode 100644 NET/worlds/core/ServerTableManager.java (limited to 'NET/worlds/core/ServerTableManager.java') diff --git a/NET/worlds/core/ServerTableManager.java b/NET/worlds/core/ServerTableManager.java new file mode 100644 index 0000000..b118755 --- /dev/null +++ b/NET/worlds/core/ServerTableManager.java @@ -0,0 +1,336 @@ +/* */ package NET.worlds.core; +/* */ +/* */ import NET.worlds.console.Gamma; +/* */ import NET.worlds.console.ProgressBar; +/* */ 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.PrintStream; +/* */ import java.io.RandomAccessFile; +/* */ import java.io.StringReader; +/* */ import java.util.StringTokenizer; +/* */ import java.util.Vector; +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ public class ServerTableManager +/* */ implements BGLoaded +/* */ { +/* */ private Hashtable tables; +/* */ private String tableFile; +/* 36 */ private int fileVersion = 0; +/* */ +/* */ public int getFileVersion() { +/* 39 */ return this.fileVersion; +/* */ } +/* */ +/* 42 */ public static ServerTableManager theTableManager = null; +/* */ +/* */ public static ServerTableManager instance() { +/* 45 */ if (theTableManager == null) +/* 46 */ theTableManager = new ServerTableManager(); +/* 47 */ return theTableManager; +/* */ } +/* */ +/* */ public String[] getTable(String tableName) { +/* 51 */ Object table = this.tables.get(tableName); +/* */ +/* 53 */ if (table == null) +/* 54 */ System.out.println("Requested table " + tableName + " not found."); +/* 55 */ return (String[])table; +/* */ } +/* */ +/* */ private ServerTableManager() { +/* 59 */ this.tables = new Hashtable(); +/* */ +/* 61 */ this.tableFile = IniFile.override().getIniString("ServerTableFile", +/* 62 */ "tables/tables.dat"); +/* */ +/* 64 */ if (IniFile.gamma().getIniInt("encryptTables", 0) == 1) { +/* 65 */ encryptTablesFile(); +/* */ } +/* 67 */ loadTableFile(); +/* */ } +/* */ +/* */ private boolean loadTableFile() { +/* 71 */ URL tableURL = URL.make(NetUpdate.getUpgradeServerURL() + this.tableFile); +/* */ +/* 73 */ boolean syncLoad = IniFile.gamma().getIniInt("synchronousTableLoad", 1) == 1; +/* */ +/* 75 */ if (Gamma.loadProgress != null) +/* */ { +/* 77 */ Gamma.loadProgress.setMessage("Downloading global server information..."); +/* 78 */ Gamma.loadProgress.advance(); +/* */ } +/* */ +/* 81 */ if (syncLoad) { +/* 82 */ CacheFile cf = Cache.getFile(tableURL); +/* 83 */ cf.waitUntilLoaded(); +/* 84 */ if (!cf.error()) { +/* 85 */ syncBackgroundLoad(cf.getLocalName(), null); +/* */ } +/* */ } +/* */ +/* */ +/* 90 */ boolean ok = parseFile(); +/* */ +/* 92 */ if (!syncLoad) { +/* 93 */ BackgroundLoader.get(this, tableURL); +/* */ } +/* 95 */ return ok; +/* */ } +/* */ +/* */ public synchronized Object asyncBackgroundLoad(String localName, URL remoteURL) +/* */ { +/* 100 */ return localName; +/* */ } +/* */ +/* */ public boolean syncBackgroundLoad(Object obj, URL remoteURL) { +/* 104 */ String localName = (String)obj; +/* 105 */ if ((localName != null) && (new File(localName).exists())) { +/* 106 */ ProgressDialog.copyFile(localName, this.tableFile); +/* */ } +/* 108 */ return false; +/* */ } +/* */ +/* */ public Room getBackgroundLoadRoom() { +/* 112 */ return null; +/* */ } +/* */ +/* 115 */ private static String tableStart = "private static String[] "; +/* 116 */ private static String version = "VERSION"; +/* */ +/* */ private boolean parseFile() { +/* 119 */ BufferedReader buf = null; +/* */ try +/* */ { +/* 122 */ RandomAccessFile fIn = new RandomAccessFile(this.tableFile, "r"); +/* 123 */ byte[] encrypted = new byte[fIn.readInt()]; +/* 124 */ fIn.readFully(encrypted); +/* 125 */ fIn.close(); +/* 126 */ String decrypted = decrypt(encrypted); +/* 127 */ buf = new BufferedReader(new StringReader(decrypted)); +/* */ +/* */ String line; +/* 130 */ while ((line = buf.readLine()) != null) { +/* 131 */ String line = line.trim(); +/* 132 */ if (!line.startsWith("//")) +/* */ { +/* 134 */ if (line.length() != 0) +/* */ { +/* */ +/* */ +/* 138 */ if (line.startsWith(version)) { +/* 139 */ StringTokenizer tok = new StringTokenizer(line); +/* 140 */ String versionString = ""; +/* 141 */ while (tok.hasMoreTokens()) { +/* 142 */ versionString = tok.nextToken(); +/* */ } +/* 144 */ if (versionString != "") { +/* 145 */ this.fileVersion = Double.valueOf(versionString).intValue(); +/* */ } +/* */ } +/* */ +/* */ +/* 150 */ if (line.startsWith(tableStart)) { +/* 151 */ String tableName = line.substring(tableStart.length()); +/* 152 */ StringTokenizer tok = new StringTokenizer(tableName); +/* */ +/* 154 */ if (tok.hasMoreTokens()) +/* 155 */ parseTable(buf, tok.nextToken().trim()); +/* */ } +/* */ } +/* */ } +/* */ } +/* 160 */ buf.close(); +/* */ } catch (FileNotFoundException e) { +/* 162 */ System.out.println(e); +/* 163 */ return false; +/* */ } catch (IOException e) { +/* 165 */ System.out.println(e); +/* 166 */ return false; +/* */ } +/* */ +/* 169 */ return true; +/* */ } +/* */ +/* */ private String stripQuotes(String in) { +/* 173 */ String out = new String(in); +/* */ +/* */ +/* */ +/* 177 */ if (out.charAt(0) == '"') +/* 178 */ out = out.substring(1); +/* 179 */ if (out.charAt(out.length() - 1) == '"') +/* 180 */ out = out.substring(0, out.length() - 1); +/* */ int idx; +/* 182 */ while ((idx = out.indexOf('"')) != -1) { +/* */ int idx; +/* 184 */ out = out.substring(0, idx) + out.substring(idx + 1); +/* */ } +/* */ +/* 187 */ return out; +/* */ } +/* */ +/* */ private void parseTable(BufferedReader buf, String tableName) +/* */ throws IOException +/* */ { +/* 193 */ String lastEntry = null; +/* */ +/* 195 */ Vector strings = new Vector(); +/* */ StringTokenizer tok; +/* 197 */ label176: String line; for (; (line = buf.readLine()) != null; +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* 209 */ tok.hasMoreTokens()) +/* */ { +/* 198 */ String line = line.trim(); +/* 199 */ if ((line.length() == 0) || (line.startsWith("//"))) { +/* */ break label176; +/* */ } +/* 202 */ if (line.startsWith("};")) { +/* */ break; +/* */ } +/* */ +/* */ +/* 207 */ tok = new StringTokenizer(line, ",\n\r"); +/* */ +/* 209 */ continue; +/* 210 */ String token = tok.nextToken().trim(); +/* */ +/* 212 */ if (token.startsWith("+ \"")) { +/* 213 */ if (lastEntry != null) { +/* 214 */ token = token.substring(2); +/* 215 */ lastEntry = lastEntry + stripQuotes(token); +/* 216 */ strings.removeElement(strings.lastElement()); +/* 217 */ strings.addElement(lastEntry); +/* */ } +/* */ } else { +/* 220 */ String entry = stripQuotes(token); +/* 221 */ lastEntry = entry; +/* 222 */ strings.addElement(entry); +/* */ } +/* */ } +/* */ +/* */ +/* */ +/* 228 */ int numStrings = strings.size(); +/* 229 */ String[] table = new String[numStrings]; +/* 230 */ for (int i = 0; i < numStrings; i++) { +/* 231 */ table[i] = ((String)strings.elementAt(i)); +/* */ } +/* */ +/* */ +/* 235 */ System.out.println("Adding table " + tableName); +/* 236 */ this.tables.put(tableName, table); +/* */ } +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ private byte[] encrypt(String in) +/* */ { +/* */ try +/* */ { +/* 247 */ inBytes = in.getBytes("UTF8"); +/* */ } catch (Exception e) { byte[] inBytes; +/* 249 */ System.out.println("Error encoding to UTF8" + e.toString()); +/* 250 */ return null; +/* */ } +/* */ byte[] inBytes; +/* 253 */ byte[] outBytes = new byte[inBytes.length]; +/* */ +/* 255 */ int len = inBytes.length; +/* */ +/* 257 */ outBytes[0] = inBytes[0]; +/* 258 */ for (int idx = 1; idx < len; idx++) { +/* 259 */ outBytes[idx] = ((byte)(inBytes[idx] ^ outBytes[(idx - 1)])); +/* */ } +/* */ +/* 262 */ return outBytes; +/* */ } +/* */ +/* */ private String decrypt(byte[] in) { +/* 266 */ byte[] outBytes = new byte[in.length]; +/* */ +/* 268 */ int len = in.length; +/* */ +/* 270 */ outBytes[0] = in[0]; +/* 271 */ for (int idx = 1; idx < len; idx++) { +/* 272 */ outBytes[idx] = ((byte)(in[idx] ^ in[(idx - 1)])); +/* */ } +/* */ +/* */ try +/* */ { +/* 277 */ out = new String(outBytes, "UTF8"); +/* */ } catch (Exception e) { String out; +/* 279 */ System.out.println("Error encoding UTF8 " + e.toString()); +/* 280 */ return null; +/* */ } +/* */ String out; +/* 283 */ return out; +/* */ } +/* */ +/* */ private void encryptTablesFile() { +/* */ try { +/* 288 */ System.out.println("Encoding tables file..."); +/* */ +/* 290 */ RandomAccessFile inFilth = new RandomAccessFile("..\\tables.txt", +/* 291 */ "r"); +/* 292 */ String inStr = new String(); +/* 293 */ while (inFilth.getFilePointer() < inFilth.length()) { +/* 294 */ String in = inFilth.readLine(); +/* 295 */ System.out.println(in); +/* 296 */ inStr = inStr + in + "\r\n"; +/* */ } +/* 298 */ inFilth.close(); +/* */ +/* 300 */ byte[] encrypted = encrypt(inStr); +/* */ +/* 302 */ RandomAccessFile out = new RandomAccessFile(this.tableFile, "rw"); +/* 303 */ RandomAccessFile out2 = new RandomAccessFile("..\\tables.dat", "rw"); +/* 304 */ out.writeInt(encrypted.length); +/* 305 */ out2.writeInt(encrypted.length); +/* 306 */ out.write(encrypted); +/* 307 */ out2.write(encrypted); +/* 308 */ out.close(); +/* 309 */ out2.close(); +/* */ +/* 311 */ System.out.println("Tables file " + this.tableFile + +/* 312 */ " successfully written."); +/* */ } catch (Exception e) { +/* 314 */ System.out.println("Error encoding tables file: " + e.toString()); +/* */ } +/* */ } +/* */ } + + +/* Location: C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\core\ServerTableManager.class + * Java compiler version: 6 (50.0) + * JD-Core Version: 0.7.1 + */ \ No newline at end of file -- cgit v1.2.3