fix: Minecraft 1.20 crashing the semver parser #12

Merged
Ecorous merged 6 commits from fix/semver-crash-mc1.20 into main 2024-07-07 07:50:52 -04:00
2 changed files with 8 additions and 4 deletions
Showing only changes of commit de85222cbf - Show all commits

View file

@ -4,6 +4,6 @@ import java.io.IOException;
public class SemVerParseException extends IOException {
public SemVerParseException(String message) {
super("Failed to parse SemVer: " + message);
super("Failed to parse SemVer: `" + message + "`");
}
}

View file

@ -9,12 +9,14 @@ import java.util.regex.Pattern;
import dev.frogmc.frogloader.api.mod.SemVer;
import dev.frogmc.frogloader.impl.SemVerParseException;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.jetbrains.annotations.Nullable;
public record SemVerImpl(int major, int minor, int patch, String prerelease, String build) implements SemVer {
// Adapted from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
private static final Pattern SEMVER_PATTERN = Pattern.compile("^(?<major>0|[1-9]\\d*)\\." +
"(?<minor>0|[1-9]\\d*)\\." +
"(?<patch>0|[1-9]\\d*)" +
"(?<minor>0|[1-9]\\d*)" +
"(\\.(?<patch>0|[1-9]\\d*))?" +
"(?:-(?<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)" +
"(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?" +
"(?:\\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$");
@ -27,7 +29,9 @@ public record SemVerImpl(int major, int minor, int patch, String prerelease, Str
int major = Integer.parseInt(matcher.group("major"));
int minor = Integer.parseInt(matcher.group("minor"));
int patch = Integer.parseInt(matcher.group("patch"));
// minecraft treats the `patch` component as optional...
@Nullable String patchString = matcher.group("patch");
int patch = patchString == null ? 0 : Integer.parseInt(matcher.group("patch"));
owlsys marked this conversation as resolved Outdated

nitpick: could use the previously assigned variable in the Integer.parseInt call

nitpick: could use the previously assigned variable in the `Integer.parseInt` call
Outdated
Review

wdym?

wdym?
int patch = patchString == null ? 0 : Integer.parseInt(matcher.group(patchString));
```java int patch = patchString == null ? 0 : Integer.parseInt(matcher.group(patchString)); ```
Outdated
Review

i'm not understanding sorry, what should i change it to?

i'm not understanding sorry, what should i change it to?
Outdated
Review

OH, got it, forgot to actually us the var i added lol

OH, got it, forgot to actually us the var i added lol
String prerelease = matcher.group("prerelease");
String buildmetadata = matcher.group("buildmetadata");
return new SemVerImpl(major, minor, patch, prerelease, buildmetadata);