Created
June 22, 2019 18:26
-
-
Save JDLogic/38e778f4145170fa19e64b3672ead6c8 to your computer and use it in GitHub Desktop.
MC ASCII Font Loader
This file contains hidden or 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
package net.minecraftforge.fml.client; | |
import com.google.common.base.CharMatcher; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.gui.FontRenderer; | |
import net.minecraft.client.gui.fonts.Font; | |
import net.minecraft.client.gui.fonts.providers.GlyphProviderTypes; | |
import net.minecraft.client.gui.fonts.providers.IGlyphProvider; | |
import net.minecraft.client.renderer.texture.TextureManager; | |
import net.minecraft.resources.FallbackResourceManager; | |
import net.minecraft.resources.IResourcePack; | |
import net.minecraft.resources.ResourcePackType; | |
import net.minecraft.util.JSONUtils; | |
import net.minecraft.util.ResourceLocation; | |
import net.minecraftforge.fml.SidedProvider; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Arrays; | |
import java.util.function.Function; | |
import static net.minecraftforge.fml.Logging.SPLASH; | |
public class SimpleFontLoader | |
{ | |
private static Logger LOGGER = LogManager.getLogger(); | |
private static FallbackResourceManager resourceManager = new FallbackResourceManager(ResourcePackType.CLIENT_RESOURCES); | |
private static TextureManager textureManager = new TextureManager(resourceManager); | |
private static IResourcePack mcPack = Minecraft.getInstance().getPackFinder().getVanillaPack(); | |
private static String allowedChars; | |
private static CharMatcher disallowedCharMatcher; | |
static | |
{ | |
resourceManager.addResourcePack(mcPack); | |
} | |
public static String stripChars(String message) | |
{ | |
if (disallowedCharMatcher == null) return SidedProvider.STRIPCHARS.<Function<String, String>>get().apply(message); | |
return disallowedCharMatcher.removeFrom(net.minecraft.util.StringUtils.stripControlCodes(message)); | |
} | |
public static void cleanup() | |
{ | |
resourceManager = null; | |
textureManager = null; | |
mcPack = null; | |
allowedChars = null; | |
disallowedCharMatcher = null; | |
} | |
public static FontRenderer getFontRenderer() | |
{ | |
return loadFont(new ResourceLocation("font/default.json"), new ResourceLocation("font/ascii.png")); | |
} | |
private static FontRenderer loadFont(ResourceLocation fontJsonLoc, ResourceLocation fontTextureLoc) | |
{ | |
FontRenderer fontRenderer; | |
try(InputStream s = mcPack.getResourceStream(ResourcePackType.CLIENT_RESOURCES, fontJsonLoc)) | |
{ | |
Gson gson = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create(); | |
JsonArray jsonarray = JSONUtils.getJsonArray(JSONUtils.fromJson(gson, IOUtils.toString(s, StandardCharsets.UTF_8), JsonObject.class), "providers"); | |
for(int i = jsonarray.size() - 1; i >= 0; --i) | |
{ | |
JsonObject provider = JSONUtils.getJsonObject(jsonarray.get(i), "providers[" + i + "]"); | |
if (JSONUtils.hasField(provider, "file")) | |
{ | |
String id = JSONUtils.getString(provider, "file"); | |
if (id.equals(fontTextureLoc.toString())) | |
{ | |
GlyphProviderTypes providerType = GlyphProviderTypes.byName(JSONUtils.getString(provider, "type")); | |
IGlyphProvider iglyphprovider = providerType.getFactory(provider).create(resourceManager); | |
if (iglyphprovider == null) throw new RuntimeException("Error creating glyph provider"); | |
fontRenderer = new FontRenderer(textureManager, new Font(textureManager, new ResourceLocation(id))); | |
fontRenderer.setGlyphProviders(Arrays.asList(iglyphprovider)); | |
allowedChars = ""; | |
if (JSONUtils.hasField(provider, "chars")) | |
{ | |
JsonArray charArray = JSONUtils.getJsonArray(provider, "chars"); | |
charArray.forEach(e -> allowedChars = allowedChars.concat(e.getAsString())); | |
disallowedCharMatcher = CharMatcher.anyOf(allowedChars).negate(); | |
} | |
else | |
{ | |
disallowedCharMatcher = null; | |
} | |
return fontRenderer; | |
} | |
} | |
} | |
throw new RuntimeException("Could not find provider for " + fontTextureLoc); | |
} | |
catch(IOException | RuntimeException e) | |
{ | |
LOGGER.error(SPLASH,"Error loading font json from file: {}", fontJsonLoc, e); | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment