Skip to content

Commit

Permalink
additionally support loading lang files on 1.13+
Browse files Browse the repository at this point in the history
  • Loading branch information
moehreag committed Jan 28, 2024
1 parent 7b4360a commit d5c97a1
Showing 1 changed file with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.ornithemc.osl.resource.loader.impl.mixin.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.List;
Expand All @@ -15,7 +15,6 @@
import net.minecraft.client.resource.manager.ResourceManager;
import net.minecraft.resource.Identifier;
import net.minecraft.resource.Resource;
import org.quiltmc.parsers.json.JsonReader;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -38,15 +37,12 @@ public abstract class TranslationStorageMixin {
)
private void osl$resource_loader$loadJsonFiles(TranslationStorage instance, List<Resource> resources, Operation<Void> original,
@Local(ordinal = 0) String languageCode, @Local(ordinal = 1) String originalPath,
@Local(ordinal = 2) String namespace, @Local(argsOnly = true) ResourceManager manager){
String[] paths = new String[]{originalPath, String.format("lang/%s.json", languageCode),
@Local(ordinal = 2) String namespace, @Local(argsOnly = true) ResourceManager manager) throws IOException{
String[] paths = new String[]{originalPath, String.format("lang/%s.lang", languageCode),
String.format("lang/%s.json", languageCode.toLowerCase(Locale.ROOT)),
String.format("lang/%s.lang", languageCode.toLowerCase(Locale.ROOT))};
for (String s : paths){
try {
original.call(instance, manager.getResources(new Identifier(namespace, s)));
} catch (IOException ignored) {
}
original.call(instance, manager.getResources(new Identifier(namespace, s)));
}
}

Expand All @@ -57,30 +53,23 @@ public abstract class TranslationStorageMixin {
target = "Lnet/minecraft/client/resource/language/TranslationStorage;load(Ljava/io/InputStream;)V"
)
)
private void osl$resource_loader$loadJsonFiles$2(TranslationStorage instance, InputStream is, Operation<Void> original, @Local Resource resource){
if (resource.getLocation().getPath().endsWith(".json")){
loadJson(is);
private void osl$resource_loader$loadJsonFiles$2(TranslationStorage instance, InputStream is, Operation<Void> original, @Local Resource resource) throws IOException {
if (resource.getLocation().getPath().endsWith(".lang")){
loadLang(is);
} else {
original.call(instance, is);
}
}

@Unique
private void loadJson(InputStream stream) {
private void loadLang(InputStream stream) throws IOException {
if (stream == null) {
return;
}
JsonReader reader = JsonReader.json(new InputStreamReader(stream));

Map<String, String> entries = new HashMap<>();
try {
reader.beginObject();
while (reader.hasNext()) {
entries.put(reader.nextName(), reader.nextString());
}
reader.endObject();
} catch (IOException ignored) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
reader.lines().filter(l -> !l.startsWith("#"))
.map(line -> line.split("=", 2)).forEach(key -> translations.put(key[0], key[1]));
}
translations.putAll(entries);
}
}

0 comments on commit d5c97a1

Please sign in to comment.