Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Auto downloader + Force patch #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 160 additions & 44 deletions src/main/java/org/spigotmc/patcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,182 @@

import com.google.common.hash.Hashing;
import com.google.common.io.Files;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import net.md_5.jbeat.Patcher;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main
{
import net.md_5.jbeat.Patcher;

public static void main(String[] args) throws Exception
{
if ( args.length != 3 )
{
System.out.println( "Welcome to the Spigot patch applicator." );
System.out.println( "In order to use this tool you will need to specify three command line arguments as follows:" );
System.out.println( "\tjava -jar SpigotPatcher.jar original.jar patch.bps output.jar" );
System.out.println( "This will apply the specified patch to the original jar and save it to the output jar" );
System.out.println( "Please ensure that you save your original jar for later use." );
System.out.println( "If you have any queries, please direct them to http://www.spigotmc.org/" );
public class Main {

public static void main(String[] args) throws Exception {
boolean forcePatch = false;

if (args.length < 3) {
System.out.println("Welcome to the Spigot patch applicator.");
System.out
.println("In order to use this tool you will need to specify three command line arguments as follows:");
System.out
.println("\tjava -jar SpigotPatcher.jar original.jar patch.bps output.jar");
System.out
.println("This will apply the specified patch to the original jar and save it to the output jar");
System.out
.println("Please ensure that you save your original jar for later use.");
System.out
.println("If you have any queries, please direct them to http://www.spigotmc.org/");
return;
}

File originalFile = new File( args[0] );
File patchFile = new File( args[1] );
File outputFile = new File( args[2] );

if ( !originalFile.canRead() )
{
System.err.println( "Specified original file " + originalFile + " does not exist or cannot be read!" );

File originalFile = new File(args[0]);
File patchFile = null;
if (args[1].equals("download")) {
// Download the latest patch file
patchFile = downloadPatch();
} else {
patchFile = new File(args[1]);
}
if (patchFile == null) {
return;
}
if ( !patchFile.canRead() )
{
System.err.println( "Specified patch file " + patchFile + " does not exist or cannot be read!!" );

File outputFile = new File(args[2]);
if (args.length == 4) {
if (args[3].equals("force")) {
forcePatch = true; // Force the patch (overwrite)
}
}

if (!originalFile.canRead()) {
System.err.println("Specified original file " + originalFile
+ " does not exist or cannot be read!");
return;
}
if ( outputFile.exists() )
{
System.err.println( "Specified output file " + outputFile + " exists, please remove it before running this program!" );
if (!patchFile.canRead()) {
System.err.println("Specified patch file " + patchFile
+ " does not exist or cannot be read!!");
return;
}
if ( !outputFile.createNewFile() )
{
System.out.println( "Could not create specified output file " + outputFile + " please ensure that it is in a valid directory which can be written to." );
if (outputFile.exists()) {
if (!forcePatch) {
System.err
.println("Specified output file "
+ outputFile
+ " exists, please remove it before running this program!");
return;
} else {
outputFile.delete();
}
}
if (!outputFile.createNewFile()) {
System.out
.println("Could not create specified output file "
+ outputFile
+ " please ensure that it is in a valid directory which can be written to.");
return;
}

System.out.println( "***** Starting patching process, please wait." );
System.out.println( "\tInput md5 Checksum: " + Files.hash( originalFile, Hashing.md5() ) );
System.out.println( "\tPatch md5 Checksum: " + Files.hash( patchFile, Hashing.md5() ) );

try
{
new Patcher( patchFile, originalFile, outputFile ).patch();
} catch ( Exception ex )
{
System.err.println( "***** Exception occured whilst patching file!" );
System.out.println("***** Starting patching process, please wait.");
System.out.println("\tInput md5 Checksum: "
+ Files.hash(originalFile, Hashing.md5()));
System.out.println("\tPatch md5 Checksum: "
+ Files.hash(patchFile, Hashing.md5()));

try {
new Patcher(patchFile, originalFile, outputFile).patch();
} catch (Exception ex) {
System.err.println("***** Exception occured whilst patching file!");
ex.printStackTrace();
outputFile.delete();
return;
}

System.out.println( "***** Your file has been patched and verified! We hope you enjoy using Spigot!" );
System.out.println( "\tOutput md5 Checksum: " + Files.hash( outputFile, Hashing.md5() ) );

System.out
.println("***** Your file has been patched and verified! We hope you enjoy using Spigot!");
System.out.println("\tOutput md5 Checksum: "
+ Files.hash(outputFile, Hashing.md5()));
}

public static File downloadPatch() {
List<String> allPatches = new ArrayList<String>();
try {
System.out.println("\tChecking spigot for the latest patch ...");
String url = "http://www.spigotmc.org/spigot-updates/";
// Fetch the index
URL address = new URL(url);
HttpURLConnection con = (HttpURLConnection) address
.openConnection();
con.setRequestMethod("GET");

// Cloudfare doesn't like empty user agents
con.setRequestProperty("User-Agent", "Mozilla/5.0");

BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

String sourceStr = response.toString();
Matcher m = Pattern.compile("spigot([-+]\\d+)([a-z])\\.bps")
.matcher(sourceStr);
while (m.find()) {
allPatches.add(m.group()); // Patches will be added twice (but
// we just need the last one)
// URL + Text
}
} catch (Exception ex) {
// Error
System.out
.println("\tSomething went wrong checking for the latest patch!");
System.out
.println("\tPlease download it manually from: http://spigotmc.org/spigot-updates/");
}
if (allPatches.size() != 0)
try {
System.out.println("\tDownloading latest file ["
+ allPatches.get(allPatches.size() - 1) + "] ...");
URL address = new URL("http://www.spigotmc.org/spigot-updates/"
+ allPatches.get(allPatches.size() - 1));
HttpURLConnection con = (HttpURLConnection) address
.openConnection();
con.setRequestMethod("GET");

// Cloudfare doesn't like empty user agents
con.setRequestProperty("User-Agent", "Mozilla/5.0");

File file = new File(allPatches.get(allPatches.size() - 1));
BufferedInputStream bis = new BufferedInputStream(
con.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file.getName()));
int i = 0;
while ((i = bis.read()) != -1) {
bos.write(i);
}
bos.flush();
bis.close();
bos.close();
return file;
} catch (Exception ex) {
// Error
System.out
.println("\tSomething went wrong downloading the latest patch!");
System.out
.println("\tPlease download it manually from: http://spigotmc.org/spigot-updates/");
}
return null;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No } Needed, Ending with 2x } (1 for the method downloadPatch) and 1 for the class