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

159 lines
6.8 KiB
Java
Raw Normal View History

2020-11-24 22:04:13 +01:00
package io.gitlab.jfronny.respackopts;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
2021-09-15 16:45:54 +02:00
import io.gitlab.jfronny.respackopts.filters.DirFilterEventImpl;
import io.gitlab.jfronny.respackopts.filters.FileFilterEventImpl;
import io.gitlab.jfronny.respackopts.gson.*;
import io.gitlab.jfronny.respackopts.gson.entry.BooleanEntrySerializer;
import io.gitlab.jfronny.respackopts.gson.entry.ConfigBranchSerializer;
import io.gitlab.jfronny.respackopts.gson.entry.EnumEntrySerializer;
import io.gitlab.jfronny.respackopts.gson.entry.NumericEntrySerializer;
import io.gitlab.jfronny.respackopts.integration.FrexCompat;
import io.gitlab.jfronny.respackopts.model.ConfigFile;
import io.gitlab.jfronny.respackopts.model.DirRpo;
import io.gitlab.jfronny.respackopts.model.FileRpo;
import io.gitlab.jfronny.respackopts.model.condition.Condition;
import io.gitlab.jfronny.respackopts.model.tree.ConfigBooleanEntry;
import io.gitlab.jfronny.respackopts.model.tree.ConfigBranch;
import io.gitlab.jfronny.respackopts.model.tree.ConfigEnumEntry;
import io.gitlab.jfronny.respackopts.model.tree.ConfigNumericEntry;
import io.gitlab.jfronny.respackopts.util.GuiFactory;
import io.gitlab.jfronny.respackopts.util.MetaCache;
import io.gitlab.jfronny.respackopts.util.RpoCommand;
import meteordevelopment.starscript.Script;
2021-08-24 17:42:46 +02:00
import meteordevelopment.starscript.StandardLib;
import meteordevelopment.starscript.Starscript;
2020-11-24 22:04:13 +01:00
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
2021-10-09 13:09:57 +02:00
import net.fabricmc.loader.impl.gui.FabricGuiEntry;
2021-08-24 17:42:46 +02:00
import net.minecraft.client.MinecraftClient;
import net.minecraft.server.integrated.IntegratedServer;
2021-06-10 13:10:12 +02:00
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2020-11-24 22:04:13 +01:00
2021-06-10 16:11:19 +02:00
import java.io.IOException;
2020-11-24 22:04:13 +01:00
import java.nio.file.Files;
import java.nio.file.Path;
2021-06-10 16:11:19 +02:00
import java.util.HashSet;
import java.util.Set;
2021-08-24 17:42:46 +02:00
import java.util.concurrent.CompletableFuture;
2020-11-24 22:04:13 +01:00
@Environment(EnvType.CLIENT)
public class Respackopts implements ClientModInitializer {
public static final Integer META_VERSION = 6;
public static final String FILE_EXTENSION = ".rpo";
public static final Gson GSON;
2021-06-10 13:10:12 +02:00
2021-08-24 17:42:46 +02:00
public static Starscript STAR_SCRIPT;
public static final String ID = "respackopts";
public static final Logger LOGGER = LogManager.getFormatterLogger(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(Script.class, new ScriptDeserializer())
.registerTypeAdapter(FileRpo.class, new FileRpoDeserializer())
.registerTypeAdapter(DirRpo.class, new DirRpoDeserializer())
.registerTypeAdapter(Condition.class, new ConditionDeserializer())
2021-10-21 18:45:31 +02:00
.registerTypeAdapterFactory(new SingleEntrySetTypeAdapterFactory())
.registerTypeAdapterFactory(new SingleEntryListTypeAdapterFactory())
.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);
}
try {
STAR_SCRIPT = new Starscript();
} catch (Exception e) {
2021-08-24 17:42:46 +02:00
FabricGuiEntry.displayCriticalError(new Exception("Respackopts could not initialize Starscript. Expect issues with packs that use it", e), false);
}
StandardLib.init(STAR_SCRIPT);
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-08-24 17:42:46 +02:00
SAVE_ACTIONS.add(() -> {
MetaCache.forEach((id, branch) -> STAR_SCRIPT.set(sanitizeString(id), branch::buildStarscript));
2021-08-24 17:42:46 +02:00
});
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((id, branch) -> branch.buildShader(sb, sanitizeString(id)));
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
}