SHOW:
|
|
- or go back to the newest paste.
| 1 | package net.launcher.utils; | |
| 2 | ||
| 3 | import java.awt.Font; | |
| 4 | import java.awt.image.BufferedImage; | |
| 5 | import java.io.BufferedInputStream; | |
| 6 | import java.io.BufferedReader; | |
| 7 | import java.io.DataInputStream; | |
| 8 | import java.io.DataOutputStream; | |
| 9 | import java.io.File; | |
| 10 | import java.io.FileOutputStream; | |
| 11 | import java.io.IOException; | |
| 12 | import java.io.InputStream; | |
| 13 | import java.io.InputStreamReader; | |
| 14 | import java.lang.reflect.Field; | |
| 15 | import java.net.InetSocketAddress; | |
| 16 | import java.net.Socket; | |
| 17 | import java.net.URI; | |
| 18 | import java.net.URL; | |
| 19 | import java.net.URLClassLoader; | |
| 20 | import java.security.MessageDigest; | |
| 21 | import java.text.SimpleDateFormat; | |
| 22 | import java.util.Date; | |
| 23 | import java.util.HashMap; | |
| 24 | import java.util.Map; | |
| 25 | ||
| 26 | import javax.imageio.ImageIO; | |
| 27 | ||
| 28 | import net.launcher.components.Files; | |
| 29 | import net.launcher.components.Frame; | |
| 30 | import net.launcher.run.Settings; | |
| 31 | import net.launcher.run.Starter; | |
| 32 | ||
| 33 | public class BaseUtils | |
| 34 | {
| |
| 35 | public static final String empty = ""; | |
| 36 | public static final String http = "http://"; | |
| 37 | public static int logNumber = 0; | |
| 38 | public static ConfigUtils config = new ConfigUtils("/launcher.config", getConfigName());
| |
| 39 | ||
| 40 | public static Map<String, Font> fonts = new HashMap<String, Font>(); | |
| 41 | public static Map<String, BufferedImage> imgs = new HashMap<String, BufferedImage>(); | |
| 42 | ||
| 43 | public static BufferedImage getLocalImage(String name) | |
| 44 | {
| |
| 45 | try | |
| 46 | {
| |
| 47 | if(imgs.containsKey(name)) return (BufferedImage)imgs.get(name); | |
| 48 | ||
| 49 | BufferedImage img = ImageIO.read(BaseUtils.class.getResource("/net/launcher/theme/" + name + ".png"));
| |
| 50 | imgs.put(name, img); | |
| 51 | send("Opened local image: " + name + ".png");
| |
| 52 | return img; | |
| 53 | } | |
| 54 | catch(Exception e) | |
| 55 | {
| |
| 56 | sendErr("Fail to open local image: " + name + ".png");
| |
| 57 | return getEmptyImage(); | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | public static BufferedImage getEmptyImage() | |
| 62 | {
| |
| 63 | return new BufferedImage(9, 9, BufferedImage.TYPE_INT_ARGB); | |
| 64 | } | |
| 65 | ||
| 66 | public static void send(String msg) | |
| 67 | {
| |
| 68 | String prefix = null; | |
| 69 | if(Integer.toString(logNumber).length() == 1) prefix = "00" + logNumber + " "; | |
| 70 | else if(Integer.toString(logNumber).length() == 2) prefix = "0" + logNumber + " "; | |
| 71 | else if(Integer.toString(logNumber).length() == 3) prefix = logNumber + " "; | |
| 72 | else prefix = "999 "; | |
| 73 | if(Settings.debug) System.out.println(prefix + msg); | |
| 74 | logNumber++; | |
| 75 | } | |
| 76 | ||
| 77 | public static void sendErr(String err) | |
| 78 | {
| |
| 79 | String prefix = null; | |
| 80 | if(Integer.toString(logNumber).length() == 1) prefix = "00" + logNumber + " "; | |
| 81 | else if(Integer.toString(logNumber).length() == 2) prefix = "0" + logNumber + " "; | |
| 82 | else if(Integer.toString(logNumber).length() == 3) prefix = logNumber + " "; | |
| 83 | else prefix = "999 "; | |
| 84 | if(Settings.debug)System.err.println(prefix + err); | |
| 85 | logNumber++; | |
| 86 | } | |
| 87 | ||
| 88 | public static boolean contains(int x, int y, int xx, int yy, int w, int h) | |
| 89 | {
| |
| 90 | return (x >= xx) && (y >= yy) && (x < xx + w) && (y < yy + h); | |
| 91 | } | |
| 92 | ||
| 93 | public static File getConfigName() | |
| 94 | {
| |
| 95 | String home = System.getProperty("user.home", "");
| |
| 96 | String path = File.separator + Settings.baseconf + File.separator + "launcher.config"; | |
| 97 | switch(getPlatform()) | |
| 98 | {
| |
| 99 | case 1: return new File(System.getProperty("user.home", "") + path);
| |
| 100 | case 2: | |
| 101 | String appData = System.getenv(Settings.basedir); | |
| 102 | if(appData != null) return new File(appData + path); | |
| 103 | else return new File(home + path); | |
| 104 | case 3: return new File(home, "Library/Application Support/" + path); | |
| 105 | default: return new File(home + path); | |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | public static File getMcDir() | |
| 110 | {
| |
| 111 | String home = System.getProperty("user.home", "");
| |
| 112 | String path = Settings.pathconst.replaceAll("%SERVERNAME%", getClientName());
| |
| 113 | switch(getPlatform()) | |
| 114 | {
| |
| 115 | case 1: return new File(System.getProperty("user.home", ""), path);
| |
| 116 | case 2: | |
| 117 | String appData = System.getenv(Settings.basedir); | |
| 118 | if(appData != null) return new File(appData, path); | |
| 119 | else return new File(home, path); | |
| 120 | case 3: return new File(home, "Library/Application Support/" + path); | |
| 121 | default: return new File(home, path); | |
| 122 | } | |
| 123 | } | |
| 124 | ||
| 125 | public static int getPlatform() | |
| 126 | {
| |
| 127 | String osName = System.getProperty("os.name").toLowerCase();
| |
| 128 | ||
| 129 | if(osName.contains("win")) return 2;
| |
| 130 | if(osName.contains("mac")) return 3;
| |
| 131 | if(osName.contains("solaris")) return 1;
| |
| 132 | if(osName.contains("sunos")) return 1;
| |
| 133 | if(osName.contains("linux")) return 0;
| |
| 134 | if(osName.contains("unix")) return 0;
| |
| 135 | ||
| 136 | return 4; | |
| 137 | } | |
| 138 | ||
| 139 | public static String buildUrl(String s) | |
| 140 | {
| |
| 141 | return http + Settings.domain + "/" + Settings.siteDir + "/" + s; | |
| 142 | } | |
| 143 | ||
| 144 | static | |
| 145 | {
| |
| 146 | config.load(); | |
| 147 | } | |
| 148 | ||
| 149 | public static void setProperty(String s, Object value) | |
| 150 | {
| |
| 151 | if(config.checkProperty(s)) config.changeProperty(s,value); | |
| 152 | else config.put(s,value); | |
| 153 | } | |
| 154 | ||
| 155 | public static String getPropertyString(String s) | |
| 156 | {
| |
| 157 | if(config.checkProperty(s)) return config.getPropertyString(s); | |
| 158 | return null; | |
| 159 | } | |
| 160 | ||
| 161 | public static boolean getPropertyBoolean(String s) | |
| 162 | {
| |
| 163 | if(config.checkProperty(s)) return config.getPropertyBoolean(s); | |
| 164 | return false; | |
| 165 | } | |
| 166 | ||
| 167 | public static int getPropertyInt(String s) | |
| 168 | {
| |
| 169 | if(config.checkProperty(s)) return config.getPropertyInteger(s); | |
| 170 | return 0; | |
| 171 | } | |
| 172 | ||
| 173 | public static int getPropertyInt(String s, int d) | |
| 174 | {
| |
| 175 | if(config.checkProperty(s)) return config.getPropertyInteger(s); | |
| 176 | return d; | |
| 177 | } | |
| 178 | ||
| 179 | public static boolean getPropertyBoolean(String s, boolean b) | |
| 180 | {
| |
| 181 | if(config.checkProperty(s)) return config.getPropertyBoolean(s); | |
| 182 | return b; | |
| 183 | } | |
| 184 | ||
| 185 | public static String[] getServerNames() | |
| 186 | {
| |
| 187 | String[] serverNames = new String[Settings.servers.length]; | |
| 188 | ||
| 189 | for(int i = 0; i < Settings.servers.length; i++) | |
| 190 | {
| |
| 191 | serverNames[i] = Settings.servers[i].split(", ")[0];
| |
| 192 | } | |
| 193 | return serverNames; | |
| 194 | } | |
| 195 | ||
| 196 | public static String getClientName() | |
| 197 | {
| |
| 198 | if(Settings.useMulticlient) | |
| 199 | {
| |
| 200 | return Frame.main.servers.getSelected().replaceAll(" ", empty).toLowerCase();
| |
| 201 | } | |
| 202 | return "main"; | |
| 203 | } | |
| 204 | ||
| 205 | public static void openURL(String url) | |
| 206 | {
| |
| 207 | try | |
| 208 | {
| |
| 209 | Object o = Class.forName("java.awt.Desktop").getMethod("getDesktop", new Class[0]).invoke(null, new Object[0]);
| |
| 210 | o.getClass().getMethod("browse", new Class[] { URI.class }).invoke(o, new Object[] { new URI(url)});
| |
| 211 | } catch (Throwable e) {}
| |
| 212 | } | |
| 213 | ||
| 214 | public static void deleteDirectory(File file) | |
| 215 | {
| |
| 216 | if(!file.exists()) return; | |
| 217 | if(file.isDirectory()) | |
| 218 | {
| |
| 219 | for(File f : file.listFiles()) | |
| 220 | deleteDirectory(f); | |
| 221 | file.delete(); | |
| 222 | } | |
| 223 | else file.delete(); | |
| 224 | } | |
| 225 | ||
| 226 | public static BufferedImage getSkinImage(String name) | |
| 227 | {
| |
| 228 | try | |
| 229 | {
| |
| 230 | BufferedImage img = ImageIO.read(new URL(buildUrl(Settings.skins + name + ".png"))); | |
| 231 | send("Skin loaded: " + buildUrl(Settings.skins + name + ".png"));
| |
| 232 | return img; | |
| 233 | } | |
| 234 | catch(Exception e) | |
| 235 | {
| |
| 236 | sendErr("Skin not found: " + buildUrl(Settings.skins + name + ".png"));
| |
| 237 | return getLocalImage("skin");
| |
| 238 | } | |
| 239 | } | |
| 240 | ||
| 241 | public static BufferedImage getCloakImage(String name) | |
| 242 | {
| |
| 243 | try | |
| 244 | {
| |
| 245 | BufferedImage img = ImageIO.read(new URL(buildUrl(Settings.cloaks + name + ".png"))); | |
| 246 | send("Cloak loaded: " + buildUrl(Settings.cloaks + name + ".png"));
| |
| 247 | return img; | |
| 248 | } | |
| 249 | catch(Exception e) | |
| 250 | {
| |
| 251 | sendErr("Cloak not found: " + buildUrl(Settings.cloaks + name + ".png"));
| |
| 252 | return new BufferedImage(22, 17, 2); | |
| 253 | } | |
| 254 | } | |
| 255 | ||
| 256 | public static String execute(String surl, Object[] params) | |
| 257 | {
| |
| 258 | try | |
| 259 | {
| |
| 260 | send("Openning stream: " + surl);
| |
| 261 | URL url = new URL(surl); | |
| 262 | ||
| 263 | InputStream is = PostUtils.post(url, params); | |
| 264 | BufferedReader rd = new BufferedReader(new InputStreamReader(is)); | |
| 265 | ||
| 266 | StringBuffer response = new StringBuffer(); | |
| 267 | String line; | |
| 268 | while((line=rd.readLine())!=null){ response.append(line); }
| |
| 269 | rd.close(); | |
| 270 | ||
| 271 | String str1 = response.toString().trim(); | |
| 272 | send("Stream opened for " + surl + " completed, return answer: ");
| |
| 273 | send(" \"" + str1 + "\"");
| |
| 274 | return str1; | |
| 275 | } catch(Exception e) | |
| 276 | {
| |
| 277 | sendErr("Stream for " + surl + " not ensablished, return null");
| |
| 278 | return null; | |
| 279 | } | |
| 280 | } | |
| 281 | ||
| 282 | public static Font getFont(String name, float size) | |
| 283 | {
| |
| 284 | try | |
| 285 | {
| |
| 286 | if(fonts.containsKey(name)) return (Font)fonts.get(name).deriveFont(size); | |
| 287 | ||
| 288 | send("Creating font: " + name);
| |
| 289 | Font font = Font.createFont(Font.TRUETYPE_FONT, BaseUtils.class.getResourceAsStream("/net/launcher/theme/" + name + ".ttf"));
| |
| 290 | fonts.put(name, font); | |
| 291 | return font.deriveFont(size); | |
| 292 | } catch(Exception e) | |
| 293 | {
| |
| 294 | send("Failed create font!");
| |
| 295 | throwException(e, Frame.main); | |
| 296 | return null; | |
| 297 | } | |
| 298 | } | |
| 299 | ||
| 300 | public static void throwException(Exception e, Frame main) | |
| 301 | {
| |
| 302 | e.printStackTrace(); | |
| 303 | main.panel.removeAll(); | |
| 304 | main.addFrameComp(); | |
| 305 | StackTraceElement[] el = e.getStackTrace(); | |
| 306 | main.panel.tmpString = empty; | |
| 307 | main.panel.tmpString += e.toString() + "<:>"; | |
| 308 | for(StackTraceElement i : el) | |
| 309 | {
| |
| 310 | main.panel.tmpString += "at " + i.toString() + "<:>"; | |
| 311 | } | |
| 312 | main.panel.type = 3; | |
| 313 | main.repaint(); | |
| 314 | } | |
| 315 | ||
| 316 | - | public static int servtype = 2; |
| 316 | + | public static int servtype = 3; |
| 317 | public static String[] pollServer(String ip, int port) | |
| 318 | {
| |
| 319 | Socket soc = null; | |
| 320 | DataInputStream dis = null; | |
| 321 | DataOutputStream dos = null; | |
| 322 | ||
| 323 | try | |
| 324 | {
| |
| 325 | soc = new Socket(); | |
| 326 | soc.setSoTimeout(3000); | |
| 327 | soc.setTcpNoDelay(true); | |
| 328 | soc.setTrafficClass(18); | |
| 329 | soc.connect(new InetSocketAddress(ip, port), 3000); | |
| 330 | dis = new DataInputStream(soc.getInputStream()); | |
| 331 | dos = new DataOutputStream(soc.getOutputStream()); | |
| 332 | dos.write(254); | |
| 333 | ||
| 334 | if (dis.read() != 255) | |
| 335 | {
| |
| 336 | throw new IOException("Bad message");
| |
| 337 | } | |
| 338 | String servc = readString(dis, 256); | |
| 339 | servc.substring(3); | |
| 340 | if (servc.substring(0,1).equalsIgnoreCase("§") && servc.substring(1,2).equalsIgnoreCase("1"))
| |
| 341 | {
| |
| 342 | servtype = 1; | |
| 343 | return servc.split("\u0000");
| |
| 344 | ||
| 345 | } | |
| 346 | else | |
| 347 | {
| |
| 348 | servtype = 2; | |
| 349 | return servc.split("§");
| |
| 350 | } | |
| 351 | ||
| 352 | } catch (Exception e) | |
| 353 | {
| |
| 354 | return new String[] { null, null, null };
| |
| 355 | } finally | |
| 356 | {
| |
| 357 | try { dis.close(); } catch (Exception e) {}
| |
| 358 | try { dos.close(); } catch (Exception e) {}
| |
| 359 | try { soc.close(); } catch (Exception e) {}
| |
| 360 | } | |
| 361 | } | |
| 362 | ||
| 363 | public static int parseInt(String integer, int def) | |
| 364 | {
| |
| 365 | try | |
| 366 | {
| |
| 367 | return Integer.parseInt(integer.trim()); | |
| 368 | } | |
| 369 | catch (Exception e) | |
| 370 | {
| |
| 371 | return def; | |
| 372 | } | |
| 373 | } | |
| 374 | ||
| 375 | public static String readString(DataInputStream is, int d) throws IOException | |
| 376 | {
| |
| 377 | short word = is.readShort(); | |
| 378 | if (word > d) throw new IOException(); | |
| 379 | if (word < 0) throw new IOException(); | |
| 380 | StringBuilder res = new StringBuilder(); | |
| 381 | for (int i = 0; i < word; i++) | |
| 382 | {
| |
| 383 | res.append(is.readChar()); | |
| 384 | } | |
| 385 | return res.toString(); | |
| 386 | } | |
| 387 | ||
| 388 | public static String genServerStatus(String[] args) | |
| 389 | {
| |
| 390 | if (servtype == 1) | |
| 391 | {
| |
| 392 | if(args[0] == null && args[1] == null && args[2] == null) return "Сервер выключен"; | |
| 393 | if(args[4] != null && args[5] != null) | |
| 394 | {
| |
| 395 | if(args[4].equals(args[5])) return "Сервер переполнен (Всего слотов: " + args[4] + ")"; | |
| 396 | return "На сервере " + args[4] + " из " + args[5] + " игроков"; | |
| 397 | } | |
| 398 | } | |
| 399 | else | |
| 400 | if (servtype == 2) | |
| 401 | {
| |
| 402 | ||
| 403 | ||
| 404 | if(args[0] == null && args[1] == null && args[2] == null) return "Сервер выключен"; | |
| 405 | if(args[1] != null && args[2] != null) | |
| 406 | {
| |
| 407 | if(args[1].equals(args[2])) return "Сервер переполнен (Всего слотов: " + args[2] + ")"; | |
| 408 | return "На сервере " + args[1] + " из " + args[2] + " игроков"; | |
| 409 | } | |
| 410 | } | |
| 411 | return "Ошибка получения информации"; | |
| 412 | } | |
| 413 | ||
| 414 | public static BufferedImage genServerIcon(String[] args) | |
| 415 | {
| |
| 416 | if(args[0] == null && args[1] == null && args[2] == null) return Files.light.getSubimage(0, 0, Files.light.getHeight(), Files.light.getHeight()); | |
| 417 | if(args[1] != null && args[2] != null) | |
| 418 | {
| |
| 419 | if(args[1].equals(args[2])) return Files.light.getSubimage(Files.light.getHeight(), 0, Files.light.getHeight(), Files.light.getHeight()); | |
| 420 | return Files.light.getSubimage(Files.light.getHeight() * 2, 0, Files.light.getHeight(), Files.light.getHeight()); | |
| 421 | } | |
| 422 | return Files.light.getSubimage(Files.light.getHeight() * 3, 0, Files.light.getHeight(), Files.light.getHeight()); | |
| 423 | } | |
| 424 | ||
| 425 | public static void restart() | |
| 426 | {
| |
| 427 | send("Restarting launcher...");
| |
| 428 | try | |
| 429 | {
| |
| 430 | Starter.main(null); | |
| 431 | } catch (Exception e) | |
| 432 | {
| |
| 433 | e.printStackTrace(); | |
| 434 | return; | |
| 435 | } | |
| 436 | System.exit(0); | |
| 437 | } | |
| 438 | ||
| 439 | public static String unix2hrd(long unix) | |
| 440 | {
| |
| 441 | return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date(unix * 1000));
| |
| 442 | } | |
| 443 | ||
| 444 | public void delete(File file) | |
| 445 | {
| |
| 446 | if(!file.exists()) return; | |
| 447 | ||
| 448 | if(file.isDirectory()) | |
| 449 | {
| |
| 450 | for(File f : file.listFiles()) delete(f); | |
| 451 | file.delete(); | |
| 452 | } else | |
| 453 | {
| |
| 454 | file.delete(); | |
| 455 | } | |
| 456 | } | |
| 457 | ||
| 458 | public static boolean checkLink(String l) | |
| 459 | {
| |
| 460 | if(l.contains("#"))
| |
| 461 | {
| |
| 462 | return false; | |
| 463 | } | |
| 464 | return true; | |
| 465 | } | |
| 466 | ||
| 467 | public static boolean existLink(String l) | |
| 468 | {
| |
| 469 | if(l.contains("@"))
| |
| 470 | {
| |
| 471 | return true; | |
| 472 | } | |
| 473 | return false; | |
| 474 | } | |
| 475 | ||
| 476 | public static void patchDir(URLClassLoader cl) | |
| 477 | {
| |
| 478 | if(!Settings.patchDir) return; | |
| 479 | ||
| 480 | try | |
| 481 | {
| |
| 482 | String mcver = Settings.servers[Frame.main.servers.getSelectedIndex()].split(", ")[3];
| |
| 483 | ||
| 484 | send("Changing client dir...");
| |
| 485 | send("Client: " + getClientName() + "::" + mcver);
| |
| 486 | send("Searching in version database...");
| |
| 487 | ||
| 488 | for(int j = 0; j < Settings.mcversions.length; j++) | |
| 489 | {
| |
| 490 | if(mcver.equals(Settings.mcversions[j].split("::")[0]))
| |
| 491 | {
| |
| 492 | send("Index #" + j + ", Patching...");
| |
| 493 | Field f = cl.loadClass(Settings.mcclass).getDeclaredField(Settings.mcversions[j].split("::")[1]);
| |
| 494 | Field.setAccessible(new Field[] { f }, true);
| |
| 495 | f.set(null, getMcDir()); | |
| 496 | send("File patched: " + Settings.mcclass + "::" + Settings.mcversions[j].split("::")[1]);
| |
| 497 | send("Patching succesful, herobrine removed.");
| |
| 498 | return; | |
| 499 | } | |
| 500 | } | |
| 501 | sendErr("Error: Client version not correct.");
| |
| 502 | } catch(Exception e) | |
| 503 | {
| |
| 504 | sendErr("Error: Client field not correct.");
| |
| 505 | } | |
| 506 | } | |
| 507 | ||
| 508 | public static void updateLauncher() throws Exception | |
| 509 | {
| |
| 510 | send("Launcher updater started...");
| |
| 511 | send("Downloading file: " + Settings.updateFile);
| |
| 512 | ||
| 513 | InputStream is = new BufferedInputStream(new URL(Settings.updateFile).openStream()); | |
| 514 | FileOutputStream fos = new FileOutputStream(Starter.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); | |
| 515 | ||
| 516 | int bs = 0; | |
| 517 | byte[] buffer = new byte[65536]; | |
| 518 | MessageDigest md5 = MessageDigest.getInstance("MD5");
| |
| 519 | while((bs = is.read(buffer, 0, buffer.length)) != -1) | |
| 520 | {
| |
| 521 | fos.write(buffer, 0, bs); | |
| 522 | md5.update(buffer, 0, bs); | |
| 523 | } | |
| 524 | is.close(); | |
| 525 | fos.close(); | |
| 526 | BaseUtils.send("File downloaded: " + Settings.updateFile);
| |
| 527 | Starter.main(null); | |
| 528 | System.exit(0); | |
| 529 | } | |
| 530 | } |