Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Keep track of paperd protocol file in Paperclip as well
Browse files Browse the repository at this point in the history
paperd reads this file to determine the protocol version the jar
supports. Since Paperclip wraps our output jar too we need to include
this protocol version file in Paperclip as well.

Also this commits refactors and modernizes the project in general,
updating / removing dependencies, cleaning up the code, and moving the
code to the io.papermc package.
  • Loading branch information
DenWav committed Jul 18, 2019
1 parent f6c2a15 commit 2c36e4b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 101 deletions.
27 changes: 8 additions & 19 deletions pom.xml
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.destroystokyo</groupId>
<groupId>io.papermc</groupId>
<artifactId>paperclip-maven-plugin</artifactId>
<version>1.4-SNAPSHOT</version>
<version>1.5-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Paperclip Maven Plugin</name>
Expand All @@ -29,11 +29,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<version>3.6.0</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>

<executions>
<execution>
<id>mojo-descriptor</id>
Expand All @@ -46,7 +45,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -66,38 +65,28 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>3.0-alpha-2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.jbsdiff</groupId>
<artifactId>jbsdiff</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<version>2.8.5</version>
</dependency>
</dependencies>
</project>

This file was deleted.

@@ -1,93 +1,116 @@
/*
* Paperclip Maven Plugin - Generates the Data class + patch file for Paperclip
*
* Copyright (c) 2016 Kyle Wood (DemonWav)
* https://github.com/PaperSpigot/Paper
* Copyright (c) 2019 Kyle Wood (DemonWav)
* https://github.com/PaperMC/PaperclipMavenPlugin
*
* MIT License
*/

package com.destroystokyo.paperclipmavenplugin;
package io.papermc.paperclipmavenplugin;

