| 1 | package com.mojang.realmsclient.dto;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.realmsclient.util.JsonUtils;
|
| 7 | import java.net.URI;
|
| 8 | import java.net.URISyntaxException;
|
| 9 | import java.util.regex.Matcher;
|
| 10 | import java.util.regex.Pattern;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.util.LenientJsonParser;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public record UploadInfo(boolean worldClosed, @Nullable String token, URI uploadEndpoint) {
|
| 19 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 20 | private static final String DEFAULT_SCHEMA = "http://";
|
| 21 | private static final int DEFAULT_PORT = 8080;
|
| 22 | private static final Pattern URI_SCHEMA_PATTERN = Pattern.compile("^[a-zA-Z][-a-zA-Z0-9+.]+:");
|
| 23 |
|
| 24 | public static @Nullable UploadInfo parse(final String json) {
|
| 25 | try {
|
| 26 | JsonObject jsonObject = LenientJsonParser.parse(json).getAsJsonObject();
|
| 27 | String endpointStr = JsonUtils.getStringOr("uploadEndpoint", jsonObject, null);
|
| 28 | if (endpointStr != null) {
|
| 29 | int endpointPort = JsonUtils.getIntOr("port", jsonObject, -1);
|
| 30 | URI uploadEndpoint = assembleUri(endpointStr, endpointPort);
|
| 31 | if (uploadEndpoint != null) {
|
| 32 | boolean worldClosed = JsonUtils.getBooleanOr("worldClosed", jsonObject, false);
|
| 33 | String token = JsonUtils.getStringOr("token", jsonObject, null);
|
| 34 | return new UploadInfo(worldClosed, token, uploadEndpoint);
|
| 35 | }
|
| 36 | }
|
| 37 | } catch (Exception e) {
|
| 38 | LOGGER.error("Could not parse UploadInfo", e);
|
| 39 | }
|
| 40 |
|
| 41 | return null;
|
| 42 | }
|
| 43 |
|
| 44 | @VisibleForTesting
|
| 45 | public static @Nullable URI assembleUri(final String endpoint, final int portOverride) {
|
| 46 | Matcher matcher = URI_SCHEMA_PATTERN.matcher(endpoint);
|
| 47 | String endpointWithSchema = ensureEndpointSchema(endpoint, matcher);
|
| 48 |
|
| 49 | try {
|
| 50 | URI result = new URI(endpointWithSchema);
|
| 51 | int selectedPort = selectPortOrDefault(portOverride, result.getPort());
|
| 52 | return selectedPort != result.getPort()
|
| 53 | ? new URI(result.getScheme(), result.getUserInfo(), result.getHost(), selectedPort, result.getPath(), result.getQuery(), result.getFragment())
|
| 54 | : result;
|
| 55 | } catch (URISyntaxException e) {
|
| 56 | LOGGER.warn("Failed to parse URI {}", endpointWithSchema, e);
|
| 57 | return null;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | private static int selectPortOrDefault(final int portOverride, final int parsedPort) {
|
| 62 | if (portOverride != -1) {
|
| 63 | return portOverride;
|
| 64 | } else {
|
| 65 | return parsedPort != -1 ? parsedPort : 8080;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | private static String ensureEndpointSchema(final String endpoint, final Matcher matcher) {
|
| 70 | return matcher.find() ? endpoint : "http://" + endpoint;
|
| 71 | }
|
| 72 |
|
| 73 | public static String createRequest(final @Nullable String uploadToken) {
|
| 74 | JsonObject request = new JsonObject();
|
| 75 | if (uploadToken != null) {
|
| 76 | request.addProperty("token", uploadToken);
|
| 77 | }
|
| 78 |
|
| 79 | return request.toString();
|
| 80 | }
|
| 81 | }
|
| 82 |
|