Skip to content

Commit

Permalink
Remove google guava dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Jul 29, 2023
1 parent 2c57a11 commit 92f627f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 48 deletions.
100 changes: 76 additions & 24 deletions common/src/main/java/dev/booky/stackdeobf/http/VerifiableUrl.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dev.booky.stackdeobf.http;
// Created by booky10 in StackDeobfuscator (01:57 10.06.23)

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.net.URI;
import java.net.http.HttpRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand All @@ -21,20 +21,20 @@ public final class VerifiableUrl {

private final URI uri;
private final HashType hashType;
private final HashCode hashCode;
private final HashResult hashResult;

public VerifiableUrl(URI uri, HashType hashType, String hash) {
this(uri, hashType, HashCode.fromString(hash));
this(uri, hashType, new HashResult(hash));
}

public VerifiableUrl(URI uri, HashType hashType, byte[] hash) {
this(uri, hashType, HashCode.fromBytes(hash));
this(uri, hashType, new HashResult(hash));
}

public VerifiableUrl(URI uri, HashType hashType, HashCode hashCode) {
public VerifiableUrl(URI uri, HashType hashType, HashResult hashResult) {
this.uri = uri;
this.hashType = hashType;
this.hashCode = hashCode;
this.hashResult = hashResult;
}

public static CompletableFuture<VerifiableUrl> resolve(URI url, HashType hashType, Executor executor) {
Expand Down Expand Up @@ -81,13 +81,13 @@ public static CompletableFuture<VerifiableUrl> resolveByMd5Header(URI url, Execu

public CompletableFuture<byte[]> get(Executor executor) {
return HttpUtil.getAsync(this.uri, executor).thenApply(bytes -> {
HashCode hashCode = this.hashType.hash(bytes);
HashResult hashResult = this.hashType.hash(bytes);
LOGGER.info("Verifying {} hash {} for {}...",
this.hashType, hashCode, this.uri);
this.hashType, hashResult, this.uri);

if (!hashCode.equals(this.hashCode)) {
throw new FailedUrlVerificationException(this.hashType + " hash " + hashCode + " doesn't match "
+ this.hashCode + " for " + this.uri);
if (!hashResult.equals(this.hashResult)) {
throw new FailedUrlVerificationException(this.hashType + " hash " + hashResult + " doesn't match "
+ this.hashResult + " for " + this.uri);
}
return bytes;
});
Expand All @@ -99,27 +99,79 @@ public URI getUrl() {

public enum HashType {

@SuppressWarnings("deprecation")
MD5(".md5", Hashing.md5()),
@SuppressWarnings("deprecation")
SHA1(".sha1", Hashing.sha1()),
SHA256(".sha256", Hashing.sha256()),
SHA512(".sha512", Hashing.sha512());
MD5(".md5", "MD5"),
SHA1(".sha1", "SHA-1"),
SHA256(".sha256", "SHA-256"),
SHA512(".sha512", "SHA-512");

private final String extension;
private final HashFunction hashFunction;
private final MessageDigest digester;

HashType(String extension, HashFunction hashFunction) {
HashType(String extension, String algoName) {
this.extension = extension;
this.hashFunction = hashFunction;

try {
this.digester = MessageDigest.getInstance(algoName);
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException("Illegal algorithm provided: " + algoName, exception);
}
}

public HashCode hash(byte[] bytes) {
return this.hashFunction.hashBytes(bytes);
public HashResult hash(byte[] bytes) {
byte[] resultBytes;
synchronized (this.digester) {
resultBytes = this.digester.digest(bytes);
}
return new HashResult(resultBytes);
}

public String getExtension() {
return this.extension;
}
}

public record HashResult(byte[] bytes) {

private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();

public HashResult(String string) {
this(decode(string));
}

private static byte[] decode(String string) {
if (string.length() % 2 != 0) {
throw new IllegalArgumentException("Illegal hash string: " + string);
}

byte[] bytes = new byte[string.length() / 2];
for (int i = 0; i < string.length(); i += 2) {
int b = Character.digit(string.charAt(i), 16) << 4;
b |= Character.digit(string.charAt(i + 1), 16);
bytes[i / 2] = (byte) b;
}
return bytes;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof HashResult that)) return false;
return Arrays.equals(this.bytes, that.bytes);
}

@Override
public int hashCode() {
return Arrays.hashCode(this.bytes);
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder(2 * this.bytes.length);
for (byte eightBits : this.bytes) {
builder.append(HEX_DIGITS[(eightBits >> 4) & 0xF]);
builder.append(HEX_DIGITS[eightBits & 0xF]);
}
return builder.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.booky.stackdeobf.mappings;
// Created by booky10 in StackDeobfuscator (17:04 20.03.23)

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import dev.booky.stackdeobf.mappings.providers.AbstractMappingProvider;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
Expand All @@ -31,7 +31,7 @@ private CachedMappings() {
public static CompletableFuture<CachedMappings> create(Path cacheDir, AbstractMappingProvider provider) {
LOGGER.info("Creating asynchronous mapping cache executor...");
ExecutorService executor = Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder().setNameFormat("Mappings Cache Thread").setDaemon(true).build());
new BasicThreadFactory.Builder().namingPattern("Mappings Cache Thread #%d").daemon(true).build());

return create(cacheDir, provider, executor)
// needs to be executed asynchronously, otherwise the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.booky.stackdeobf.mappings.providers;
// Created by booky10 in StackDeobfuscator (14:35 23.03.23)

import com.google.common.base.Preconditions;
import dev.booky.stackdeobf.util.VersionData;
import net.fabricmc.mappingio.MappingVisitor;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -43,7 +42,9 @@ private static Path getCacheDir(Path fallbackDir) {
}
}

Preconditions.checkState(Files.isDirectory(cacheDir), cacheDir + " has to be a directory");
if (!Files.isDirectory(cacheDir)) {
throw new IllegalMonitorStateException(cacheDir + " has to be a directory");
}
return cacheDir;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.booky.stackdeobf.mappings.providers;
// Created by booky10 in StackDeobfuscator (17:42 23.03.23)

import com.google.common.base.Preconditions;
import dev.booky.stackdeobf.util.VersionData;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingVisitor;
Expand Down Expand Up @@ -125,8 +124,9 @@ private void readPath(Path path, MappingVisitor visitor, MappingFormat format) t
for (Path directory : archive.getRootDirectories()) {
try (Stream<Path> files = Files.list(directory)) {
for (Path subPath : files.toList()) {
Preconditions.checkState(singlePath == null,
"More than one file found in " + path.toAbsolutePath());
if (singlePath != null) {
throw new IllegalStateException("More than one file found in " + path.toAbsolutePath());
}
singlePath = subPath;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.booky.stackdeobf.mappings.providers;
// Created by booky10 in StackDeobfuscator (16:57 23.03.23)

import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -56,8 +55,11 @@ Use and modification of this document or the source code (in any form) of Minecr

public MojangMappingProvider(VersionData versionData, String environment) {
super(versionData, "mojang");
Preconditions.checkState(versionData.getWorldVersion() >= 2203 || versionData.getWorldVersion() == 1976,
"Mojang mappings are only provided by mojang starting from 19w36a (excluding 1.14.4)");

if (versionData.getWorldVersion() < 2203 && versionData.getWorldVersion() != 1976) {
throw new IllegalStateException("Mojang mappings are only provided by mojang starting from 19w36a (excluding 1.14.4)");
}

this.environment = environment.toLowerCase(Locale.ROOT);
this.intermediary = new IntermediaryMappingProvider(versionData);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.booky.stackdeobf.mappings.providers;
// Created by booky10 in StackDeobfuscator (22:06 23.03.23)

import com.google.common.base.Preconditions;
import dev.booky.stackdeobf.http.VerifiableUrl;
import dev.booky.stackdeobf.util.MavenArtifactInfo;
import dev.booky.stackdeobf.util.VersionData;
Expand Down Expand Up @@ -51,8 +50,9 @@ public QuiltMappingProvider(VersionData versionData) {
super(versionData, "quilt", versionData.getWorldVersion() >= 3120
? MAPPINGS_ARTIFACT_POST_1192 : MAPPINGS_ARTIFACT_PRE_1192,
VerifiableUrl.HashType.SHA512);
Preconditions.checkState(this.versionData.getWorldVersion() >= 2975,
"Quilt mappings are only supported for 1.18.2 and higher");
if (this.versionData.getWorldVersion() < 2975) {
throw new IllegalStateException("Quilt mappings are only supported for 1.18.2 and higher");
}
this.intermediary = new IntermediaryMappingProvider(versionData);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dev.booky.stackdeobf.util;
// Created by booky10 in StackDeobfuscator (20:21 30.03.23)

import com.google.common.base.Preconditions;
import dev.booky.stackdeobf.http.VerifiableUrl;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.util.Objects;
Expand All @@ -27,7 +26,9 @@ public MavenArtifactInfo(String repoUrl, String groupId, String artifactId, @Nul

public static MavenArtifactInfo parse(String repoUrl, String info) {
String[] split = StringUtils.split(info, ':');
Preconditions.checkState(split.length == 2 || split.length == 3, "Artifact info is invalid: " + info);
if (split.length != 2 && split.length != 3) {
throw new IllegalArgumentException("Artifact info is invalid: " + info);
}

String groupId = split[0], artifactId = split[1];
String classifier = split.length > 2 ? split[2] : null;
Expand Down
15 changes: 9 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[versions]
loom = "1.3-SNAPSHOT"
shadow = "8.1.1"

fabric-loader = "0.14.21"
fabric-mappingio = "0.3.0"
minecraft = "1.20.1"
Expand All @@ -8,16 +11,16 @@ builtin-log4j = "2.19.0"
builtin-commons-io = "2.11.0"
builtin-commons-lang3 = "3.12.0"
builtin-google-gson = "2.10"
builtin-google-guava = "31.1-jre"
builtin-fastutil = "8.5.9"
builtin-jetbrains-annotations = "24.0.1"

# web project
javalin = "5.6.1"
caffeine = "3.1.6"

[plugins]
loom = "fabric-loom:1.3-SNAPSHOT"
shadow = "com.github.johnrengelman.shadow:8.1.1"
loom = { id = "fabric-loom", version.ref = "loom" }
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }

[libraries]
fabric-loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabric-loader" }
Expand All @@ -31,8 +34,8 @@ builtin-log4j-slf4j2-impl = { module = "org.apache.logging.log4j:log4j-slf4j2-im
builtin-commons-io = { module = "commons-io:commons-io", version.ref = "builtin-commons-io" }
builtin-commons-lang3 = { module = "org.apache.commons:commons-lang3", version.ref = "builtin-commons-lang3" }
builtin-google-gson = { module = "com.google.code.gson:gson", version.ref = "builtin-google-gson" }
builtin-google-guava = { module = "com.google.guava:guava", version.ref = "builtin-google-guava" }
builtin-fastutil = { module = "it.unimi.dsi:fastutil", version.ref = "builtin-fastutil" }
builtin-jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "builtin-jetbrains-annotations" }

# web
javalin = { module = "io.javalin:javalin", version.ref = "javalin" }
Expand All @@ -43,8 +46,8 @@ log4j-iostreams = { module = "org.apache.logging.log4j:log4j-iostreams", version
# built in minecraft, required for compiling
builtin = ["builtin-log4j-api", "builtin-log4j-core",
"builtin-commons-io", "builtin-commons-lang3",
"builtin-google-gson", "builtin-google-guava",
"builtin-fastutil"]
"builtin-google-gson", "builtin-fastutil",
"builtin-jetbrains-annotations"]

# web
log4j = ["builtin-log4j-api", "builtin-log4j-core", "builtin-log4j-slf4j2-impl", "log4j-iostreams"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public final class StackDeobfMain {

private static final String HTTP_BIND = System.getProperty("web.bind", "localhost");
private static final int HTTP_PORT = Integer.getInteger("web.port", 8080);
private static final int HTTP_PORT = Integer.getInteger("web.port", 8082);

static {
System.setProperty("java.awt.headless", "true");
Expand Down

0 comments on commit 92f627f

Please sign in to comment.