Skip to content

Commit

Permalink
dump config on close, not immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Jan 20, 2024
1 parent 022a4ee commit 4024256
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 22 deletions.
20 changes: 19 additions & 1 deletion src/main/java/org/cubewhy/celestial/Celestial.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/cubewhy/celestial/files/ConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ 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) {
if (!this.config.has(key)) {
log.info("Init value " + key + " -> " + value);
this.config.add(key, value);
}
return this.save();
return this;
}

public ConfigFile initValue(String key, String value) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/cubewhy/celestial/files/ProxyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.charset.StandardCharsets;

public class ProxyConfig extends ConfigFile {

@Getter
public static class Mirror {

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ private void addArg(String arg, @NotNull DefaultListModel<String> 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<String> model) {
String arg = model.get(index);
model.remove(index);
this.array.remove(index);
log.info("Remove a arg to " + this.key + " -> " + arg);
config.save();
}
}
29 changes: 21 additions & 8 deletions src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();

}

/**
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -421,7 +438,6 @@ private void addUnclaimed(JPanel basePanel, @NotNull JsonObject json) {
v = source.getSelectedItem().toString();
}
json.addProperty(key, v);
config.save();
});
return cb;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -482,15 +496,14 @@ 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
public void focusLost(FocusEvent e) {
JTextField source = (JTextField) e.getSource();
// save value
json.addProperty(key, source.getText());
config.save();
}
});
return input;
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/languages/launcher.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
6 changes: 5 additions & 1 deletion src/main/resources/languages/launcher_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
6 changes: 6 additions & 0 deletions src/main/resources/languages/launcher_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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=プロキシを使う
6 changes: 5 additions & 1 deletion src/main/resources/languages/launcher_ko.properties
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,8 @@ gui.settings.folder.main=구성 폴더
gui.settings.folder.theme=테마 폴더
gui.settings.folder.log=로그 폴더
gui.addon.toggle.disable=장애를 입히다
gui.addon.toggle.enable=할 수 있게 하다
gui.addon.toggle.enable=할 수 있게 하다
gui.settings.proxy=대리
gui.settings.proxy.mirror=거울
gui.settings.proxy.address=프록시 주소:
gui.settings.proxy.state=프록시 사용
6 changes: 5 additions & 1 deletion src/main/resources/languages/launcher_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,8 @@ gui.settings.folder.main=设置文件夹
gui.settings.folder.theme=主题文件夹
gui.settings.folder.log=日志文件夹
gui.addon.toggle.disable=禁用
gui.addon.toggle.enable=启用
gui.addon.toggle.enable=启用
gui.settings.proxy=代理
gui.settings.proxy.mirror=镜像源
gui.settings.proxy.address=代理地址:
gui.settings.proxy.state=使用代理

0 comments on commit 4024256

Please sign in to comment.