import com.google.gson.Gson;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.jbsdiff.Diff;
import org.jbsdiff.InvalidHeaderException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Mojo(name = "generate-data", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class GenerateDataMojo extends AbstractMojo {

private static final String PROTOCOL_FILE = "com.destroystokyo.paper.daemon.protocol";

@Parameter(required = true)
private File vanillaMinecraft;
private Path vanillaMinecraft;

@Parameter(required = true)
private File paperMinecraft;
private Path paperMinecraft;

@Parameter(defaultValue = "target/generated-resources")
private File generatedResourceLocation;
private Path generatedResourceLocation;

@Parameter(required = true)
private String mcVersion;

public GenerateDataMojo() {
}

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
final File patch = new File(generatedResourceLocation, "paperMC.patch");
final File json = new File(generatedResourceLocation, "patch.json");
public void execute() throws MojoExecutionException {
final Path patch = generatedResourceLocation.resolve("paperMC.patch");
final Path json = generatedResourceLocation.resolve("patch.json");
final Path protocol = generatedResourceLocation.resolve("META-INF/" + PROTOCOL_FILE);

// Create the directory if needed
if (!generatedResourceLocation.exists()) {
if (Files.notExists(generatedResourceLocation)) {
try {
FileUtils.forceMkdir(generatedResourceLocation);
try {
FileUtils.forceDelete(patch);
} catch (FileNotFoundException ignored) {}
Files.createDirectories(generatedResourceLocation);
Files.deleteIfExists(patch);
} catch (IOException e) {
throw new MojoExecutionException("Could not create source directory", e);
}
}

if (!vanillaMinecraft.exists()) {
if (Files.notExists(vanillaMinecraft)) {
throw new MojoExecutionException("vanillaMinecraft jar does not exist!");
}

if (!paperMinecraft.exists()) {
if (Files.notExists(paperMinecraft)) {
throw new MojoExecutionException("paperMinecraft jar does not exist!");
}

final URI zipUri;
try {
final URI jarUri = paperMinecraft.toUri();
zipUri = new URI("jar:" + jarUri.getScheme(), jarUri.getPath(), null);
} catch (final URISyntaxException e) {
throw new MojoExecutionException("Failed to create jar URI for " + paperMinecraft);
}

protocolCheck:
try (final FileSystem zipFs = FileSystems.newFileSystem(zipUri, new HashMap<>())) {
final Path protocolPath = zipFs.getPath("META-INF", PROTOCOL_FILE);
if (Files.notExists(protocolPath)) {
Files.deleteIfExists(protocol);
break protocolCheck;
}

Files.createDirectories(protocol.getParent());
Files.copy(protocolPath, protocol, StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) {
throw new MojoExecutionException("Failed to read " + paperMinecraft + " contents", e);
}

// Read the files into memory
getLog().info("Reading jars into memory");
final byte[] vanillaMinecraftBytes;
final byte[] paperMinecraftBytes;
try {
vanillaMinecraftBytes = Files.readAllBytes(vanillaMinecraft.toPath());
paperMinecraftBytes = Files.readAllBytes(paperMinecraft.toPath());
vanillaMinecraftBytes = Files.readAllBytes(vanillaMinecraft);
paperMinecraftBytes = Files.readAllBytes(paperMinecraft);
} catch (IOException e) {
throw new MojoExecutionException("Error reading jars", e);
}

getLog().info("Creating patch");
try (final FileOutputStream paperMinecraftPatch = new FileOutputStream(patch)) {
try (final OutputStream paperMinecraftPatch = Files.newOutputStream(patch)) {
Diff.diff(vanillaMinecraftBytes, paperMinecraftBytes, paperMinecraftPatch);
} catch (InvalidHeaderException | IOException | CompressorException e) {
} catch (final InvalidHeaderException | IOException | CompressorException e) {
throw new MojoExecutionException("Error creating patches", e);
}

Expand All @@ -100,7 +123,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

// Vanilla's URL uses a SHA1 hash of the vanilla server jar
// todo - bug demonwav about keeping this or what
final MessageDigest digestSha1;
try {
digestSha1 = MessageDigest.getInstance("SHA1");
Expand All @@ -114,23 +136,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
final byte[] paperMinecraftHash = digest.digest(paperMinecraftBytes);

final PatchData data = new PatchData();
data.setOriginalHash(toHex(vanillaMinecraftHash));
data.setPatchedHash(toHex(paperMinecraftHash));
data.setPatch("paperMC.patch");
data.setSourceUrl("https://launcher.mojang.com/v1/objects/" + toHex(vanillaSha1).toLowerCase() + "/server.jar");

data.setVersion(mcVersion);
data.originalHash = toHex(vanillaMinecraftHash);
data.patchedHash = toHex(paperMinecraftHash);
data.patch = "paperMC.patch";
data.sourceUrl = "https://launcher.mojang.com/v1/objects/" + toHex(vanillaSha1).toLowerCase() + "/server.jar";
data.version = mcVersion;

getLog().info("Writing json file");
Gson gson = new Gson();
String jsonString = gson.toJson(data);

try (
final FileOutputStream fs = new FileOutputStream(json);
final OutputStreamWriter writer = new OutputStreamWriter(fs)
) {
writer.write(jsonString);
} catch (IOException e) {
try (final BufferedWriter writer = Files.newBufferedWriter(json)) {
new Gson().toJson(data, writer);
} catch (final IOException e) {
e.printStackTrace();
}
}
Expand All @@ -142,4 +157,19 @@ private String toHex(final byte[] hash) {
}
return sb.toString();
}

@SuppressWarnings("unused")
public void setVanillaMinecraft(final File vanillaMinecraft) {
this.vanillaMinecraft = vanillaMinecraft.toPath();
}

@SuppressWarnings("unused")
public void setPaperMinecraft(final File paperMinecraft) {
this.paperMinecraft = paperMinecraft.toPath();
}

@SuppressWarnings("unused")
public void setGeneratedResourceLocation(final File generatedResourceLocation) {
this.generatedResourceLocation = generatedResourceLocation.toPath();
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/papermc/paperclipmavenplugin/PatchData.java
@@ -0,0 +1,18 @@
/*
* Paperclip Maven Plugin - Generates the Data class + patch file for Paperclip
*
* Copyright (c) 2019 Kyle Wood (DemonWav)
* https://github.com/PaperMC/PaperclipMavenPlugin
*
* MIT License
*/

package io.papermc.paperclipmavenplugin;

class PatchData {
String patch;
String sourceUrl;
String originalHash;
String patchedHash;
String version;
}

0 comments on commit 2c36e4b

Please sign in to comment.