PageRenderTime 883ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Main.java

http://github.com/traitor/Minecraft-Server-Mod
Java | 145 lines | 117 code | 26 blank | 2 comment | 10 complexity | 870e34932d0bdf80b7b6c30480da26a8 MD5 | raw file
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.lang.reflect.Method;
  6. import java.net.URL;
  7. import java.net.URLClassLoader;
  8. import java.nio.channels.Channels;
  9. import java.nio.channels.ReadableByteChannel;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import java.util.zip.CRC32;
  13. import java.util.zip.CheckedInputStream;
  14. public class Main {
  15. public static final long minecraft_server = 0x367f8840L;
  16. public static final long minecraft_servero = 0xbba4f2d3L;
  17. public static final long mysql = 3001390372L;
  18. public static final long jarjar = 233379283L;
  19. public static final long rules = 0x9c44d10cL;
  20. public static final Logger log = Logger.getLogger("Minecraft");
  21. public static void main(String[] args) throws IOException {
  22. if (!fileExists("minecraft_servero.jar")) {
  23. if (!fileExists("jarjar.jar") || !fileExists("rules.rules")) {
  24. log("-----------------------------");
  25. log("jarjar.jar and/or rules.rules are missing!");
  26. log("-----------------------------");
  27. System.exit(0);
  28. }
  29. checkCRC32("jarjar.jar", jarjar);
  30. checkCRC32("rules.rules", rules);
  31. if (!fileExists("minecraft_server.jar")) {
  32. log("Missing minecraft_servero.jar, Downloading minecraft_server.jar...");
  33. downloadFile("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar", "minecraft_server.jar");
  34. checkCRC32("minecraft_server.jar", minecraft_server);
  35. log("Finished downloading minecraft_server.jar, start converting minecraft_server.jar to minecraft_servero.jar...");
  36. } else
  37. log("Missing minecraft_servero.jar, start converting minecraft_server.jar to minecraft_servero.jar...");
  38. try {
  39. com.tonicsystems.jarjar.Main.main(new String[] { "process", "rules.rules", "minecraft_server.jar", "minecraft_servero.jar" });
  40. } catch (Throwable t) {
  41. log.log(Level.SEVERE, null, t);
  42. }
  43. checkCRC32("minecraft_servero.jar", minecraft_servero);
  44. log("Finished converting minecraft_server.jar, Starting minecraft server...");
  45. dynamicLoadJar("minecraft_servero.jar");
  46. } else
  47. checkCRC32("minecraft_servero.jar", minecraft_servero);
  48. if (etc.getInstance().getDataSourceType().equalsIgnoreCase("mysql"))
  49. checkCRC32("mysql-connector-java-bin.jar", mysql);
  50. if (checkForUpdate())
  51. System.out.println("Update found.");
  52. // derp.
  53. // This mod works with GUI now :D
  54. try {
  55. net.minecraft.server.MinecraftServer.main(args);
  56. } catch (Throwable t) {
  57. log.log(Level.SEVERE, null, t);
  58. }
  59. new DeadLockDetector();
  60. }
  61. public static boolean fileExists(String filename) {
  62. return new File(filename).exists();
  63. }
  64. public static void checkCRC32(String fileName, long crc) throws IOException {
  65. if (etc.getInstance().getTainted())
  66. return;
  67. long checksum = getCRC32(fileName);
  68. if (checksum != crc) {
  69. log("-----------------------------");
  70. log(fileName + " does not match checksum! Checksum found: " + checksum + ", required checksum: " + crc + ".");
  71. log("This means some of your files are either corrupted,outdated or to new(minecraft got updated?).");
  72. log("If you still want to run the server, delete version.txt to run the server in tainted mode.");
  73. log("-----------------------------");
  74. System.exit(0);
  75. }
  76. }
  77. public static long getCRC32(String fileName) throws IOException {
  78. FileInputStream stream = new FileInputStream(fileName);
  79. CheckedInputStream cis = new CheckedInputStream(stream, new CRC32());
  80. byte[] buf = new byte[128];
  81. while (cis.read(buf) >= 0) {
  82. }
  83. long rt = cis.getChecksum().getValue();
  84. stream.close();
  85. cis.close();
  86. return rt;
  87. }
  88. public static void printCRC() throws IOException {
  89. log("Minecraft_server CRC32: \t" + getCRC32("minecraft_server.jar"));
  90. log("Minecraft_servero CRC32: \t" + getCRC32("minecraft_servero.jar"));
  91. log("Jarjar CRC32: \t\t\t" + getCRC32("jarjar.jar"));
  92. log("Rules CRC32: \t\t\t" + getCRC32("rules.rules"));
  93. log("Mysql CRC32: \t\t\t" + getCRC32("mysql-connector-java-bin.jar"));
  94. }
  95. public static void downloadFile(String website, String fileLocation) throws IOException {
  96. URL url = new URL(website);
  97. ReadableByteChannel rbc = Channels.newChannel(url.openStream());
  98. FileOutputStream fos = new FileOutputStream(fileLocation);
  99. fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  100. }
  101. public static void log(String str) {
  102. System.out.println(str);
  103. }
  104. public static void dynamicLoadJar(String fileName) throws IOException {
  105. URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
  106. Class<?> sysclass = URLClassLoader.class;
  107. try {
  108. Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
  109. method.setAccessible(true);
  110. method.invoke(sysloader, new Object[] { (new File(fileName)).toURI().toURL() });
  111. } catch (Throwable t) {
  112. t.printStackTrace();
  113. throw new IOException("Error, could not add URL to system classloader");
  114. }
  115. }
  116. public static boolean checkForUpdate() {
  117. return false;
  118. }
  119. }