2020-11-24 22:04:13 +01:00
|
|
|
package io.gitlab.jfronny.respackopts;
|
|
|
|
|
2022-06-04 19:43:49 +02:00
|
|
|
import io.gitlab.jfronny.gson.*;
|
2022-06-07 13:52:19 +02:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.*;
|
2022-06-04 19:43:49 +02:00
|
|
|
import io.gitlab.jfronny.respackopts.filters.*;
|
2021-10-03 16:45:29 +02:00
|
|
|
import io.gitlab.jfronny.respackopts.gson.*;
|
2022-06-04 19:43:49 +02:00
|
|
|
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
|
|
|
|
2022-06-04 19:43:49 +02: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 {
|
2022-06-04 19:43:49 +02:00
|
|
|
public static final Integer META_VERSION = 8;
|
2021-10-03 16:45:29 +02:00
|
|
|
public static final String FILE_EXTENSION = ".rpo";
|
|
|
|
public static final Gson GSON;
|
2021-06-10 13:10:12 +02:00
|
|
|
|
2021-10-03 16:45:29 +02:00
|
|
|
public static final String ID = "respackopts";
|
2022-01-23 15:27:00 +01:00
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(ID);
|
2021-10-03 16:45:29 +02:00
|
|
|
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<>();
|
2021-10-03 16:45:29 +02:00
|
|
|
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;
|
2021-11-19 15:16:08 +01:00
|
|
|
public static Path FALLBACK_CONF_DIR;
|
2021-10-03 16:45:29 +02:00
|
|
|
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-10-03 16:45:29 +02:00
|
|
|
|
2021-06-10 13:10:12 +02:00
|
|
|
static {
|
2021-10-03 16:45:29 +02:00
|
|
|
GSON = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(ConfigEnumEntry.class, new EnumEntrySerializer())
|
|
|
|
.registerTypeAdapter(ConfigNumericEntry.class, new NumericEntrySerializer())
|
|
|
|
.registerTypeAdapter(ConfigBooleanEntry.class, new BooleanEntrySerializer())
|
|
|
|
.registerTypeAdapter(ConfigBranch.class, new ConfigBranchSerializer())
|
2022-06-04 19:43:49 +02:00
|
|
|
.registerTypeAdapter(Expr.class, new ExprDeserializer())
|
2022-06-07 13:52:19 +02:00
|
|
|
.registerTypeAdapter(StringExpr.class, new StringExprDeserializer())
|
|
|
|
.registerTypeAdapter(BoolExpr.class, new BoolExprDeserializer())
|
2022-06-04 19:43:49 +02:00
|
|
|
.setLenient()
|
2021-10-03 16:45:29 +02:00
|
|
|
.setPrettyPrinting()
|
|
|
|
.create();
|
2021-08-24 17:42:46 +02:00
|
|
|
try {
|
2021-11-19 15:16:08 +01:00
|
|
|
FALLBACK_CONF_DIR = FabricLoader.getInstance().getConfigDir().resolve(ID);
|
2021-10-03 16:45:29 +02:00
|
|
|
CONFIG = ConfigFile.load();
|
2021-08-24 17:42:46 +02:00
|
|
|
} catch (Throwable e) {
|
2021-10-03 16:45:29 +02:00
|
|
|
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() {
|
2020-12-28 11:31:12 +01:00
|
|
|
try {
|
2021-11-19 15:16:08 +01:00
|
|
|
Files.createDirectories(FALLBACK_CONF_DIR);
|
2020-12-28 11:31:12 +01:00
|
|
|
} catch (IOException e) {
|
2021-10-03 16:45:29 +02:00
|
|
|
LOGGER.error("Could not initialize config directory", e);
|
2020-12-28 11:31:12 +01:00
|
|
|
}
|
2021-10-03 16:45:29 +02:00
|
|
|
if (CONFIG.debugLogs)
|
|
|
|
SAVE_ACTIONS.add(() -> LOGGER.info("Save"));
|
2021-10-03 19:26:26 +02:00
|
|
|
SAVE_ACTIONS.add(() -> {
|
|
|
|
if (CONFIG.debugLogs)
|
2021-11-09 15:16:46 +01:00
|
|
|
LOGGER.info("Generating shader code");
|
2021-10-03 19:26:26 +02:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2021-10-06 17:38:43 +02:00
|
|
|
sb.append("#ifndef respackopts_loaded");
|
|
|
|
sb.append("\n#define respackopts_loaded");
|
2022-03-27 11:39:01 +02:00
|
|
|
MetaCache.forEach((key, state) -> state.configBranch().buildShader(sb, sanitizeString(state.packId())));
|
2021-10-06 17:38:43 +02:00
|
|
|
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();
|
2021-10-03 16:45:29 +02:00
|
|
|
if (CONFIG.debugCommands)
|
2021-09-15 18:37:07 +02:00
|
|
|
RpoCommand.register();
|
2021-11-17 16:02:37 +01:00
|
|
|
if (FabricLoader.getInstance().isModLoaded("frex")) {
|
|
|
|
FrexCompat.onInitializeFrex();
|
|
|
|
}
|
2020-11-25 14:28:29 +01:00
|
|
|
}
|
|
|
|
|
2021-07-08 16:27:31 +02:00
|
|
|
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() {
|
2021-10-03 16:45:29 +02:00
|
|
|
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
|
|
|
}
|