Skip to content

Commit

Permalink
disable addons
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Jan 18, 2024
1 parent 385e590 commit aa51874
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'org.cubewhy.celestial'
version = '1.4-pre4-SNAPSHOT'
version = '1.4-SNAPSHOT'

println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getProperty('java.vm.version') + '(' + System.getProperty('java.vendor') + ') Arch: ' + System.getProperty('os.arch'))
println('Celestial Launcher -> https://www.lunarclient.top/')
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cubewhy/celestial/Celestial.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public static GameArgsResult getArgs(String version, String branch, String modul
args.add("-Xmx" + ram + "m");
args.addAll(LauncherData.getDefaultJvmArgs(json, installation));
// === javaagents ===
List<JavaAgent> javaAgents = JavaAgent.findAll();
List<JavaAgent> javaAgents = JavaAgent.findEnabled();
int size = javaAgents.size();
if (size != 0) {
log.info(String.format("Found %s javaagent%s (Except loaders)", size, (size == 1) ? "" : "s"));
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/org/cubewhy/celestial/game/BaseAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
package org.cubewhy.celestial.game;

import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.List;

public abstract class BaseAddon {


protected static @Nullable File autoCopy(@NotNull File file, File folder) throws IOException {
String name = file.getName();
if (!name.endsWith(".jar")) {
Expand All @@ -29,4 +28,20 @@ public abstract class BaseAddon {
FileUtils.copyFile(file, target);
return target;
}

public abstract boolean isEnabled();

/**
* Toggle state
*
* @return true equals Enable, false equals Disable
*/
public abstract boolean toggle();

protected boolean toggle0(File file) {
if (isEnabled()) {
return !file.renameTo(new File(file.getPath() + ".disabled"));
}
return file.renameTo(new File(file.getPath().substring(0, file.getPath().length() - 9)));
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/cubewhy/celestial/game/addon/FabricMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ public FabricMod(File file) {
public String toString() {
return this.file.getName();
}

@Override
public boolean isEnabled() {
// TODO fabric: isEnabled
return true;
}

@Override
public boolean toggle() {
return toggle0(file);
}
}
41 changes: 36 additions & 5 deletions src/main/java/org/cubewhy/celestial/game/addon/JavaAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -87,20 +88,40 @@ public JavaAgent(String path) {
}

/**
* Find all javaagents in the javaagent folder
* Find all mods in the weave mods folder
*/
@NotNull
@Contract(pure = true)
public static List<JavaAgent> findAll() {
public static List<JavaAgent> findEnabled() {
List<JavaAgent> list = new ArrayList<>();
for (File file : Objects.requireNonNull(javaAgentFolder.listFiles())) {
if (file.getName().endsWith(".jar") && file.isFile()) {
list.add(new JavaAgent(file, findAgentArg(file.getName())));
if (javaAgentFolder.isDirectory()) {
for (File file : Objects.requireNonNull(javaAgentFolder.listFiles())) {
if (file.getName().endsWith(".jar") && file.isFile()) {
list.add(new JavaAgent(file));
}
}
}
return list;
}

public static @NotNull List<JavaAgent> findDisabled() {
List<JavaAgent> list = new ArrayList<>();
if (javaAgentFolder.isDirectory()) {
for (File file : Objects.requireNonNull(javaAgentFolder.listFiles())) {
if (file.getName().endsWith(".jar.disabled") && file.isFile()) {
list.add(new JavaAgent(file));
}
}
}
return list;
}

public static @NotNull List<JavaAgent> findAll() {
List<JavaAgent> list = findEnabled();
list.addAll(findDisabled());
return Collections.unmodifiableList(list);
}

/**
* Set param for a Javaagent
*
Expand Down Expand Up @@ -181,4 +202,14 @@ public String toString() {
}
return result;
}

@Override
public boolean isEnabled() {
return this.file.getName().endsWith(".jar");
}

@Override
public boolean toggle() {
return toggle0(file);
}
}
31 changes: 30 additions & 1 deletion src/main/java/org/cubewhy/celestial/game/addon/LunarCNMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -47,7 +48,7 @@ public LunarCNMod(File file) {
*/
@NotNull
@Contract(pure = true)
public static List<LunarCNMod> findAll() {
public static List<LunarCNMod> findEnabled() {
List<LunarCNMod> list = new ArrayList<>();
if (modFolder.isDirectory()) {
for (File file : Objects.requireNonNull(modFolder.listFiles())) {
Expand All @@ -59,6 +60,24 @@ public static List<LunarCNMod> findAll() {
return list;
}

public static @NotNull List<LunarCNMod> findDisabled() {
List<LunarCNMod> list = new ArrayList<>();
if (modFolder.isDirectory()) {
for (File file : Objects.requireNonNull(modFolder.listFiles())) {
if (file.getName().endsWith(".jar.disabled") && file.isFile()) {
list.add(new LunarCNMod(file));
}
}
}
return list;
}

public static @NotNull List<LunarCNMod> findAll() {
List<LunarCNMod> list = findEnabled();
list.addAll(findDisabled());
return Collections.unmodifiableList(list);
}

@Contract(" -> new")
public static @NotNull File getInstallation() {
return new File(config.getValue("addon").getAsJsonObject().get("lunarcn").getAsJsonObject().get("installation").getAsString());
Expand All @@ -79,4 +98,14 @@ public static boolean checkUpdate() throws MalformedURLException {
public String toString() {
return this.file.getName();
}

@Override
public boolean isEnabled() {
return this.file.getName().endsWith(".jar");
}

@Override
public boolean toggle() {
return toggle0(file);
}
}
34 changes: 32 additions & 2 deletions src/main/java/org/cubewhy/celestial/game/addon/WeaveMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand All @@ -39,7 +40,7 @@ public WeaveMod(File file) {
*/
@NotNull
@Contract(pure = true)
public static List<WeaveMod> findAll() {
public static List<WeaveMod> findEnabled() {
List<WeaveMod> list = new ArrayList<>();
if (modFolder.isDirectory()) {
for (File file : Objects.requireNonNull(modFolder.listFiles())) {
Expand All @@ -51,12 +52,31 @@ public static List<WeaveMod> findAll() {
return list;
}

public static @NotNull List<WeaveMod> findDisabled() {
List<WeaveMod> list = new ArrayList<>();
if (modFolder.isDirectory()) {
for (File file : Objects.requireNonNull(modFolder.listFiles())) {
if (file.getName().endsWith(".jar.disabled") && file.isFile()) {
list.add(new WeaveMod(file));
}
}
}
return list;
}

public static @NotNull List<WeaveMod> findAll() {
List<WeaveMod> list = findEnabled();
list.addAll(findDisabled());
return Collections.unmodifiableList(list);
}

public static @Nullable WeaveMod add(@NotNull File file) throws IOException {
File target = autoCopy(file, modFolder);
return (target == null) ? null : new WeaveMod(target);
}

public static File getInstallation() {
@Contract(" -> new")
public static @NotNull File getInstallation() {
return new File(config.getValue("addon").getAsJsonObject().get("weave").getAsJsonObject().get("installation").getAsString());
}

Expand All @@ -69,4 +89,14 @@ public static boolean checkUpdate() throws MalformedURLException {
log.info("Updating Weave Loader");
return AddonUtils.downloadLoader("Weave-MC/Weave-Loader", new File(config.getValue("addon").getAsJsonObject().getAsJsonObject("weave").get("installation").getAsString()));
}

@Override
public boolean isEnabled() {
return file.getName().endsWith(".jar");
}

@Override
public boolean toggle() {
return toggle0(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
package org.cubewhy.celestial.gui.elements;

import lombok.extern.slf4j.Slf4j;
import org.cubewhy.celestial.event.EventTarget;
import org.cubewhy.celestial.event.impl.AddonAddEvent;
import org.cubewhy.celestial.event.impl.CreateLauncherEvent;
import org.cubewhy.celestial.game.BaseAddon;
import org.cubewhy.celestial.game.addon.FabricMod;
import org.cubewhy.celestial.game.addon.JavaAgent;
Expand All @@ -25,7 +23,8 @@
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

Expand All @@ -40,6 +39,10 @@ public class GuiAddonManager extends JPanel {
private final DefaultListModel<JavaAgent> agentList = new DefaultListModel<>();
private final DefaultListModel<FabricMod> fabricList = new DefaultListModel<>();

private final JMenuItem toggleWeave = new JMenuItem("toggle");
private final JMenuItem toggleCN = new JMenuItem("toggle");
private final JMenuItem toggleAgent = new JMenuItem("toggle");

public GuiAddonManager() {
this.setBorder(new TitledBorder(null, f.getString("gui.addons.title"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.orange));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
Expand All @@ -58,8 +61,26 @@ private void initGui() {
JList<WeaveMod> jListWeave = new JList<>(weaveList);
JList<JavaAgent> jListAgents = new JList<>(agentList);
JList<FabricMod> jListFabric = new JList<>(fabricList);

toggleWeave.addActionListener(e -> {
jListWeave.getSelectedValue().toggle();
weaveList.removeAllElements();
loadWeaveMods(weaveList);
});
toggleCN.addActionListener(e -> {
jListLunarCN.getSelectedValue().toggle();
lunarcnList.removeAllElements();
loadLunarCNMods(lunarcnList);
});
toggleAgent.addActionListener(e -> {
jListAgents.getSelectedValue().toggle();
agentList.removeAllElements();
loadAgents(agentList);
});

// menus
JPopupMenu agentMenu = new JPopupMenu();
agentMenu.add(toggleAgent);
JMenuItem manageArg = new JMenuItem(f.getString("gui.addon.agents.arg"));
JMenuItem removeAgent = new JMenuItem(f.getString("gui.addon.agents.remove"));
JMenuItem renameAgent = new JMenuItem(f.getString("gui.addon.rename"));
Expand Down Expand Up @@ -111,6 +132,7 @@ private void initGui() {

// weave menu
JPopupMenu weaveMenu = new JPopupMenu();
weaveMenu.add(toggleWeave);
JMenuItem renameWeaveMod = new JMenuItem(f.getString("gui.addon.rename"));
JMenuItem removeWeaveMod = new JMenuItem(f.getString("gui.addon.mods.weave.remove"));
weaveMenu.add(renameWeaveMod);
Expand Down Expand Up @@ -141,6 +163,7 @@ private void initGui() {
});

JPopupMenu lunarCNMenu = new JPopupMenu();
lunarCNMenu.add(toggleCN);
JMenuItem renameLunarCNMod = new JMenuItem(f.getString("gui.addon.rename"));
JMenuItem removeLunarCNMod = new JMenuItem(f.getString("gui.addon.mods.cn.remove"));
lunarCNMenu.add(renameLunarCNMod);
Expand Down Expand Up @@ -434,6 +457,16 @@ public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
int index = list.locationToIndex(e.getPoint());
list.setSelectedIndex(index);
BaseAddon current = list.getSelectedValue();
if (current.isEnabled()) {
toggleWeave.setText(f.getString("gui.addon.toggle.disable"));
toggleAgent.setText(f.getString("gui.addon.toggle.disable"));
toggleCN.setText(f.getString("gui.addon.toggle.disable"));
} else {
toggleWeave.setText(f.getString("gui.addon.toggle.enable"));
toggleAgent.setText(f.getString("gui.addon.toggle.enable"));
toggleCN.setText(f.getString("gui.addon.toggle.enable"));
}
menu.show(list, e.getX(), e.getY());
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/languages/launcher.properties
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,6 @@ gui.addon.mods.fabric.remove.success=Fabric mod %s successfully removed
gui.addon.mods.fabric.remove.confirm.title=Confirm removal
gui.settings.folder.main=Config Folder
gui.settings.folder.theme=Themes Folder
gui.settings.folder.log=Logs Folder
gui.settings.folder.log=Logs Folder
gui.addon.toggle.disable=Disable
gui.addon.toggle.enable=Enable
4 changes: 3 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,6 @@ gui.addon.mods.fabric.remove.success=Fabric mod %s successfully removed
gui.addon.mods.fabric.remove.confirm.title=Confirm removal
gui.settings.folder.main=Config Folder
gui.settings.folder.theme=Themes Folder
gui.settings.folder.log=Logs Folder
gui.settings.folder.log=Logs Folder
gui.addon.toggle.disable=Disable
gui.addon.toggle.enable=Enable
2 changes: 2 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,5 @@ gui.addon.mods.fabric.remove.confirm.title=削除の確認
gui.settings.folder.main=設定フォルダー
gui.settings.folder.theme=テーマフォルダー
gui.settings.folder.log=ログフォルダー
gui.addon.toggle.disable=無効にする
gui.addon.toggle.enable=有効にする
4 changes: 3 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,6 @@ gui.addon.mods.fabric.remove.success=Fabric 모드 %s이(가) 성공적으로
gui.addon.mods.fabric.remove.confirm.title=제거 확인
gui.settings.folder.main=구성 폴더
gui.settings.folder.theme=테마 폴더
gui.settings.folder.log=로그 폴더
gui.settings.folder.log=로그 폴더
gui.addon.toggle.disable=장애를 입히다
gui.addon.toggle.enable=할 수 있게 하다
Loading

0 comments on commit aa51874

Please sign in to comment.