| 1 | package com.mojang.realmsclient.dto;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.realmsclient.util.JsonUtils;
|
| 7 | import java.time.Instant;
|
| 8 | import java.time.ZoneId;
|
| 9 | import java.time.ZonedDateTime;
|
| 10 | import java.util.HashMap;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Map.Entry;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 | import org.slf4j.Logger;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class Backup extends ValueObject {
|
| 20 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 21 | public final String backupId;
|
| 22 | public final Instant lastModified;
|
| 23 | public final long size;
|
| 24 | public boolean uploadedVersion;
|
| 25 | public final Map<String, String> metadata;
|
| 26 | public final Map<String, String> changeList = new HashMap<>();
|
| 27 |
|
| 28 | private Backup(final String backupId, final Instant lastModified, final long size, final Map<String, String> metadata) {
|
| 29 | this.backupId = backupId;
|
| 30 | this.lastModified = lastModified;
|
| 31 | this.size = size;
|
| 32 | this.metadata = metadata;
|
| 33 | }
|
| 34 |
|
| 35 | public ZonedDateTime lastModifiedDate() {
|
| 36 | return ZonedDateTime.ofInstant(this.lastModified, ZoneId.systemDefault());
|
| 37 | }
|
| 38 |
|
| 39 | public static @Nullable Backup parse(final JsonElement node) {
|
| 40 | JsonObject object = node.getAsJsonObject();
|
| 41 |
|
| 42 | try {
|
| 43 | String backupId = JsonUtils.getStringOr("backupId", object, "");
|
| 44 | Instant lastModifiedDate = JsonUtils.getDateOr("lastModifiedDate", object);
|
| 45 | long size = JsonUtils.getLongOr("size", object, 0L);
|
| 46 | Map<String, String> metadata = new HashMap<>();
|
| 47 | if (object.has("metadata")) {
|
| 48 | JsonObject metadataObject = object.getAsJsonObject("metadata");
|
| 49 |
|
| 50 | for (Entry<String, JsonElement> elem : metadataObject.entrySet()) {
|
| 51 | if (!elem.getValue().isJsonNull()) {
|
| 52 | metadata.put(elem.getKey(), elem.getValue().getAsString());
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | return new Backup(backupId, lastModifiedDate, size, metadata);
|
| 58 | } catch (Exception e) {
|
| 59 | LOGGER.error("Could not parse Backup", e);
|
| 60 | return null;
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|