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.
This commit is contained in:
Redned 2021-07-18 15:20:40 -05:00 committed by RednedEpic
parent b4921132e1
commit 1ad952b581
2 changed files with 129 additions and 117 deletions

View file

@ -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,11 +314,10 @@ public class LocaleUtils {
public static void init() {
// no-op
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class VersionManifest {
static class VersionManifest {
@JsonProperty("latest")
private LatestVersion latestVersion;
@ -325,7 +327,7 @@ class VersionManifest {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class LatestVersion {
static class LatestVersion {
@JsonProperty("release")
private String release;
@ -335,7 +337,7 @@ class LatestVersion {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class Version {
static class Version {
@JsonProperty("id")
private String id;
@ -354,7 +356,7 @@ class Version {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class VersionInfo {
static class VersionInfo {
@JsonProperty("id")
private String id;
@ -376,7 +378,7 @@ class VersionInfo {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class VersionDownload {
static class VersionDownload {
@JsonProperty("sha1")
private String sha1;
@ -389,7 +391,7 @@ class VersionDownload {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class AssetIndex {
static class AssetIndex {
@JsonProperty("id")
private String id;
@ -408,10 +410,11 @@ class AssetIndex {
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
class Asset {
static class Asset {
@JsonProperty("hash")
private String hash;
@JsonProperty("size")
private int size;
}
}

View file

@ -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;