Respackopts/src/main/java/io/gitlab/jfronny/respackopts/Respackopts.java

127 lines
4.9 KiB
Java
Raw Normal View History

2020-11-24 22:04:13 +01:00
package io.gitlab.jfronny.respackopts;
import io.gitlab.jfronny.gson.*;
2022-06-07 13:52:19 +02:00
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.respackopts.filters.*;
import io.gitlab.jfronny.respackopts.gson.*;
import io.gitlab.jfronny.respackopts.gson.entry.*;
import io.gitlab.jfronny.respackopts.integration.*;
import io.gitlab.jfronny.respackopts.model.*;
import io.gitlab.jfronny.respackopts.model.tree.*;
import io.gitlab.jfronny.respackopts.util.*;
import net.fabricmc.api.*;
import net.fabricmc.loader.api.*;
import net.minecraft.client.*;
import net.minecraft.server.integrated.*;
import net.minecraft.util.*;
import org.slf4j.*;
2020-11-24 22:04:13 +01:00
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
2020-11-24 22:04:13 +01:00
@Environment(EnvType.CLIENT)
public class Respackopts implements ClientModInitializer {
public static final Integer META_VERSION = 8;
public static final String FILE_EXTENSION = ".rpo";
public static final Gson GSON;
2021-06-10 13:10:12 +02:00
public static final String ID = "respackopts";
2022-01-23 15:27:00 +01:00
public static final Logger LOGGER = LoggerFactory.getLogger(ID);
public static final Identifier CONF_ID = new Identifier(ID, "conf.json");
2021-09-15 18:37:07 +02:00
public static final Set<Runnable> SAVE_ACTIONS = new HashSet<>();
public static final GuiFactory GUI_FACTORY = new GuiFactory();
2021-08-24 17:42:46 +02:00
2021-09-15 16:45:54 +02:00
public static boolean forcePackReload = false;
public static Path FALLBACK_CONF_DIR;
public static ConfigFile CONFIG;
2021-10-03 19:26:26 +02:00
public static final Identifier RPO_SHADER_ID = new Identifier(Respackopts.ID, "config_supplier");
private static String shaderImportSource;
2021-06-10 13:10:12 +02:00
static {
GSON = new GsonBuilder()
.registerTypeAdapter(ConfigEnumEntry.class, new EnumEntrySerializer())
.registerTypeAdapter(ConfigNumericEntry.class, new NumericEntrySerializer())
.registerTypeAdapter(ConfigBooleanEntry.class, new BooleanEntrySerializer())
.registerTypeAdapter(ConfigBranch.class, new ConfigBranchSerializer())
.registerTypeAdapter(Expr.class, new ExprDeserializer())
2022-06-07 13:52:19 +02:00
.registerTypeAdapter(StringExpr.class, new StringExprDeserializer())
.registerTypeAdapter(BoolExpr.class, new BoolExprDeserializer())
.registerTypeAdapter(Condition.class, new ConditionDeserializer())
.setLenient()
.setPrettyPrinting()
.create();
2021-08-24 17:42:46 +02:00
try {
FALLBACK_CONF_DIR = FabricLoader.getInstance().getConfigDir().resolve(ID);
CONFIG = ConfigFile.load();
2021-08-24 17:42:46 +02:00
} catch (Throwable e) {
LOGGER.error("Could not resolve config directory", e);
}
2021-06-10 13:10:12 +02:00
}
2020-11-24 22:04:13 +01:00
@Override
public void onInitializeClient() {
try {
Files.createDirectories(FALLBACK_CONF_DIR);
} catch (IOException e) {
LOGGER.error("Could not initialize config directory", e);
}
if (CONFIG.debugLogs)
SAVE_ACTIONS.add(() -> LOGGER.info("Save"));
2021-10-03 19:26:26 +02:00
SAVE_ACTIONS.add(() -> {
if (CONFIG.debugLogs)
LOGGER.info("Generating shader code");
2021-10-03 19:26:26 +02:00
StringBuilder sb = new StringBuilder();
sb.append("#ifndef respackopts_loaded");
sb.append("\n#define respackopts_loaded");
MetaCache.forEach((key, state) -> state.configBranch().buildShader(sb, sanitizeString(state.packId())));
sb.append("\n#endif");
2021-10-03 19:26:26 +02:00
shaderImportSource = sb.toString();
});
2021-09-15 16:45:54 +02:00
DirFilterEventImpl.init();
FileFilterEventImpl.init();
if (CONFIG.debugCommands)
2021-09-15 18:37:07 +02:00
RpoCommand.register();
if (FabricLoader.getInstance().isModLoaded("frex")) {
FrexCompat.onInitializeFrex();
}
}
public static String sanitizeString(String s) {
// This trims whitespace/underscores and removes non-alphabetical or underscore characters
// ^ = start of string
// $ = end of string
// * = zero or more times
// [\\s_] = whitespace or underscores
// | = or
// [^a-zA-Z_] = not character or underscore
return s.replaceAll("[^a-zA-Z_]|^[\\s_]*|[\\s_]*$", "");
}
2021-08-24 17:42:46 +02:00
public static CompletableFuture<Void> forceReloadResources() {
if (CONFIG.debugLogs)
LOGGER.info("Forcing resource reload");
2021-08-24 17:42:46 +02:00
return CompletableFuture.allOf(MinecraftClient.getInstance().reloadResources(),
reloadData());
}
public static CompletableFuture<Void> reloadData() {
IntegratedServer is = MinecraftClient.getInstance().getServer();
if (is != null) {
is.getDataPackManager().scanPacks();
return is.reloadResources(is.getDataPackManager().getEnabledNames());
}
return CompletableFuture.completedFuture(null);
}
2021-10-03 19:26:26 +02:00
public static String getShaderImportSource() {
if (shaderImportSource == null) {
Respackopts.LOGGER.error("Shader import source is null");
return "";
}
return shaderImportSource;
}
2020-11-24 22:04:13 +01:00
}