3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-09-17 00:33:47 +02:00

Handle locale stuff downloading of main thread and add doc to Object2IntBiMap

Technically LocaleUtils is not thread safe and people running Geyser for the first time on slow internet connections may see some untranslated messages. However, we were seeing startup times of about 1+ minutes on these slow connections, and it's better that players see untranslated messages for a short period of time rather than having to wait over a minute for the program to start up.

Once the locale is installed, it doesn't need to be redownloaded again (unless there is a game update) and all translated messages will just work once this download is complete without clients needing to relog.
Dieser Commit ist enthalten in:
Redned 2021-07-18 15:20:40 -05:00 committet von RednedEpic
Ursprung b4921132e1
Commit 1ad952b581
2 geänderte Dateien mit 129 neuen und 117 gelöschten Zeilen

Datei anzeigen

@ -39,6 +39,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipFile;
public class LocaleUtils {
@ -56,14 +57,14 @@ public class LocaleUtils {
localesFolder.mkdir();
// Download the latest asset list and cache it
generateAssetCache();
downloadAndLoadLocale(LanguageUtils.getDefaultLocale());
generateAssetCache().whenComplete((aVoid, ex) -> downloadAndLoadLocale(LanguageUtils.getDefaultLocale()));
}
/**
* Fetch the latest versions asset cache from Mojang so we can grab the locale files later
*/
private static void generateAssetCache() {
private static CompletableFuture<Void> generateAssetCache() {
return CompletableFuture.supplyAsync(() -> {
try {
// Get the version manifest from Mojang
VersionManifest versionManifest = GeyserConnector.JSON_MAPPER.readValue(WebUtils.getBody("https://launchermeta.mojang.com/mc/game/version_manifest.json"), VersionManifest.class);
@ -103,6 +104,8 @@ public class LocaleUtils {
} catch (Exception e) {
GeyserConnector.getInstance().getLogger().error(LanguageUtils.getLocaleStringLog("geyser.locale.fail.asset_cache", (!e.getMessage().isEmpty() ? e.getMessage() : e.getStackTrace())));
}
return null;
});
}
/**
@ -311,31 +314,30 @@ public class LocaleUtils {
public static void init() {
// no-op
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class VersionManifest {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
static class VersionManifest {
@JsonProperty("latest")
private LatestVersion latestVersion;
@JsonProperty("versions")
private List<Version> versions;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class LatestVersion {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
static class LatestVersion {
@JsonProperty("release")
private String release;
@JsonProperty("snapshot")
private String snapshot;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class Version {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
static class Version {
@JsonProperty("id")
private String id;
@ -350,11 +352,11 @@ class Version {
@JsonProperty("releaseTime")
private String releaseTime;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class VersionInfo {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
static class VersionInfo {
@JsonProperty("id")
private String id;
@ -372,11 +374,11 @@ class VersionInfo {
@JsonProperty("downloads")
private Map<String, VersionDownload> downloads;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class VersionDownload {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
static class VersionDownload {
@JsonProperty("sha1")
private String sha1;
@ -385,11 +387,11 @@ class VersionDownload {
@JsonProperty("url")
private String url;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class AssetIndex {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
static class AssetIndex {
@JsonProperty("id")
private String id;
@ -404,14 +406,15 @@ class AssetIndex {
@JsonProperty("url")
private String url;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class Asset {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
static class Asset {
@JsonProperty("hash")
private String hash;
@JsonProperty("size")
private int size;
}
}

Datei anzeigen

@ -37,6 +37,15 @@ import org.jetbrains.annotations.NotNull;
import java.util.Map;
import java.util.Objects;
/**
* A primitive int BiMap implementation built around fastutil to
* reduce boxing and the memory footprint. Protocol has a
* {@link com.nukkitx.protocol.util.Int2ObjectBiMap} class, but it
* does not extend the Map interface making it difficult to utilize
* it in for loops and the registry system.
*
* @param <T> the value
*/
public class Object2IntBiMap<T> implements Object2IntMap<T> {
private final Object2IntMap<T> forwards;
private final Int2ObjectMap<T> backwards;