diff --git a/src/main/java/org/cubewhy/celestial/Celestial.java b/src/main/java/org/cubewhy/celestial/Celestial.java index 170caad9..9ccb11f3 100644 --- a/src/main/java/org/cubewhy/celestial/Celestial.java +++ b/src/main/java/org/cubewhy/celestial/Celestial.java @@ -25,6 +25,8 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -183,7 +185,22 @@ private static void run(String[] args) throws Exception { launcherFrame = new GuiLauncher(); new CreateLauncherEvent(launcherFrame).call(); launcherFrame.setVisible(true); - launcherFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + launcherFrame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + log.info("Closing celestial, dumping configs..."); + // dump configs + proxy.save(); + config.save(); + launcherFrame.dispose(); + } + + @Override + public void windowClosed(WindowEvent e) { + System.exit(0); // exit java + } + }); } private static void checkJava() throws IOException { @@ -254,6 +271,7 @@ private static void initConfig() { .initValue("program-args", new JsonArray()) // args of the game .initValue("javaagents", new JsonObject()); // lc addon // init language + config.save(); log.info("Initializing language manager"); locale = Locale.forLanguageTag(config.getValue("language").getAsString()); userLanguage = locale.getLanguage(); diff --git a/src/main/java/org/cubewhy/celestial/files/ConfigFile.java b/src/main/java/org/cubewhy/celestial/files/ConfigFile.java index 5dd35bd4..d031e830 100644 --- a/src/main/java/org/cubewhy/celestial/files/ConfigFile.java +++ b/src/main/java/org/cubewhy/celestial/files/ConfigFile.java @@ -23,27 +23,27 @@ public ConfigFile(File file) { public ConfigFile setValue(String key, String value) { this.config.addProperty(key, value); - return this.save(); + return this; } public ConfigFile setValue(String key, char value) { this.config.addProperty(key, value); - return this.save(); + return this; } public ConfigFile setValue(String key, Number value) { this.config.addProperty(key, value); - return this.save(); + return this; } public ConfigFile setValue(String key, boolean value) { this.config.addProperty(key, value); - return this.save(); + return this; } public ConfigFile setValue(String key, JsonObject value) { this.config.add(key, value); - return this.save(); + return this; } public ConfigFile initValue(String key, JsonElement value) { @@ -51,7 +51,7 @@ public ConfigFile initValue(String key, JsonElement value) { log.info("Init value " + key + " -> " + value); this.config.add(key, value); } - return this.save(); + return this; } public ConfigFile initValue(String key, String value) { diff --git a/src/main/java/org/cubewhy/celestial/files/ProxyConfig.java b/src/main/java/org/cubewhy/celestial/files/ProxyConfig.java index db4f7c72..f2233cd8 100644 --- a/src/main/java/org/cubewhy/celestial/files/ProxyConfig.java +++ b/src/main/java/org/cubewhy/celestial/files/ProxyConfig.java @@ -18,6 +18,7 @@ import java.nio.charset.StandardCharsets; public class ProxyConfig extends ConfigFile { + @Getter public static class Mirror { @@ -53,10 +54,14 @@ public boolean getState() { } public Proxy getProxy() throws MalformedURLException { - URL address = new URL(this.getValue("proxy").getAsString()); + URL address = new URL(this.getProxyAddress()); return new Proxy(getType(address.getProtocol()), new InetSocketAddress(address.getHost(), address.getPort())); } + public String getProxyAddress() { + return this.getValue("proxy").getAsString(); + } + private Proxy.Type getType(@NotNull String protocol) { return switch (protocol) { case "http" -> Proxy.Type.HTTP; diff --git a/src/main/java/org/cubewhy/celestial/gui/dialogs/ArgsConfigDialog.java b/src/main/java/org/cubewhy/celestial/gui/dialogs/ArgsConfigDialog.java index fc9c5795..140d3766 100644 --- a/src/main/java/org/cubewhy/celestial/gui/dialogs/ArgsConfigDialog.java +++ b/src/main/java/org/cubewhy/celestial/gui/dialogs/ArgsConfigDialog.java @@ -85,7 +85,6 @@ private void addArg(String arg, @NotNull DefaultListModel model) { this.array.add(arg); model.addElement(arg); log.info("Add a arg to " + this.key + " -> " + arg); - config.save(); } private void removeArg(int index, @NotNull DefaultListModel model) { @@ -93,6 +92,5 @@ private void removeArg(int index, @NotNull DefaultListModel model) { model.remove(index); this.array.remove(index); log.info("Remove a arg to " + this.key + " -> " + arg); - config.save(); } } diff --git a/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java b/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java index 50e8ae66..222b83a3 100644 --- a/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java +++ b/src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java @@ -289,6 +289,23 @@ private void initGui() { claim("program-args", panelGame); claim("resize"); + JPanel panelProxy = new JPanel(); + panelProxy.setBorder(new TitledBorder(null, f.getString("gui.settings.proxy"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.orange)); + panelProxy.setLayout(new VerticalFlowLayout(VerticalFlowLayout.LEFT)); + + JPanel p18 = new JPanel(); + p18.add(new JLabel(f.getString("gui.settings.proxy.address"))); + + p18.add(getAutoSaveTextField(proxy.getConfig(), "proxy")); + p18.add(getAutoSaveCheckBox(proxy.getConfig(), "state", f.getString("gui.settings.proxy.state"))); + + JButton btnMirror = new JButton(f.getString("gui.settings.proxy.mirror")); + // TODO GuiMirror + panelProxy.add(btnMirror); + panelProxy.add(p18); + + panel.add(panelProxy); + if (config.getConfig().keySet().size() != claimed.size()) { JPanel panelUnclaimed = new JPanel(); panelUnclaimed.setBorder(new TitledBorder(null, f.getString("gui.settings.unclaimed"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.orange)); @@ -315,7 +332,7 @@ private JButton getSelectInstallationButton(@NotNull File Installation, String n private void setModLoaderInstallation(String key, @NotNull File file) { config.getValue("addon").getAsJsonObject().getAsJsonObject(key).addProperty("installation", file.getPath()); - config.save(); + } /** @@ -338,7 +355,7 @@ private void toggleLoader(@Nullable String type) { JsonObject cn = addon.get("lunarcn").getAsJsonObject(); weave.addProperty("enable", b1); cn.addProperty("enable", b2); - config.save(); + } private boolean isLoaderSelected(String type) { @@ -353,7 +370,7 @@ private boolean isLoaderSelected(String type) { log.warn("Weave cannot load with LunarCN, auto corrected"); weave.addProperty("enable", false); cn.addProperty("enable", false); - config.save(); + return isLoaderSelected(null); } if (type == null) { @@ -421,7 +438,6 @@ private void addUnclaimed(JPanel basePanel, @NotNull JsonObject json) { v = source.getSelectedItem().toString(); } json.addProperty(key, v); - config.save(); }); return cb; } @@ -455,7 +471,6 @@ private static JSpinner getAutoSaveSpinner(@NotNull JsonObject json, String key, JSpinner source = (JSpinner) e.getSource(); Number v = (Number) source.getValue(); json.addProperty(key, v); - config.save(); }); textField.setColumns(20); return spinner; @@ -469,7 +484,6 @@ private static JCheckBox getAutoSaveCheckBox(@NotNull JsonObject json, String ke cb.addActionListener((e) -> { JCheckBox source = (JCheckBox) e.getSource(); json.addProperty(key, source.isSelected()); - config.save(); }); return cb; } @@ -482,7 +496,7 @@ private static JTextField getAutoSaveTextField(@NotNull JsonObject json, String JTextField source = (JTextField) e.getSource(); // save value json.addProperty(key, source.getText()); - config.save(); + }); input.addFocusListener(new FocusAdapter() { @Override @@ -490,7 +504,6 @@ public void focusLost(FocusEvent e) { JTextField source = (JTextField) e.getSource(); // save value json.addProperty(key, source.getText()); - config.save(); } }); return input; diff --git a/src/main/resources/languages/launcher.properties b/src/main/resources/languages/launcher.properties index 92371364..5acd4b0f 100644 --- a/src/main/resources/languages/launcher.properties +++ b/src/main/resources/languages/launcher.properties @@ -195,4 +195,8 @@ gui.settings.folder.main=Config Folder gui.settings.folder.theme=Themes Folder gui.settings.folder.log=Logs Folder gui.addon.toggle.disable=Disable -gui.addon.toggle.enable=Enable \ No newline at end of file +gui.addon.toggle.enable=Enable +gui.settings.proxy=Proxy +gui.settings.proxy.mirror=Mirror +gui.settings.proxy.address=Proxy address: +gui.settings.proxy.state=Use proxy \ No newline at end of file diff --git a/src/main/resources/languages/launcher_en.properties b/src/main/resources/languages/launcher_en.properties index 92371364..5acd4b0f 100644 --- a/src/main/resources/languages/launcher_en.properties +++ b/src/main/resources/languages/launcher_en.properties @@ -195,4 +195,8 @@ gui.settings.folder.main=Config Folder gui.settings.folder.theme=Themes Folder gui.settings.folder.log=Logs Folder gui.addon.toggle.disable=Disable -gui.addon.toggle.enable=Enable \ No newline at end of file +gui.addon.toggle.enable=Enable +gui.settings.proxy=Proxy +gui.settings.proxy.mirror=Mirror +gui.settings.proxy.address=Proxy address: +gui.settings.proxy.state=Use proxy \ No newline at end of file diff --git a/src/main/resources/languages/launcher_ja.properties b/src/main/resources/languages/launcher_ja.properties index f2801783..ec43576f 100644 --- a/src/main/resources/languages/launcher_ja.properties +++ b/src/main/resources/languages/launcher_ja.properties @@ -204,3 +204,9 @@ gui.settings.folder.theme=テーマフォルダー gui.settings.folder.log=ログフォルダー gui.addon.toggle.disable=無効にする gui.addon.toggle.enable=有効にする +gui.settings.proxy=プロキシ +gui.settings.proxy.mirror=鏡 +gui.settings.proxy.address=プロキシアドレス: +gui.settings.proxy.address=プロキシアドレス: +gui.settings.proxy.address=プロキシアドレス: +gui.settings.proxy.state=プロキシを使う diff --git a/src/main/resources/languages/launcher_ko.properties b/src/main/resources/languages/launcher_ko.properties index 0586cd5a..2f206ddc 100644 --- a/src/main/resources/languages/launcher_ko.properties +++ b/src/main/resources/languages/launcher_ko.properties @@ -201,4 +201,8 @@ gui.settings.folder.main=구성 폴더 gui.settings.folder.theme=테마 폴더 gui.settings.folder.log=로그 폴더 gui.addon.toggle.disable=장애를 입히다 -gui.addon.toggle.enable=할 수 있게 하다 \ No newline at end of file +gui.addon.toggle.enable=할 수 있게 하다 +gui.settings.proxy=대리 +gui.settings.proxy.mirror=거울 +gui.settings.proxy.address=프록시 주소: +gui.settings.proxy.state=프록시 사용 \ No newline at end of file diff --git a/src/main/resources/languages/launcher_zh.properties b/src/main/resources/languages/launcher_zh.properties index 3ab36ba5..4444533d 100644 --- a/src/main/resources/languages/launcher_zh.properties +++ b/src/main/resources/languages/launcher_zh.properties @@ -194,4 +194,8 @@ gui.settings.folder.main=设置文件夹 gui.settings.folder.theme=主题文件夹 gui.settings.folder.log=日志文件夹 gui.addon.toggle.disable=禁用 -gui.addon.toggle.enable=启用 \ No newline at end of file +gui.addon.toggle.enable=启用 +gui.settings.proxy=代理 +gui.settings.proxy.mirror=镜像源 +gui.settings.proxy.address=代理地址: +gui.settings.proxy.state=使用代理 \ No newline at end of file