Skip to content

Improve language loader #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.MalformedJsonException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -75,14 +73,9 @@ public Map<String, String> getLanguage(String languageCode) {

this.files.getOrDefault(languageCode, Collections.emptyList()).forEach(file -> {
try (InputStream inputStream = Files.newInputStream(file)) {
JsonUtil.loadStringMapFromJson(inputStream, result::put);
JsonUtil.loadLanguageMapFromJson(inputStream, result::put);
MagicLib.getLogger().debug("Loaded language file {}.", file);
} catch (Exception e) {
if (e instanceof JsonSyntaxException && e.getCause() instanceof MalformedJsonException) {
MagicLib.getLogger().error("Failed to load language file {}.", file);
return;
}

MagicLib.getLogger().error("Failed to load language file {}.", file, e);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,15 @@ private void loadFromJar(@NotNull JarFile jar) {

@Internal
public static boolean loadFromEntry(@NotNull ZipEntry entry, InputStream inputStream,
Function<String, Map<String, String>> languageMapGetter) {
Function<String, Map<String, String>> languageMapGetter) {
Matcher matcher = LanguageProvider.LANGUAGE_PATH_PATTERN.matcher(entry.getName());

if (!matcher.find()) {
return false;
}

Map<String, String> language = languageMapGetter.apply(matcher.group(2));

try {
JsonUtil.loadStringMapFromJson(inputStream, language::put);
return true;
} catch (Exception e) {
return false;
}
JsonUtil.loadLanguageMapFromJson(inputStream, language::put);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package top.hendrixshen.magiclib.util;

import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand All @@ -12,10 +14,32 @@

public class JsonUtil {
public static void loadStringMapFromJson(InputStream inputStream, BiConsumer<String, String> biConsumer) {
JsonObject jsonObject = GsonUtil.GSON.fromJson(new InputStreamReader(inputStream,
StandardCharsets.UTF_8), JsonObject.class);
jsonObject.entrySet().forEach(entry -> biConsumer.accept(entry.getKey(),
entry.getValue().getAsString()));
JsonUtil.loadLanguageMapFromJson(inputStream, biConsumer);
}

public static void loadLanguageMapFromJson(InputStream inputStream, BiConsumer<String, String> biConsumer) {
try (
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
JsonReader reader = new JsonReader(inputStreamReader)
) {
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
reader.beginObject();

while (reader.hasNext()) {
String key = reader.nextName();

if (reader.peek() == JsonToken.STRING) {
biConsumer.accept(key, reader.nextString());
} else {
reader.skipValue();
}
}

reader.endObject();
}
} catch (IOException ignore) {
// ignore.
}
}

public static JsonObject readJson(@NotNull URL url) throws IOException {
Expand Down