From ab7256dff5d6d37488081ba7a01b36d3ee9ef563 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Sun, 5 Jun 2022 17:43:23 +0200 Subject: refactor (#36) * refactor * fix vig compat * fix nanovg thingy * e * finalize * gui utils package thingy --- .../java/cc/polyfrost/oneconfig/OneConfig.java | 85 --- .../oneconfig/command/OneConfigCommand.java | 38 -- .../java/cc/polyfrost/oneconfig/config/Config.java | 263 +++++++ .../oneconfig/config/OneConfigConfig.java | 91 --- .../config/compatibility/VigilanceConfig.java | 202 ------ .../config/compatibility/VigilantAccessor.java | 11 - .../compatibility/vigilance/VigilanceConfig.java | 202 ++++++ .../compatibility/vigilance/VigilantAccessor.java | 11 + .../cc/polyfrost/oneconfig/config/data/Mod.java | 5 +- .../oneconfig/config/data/OptionCategory.java | 7 - .../oneconfig/config/data/OptionPage.java | 19 - .../oneconfig/config/data/OptionSubcategory.java | 122 ---- .../oneconfig/config/elements/BasicOption.java | 98 +++ .../oneconfig/config/elements/OptionCategory.java | 7 + .../oneconfig/config/elements/OptionPage.java | 16 + .../config/elements/OptionSubcategory.java | 122 ++++ .../oneconfig/config/interfaces/BasicOption.java | 98 --- .../oneconfig/config/interfaces/Config.java | 259 ------- .../oneconfig/config/migration/Migrator.java | 14 - .../config/migration/VigilanceMigrator.java | 84 --- .../config/migration/vigilance/Migrator.java | 14 + .../migration/vigilance/VigilanceMigrator.java | 84 +++ .../oneconfig/config/profiles/Profiles.java | 4 +- .../java/cc/polyfrost/oneconfig/gui/Colors.java | 46 ++ .../java/cc/polyfrost/oneconfig/gui/HudGui.java | 2 +- .../cc/polyfrost/oneconfig/gui/OneConfigGui.java | 39 +- .../java/cc/polyfrost/oneconfig/gui/SideBar.java | 15 +- .../oneconfig/gui/animations/Animation.java | 2 +- .../oneconfig/gui/elements/BasicButton.java | 10 +- .../oneconfig/gui/elements/BasicElement.java | 2 +- .../oneconfig/gui/elements/ColorSelector.java | 59 +- .../polyfrost/oneconfig/gui/elements/ModCard.java | 20 +- .../oneconfig/gui/elements/ModUpdateCard.java | 8 - .../polyfrost/oneconfig/gui/elements/Slider.java | 10 +- .../gui/elements/config/ConfigButton.java | 10 +- .../gui/elements/config/ConfigCheckbox.java | 18 +- .../gui/elements/config/ConfigColorElement.java | 14 +- .../gui/elements/config/ConfigDropdown.java | 42 +- .../gui/elements/config/ConfigDualOption.java | 18 +- .../gui/elements/config/ConfigHeader.java | 14 +- .../oneconfig/gui/elements/config/ConfigInfo.java | 14 +- .../gui/elements/config/ConfigKeyBind.java | 12 +- .../gui/elements/config/ConfigPageButton.java | 16 +- .../gui/elements/config/ConfigSlider.java | 20 +- .../gui/elements/config/ConfigSwitch.java | 12 +- .../gui/elements/config/ConfigTextBox.java | 14 +- .../gui/elements/text/NumberInputField.java | 8 +- .../oneconfig/gui/elements/text/SearchField.java | 14 - .../gui/elements/text/TextInputField.java | 36 +- .../polyfrost/oneconfig/gui/pages/CreditsPage.java | 6 +- .../cc/polyfrost/oneconfig/gui/pages/HomePage.java | 14 +- .../oneconfig/gui/pages/ModConfigPage.java | 10 +- .../cc/polyfrost/oneconfig/gui/pages/ModsPage.java | 11 +- .../cc/polyfrost/oneconfig/gui/pages/Page.java | 4 +- .../java/cc/polyfrost/oneconfig/hud/BasicHud.java | 2 +- .../java/cc/polyfrost/oneconfig/hud/TextHud.java | 2 +- .../cc/polyfrost/oneconfig/init/OneConfigInit.java | 24 - .../cc/polyfrost/oneconfig/internal/OneConfig.java | 85 +++ .../internal/command/OneConfigCommand.java | 38 ++ .../oneconfig/internal/config/OneConfigConfig.java | 48 ++ .../oneconfig/internal/gui/BlurHandler.java | 148 ++++ .../oneconfig/internal/init/OneConfigInit.java | 24 + .../internal/mixin/FontRendererMixin.java | 15 + .../internal/mixin/GuiIngameForgeMixin.java | 17 + .../oneconfig/internal/mixin/MinecraftMixin.java | 76 +++ .../internal/mixin/NetHandlerPlayClientMixin.java | 36 + .../internal/mixin/NetworkManagerMixin.java | 23 + .../internal/mixin/ShaderGroupAccessor.java | 14 + .../oneconfig/internal/mixin/WorldClientMixin.java | 21 + .../oneconfig/internal/plugin/LoadingPlugin.java | 53 ++ .../internal/plugin/Lwjgl2FunctionProvider.java | 39 ++ .../internal/plugin/OneConfigMixinPlugin.java | 52 ++ .../internal/plugin/asm/ClassTransformer.java | 64 ++ .../internal/plugin/asm/ITransformer.java | 9 + .../asm/tweakers/NanoVGGLConfigTransformer.java | 43 ++ .../plugin/asm/tweakers/VigilantTransformer.java | 105 +++ .../cc/polyfrost/oneconfig/lwjgl/BlurHandler.java | 148 ---- .../polyfrost/oneconfig/lwjgl/RenderManager.java | 755 --------------------- .../cc/polyfrost/oneconfig/lwjgl/font/Font.java | 41 -- .../oneconfig/lwjgl/font/FontManager.java | 31 - .../cc/polyfrost/oneconfig/lwjgl/font/Fonts.java | 15 - .../oneconfig/lwjgl/image/ImageLoader.java | 190 ------ .../cc/polyfrost/oneconfig/lwjgl/image/Images.java | 19 - .../cc/polyfrost/oneconfig/lwjgl/image/SVGs.java | 52 -- .../lwjgl/plugin/Lwjgl2FunctionProvider.java | 39 -- .../polyfrost/oneconfig/lwjgl/scissor/Scissor.java | 27 - .../oneconfig/lwjgl/scissor/ScissorManager.java | 69 -- .../oneconfig/mixin/FontRendererMixin.java | 15 - .../oneconfig/mixin/GuiIngameForgeMixin.java | 17 - .../polyfrost/oneconfig/mixin/MinecraftMixin.java | 76 --- .../oneconfig/mixin/NetHandlerPlayClientMixin.java | 36 - .../oneconfig/mixin/NetworkManagerMixin.java | 23 - .../oneconfig/mixin/ShaderGroupAccessor.java | 14 - .../oneconfig/mixin/WorldClientMixin.java | 21 - .../polyfrost/oneconfig/plugin/LoadingPlugin.java | 54 -- .../oneconfig/plugin/OneConfigMixinPlugin.java | 52 -- .../oneconfig/plugin/asm/ClassTransformer.java | 64 -- .../oneconfig/plugin/asm/ITransformer.java | 9 - .../asm/tweakers/NanoVGGLConfigTransformer.java | 43 -- .../plugin/asm/tweakers/VigilantTransformer.java | 105 --- .../oneconfig/renderer/RenderManager.java | 755 +++++++++++++++++++++ .../cc/polyfrost/oneconfig/renderer/font/Font.java | 41 ++ .../oneconfig/renderer/font/FontManager.java | 47 ++ .../polyfrost/oneconfig/renderer/font/Fonts.java | 15 + .../oneconfig/renderer/image/ImageLoader.java | 190 ++++++ .../polyfrost/oneconfig/renderer/image/Images.java | 19 + .../polyfrost/oneconfig/renderer/image/SVGs.java | 52 ++ .../oneconfig/renderer/scissor/Scissor.java | 27 + .../oneconfig/renderer/scissor/ScissorManager.java | 69 ++ .../polyfrost/oneconfig/test/ButtonTestPage.java | 2 +- .../cc/polyfrost/oneconfig/test/TestConfig.java | 5 +- .../oneconfig/test/TestNanoVGGui_Test.java | 4 +- .../cc/polyfrost/oneconfig/utils/GuiUtils.java | 62 -- .../cc/polyfrost/oneconfig/utils/OneUIScreen.java | 146 ---- .../cc/polyfrost/oneconfig/utils/TextUtils.java | 6 +- .../oneconfig/utils/color/ColorPalette.java | 10 +- .../utils/commands/annotations/Command.java | 3 +- .../cc/polyfrost/oneconfig/utils/gui/GuiUtils.java | 63 ++ .../polyfrost/oneconfig/utils/gui/OneUIScreen.java | 147 ++++ 119 files changed, 3447 insertions(+), 3445 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/OneConfig.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/command/OneConfigCommand.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/Config.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilanceConfig.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilantAccessor.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/data/OptionCategory.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/data/OptionPage.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/data/OptionSubcategory.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/interfaces/BasicOption.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/Migrator.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/VigilanceMigrator.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/Colors.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/ModUpdateCard.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/text/SearchField.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/init/OneConfigInit.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/OneConfig.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/command/OneConfigCommand.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/config/OneConfigConfig.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/gui/BlurHandler.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/init/OneConfigInit.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/mixin/FontRendererMixin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/mixin/GuiIngameForgeMixin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/mixin/MinecraftMixin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetHandlerPlayClientMixin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/mixin/NetworkManagerMixin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/mixin/ShaderGroupAccessor.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/mixin/WorldClientMixin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/plugin/LoadingPlugin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/plugin/Lwjgl2FunctionProvider.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/plugin/OneConfigMixinPlugin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/ClassTransformer.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/ITransformer.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/tweakers/NanoVGGLConfigTransformer.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/tweakers/VigilantTransformer.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/RenderManager.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/font/Font.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/font/FontManager.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/font/Fonts.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/image/Images.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/image/SVGs.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/Lwjgl2FunctionProvider.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/Scissor.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/ScissorManager.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/FontRendererMixin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/NetworkManagerMixin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/ShaderGroupAccessor.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/WorldClientMixin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/plugin/LoadingPlugin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/plugin/OneConfigMixinPlugin.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/plugin/asm/ClassTransformer.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/plugin/asm/ITransformer.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/plugin/asm/tweakers/NanoVGGLConfigTransformer.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/plugin/asm/tweakers/VigilantTransformer.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/font/Font.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/image/ImageLoader.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/image/Images.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/image/SVGs.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/scissor/Scissor.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/scissor/ScissorManager.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java (limited to 'src/main/java/cc') diff --git a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java b/src/main/java/cc/polyfrost/oneconfig/OneConfig.java deleted file mode 100644 index e8612b6..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java +++ /dev/null @@ -1,85 +0,0 @@ -package cc.polyfrost.oneconfig; - -import cc.polyfrost.oneconfig.command.OneConfigCommand; -import cc.polyfrost.oneconfig.config.OneConfigConfig; -import cc.polyfrost.oneconfig.config.core.ConfigCore; -import cc.polyfrost.oneconfig.config.data.Mod; -import cc.polyfrost.oneconfig.config.data.ModType; -import cc.polyfrost.oneconfig.events.EventManager; -import cc.polyfrost.oneconfig.hud.HudCore; -import cc.polyfrost.oneconfig.lwjgl.BlurHandler; -import cc.polyfrost.oneconfig.test.TestCommand; -import cc.polyfrost.oneconfig.test.TestConfig; -import cc.polyfrost.oneconfig.utils.GuiUtils; -import cc.polyfrost.oneconfig.utils.commands.CommandManager; -import cc.polyfrost.oneconfig.utils.hypixel.HypixelUtils; -import net.minecraftforge.fml.common.DummyModContainer; -import net.minecraftforge.fml.common.Loader; -import net.minecraftforge.fml.common.ModContainer; -import net.minecraftforge.fml.common.ModMetadata; - -import java.io.File; -import java.util.ArrayList; -import java.util.LinkedHashSet; -import java.util.List; - -/** - * The main class of OneConfig. - */ -@net.minecraftforge.fml.common.Mod(modid = "@ID@", name = "@NAME@", version = "@VER@") -public class OneConfig { - public static final File oneConfigDir = new File("./OneConfig"); - public static final List loadedMods = new ArrayList<>(); - public static final List loadedOtherMods = new ArrayList<>(); - public static OneConfigConfig config; - public static TestConfig testConfig; - private static boolean preLaunched = false; - private static boolean initialized = false; - - /** - * Called before mods are loaded. - *

SHOULD NOT BE CALLED!

- */ - public static void preLaunch() { - if (preLaunched) return; - if (!net.minecraft.launchwrapper.Launch.blackboard.containsKey("oneconfig.initialized")) { - throw new RuntimeException("OneConfig has not been initialized! Please add the OneConfig tweaker or call OneConfigInit via an ITweaker or a FMLLoadingPlugin!"); - } - oneConfigDir.mkdirs(); - new File(oneConfigDir, "profiles").mkdirs(); - config = new OneConfigConfig(); - preLaunched = true; - } - - /** - * Called after mods are loaded. - *

SHOULD NOT BE CALLED!

- */ - @SuppressWarnings("ResultOfMethodCallIgnored") - public static void init() { - if (initialized) return; - GuiUtils.getDeltaTime(); // called to make sure static initializer is called - BlurHandler.INSTANCE.load(); - testConfig = new TestConfig(); - CommandManager.INSTANCE.registerCommand(OneConfigCommand.class); - CommandManager.INSTANCE.registerCommand(TestCommand.class); - EventManager.INSTANCE.register(new HudCore()); - EventManager.INSTANCE.register(HypixelUtils.INSTANCE); - reloadModsList(); - initialized = true; - } - - public static void reloadModsList() { - loadedMods.addAll(ConfigCore.oneConfigMods); - LinkedHashSet modData = new LinkedHashSet<>(ConfigCore.oneConfigMods); - for (ModContainer mod : Loader.instance().getActiveModList()) { - ModMetadata metadata = mod.getMetadata(); - loadedOtherMods.add(metadata); - String imageName = Loader.instance().activeModContainer() == null || Loader.instance().activeModContainer().getMetadata().logoFile.trim().equals("") ? null : "/" + Loader.instance().activeModContainer().getMetadata().logoFile; - Mod newMod = new Mod(metadata.name, ModType.THIRD_PARTY, imageName); - newMod.isShortCut = true; - if (mod instanceof DummyModContainer || newMod.name.equals("OneConfig")) continue; - if (modData.add(newMod)) loadedMods.add(newMod); - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/command/OneConfigCommand.java b/src/main/java/cc/polyfrost/oneconfig/command/OneConfigCommand.java deleted file mode 100644 index d58c5a6..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/command/OneConfigCommand.java +++ /dev/null @@ -1,38 +0,0 @@ -package cc.polyfrost.oneconfig.command; - -import cc.polyfrost.oneconfig.gui.HudGui; -import cc.polyfrost.oneconfig.gui.OneConfigGui; -import cc.polyfrost.oneconfig.utils.GuiUtils; -import cc.polyfrost.oneconfig.utils.InputUtils; -import cc.polyfrost.oneconfig.utils.commands.annotations.Command; -import cc.polyfrost.oneconfig.utils.commands.annotations.Main; -import cc.polyfrost.oneconfig.utils.commands.annotations.SubCommand; - -/** - * The main OneConfig command. - */ -@Command(value = "oneconfig", aliases = {"ocfg", "oneconfig"}, description = "Access the OneConfig GUI.") -public class OneConfigCommand { - - @Main - private static void main() { - GuiUtils.displayScreen(OneConfigGui.create()); - } - - @SubCommand(value = "hud", description = "Open the OneConfig HUD config.") - private static class HUDSubCommand { - @Main - private static void main() { - GuiUtils.displayScreen(new HudGui()); - } - } - - @SubCommand(value = "destroy", description = "Destroy the cached OneConfig GUI.") - private static class DestroySubCommand { - @Main - private static void main() { - OneConfigGui.instanceToRestore = null; - InputUtils.blockClicks(false); - } - } -} \ No newline at end of file diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java new file mode 100644 index 0000000..d0d8acf --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -0,0 +1,263 @@ +package cc.polyfrost.oneconfig.config; + +import cc.polyfrost.oneconfig.config.annotations.ConfigPage; +import cc.polyfrost.oneconfig.config.annotations.Option; +import cc.polyfrost.oneconfig.config.core.ConfigCore; +import cc.polyfrost.oneconfig.config.data.*; +import cc.polyfrost.oneconfig.config.elements.BasicOption; +import cc.polyfrost.oneconfig.config.elements.OptionCategory; +import cc.polyfrost.oneconfig.config.elements.OptionPage; +import cc.polyfrost.oneconfig.config.elements.OptionSubcategory; +import cc.polyfrost.oneconfig.config.profiles.Profiles; +import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.config.*; +import cc.polyfrost.oneconfig.gui.pages.ModConfigPage; +import cc.polyfrost.oneconfig.hud.BasicHud; +import cc.polyfrost.oneconfig.hud.HudCore; +import cc.polyfrost.oneconfig.utils.gui.GuiUtils; +import com.google.gson.*; + +import java.io.*; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.*; +import java.util.function.Supplier; + +public class Config { + public final transient HashMap optionNames = new HashMap<>(); + transient protected final String configFile; + transient protected final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create(); + transient public Mod mod; + public transient boolean hasBeenInitialized = false; + public boolean enabled = true; + + /** + * @param modData information about the mod + * @param configFile file where config is stored + */ + public Config(Mod modData, String configFile) { + this.configFile = configFile; + init(modData); + } + + public void init(Mod mod) { + boolean migrate = false; + if (Profiles.getProfileFile(configFile).exists()) load(); + else if (!hasBeenInitialized && mod.migrator != null) migrate = true; + else save(); + mod.config = this; + generateOptionList(this, mod.defaultPage, mod, migrate); + if (migrate) save(); + ConfigCore.oneConfigMods.add(mod); + this.mod = mod; + hasBeenInitialized = true; + } + + /** + * Save current config to file + */ + public void save() { + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Profiles.getProfileFile(configFile).toPath()), StandardCharsets.UTF_8))) { + writer.write(gson.toJson(this)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Load file and overwrite current values + */ + public void load() { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(Profiles.getProfileFile(configFile).toPath()), StandardCharsets.UTF_8))) { + deserializePart(new JsonParser().parse(reader).getAsJsonObject(), this); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Generate the option list, for internal use only + * + * @param instance instance of target class + * @param page page to add options too + * @param mod data about the mod + * @param migrate whether the migrator should be run + */ + protected void generateOptionList(Object instance, OptionPage page, Mod mod, boolean migrate) { + Class clazz = instance.getClass(); + for (Field field : clazz.getDeclaredFields()) { + String pagePrefix = page.equals(mod.defaultPage) ? "" : page.name + "."; + if (!field.isAnnotationPresent(Option.class) && !field.isAnnotationPresent(ConfigPage.class)) { + processCustomOption(field, page); + continue; + } else if (field.isAnnotationPresent(ConfigPage.class)) { + ConfigPage option = field.getAnnotation(ConfigPage.class); + if (!page.categories.containsKey(option.category())) + page.categories.put(option.category(), new OptionCategory()); + OptionCategory category = page.categories.get(option.category()); + if (category.subcategories.size() == 0 || !category.subcategories.get(category.subcategories.size() - 1).getName().equals(option.subcategory())) + category.subcategories.add(new OptionSubcategory(option.subcategory())); + OptionSubcategory subcategory = category.subcategories.get(category.subcategories.size() - 1); + OptionPage newPage = new OptionPage(option.name(), mod); + try { + field.setAccessible(true); + Object object = field.get(clazz); + generateOptionList(object, newPage, mod, migrate); + ConfigPageButton configPageButton = new ConfigPageButton(field, instance, option.name(), option.description(), newPage); + switch (option.location()) { + case TOP: + subcategory.topButtons.add(configPageButton); + break; + case BOTTOM: + subcategory.bottomButtons.add(configPageButton); + break; + } + optionNames.put(pagePrefix + field.getName(), configPageButton); + } catch (IllegalAccessException ignored) { + } + continue; + } + Option option = field.getAnnotation(Option.class); + if (!page.categories.containsKey(option.category())) + page.categories.put(option.category(), new OptionCategory()); + OptionCategory category = page.categories.get(option.category()); + if (category.subcategories.size() == 0 || !category.subcategories.get(category.subcategories.size() - 1).getName().equals(option.subcategory())) + category.subcategories.add(new OptionSubcategory(option.subcategory())); + if (migrate) { + try { + Object value = mod.migrator.getValue(field, option.name(), option.category(), option.subcategory()); + if (value != null) { + field.setAccessible(true); + field.set(instance, value); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + ArrayList options = category.subcategories.get(category.subcategories.size() - 1).options; + switch (option.type()) { + case SWITCH: + options.add(new ConfigSwitch(field, instance, option.name(), option.size())); + break; + case CHECKBOX: + options.add(new ConfigCheckbox(field, instance, option.name(), option.size())); + break; + case TEXT: + options.add(new ConfigTextBox(field, instance, option.name(), option.size(), option.placeholder(), option.secure(), option.multiLine())); + break; + case DUAL_OPTION: + options.add(new ConfigDualOption(field, instance, option.name(), option.size(), option.options())); + break; + case DROPDOWN: + options.add(new ConfigDropdown(field, instance, option.name(), option.size(), option.options())); + break; + case SLIDER: + options.add(new ConfigSlider(field, instance, option.name(), option.size(), option.min(), option.max(), option.step())); + break; + case INFO: + options.add(new ConfigInfo(field, instance, option.name(), option.size(), option.infoType())); + break; + case COLOR: + options.add(new ConfigColorElement(field, instance, option.name(), option.size())); + break; + case HEADER: + options.add(new ConfigHeader(field, instance, option.name(), option.size())); + break; + case BUTTON: + options.add(new ConfigButton(field, instance, option.name(), option.size(), option.buttonText())); + break; + case KEYBIND: + options.add(new ConfigKeyBind(field, instance, option.name(), option.size())); + break; + case HUD: + try { + field.setAccessible(true); + BasicHud hud = (BasicHud) field.get(instance); + HudCore.huds.add(hud); + options.add(new ConfigHeader(field, hud, option.name(), 1)); + options.add(new ConfigSwitch(hud.getClass().getField("enabled"), hud, "Enabled", 1)); + options.add(new ConfigCheckbox(hud.getClass().getField("rounded"), hud, "Rounded corners", 1)); + options.get(options.size() - 1).setDependency(() -> hud.enabled); + options.add(new ConfigCheckbox(hud.getClass().getField("border"), hud, "Outline/border", 1)); + options.get(options.size() - 1).setDependency(() -> hud.enabled); + options.add(new ConfigColorElement(hud.getClass().getField("bgColor"), hud, "Background color:", 1)); + options.get(options.size() - 1).setDependency(() -> hud.enabled); + options.add(new ConfigColorElement(hud.getClass().getField("borderColor"), hud, "Border color:", 1)); + options.get(options.size() - 1).setDependency(() -> hud.enabled && hud.border); + options.add(new ConfigSlider(hud.getClass().getField("cornerRadius"), hud, "Corner radius:", 2, 0, 10, 0)); + options.get(options.size() - 1).setDependency(() -> hud.enabled && hud.rounded); + options.add(new ConfigSlider(hud.getClass().getField("borderSize"), hud, "Border thickness:", 2, 0, 10, 0)); + options.get(options.size() - 1).setDependency(() -> hud.enabled && hud.border); + options.add(new ConfigSlider(hud.getClass().getField("paddingX"), hud, "X-Padding", 2, 0, 50, 0)); + options.get(options.size() - 1).setDependency(() -> hud.enabled); + options.add(new ConfigSlider(hud.getClass().getField("paddingY"), hud, "Y-Padding", 2, 0, 50, 0)); + options.get(options.size() - 1).setDependency(() -> hud.enabled); + } catch (IllegalAccessException | NoSuchFieldException e) { + e.printStackTrace(); + } + break; + } + if (!option.type().equals(OptionType.HUD)) + optionNames.put(pagePrefix + field.getName(), options.get(options.size() - 1)); + } + } + + /** + * Overwrite this method to add your own custom option types + * + * @param field target field + * @param page page to add options too + */ + protected void processCustomOption(Field field, OptionPage page) { + } + + /** + * Deserialize part of config and load values + * + * @param json json to deserialize + * @param instance instance of target class + */ + protected void deserializePart(JsonObject json, Object instance) { + Class clazz = instance.getClass(); + for (Map.Entry element : json.entrySet()) { + String name = element.getKey(); + JsonElement value = element.getValue(); + if (value.isJsonObject()) { + Optional> innerClass = Arrays.stream(clazz.getClasses()).filter(aClass -> aClass.getSimpleName().equals(name)).findFirst(); + if (innerClass.isPresent()) { + deserializePart(value.getAsJsonObject(), innerClass.get()); + continue; + } + } + try { + Field field = clazz.getField(name); + TypeAdapter adapter = gson.getAdapter(field.getType()); + Object object = adapter.fromJsonTree(value); + field.setAccessible(true); + field.set(instance, object); + } catch (Exception ignored) { + } + } + } + + /** + * Function to open the gui of this mod + */ + public void openGui() { + if (mod == null) return; + GuiUtils.displayScreen(new OneConfigGui(new ModConfigPage(mod.defaultPage))); + } + + /** + * Disable an option if a certain condition is not met + * + * @param option The name of the field, or if the field is in a page "pageName.fieldName" + * @param condition The condition that has to be met for the option to be enabled + */ + protected void addDependency(String option, Supplier condition) { + if (!optionNames.containsKey(option)) return; + optionNames.get(option).setDependency(condition); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java b/src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java deleted file mode 100644 index b867bc0..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java +++ /dev/null @@ -1,91 +0,0 @@ -package cc.polyfrost.oneconfig.config; - -import cc.polyfrost.oneconfig.config.core.OneColor; -import cc.polyfrost.oneconfig.config.data.Mod; -import cc.polyfrost.oneconfig.config.interfaces.Config; -import com.google.gson.JsonParser; - -import java.awt.*; -import java.io.*; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; - -public class OneConfigConfig extends Config { - - // the color library - public static final int TRANSPARENT = new Color(0, 0, 0, 0).getRGB(); // Transparent - public static final int BLACK = new Color(0, 0, 0, 255).getRGB(); // Black - public static final int GRAY_900 = new Color(13, 14, 15, 255).getRGB(); // Gray 900 - public static final int GRAY_900_80 = new Color(13, 14, 15, 204).getRGB(); // Gray 900 80% - public static final int GRAY_800 = new Color(21, 22, 23, 255).getRGB(); // Gray 800 - public static final int GRAY_800_95 = new Color(21, 22, 23, 242).getRGB(); - public static final int GRAY_700 = new Color(34, 35, 38, 255).getRGB(); // Gray 700 - public static final int GRAY_600 = new Color(42, 44, 48, 255).getRGB(); // Gray 600 - public static final int GRAY_500 = new Color(49, 51, 56, 255).getRGB(); // Gray 500 // button sidebar hover, button gray normal - public static final int GRAY_500_80 = new Color(49, 51, 56, 204).getRGB(); // Gray 500 80% // button sidebar pressed - public static final int GRAY_400 = new Color(55, 59, 69, 255).getRGB(); // Gray 400 - public static final int GRAY_300 = new Color(73, 79, 92, 255).getRGB(); // Gray 300 // button gray hover - public static final int GRAY_400_80 = new Color(55, 59, 69, 204).getRGB(); // Gray 400 80% // button gray pressed - public static final int PRIMARY_800 = new Color(13, 51, 128, 255).getRGB(); // Blue 800 - public static final int PRIMARY_700 = new Color(18, 71, 178, 255).getRGB(); // Blue 700 - public static final int PRIMARY_700_80 = new Color(18, 71, 178, 204).getRGB(); // Blue 700 80% - public static final int PRIMARY_600 = new Color(20, 82, 204, 255).getRGB(); // Blue 600 // button blue normal - public static final int PRIMARY_500 = new Color(25, 103, 255, 255).getRGB(); // Blue 500 // button blue hover - public static final int PRIMARY_400 = new Color(48, 129, 242, 255).getRGB(); - public static final int WHITE_50 = new Color(255, 255, 255, 127).getRGB(); // White 60% - public static final int WHITE_60 = new Color(255, 255, 255, 153).getRGB(); // White 60% - public static final int WHITE_80 = new Color(255, 255, 255, 204).getRGB(); // White 80% - public static final int WHITE_90 = new Color(255, 255, 255, 229).getRGB(); // White 90% - public static final int WHITE_95 = new Color(255, 255, 255, 242).getRGB(); // White 90% - public static final int WHITE = new Color(255, 255, 255, 255).getRGB(); // White 100% - public static final int SUCCESS_600 = new Color(3, 152, 85).getRGB(); - public static final int SUCCESS_700 = new Color(2, 121, 72).getRGB(); - public static final int WARNING_500 = new Color(247, 144, 9).getRGB(); - public static final int WARNING_600 = new Color(220, 104, 3).getRGB(); - public static final int ERROR_600_80 = new Color(217, 32, 32, 204).getRGB(); - public static final int ERROR_600 = new Color(217, 32, 32).getRGB(); - public static final int ERROR_700 = new Color(180, 24, 24).getRGB(); // Red 700 - public static final int ERROR_800 = new Color(145, 24, 24).getRGB(); // Red 800 - public static final int ERROR_800_80 = new Color(145, 24, 24, 204).getRGB(); // Red 800 - public static final int ERROR_300 = new Color(253, 155, 155).getRGB(); - public static final int ERROR_300_80 = new Color(253, 155, 155, 204).getRGB(); - public static String currentProfile = "Default Profile"; - public static ArrayList favoriteMods = new ArrayList<>(); - public static ArrayList favoriteColors = new ArrayList<>(); - public static ArrayList recentColors = new ArrayList<>(); - public static boolean ROUNDED_CORNERS = true; - public static float CORNER_RADIUS_WIN = 20f; - public static float CORNER_RADIUS = 12f; - - public static boolean allShowShortCut = false; - - public OneConfigConfig() { - super(null, "OneConfig.json"); - } - - @Override - public void init(Mod mod) { - if (new File("OneConfig/" + configFile).exists()) load(); - else save(); - } - - @Override - public void save() { - try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get("OneConfig/" + configFile)), StandardCharsets.UTF_8))) { - writer.write(gson.toJson(this)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public void load() { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get("OneConfig/" + configFile)), StandardCharsets.UTF_8))) { - deserializePart(new JsonParser().parse(reader).getAsJsonObject(), this); - } catch (IOException e) { - e.printStackTrace(); - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilanceConfig.java b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilanceConfig.java deleted file mode 100644 index fa4f058..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilanceConfig.java +++ /dev/null @@ -1,202 +0,0 @@ -package cc.polyfrost.oneconfig.config.compatibility; - -import cc.polyfrost.oneconfig.config.core.ConfigCore; -import cc.polyfrost.oneconfig.config.core.OneColor; -import cc.polyfrost.oneconfig.config.data.Mod; -import cc.polyfrost.oneconfig.config.data.OptionCategory; -import cc.polyfrost.oneconfig.config.data.OptionPage; -import cc.polyfrost.oneconfig.config.data.OptionSubcategory; -import cc.polyfrost.oneconfig.config.interfaces.BasicOption; -import cc.polyfrost.oneconfig.config.interfaces.Config; -import cc.polyfrost.oneconfig.gui.elements.config.*; -import gg.essential.vigilance.Vigilant; -import gg.essential.vigilance.data.*; -import kotlin.reflect.KMutableProperty0; -import kotlin.reflect.jvm.ReflectJvmMapping; -import net.minecraft.client.resources.I18n; - -import java.awt.*; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Objects; - -/** - * This class is used to convert the Vigilance config to the new config system. - * It is not meant to be used outside the config system. - */ -public class VigilanceConfig extends Config { - public final Vigilant vigilant; - - public VigilanceConfig(Mod modData, String configFile, Vigilant vigilant) { - super(modData, configFile); - this.vigilant = vigilant; - init(modData); - } - - @Override - public void init(Mod mod) { - if (vigilant != null) { - mod.config = this; - generateOptionsList(mod.defaultPage); - ConfigCore.oneConfigMods.add(mod); - this.mod = mod; - } - } - - @Override - public void save() { - vigilant.markDirty(); - vigilant.writeData(); - } - - @Override - public void load() { - //no-op - } - - private void generateOptionsList(OptionPage page) { - for (PropertyData option : ((VigilantAccessor) vigilant).getPropertyCollector().getProperties()) { - PropertyAttributesExt attributes = option.getAttributesExt(); - if (attributes.getHidden()) continue; - if (!page.categories.containsKey(getCategory(attributes))) - page.categories.put(getCategory(attributes), new OptionCategory()); - OptionCategory category = page.categories.get(getCategory(attributes)); - if (category.subcategories.size() == 0 || !category.subcategories.get(category.subcategories.size() - 1).getName().equals(getSubcategory(attributes))) - category.subcategories.add(new OptionSubcategory(getSubcategory(attributes))); - ArrayList options = category.subcategories.get(category.subcategories.size() - 1).options; - switch (attributes.getType()) { - case SWITCH: - options.add(new ConfigSwitch(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2)); - break; - case CHECKBOX: - options.add(new ConfigCheckbox(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2)); - break; - case PARAGRAPH: - case TEXT: - options.add(new ConfigTextBox(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getPlaceholder(), attributes.getProtected(), attributes.getType() == PropertyType.PARAGRAPH)); - break; - case SELECTOR: - options.add(new ConfigDropdown(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getOptions().toArray(new String[0]))); - break; - case PERCENT_SLIDER: - options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, 0, 1, 0)); - break; - case DECIMAL_SLIDER: - options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getMinF(), attributes.getMaxF(), 0)); - break; - case SLIDER: - options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getMin(), attributes.getMax(), 0)); - break; - case COLOR: - options.add(new CompatConfigColorElement(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2)); - break; - case BUTTON: - options.add(new ConfigButton(() -> ((CallablePropertyValue) option.getValue()).invoke(option.getInstance()), option.getInstance(), getName(attributes), 2, attributes.getPlaceholder().isEmpty() ? "Activate" : attributes.getPlaceholder())); - break; - } - if (attributes.getType() == PropertyType.SWITCH || attributes.getType() == PropertyType.CHECKBOX) { - optionNames.put(PropertyKt.fullPropertyPath(option.getAttributesExt()), options.get(options.size() - 1)); - } - } - } - - private Field getFieldOfProperty(PropertyData data) { - if (data.getValue() instanceof FieldBackedPropertyValue) { - FieldBackedPropertyValue fieldBackedPropertyValue = (FieldBackedPropertyValue) data.getValue(); - try { - Field field = fieldBackedPropertyValue.getClass().getDeclaredField("field"); - field.setAccessible(true); - return (Field) field.get(fieldBackedPropertyValue); - } catch (IllegalAccessException | NoSuchFieldException e) { - throw new RuntimeException(e); - } - } else if (data.getValue() instanceof ValueBackedPropertyValue) { - ValueBackedPropertyValue valueBackedPropertyValue = (ValueBackedPropertyValue) data.getValue(); - try { - Field field = valueBackedPropertyValue.getClass().getDeclaredField("obj"); - field.setAccessible(true); - return (Field) field.get(valueBackedPropertyValue); - } catch (IllegalAccessException | NoSuchFieldException e) { - throw new RuntimeException(e); - } - } else if (data.getValue() instanceof KPropertyBackedPropertyValue) { - KPropertyBackedPropertyValue kPropertyBackedPropertyValue = (KPropertyBackedPropertyValue) data.getValue(); - try { - Field field = kPropertyBackedPropertyValue.getClass().getDeclaredField("property"); - field.setAccessible(true); - KMutableProperty0 property = (KMutableProperty0) field.get(kPropertyBackedPropertyValue); - return ReflectJvmMapping.getJavaField(property); - } catch (IllegalAccessException | NoSuchFieldException e) { - throw new RuntimeException(e); - } - } else throw new RuntimeException("Unknown property value type: " + data.getValue().getClass()); - } - - private String getName(PropertyAttributesExt ext) { - try { - PropertyAttributesExt.class.getDeclaredField("i18nName").setAccessible(true); - return I18n.format((String) PropertyAttributesExt.class.getDeclaredField("i18nName").get(ext)); - } catch (IllegalAccessException | NoSuchFieldException e) { - return ext.getName(); - } - } - - private String getCategory(PropertyAttributesExt ext) { - try { - PropertyAttributesExt.class.getDeclaredField("i18nCategory").setAccessible(true); - return I18n.format((String) PropertyAttributesExt.class.getDeclaredField("i18nCategory").get(ext)); - } catch (IllegalAccessException | NoSuchFieldException e) { - return ext.getCategory(); - } - } - - private String getSubcategory(PropertyAttributesExt ext) { - try { - PropertyAttributesExt.class.getDeclaredField("i18nSubcategory").setAccessible(true); - return I18n.format((String) PropertyAttributesExt.class.getDeclaredField("i18nSubcategory").get(ext)); - } catch (IllegalAccessException | NoSuchFieldException e) { - return ext.getSubcategory(); - } - } - - @SuppressWarnings("unused") - public void addDependency(PropertyData property, PropertyData dependency) { - BasicOption option = optionNames.get(PropertyKt.fullPropertyPath(property.getAttributesExt())); - if (option != null) { - option.setDependency(() -> Objects.equals(dependency.getValue().getValue(vigilant), true)); - } - } - - private static class CompatConfigColorElement extends ConfigColorElement { - private final Field color; - private Color prevColor = null; - private OneColor cachedColor = null; - - public CompatConfigColorElement(Field color, Vigilant parent, String name, int size) { - super(null, parent, name, size); - this.color = color; - } - - @Override - protected Object get() throws IllegalAccessException { - Color currentColor = (Color) color.get(parent); - if (cachedColor == null || prevColor != color.get(parent)) { - cachedColor = new OneColor(currentColor); - } - prevColor = currentColor; - return cachedColor; - } - - @Override - protected void setColor(OneColor color) { - if (cachedColor != color) { - Color newColor = new Color(color.getRGB(), true); - try { - this.color.set(parent, newColor); - } catch (IllegalAccessException ignored) { - - } - } - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilantAccessor.java b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilantAccessor.java deleted file mode 100644 index ca2b1da..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/VigilantAccessor.java +++ /dev/null @@ -1,11 +0,0 @@ -package cc.polyfrost.oneconfig.config.compatibility; - -import gg.essential.vigilance.data.PropertyCollector; - -/** - * Interface for accessing the {@link PropertyCollector} in a Vigilant config. - *

Not recommended for non-internal OneConfig usage, as Systemless will get really angry at us

- */ -public interface VigilantAccessor { - PropertyCollector getPropertyCollector(); -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java new file mode 100644 index 0000000..cfa717f --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java @@ -0,0 +1,202 @@ +package cc.polyfrost.oneconfig.config.compatibility.vigilance; + +import cc.polyfrost.oneconfig.config.core.ConfigCore; +import cc.polyfrost.oneconfig.config.core.OneColor; +import cc.polyfrost.oneconfig.config.data.Mod; +import cc.polyfrost.oneconfig.config.elements.OptionCategory; +import cc.polyfrost.oneconfig.config.elements.OptionPage; +import cc.polyfrost.oneconfig.config.elements.OptionSubcategory; +import cc.polyfrost.oneconfig.config.elements.BasicOption; +import cc.polyfrost.oneconfig.config.Config; +import cc.polyfrost.oneconfig.gui.elements.config.*; +import gg.essential.vigilance.Vigilant; +import gg.essential.vigilance.data.*; +import kotlin.reflect.KMutableProperty0; +import kotlin.reflect.jvm.ReflectJvmMapping; +import net.minecraft.client.resources.I18n; + +import java.awt.*; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Objects; + +/** + * This class is used to convert the Vigilance config to the new config system. + * It is not meant to be used outside the config system. + */ +public class VigilanceConfig extends Config { + public final Vigilant vigilant; + + public VigilanceConfig(Mod modData, String configFile, Vigilant vigilant) { + super(modData, configFile); + this.vigilant = vigilant; + init(modData); + } + + @Override + public void init(Mod mod) { + if (vigilant != null) { + mod.config = this; + generateOptionsList(mod.defaultPage); + ConfigCore.oneConfigMods.add(mod); + this.mod = mod; + } + } + + @Override + public void save() { + vigilant.markDirty(); + vigilant.writeData(); + } + + @Override + public void load() { + //no-op + } + + private void generateOptionsList(OptionPage page) { + for (PropertyData option : ((VigilantAccessor) vigilant).getPropertyCollector().getProperties()) { + PropertyAttributesExt attributes = option.getAttributesExt(); + if (attributes.getHidden()) continue; + if (!page.categories.containsKey(getCategory(attributes))) + page.categories.put(getCategory(attributes), new OptionCategory()); + OptionCategory category = page.categories.get(getCategory(attributes)); + if (category.subcategories.size() == 0 || !category.subcategories.get(category.subcategories.size() - 1).getName().equals(getSubcategory(attributes))) + category.subcategories.add(new OptionSubcategory(getSubcategory(attributes))); + ArrayList options = category.subcategories.get(category.subcategories.size() - 1).options; + switch (attributes.getType()) { + case SWITCH: + options.add(new ConfigSwitch(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2)); + break; + case CHECKBOX: + options.add(new ConfigCheckbox(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2)); + break; + case PARAGRAPH: + case TEXT: + options.add(new ConfigTextBox(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getPlaceholder(), attributes.getProtected(), attributes.getType() == PropertyType.PARAGRAPH)); + break; + case SELECTOR: + options.add(new ConfigDropdown(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getOptions().toArray(new String[0]))); + break; + case PERCENT_SLIDER: + options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, 0, 1, 0)); + break; + case DECIMAL_SLIDER: + options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getMinF(), attributes.getMaxF(), 0)); + break; + case SLIDER: + options.add(new ConfigSlider(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2, attributes.getMin(), attributes.getMax(), 0)); + break; + case COLOR: + options.add(new CompatConfigColorElement(getFieldOfProperty(option), option.getInstance(), getName(attributes), 2)); + break; + case BUTTON: + options.add(new ConfigButton(() -> ((CallablePropertyValue) option.getValue()).invoke(option.getInstance()), option.getInstance(), getName(attributes), 2, attributes.getPlaceholder().isEmpty() ? "Activate" : attributes.getPlaceholder())); + break; + } + if (attributes.getType() == PropertyType.SWITCH || attributes.getType() == PropertyType.CHECKBOX) { + optionNames.put(PropertyKt.fullPropertyPath(option.getAttributesExt()), options.get(options.size() - 1)); + } + } + } + + private Field getFieldOfProperty(PropertyData data) { + if (data.getValue() instanceof FieldBackedPropertyValue) { + FieldBackedPropertyValue fieldBackedPropertyValue = (FieldBackedPropertyValue) data.getValue(); + try { + Field field = fieldBackedPropertyValue.getClass().getDeclaredField("field"); + field.setAccessible(true); + return (Field) field.get(fieldBackedPropertyValue); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException(e); + } + } else if (data.getValue() instanceof ValueBackedPropertyValue) { + ValueBackedPropertyValue valueBackedPropertyValue = (ValueBackedPropertyValue) data.getValue(); + try { + Field field = valueBackedPropertyValue.getClass().getDeclaredField("obj"); + field.setAccessible(true); + return (Field) field.get(valueBackedPropertyValue); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException(e); + } + } else if (data.getValue() instanceof KPropertyBackedPropertyValue) { + KPropertyBackedPropertyValue kPropertyBackedPropertyValue = (KPropertyBackedPropertyValue) data.getValue(); + try { + Field field = kPropertyBackedPropertyValue.getClass().getDeclaredField("property"); + field.setAccessible(true); + KMutableProperty0 property = (KMutableProperty0) field.get(kPropertyBackedPropertyValue); + return ReflectJvmMapping.getJavaField(property); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException(e); + } + } else throw new RuntimeException("Unknown property value type: " + data.getValue().getClass()); + } + + private String getName(PropertyAttributesExt ext) { + try { + PropertyAttributesExt.class.getDeclaredField("i18nName").setAccessible(true); + return I18n.format((String) PropertyAttributesExt.class.getDeclaredField("i18nName").get(ext)); + } catch (IllegalAccessException | NoSuchFieldException e) { + return ext.getName(); + } + } + + private String getCategory(PropertyAttributesExt ext) { + try { + PropertyAttributesExt.class.getDeclaredField("i18nCategory").setAccessible(true); + return I18n.format((String) PropertyAttributesExt.class.getDeclaredField("i18nCategory").get(ext)); + } catch (IllegalAccessException | NoSuchFieldException e) { + return ext.getCategory(); + } + } + + private String getSubcategory(PropertyAttributesExt ext) { + try { + PropertyAttributesExt.class.getDeclaredField("i18nSubcategory").setAccessible(true); + return I18n.format((String) PropertyAttributesExt.class.getDeclaredField("i18nSubcategory").get(ext)); + } catch (IllegalAccessException | NoSuchFieldException e) { + return ext.getSubcategory(); + } + } + + @SuppressWarnings("unused") + public void addDependency(PropertyData property, PropertyData dependency) { + BasicOption option = optionNames.get(PropertyKt.fullPropertyPath(property.getAttributesExt())); + if (option != null) { + option.setDependency(() -> Objects.equals(dependency.getValue().getValue(vigilant), true)); + } + } + + private static class CompatConfigColorElement extends ConfigColorElement { + private final Field color; + private Color prevColor = null; + private OneColor cachedColor = null; + + public CompatConfigColorElement(Field color, Vigilant parent, String name, int size) { + super(null, parent, name, size); + this.color = color; + } + + @Override + protected Object get() throws IllegalAccessException { + Color currentColor = (Color) color.get(parent); + if (cachedColor == null || prevColor != color.get(parent)) { + cachedColor = new OneColor(currentColor); + } + prevColor = currentColor; + return cachedColor; + } + + @Override + protected void setColor(OneColor color) { + if (cachedColor != color) { + Color newColor = new Color(color.getRGB(), true); + try { + this.color.set(parent, newColor); + } catch (IllegalAccessException ignored) { + + } + } + } + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor.java b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor.java new file mode 100644 index 0000000..c449bd9 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor.java @@ -0,0 +1,11 @@ +package cc.polyfrost.oneconfig.config.compatibility.vigilance; + +import gg.essential.vigilance.data.PropertyCollector; + +/** + * Interface for accessing the {@link PropertyCollector} in a Vigilant config. + *

Not recommended for non-internal OneConfig usage, as Systemless will get really angry at us

+ */ +public interface VigilantAccessor { + PropertyCollector getPropertyCollector(); +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/data/Mod.java b/src/main/java/cc/polyfrost/oneconfig/config/data/Mod.java index 241dca7..090d1f8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/data/Mod.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/data/Mod.java @@ -1,7 +1,8 @@ package cc.polyfrost.oneconfig.config.data; -import cc.polyfrost.oneconfig.config.interfaces.Config; -import cc.polyfrost.oneconfig.config.migration.Migrator; +import cc.polyfrost.oneconfig.config.Config; +import cc.polyfrost.oneconfig.config.elements.OptionPage; +import cc.polyfrost.oneconfig.config.migration.vigilance.Migrator; import org.jetbrains.annotations.Nullable; public class Mod { diff --git a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionCategory.java b/src/main/java/cc/polyfrost/oneconfig/config/data/OptionCategory.java deleted file mode 100644 index 76b2b34..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionCategory.java +++ /dev/null @@ -1,7 +0,0 @@ -package cc.polyfrost.oneconfig.config.data; - -import java.util.ArrayList; - -public class OptionCategory { - public final ArrayList subcategories = new ArrayList<>(); -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionPage.java b/src/main/java/cc/polyfrost/oneconfig/config/data/OptionPage.java deleted file mode 100644 index 32a07b9..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionPage.java +++ /dev/null @@ -1,19 +0,0 @@ -package cc.polyfrost.oneconfig.config.data; - -import java.util.LinkedHashMap; - -public class OptionPage { - public final String name; - public final Mod mod; - /** - * Depth 1 = categories - * Depth 2 = subcategories - * Depth 3 = list of options - */ - public final LinkedHashMap categories = new LinkedHashMap<>(); - - public OptionPage(String name, Mod mod) { - this.name = name; - this.mod = mod; - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionSubcategory.java b/src/main/java/cc/polyfrost/oneconfig/config/data/OptionSubcategory.java deleted file mode 100644 index e69e077..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionSubcategory.java +++ /dev/null @@ -1,122 +0,0 @@ -package cc.polyfrost.oneconfig.config.data; - -import cc.polyfrost.oneconfig.config.OneConfigConfig; -import cc.polyfrost.oneconfig.config.interfaces.BasicOption; -import cc.polyfrost.oneconfig.gui.OneConfigGui; -import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton; -import cc.polyfrost.oneconfig.lwjgl.RenderManager; -import cc.polyfrost.oneconfig.lwjgl.font.Fonts; - -import java.util.ArrayList; - -public class OptionSubcategory { - private final String name; - public ArrayList options = new ArrayList<>(); - public ArrayList topButtons = new ArrayList<>(); - public ArrayList bottomButtons = new ArrayList<>(); - private ArrayList filteredOptions = new ArrayList<>(); - private int drawLastY; - - public OptionSubcategory(String name) { - this.name = name; - } - - public int draw(long vg, int x, int y) { - String filter = On