Commits

md_5 authored 3624ad4d1ce
Update dependencies + cleanup
No tags

src/main/java/org/spigotmc/builder/Builder.java

Modified
16 16 import difflib.Patch;
17 17 import java.awt.Desktop;
18 18 import java.io.BufferedOutputStream;
19 19 import java.io.BufferedReader;
20 20 import java.io.BufferedWriter;
21 21 import java.io.File;
22 22 import java.io.FileDescriptor;
23 23 import java.io.FileNotFoundException;
24 24 import java.io.FileOutputStream;
25 25 import java.io.FileWriter;
26 -import java.io.FilenameFilter;
27 26 import java.io.IOException;
28 27 import java.io.InputStream;
29 28 import java.io.InputStreamReader;
30 29 import java.io.OutputStream;
31 30 import java.io.PrintStream;
32 31 import java.lang.management.ManagementFactory;
33 32 import java.net.URI;
34 33 import java.net.URL;
35 34 import java.net.URLConnection;
36 35 import java.nio.file.FileSystem;
45 44 import java.util.Arrays;
46 45 import java.util.Collections;
47 46 import java.util.Date;
48 47 import java.util.Enumeration;
49 48 import java.util.LinkedList;
50 49 import java.util.List;
51 50 import java.util.function.Predicate;
52 51 import java.util.jar.JarFile;
53 52 import java.util.zip.ZipEntry;
54 53 import java.util.zip.ZipFile;
55 -import javax.net.ssl.HostnameVerifier;
56 54 import javax.net.ssl.HttpsURLConnection;
57 55 import javax.net.ssl.SSLContext;
58 -import javax.net.ssl.SSLSession;
59 56 import javax.net.ssl.TrustManager;
60 57 import javax.net.ssl.X509TrustManager;
61 58 import javax.swing.JFrame;
62 59 import javax.swing.JLabel;
63 60 import joptsimple.OptionParser;
64 61 import joptsimple.OptionSet;
65 62 import joptsimple.OptionSpec;
66 63 import joptsimple.util.EnumConverter;
67 64 import lombok.RequiredArgsConstructor;
68 65 import org.apache.commons.io.FileUtils;
472 469 File mojangMappings = new File( workDir, "minecraft_server." + versionInfo.getMinecraftVersion() + ".txt" );
473 470 if ( !mojangMappings.exists() )
474 471 {
475 472 download( versionInfo.getMappingsUrl(), mojangMappings );
476 473 }
477 474
478 475 MapUtil mapUtil = new MapUtil();
479 476 mapUtil.loadBuk( classMappings );
480 477 if ( !memberMappings.exists() )
481 478 {
482 - memberMappings = new File( workDir, "bukkit-" + mappingsVersion + "-members.csrg" );;
479 + memberMappings = new File( workDir, "bukkit-" + mappingsVersion + "-members.csrg" );
483 480 mapUtil.makeFieldMaps( mojangMappings, memberMappings, true );
484 481
485 482 runMaven( CWD, "install:install-file", "-Dfile=" + memberMappings, "-Dpackaging=csrg", "-DgroupId=org.spigotmc",
486 483 "-DartifactId=minecraft-server", "-Dversion=" + versionInfo.getSpigotVersion(), "-Dclassifier=maps-spigot-members", "-DgeneratePom=false" );
487 484
488 485 runMaven( CWD, "install:install-file", "-Dfile=" + classMappings, "-Dpackaging=csrg", "-DgroupId=org.spigotmc",
489 486 "-DartifactId=minecraft-server", "-Dversion=" + versionInfo.getSpigotVersion(), "-Dclassifier=maps-spigot", "-DgeneratePom=false" );
490 487 } else if ( !fieldMappings.exists() )
491 488 {
492 489 mapUtil.makeFieldMaps( mojangMappings, fieldMappings, false );
534 531
535 532 runMaven( CWD, "install:install-file", "-Dfile=" + finalMappedJar, "-Dpackaging=jar", "-DgroupId=org.spigotmc",
536 533 "-DartifactId=minecraft-server", "-Dversion=" + ( versionInfo.getSpigotVersion() != null ? versionInfo.getSpigotVersion() : versionInfo.getMinecraftVersion() + "-SNAPSHOT" ) );
537 534
538 535 File decompileDir = new File( workDir, "decompile-" + mappingsVersion );
539 536 if ( !decompileDir.exists() )
540 537 {
541 538 decompileDir.mkdir();
542 539
543 540 File clazzDir = new File( decompileDir, "classes" );
544 - unzip( finalMappedJar, clazzDir, new Predicate<String>()
545 - {
546 -
547 - @Override
548 - public boolean test(String input)
549 - {
550 - return input.startsWith( "net/minecraft" );
551 - }
552 - } );
541 + unzip( finalMappedJar, clazzDir, (input) -> input.startsWith( "net/minecraft" ) );
553 542 if ( versionInfo.getDecompileCommand() == null )
554 543 {
555 544 versionInfo.setDecompileCommand( "java -jar BuildData/bin/fernflower.jar -dgs=1 -hdc=0 -rbr=0 -asc=1 -udv=0 {0} {1}" );
556 545 }
557 546
558 547 runProcess( CWD, MessageFormat.format( versionInfo.getDecompileCommand(), clazzDir.getPath(), decompileDir.getPath() ).split( " " ) );
559 548 }
560 549
561 550 try
562 551 {
615 604 }
616 605 }
617 606 if ( !preludeFound )
618 607 {
619 608 readFile.add( 0, "+++" );
620 609 }
621 610
622 611 Patch parsedPatch = DiffUtils.parseUnifiedDiff( readFile );
623 612 List<?> modifiedLines = DiffUtils.patch( Files.readLines( clean, Charsets.UTF_8 ), parsedPatch );
624 613
625 - BufferedWriter bw = new BufferedWriter( new FileWriter( t ) );
626 - for ( Object line : modifiedLines )
614 + try ( BufferedWriter bw = new BufferedWriter( new FileWriter( t ) ) )
627 615 {
628 - bw.write( (String) line );
629 - bw.newLine();
616 + for ( Object line : modifiedLines )
617 + {
618 + bw.write( (String) line );
619 + bw.newLine();
620 + }
630 621 }
631 - bw.close();
632 622 } catch ( Exception ex )
633 623 {
634 624 throw new RuntimeException( "Error patching " + relativeName, ex );
635 625 }
636 626 } );
637 627 File tmpNms = new File( craftBukkit, "tmp-nms" );
638 628 FileUtils.copyDirectory( nmsDir, tmpNms );
639 629
640 630 craftBukkitGit.branchDelete().setBranchNames( "patched" ).setForce( true ).call();
641 631 craftBukkitGit.checkout().setCreateBranch( true ).setForceRefUpdate( true ).setName( "patched" ).call();
773 763 return true;
774 764 }
775 765 }
776 766
777 767 public static final String get(String url) throws IOException
778 768 {
779 769 URLConnection con = new URL( url ).openConnection();
780 770 con.setConnectTimeout( 5000 );
781 771 con.setReadTimeout( 5000 );
782 772
783 - InputStreamReader r = null;
784 - try
773 + try ( InputStreamReader r = new InputStreamReader( con.getInputStream() ) )
785 774 {
786 - r = new InputStreamReader( con.getInputStream() );
787 -
788 775 return CharStreams.toString( r );
789 - } finally
790 - {
791 - if ( r != null )
792 - {
793 - r.close();
794 - }
795 776 }
796 777 }
797 778
798 779 public static void copyJar(String path, final String jarPrefix, final String jarSuffix, File outJar) throws Exception
799 780 {
800 - File[] files = new File( path ).listFiles( new FilenameFilter()
801 - {
802 - @Override
803 - public boolean accept(File dir, String name)
804 - {
805 - return name.startsWith( jarPrefix ) && name.endsWith( jarSuffix );
806 - }
807 - } );
781 + File[] files = new File( path ).listFiles( (dir, name) -> name.startsWith( jarPrefix ) && name.endsWith( jarSuffix ) );
808 782
809 783 if ( !outJar.getParentFile().isDirectory() )
810 784 {
811 785 outJar.getParentFile().mkdirs();
812 786 }
813 787
814 788 for ( File file : files )
815 789 {
816 790 System.out.println( "Copying " + file.getName() + " to " + outJar.getAbsolutePath() );
817 791 Files.copy( file, outJar );
841 815 repo.reset().setRef( "origin/master" ).setMode( ResetCommand.ResetType.HARD ).call();
842 816 }
843 817 System.out.println( "Checked out: " + ref );
844 818
845 819 // Return true if fetch changed any tracking refs.
846 820 return !result.getTrackingRefUpdates().isEmpty();
847 821 }
848 822
849 823 public static int runMaven(File workDir, String... command) throws Exception
850 824 {
851 - List<String> args = new LinkedList<String>();
825 + List<String> args = new LinkedList<>();
852 826
853 827 if ( IS_WINDOWS )
854 828 {
855 829 args.add( maven.getAbsolutePath() + "/bin/mvn.cmd" );
856 830 } else
857 831 {
858 832 args.add( "sh" );
859 833 args.add( maven.getAbsolutePath() + "/bin/mvn" );
860 834 }
861 835
983 957 @RequiredArgsConstructor
984 958 private static class StreamRedirector implements Runnable
985 959 {
986 960
987 961 private final InputStream in;
988 962 private final PrintStream out;
989 963
990 964 @Override
991 965 public void run()
992 966 {
993 - BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
994 - try
967 + try ( BufferedReader br = new BufferedReader( new InputStreamReader( in ) ) )
995 968 {
996 969 String line;
997 970 while ( ( line = br.readLine() ) != null )
998 971 {
999 972 out.println( line );
1000 973 }
1001 974 } catch ( IOException ex )
1002 975 {
1003 976 throw new RuntimeException( ex );
1004 977 }
1005 978 }
1006 979 }
1007 980
1008 981 public static void unzip(File zipFile, File targetFolder) throws IOException
1009 982 {
1010 983 unzip( zipFile, targetFolder, null );
1011 984 }
1012 985
1013 986 public static void unzip(File zipFile, File targetFolder, Predicate<String> filter) throws IOException
1014 987 {
1015 988 targetFolder.mkdir();
1016 - ZipFile zip = new ZipFile( zipFile );
1017 -
1018 - try
989 + try ( ZipFile zip = new ZipFile( zipFile ) )
1019 990 {
1020 991 for ( Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements(); )
1021 992 {
1022 993 ZipEntry entry = entries.nextElement();
1023 994
1024 995 if ( filter != null )
1025 996 {
1026 997 if ( !filter.test( entry.getName() ) )
1027 998 {
1028 999 continue;
1034 1005 if ( entry.isDirectory() )
1035 1006 {
1036 1007 outFile.mkdirs();
1037 1008 continue;
1038 1009 }
1039 1010 if ( outFile.getParentFile() != null )
1040 1011 {
1041 1012 outFile.getParentFile().mkdirs();
1042 1013 }
1043 1014
1044 - InputStream is = zip.getInputStream( entry );
1045 - OutputStream os = new FileOutputStream( outFile );
1046 - try
1015 + try ( InputStream is = zip.getInputStream( entry ); OutputStream os = new FileOutputStream( outFile ); )
1047 1016 {
1048 1017 ByteStreams.copy( is, os );
1049 - } finally
1050 - {
1051 - is.close();
1052 - os.close();
1053 1018 }
1054 1019
1055 1020 System.out.println( "Extracted: " + outFile );
1056 1021 }
1057 - } finally
1058 - {
1059 - zip.close();
1060 1022 }
1061 1023 }
1062 1024
1063 1025 public static void clone(String url, File target) throws GitAPIException, IOException
1064 1026 {
1065 1027 System.out.println( "Starting clone of " + url + " to " + target );
1066 1028
1067 - Git result = Git.cloneRepository().setURI( url ).setDirectory( target ).call();
1068 -
1069 - try
1029 + try ( Git result = Git.cloneRepository().setURI( url ).setDirectory( target ).call() )
1070 1030 {
1071 1031 StoredConfig config = result.getRepository().getConfig();
1072 1032 config.setBoolean( "core", null, "autocrlf", autocrlf );
1073 1033 config.save();
1074 1034
1075 1035 didClone = true;
1076 1036 System.out.println( "Cloned git repository " + url + " to " + target.getAbsolutePath() + ". Current HEAD: " + commitHash( result ) );
1077 - } finally
1078 - {
1079 - result.close();
1080 1037 }
1081 1038 }
1082 1039
1083 1040 public static String commitHash(Git repo) throws GitAPIException
1084 1041 {
1085 1042 return Iterables.getOnlyElement( repo.log().setMaxCount( 1 ).call() ).getName();
1086 1043 }
1087 1044
1088 1045 public static File download(String url, File target) throws IOException
1089 1046 {
1143 1100 }
1144 1101 }
1145 1102 };
1146 1103
1147 1104 // Trust SSL certs
1148 1105 SSLContext sc = SSLContext.getInstance( "SSL" );
1149 1106 sc.init( null, trustAllCerts, new SecureRandom() );
1150 1107 HttpsURLConnection.setDefaultSSLSocketFactory( sc.getSocketFactory() );
1151 1108
1152 1109 // Trust host names
1153 - HostnameVerifier allHostsValid = new HostnameVerifier()
1154 - {
1155 - @Override
1156 - public boolean verify(String hostname, SSLSession session)
1157 - {
1158 - return true;
1159 - }
1160 - };
1161 - HttpsURLConnection.setDefaultHostnameVerifier( allHostsValid );
1162 - } catch ( NoSuchAlgorithmException ex )
1163 - {
1164 - System.out.println( "Failed to disable https certificate check" );
1165 - ex.printStackTrace( System.err );
1166 - } catch ( KeyManagementException ex )
1110 + HttpsURLConnection.setDefaultHostnameVerifier( (hostname, session) -> true );
1111 + } catch ( NoSuchAlgorithmException | KeyManagementException ex )
1167 1112 {
1168 1113 System.out.println( "Failed to disable https certificate check" );
1169 1114 ex.printStackTrace( System.err );
1170 1115 }
1171 1116 }
1172 1117
1173 1118 public static void logOutput()
1174 1119 {
1175 1120 try
1176 1121 {
1207 1152 {
1208 1153 @Override
1209 1154 @SuppressWarnings("deprecation")
1210 1155 public HashFunction getHash()
1211 1156 {
1212 1157 return Hashing.md5();
1213 1158 }
1214 1159 }, SHA1
1215 1160 {
1216 1161 @Override
1162 + @SuppressWarnings("deprecation")
1217 1163 public HashFunction getHash()
1218 1164 {
1219 1165 return Hashing.sha1();
1220 1166 }
1221 1167 }, SHA256
1222 1168 {
1223 1169 @Override
1224 1170 public HashFunction getHash()
1225 1171 {
1226 1172 return Hashing.sha256();

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut