fix assets loading bugs and language bugs

This commit is contained in:
Bixilon 2021-01-07 14:30:18 +01:00
parent 7774da2f2b
commit caad618d3f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 65 additions and 21 deletions

View File

@ -215,6 +215,8 @@ public class AssetsManager {
// generate jar assets index
generateJarAssets();
this.assetsMap.putAll(parseAssetsIndex(this.assetVersion.getJarAssetsHash()));
// download minosoft mappings
downloadAsset(AssetsSource.MINOSOFT_GIT, this.assetVersion.getMinosoftMappings());
@ -300,6 +302,10 @@ public class AssetsManager {
return readAssetByHash(this.assetsMap.get(name));
}
public String readStringAsset(String name) throws IOException {
return Util.readFile(new BufferedReader(readAsset(name)), true);
}
public InputStream readAssetAsStream(String name) throws IOException {
return readAssetAsStreamByHash(this.assetsMap.get(name));
}

View File

@ -24,7 +24,12 @@ public class Resources {
}
public static void loadVersion(String versionName, JsonObject json) {
loadVersion(Versions.getVersionByName(versionName), json);
Version version = Versions.getVersionByName(versionName);
if (version == null) {
// version not supported
return;
}
loadVersion(version, json);
}
public static void loadVersion(Version version, JsonObject json) {

View File

@ -243,7 +243,7 @@ public class Slot {
}
public String getLanguageName() {
// ToDo: What if an item identifier changed between versions? oOo
// ToDo: pre flattening names are completely broken
String[] keys = {String.format("item.%s.%s", this.item.getMod(), this.item.getIdentifier()), String.format("block.%s.%s", this.item.getMod(), this.item.getIdentifier())};
for (String key : keys) {
if (this.version.getLocaleManager().canTranslate(key)) {

View File

@ -13,9 +13,12 @@
package de.bixilon.minosoft.data.locale.minecraft;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class MinecraftLanguage {
private final String language;
@ -23,7 +26,21 @@ public class MinecraftLanguage {
protected MinecraftLanguage(String language, JsonObject json) {
this.language = language;
json.keySet().forEach((key) -> this.data.put(key.toLowerCase(), json.get(key).getAsString()));
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
this.data.put(entry.getKey().toLowerCase(Locale.ROOT), entry.getValue().getAsString());
}
}
protected MinecraftLanguage(String language, String string) {
this.language = language;
for (String line : string.split("\\r?\\n")) {
if (line.isBlank()) {
continue;
}
String[] splitLine = line.split("=", 2);
this.data.put(splitLine[0].toLowerCase(Locale.ROOT), splitLine[1]);
}
}
public String getLanguage() {

View File

@ -18,6 +18,8 @@ import de.bixilon.minosoft.util.logging.Log;
import java.io.IOException;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_18W02A;
public class MinecraftLocaleManager {
private final Version version;
private MinecraftLanguage language;
@ -34,18 +36,21 @@ public class MinecraftLocaleManager {
return this.language.translate(key, data);
}
private MinecraftLanguage loadLanguage(String language) throws IOException {
return new MinecraftLanguage(language, this.version.getAssetsManager().readJsonAsset(String.format("minecraft/lang/%s.json", language.toLowerCase())).getAsJsonObject());
private MinecraftLanguage loadLanguage(Version version, String language) throws IOException {
if (version.getVersionId() >= V_18W02A) {
return new MinecraftLanguage(language, this.version.getAssetsManager().readJsonAsset(String.format("minecraft/lang/%s.json", language.toLowerCase())).getAsJsonObject());
}
return new MinecraftLanguage(language, this.version.getAssetsManager().readStringAsset(String.format("minecraft/lang/%s.lang", language.toLowerCase())));
}
public void load(String language) {
public void load(Version version, String language) throws IOException {
long startTime = System.currentTimeMillis();
Log.verbose(String.format("Loading minecraft language file (%s) for %s", language, this.version));
try {
this.language = loadLanguage(language);
this.language = loadLanguage(version, language);
} catch (Exception e) {
e.printStackTrace();
Log.warn("Could not load minecraft language file: %s for %s", language, this.version);
throw e;
}
Log.verbose("Loaded minecraft language files for %s successfully in %dms", this.version, (System.currentTimeMillis() - startTime));
}

View File

@ -30,7 +30,6 @@ import de.bixilon.minosoft.util.logging.Log;
import de.bixilon.minosoft.util.logging.LogLevels;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
public class Version {
@ -151,22 +150,29 @@ public class Version {
}
public void initializeAssetManger(CountUpAndDownLatch latch) throws Exception {
if (this.assetsManager == null) {
this.assetsManager = new AssetsManager(Minosoft.getConfig().getBoolean(ConfigurationPaths.BooleanPaths.DEBUG_VERIFY_ASSETS), Resources.getAssetVersionByVersion(this));
this.assetsManager.downloadAllAssets(latch);
this.localeManager = new MinecraftLocaleManager(this);
if (this.assetsManager != null) {
return;
}
if (!isFlattened() && getVersionId() != ProtocolDefinition.PRE_FLATTENING_VERSION_ID) {
this.assetsManager = Versions.PRE_FLATTENING_VERSION.getAssetsManager();
this.localeManager = Versions.PRE_FLATTENING_VERSION.getLocaleManager();
return;
}
this.assetsManager = new AssetsManager(Minosoft.getConfig().getBoolean(ConfigurationPaths.BooleanPaths.DEBUG_VERIFY_ASSETS), Resources.getAssetVersionByVersion(this));
this.assetsManager.downloadAllAssets(latch);
this.localeManager = new MinecraftLocaleManager(this);
this.localeManager.load(this, Minosoft.getConfig().getString(ConfigurationPaths.StringPaths.GENERAL_LANGUAGE));
}
public void loadMappings(CountUpAndDownLatch latch) throws IOException {
public void load(CountUpAndDownLatch latch) throws Exception {
if (isLoaded()) {
// already loaded
return;
}
Version preFlatteningVersion = Versions.getVersionById(ProtocolDefinition.PRE_FLATTENING_VERSION_ID);
if (!isFlattened() && this != preFlatteningVersion && !preFlatteningVersion.isLoaded()) {
if (!isFlattened() && this != Versions.PRE_FLATTENING_VERSION && !Versions.PRE_FLATTENING_VERSION.isLoaded()) {
// no matter what, we need the version mapping for all pre flattening versions
preFlatteningVersion.loadMappings(latch);
Versions.PRE_FLATTENING_VERSION.load(latch);
}
if (isGettingLoaded()) {
// async: we don't wanna load this version twice, skip
@ -174,6 +180,7 @@ public class Version {
}
latch.countUp();
this.isGettingLoaded = true;
initializeAssetManger(latch);
Log.verbose(String.format("Loading mappings for version %s...", this));
long startTime = System.currentTimeMillis();

View File

@ -18,6 +18,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.protocol.protocol.ConnectionStates;
import de.bixilon.minosoft.protocol.protocol.Packets;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import java.util.HashMap;
@ -27,6 +28,7 @@ public class Versions {
private static final HashBiMap<Integer, Version> VERSION_PROTOCOL_ID_MAP = HashBiMap.create(500);
private static final HashBiMap<String, Version> VERSION_NAME_MAP = HashBiMap.create(500);
public static VersionMapping PRE_FLATTENING_MAPPING;
public static Version PRE_FLATTENING_VERSION;
public static Version getVersionById(int versionId) {
return VERSION_ID_MAP.get(versionId);
@ -93,6 +95,9 @@ public class Versions {
VERSION_ID_MAP.put(version.getVersionId(), version);
VERSION_PROTOCOL_ID_MAP.put(version.getProtocolId(), version);
VERSION_NAME_MAP.put(version.getVersionName(), version);
if (version.getVersionId() == ProtocolDefinition.PRE_FLATTENING_VERSION_ID) {
PRE_FLATTENING_VERSION = version;
}
return version;
}

View File

@ -129,18 +129,17 @@ public class Connection {
this.reason = ConnectionReasons.CONNECT;
setVersion(version);
try {
version.initializeAssetManger(latch); // ToDo
version.loadMappings(latch);
version.load(latch); // ToDo: show gui loader
this.customMapping.setVersion(version);
this.customMapping.setParentMapping(version.getMapping());
Log.info(String.format("Connecting to server: %s", address));
this.network.connect(address);
} catch (Exception e) {
Log.printException(e, LogLevels.DEBUG);
Log.fatal(String.format("Could not load mapping for %s. This version seems to be unsupported!", version));
this.lastException = new MappingsLoadingException("Mappings could not be loaded", e);
setConnectionState(ConnectionStates.FAILED_NO_RETRY);
}
Log.info(String.format("Connecting to server: %s", address));
this.network.connect(address);
}
public ServerAddress getAddress() {