-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement json loading for more versions
- Loading branch information
Showing
6 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ain/java/net/ornithemc/osl/resource/loader/impl/mixin/client/TranslationStorageMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package net.ornithemc.osl.resource.loader.impl.mixin.client; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import net.minecraft.client.resource.Resource; | ||
import net.minecraft.client.resource.language.TranslationStorage; | ||
import net.minecraft.client.resource.manager.ResourceManager; | ||
import net.minecraft.resource.Identifier; | ||
import org.quiltmc.parsers.json.JsonReader; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(TranslationStorage.class) | ||
public abstract class TranslationStorageMixin { | ||
|
||
@Shadow | ||
Map<String, String> translations; | ||
|
||
@WrapOperation( | ||
method = "load(Lnet/minecraft/client/resource/manager/ResourceManager;Ljava/util/List;)V", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/client/resource/language/TranslationStorage;load(Ljava/util/List;)V" | ||
) | ||
) | ||
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), | ||
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) { | ||
} | ||
} | ||
} | ||
|
||
@WrapOperation( | ||
method = "load(Ljava/util/List;)V", | ||
at = @At( | ||
value = "INVOKE", | ||
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); | ||
} else { | ||
original.call(instance, is); | ||
} | ||
} | ||
|
||
@Unique | ||
private void loadJson(InputStream stream) { | ||
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) { | ||
} | ||
translations.putAll(entries); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ain/java/net/ornithemc/osl/resource/loader/impl/mixin/client/TranslationStorageMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package net.ornithemc.osl.resource.loader.impl.mixin.client; | ||
|
||
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; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import net.minecraft.client.resource.language.TranslationStorage; | ||
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; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(TranslationStorage.class) | ||
public abstract class TranslationStorageMixin { | ||
|
||
@Final | ||
@Shadow | ||
protected Map<String, String> translations; | ||
|
||
@WrapOperation( | ||
method = "load(Lnet/minecraft/client/resource/manager/ResourceManager;Ljava/util/List;)V", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/client/resource/language/TranslationStorage;load(Ljava/util/List;)V" | ||
) | ||
) | ||
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), | ||
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) { | ||
} | ||
} | ||
} | ||
|
||
@WrapOperation( | ||
method = "load(Ljava/util/List;)V", | ||
at = @At( | ||
value = "INVOKE", | ||
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); | ||
} else { | ||
original.call(instance, is); | ||
} | ||
} | ||
|
||
@Unique | ||
private void loadJson(InputStream stream) { | ||
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) { | ||
} | ||
translations.putAll(entries); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters