[FEATURE] Profile delete and profile list commands

This commit is contained in:
Gravita 2024-02-27 15:34:04 +07:00
parent 31285a8066
commit 11382d3465
11 changed files with 111 additions and 116 deletions

View file

@ -476,6 +476,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
profile = Launcher.gsonManager.gson.fromJson(reader, ClientProfile.class);
}
profile.verify();
profile.setProfileFilePath(file);
// Add SIGNED profile to result list
result.add(profile);

View file

@ -0,0 +1,56 @@
package pro.gravit.launchserver.command.profiles;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pro.gravit.launcher.base.profiles.ClientProfile;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.Command;
import pro.gravit.utils.helper.IOHelper;
import java.nio.file.Files;
public class DeleteProfileCommand extends Command {
private final transient Logger logger = LogManager.getLogger(ListProfilesCommand.class);
public DeleteProfileCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[uuid/title]";
}
@Override
public String getUsageDescription() {
return "permanently delete profile";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
ClientProfile profile = null;
for(var p : server.getProfiles()) {
if(p.getUUID().toString().equals(args[0]) || p.getTitle().equals(args[1])) {
profile = p;
break;
}
}
if(profile == null) {
logger.error("Profile {} not found", args[0]);
return;
}
var clientDir = server.updatesDir.resolve(profile.getDir()).toAbsolutePath();
logger.warn("THIS ACTION DELETE PROFILE AND ALL FILES IN {}", clientDir);
if(!showApplyDialog("Continue?")) {
return;
}
logger.info("Delete {}", clientDir);
IOHelper.deleteDir(clientDir, true);
var profileFile = profile.getProfileFilePath();
if(profileFile == null) {
profileFile = server.profilesDir.resolve(profile.getTitle().concat(".json"));
}
logger.info("Delete {}", profileFile);
Files.deleteIfExists(profileFile);
}
}

View file

@ -0,0 +1,30 @@
package pro.gravit.launchserver.command.profiles;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.Command;
public class ListProfilesCommand extends Command {
private final transient Logger logger = LogManager.getLogger(ListProfilesCommand.class);
public ListProfilesCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return null;
}
@Override
public String getUsageDescription() {
return "show all profiles";
}
@Override
public void invoke(String... args) throws Exception {
for(var profile : server.getProfiles()) {
logger.info("{} ({}) {}", profile.getTitle(), profile.getVersion().toString(), profile.isLimited() ? "limited" : "");
}
}
}

View file

@ -9,6 +9,8 @@ public ProfilesCommand(LaunchServer server) {
this.childCommands.put("make", new MakeProfileCommand(server));
this.childCommands.put("save", new SaveProfilesCommand(server));
this.childCommands.put("clone", new CloneProfileCommand(server));
this.childCommands.put("list", new ListProfilesCommand(server));
this.childCommands.put("delete", new DeleteProfileCommand(server));
}
@Override

View file

@ -2,7 +2,6 @@
import pro.gravit.launcher.core.LauncherTrustManager;
import pro.gravit.launcher.base.modules.*;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.Version;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;

View file

@ -12,12 +12,13 @@
import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.util.*;
public final class ClientProfile implements Comparable<ClientProfile> {
private static final FileNameMatcher ASSET_MATCHER = new FileNameMatcher(
new String[0], new String[]{"indexes", "objects"}, new String[0]);
private transient Path profileFilePath;
@LauncherNetworkAPI
private String title;
@LauncherNetworkAPI
@ -461,6 +462,14 @@ public List<CompatibilityFlags> getFlags() {
return flags;
}
public Path getProfileFilePath() {
return profileFilePath;
}
public void setProfileFilePath(Path profileFilePath) {
this.profileFilePath = profileFilePath;
}
public enum ClassLoaderConfig {
AGENT, LAUNCHER, MODULE, SYSTEM_ARGS
}

View file

@ -1,59 +0,0 @@
package pro.gravit.utils;
import java.net.URL;
import java.net.URLClassLoader;
public class PublicURLClassLoader extends URLClassLoader {
/**
* Constructs a new URLClassLoader for the specified URLs using the
* default delegation parent {@code ClassLoader}. The URLs will
* be searched in the order specified for classes and resources after
* first searching in the parent class loader. Any URL that ends with
* a '/' is assumed to refer to a directory. Otherwise, the URL is
* assumed to refer to a JAR file which will be downloaded and opened
* as needed.
*
* <p>If there is a security manager, this method first
* calls the security manager's {@code checkCreateClassLoader} method
* to ensure creation of a class loader is allowed.
*
* @param urls the URLs from which to load classes and resources
* @throws SecurityException if a security manager exists and its
* {@code checkCreateClassLoader} method doesn't allow
* creation of a class loader.
* @throws NullPointerException if {@code urls} is {@code null}.
*/
public PublicURLClassLoader(URL[] urls) {
super(urls);
}
/**
* Constructs a new URLClassLoader for the given URLs. The URLs will be
* searched in the order specified for classes and resources after first
* searching in the specified parent class loader. Any {@code jar:}
* scheme URL is assumed to refer to a JAR file. Any {@code file:} scheme
* URL that ends with a '/' is assumed to refer to a directory. Otherwise,
* the URL is assumed to refer to a JAR file which will be downloaded and
* opened as needed.
*
* <p>If there is a security manager, this method first
* calls the security manager's {@code checkCreateClassLoader} method
* to ensure creation of a class loader is allowed.
*
* @param urls the URLs from which to load classes and resources
* @param parent the parent class loader for delegation
* @throws SecurityException if a security manager exists and its
* {@code checkCreateClassLoader} method doesn't allow
* creation of a class loader.
* @throws NullPointerException if {@code urls} is {@code null}.
*/
public PublicURLClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
@Override
public void addURL(URL url) {
super.addURL(url);
}
}

View file

@ -12,6 +12,8 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.Deflater;
@ -82,6 +84,15 @@ public static void close(OutputStream out) {
}
}
public static Manifest getManifest(Class<?> clazz) {
Path path = getCodeSource(clazz);
try(JarFile jar = new JarFile(path.toFile())) {
return jar.getManifest();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public static URL convertToURL(String url) {
try {
return new URL(url);

View file

@ -1,17 +1,12 @@
package pro.gravit.utils.helper;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
public final class JVMHelper {
@ -77,45 +72,11 @@ public static String getNativePrefix(JVMHelper.OS OS_TYPE) {
};
}
public static void appendVars(ProcessBuilder builder, Map<String, String> vars) {
builder.environment().putAll(vars);
}
public static Class<?> firstClass(String... names) throws ClassNotFoundException {
for (String name : names)
try {
return Class.forName(name, false, LOADER);
} catch (ClassNotFoundException ignored) {
// Expected
}
throw new ClassNotFoundException(Arrays.toString(names));
}
public static void fullGC() {
RUNTIME.gc();
LogHelper.debug("Used heap: %d MiB", RUNTIME.totalMemory() - RUNTIME.freeMemory() >> 20);
}
public static String[] getClassPath() {
return System.getProperty("java.class.path").split(File.pathSeparator);
}
public static URL[] getClassPathURL() {
String[] cp = System.getProperty("java.class.path").split(File.pathSeparator);
URL[] list = new URL[cp.length];
for (int i = 0; i < cp.length; i++) {
URL url = null;
try {
url = new URL(cp[i]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
list[i] = url;
}
return list;
}
public static X509Certificate[] getCertificates(Class<?> clazz) {
Object[] signers = clazz.getSigners();
if (signers == null) return null;
@ -140,10 +101,6 @@ private static int getCorrectOSArch() {
return System.getProperty("os.arch").contains("64") ? 64 : 32;
}
public static String getEnvPropertyCaseSensitive(String name) {
return System.getenv().get(name);
}
public static boolean isJVMMatchesSystemArch() {
return JVM_BITS == OS_BITS;
}
@ -152,16 +109,6 @@ public static String jvmProperty(String name, String value) {
return String.format("-D%s=%s", name, value);
}
public static String systemToJvmProperty(String name) {
return String.format("-D%s=%s", name, System.getProperties().getProperty(name));
}
public static void addSystemPropertyToArgs(Collection<String> args, String name) {
String property = System.getProperty(name);
if (property != null)
args.add(String.format("-D%s=%s", name, property));
}
public static void verifySystemProperties(Class<?> mainClass, boolean requireSystem) {
Locale.setDefault(Locale.US);
// Verify class loader

View file

@ -7,7 +7,6 @@
import pro.gravit.launcher.base.request.auth.GetPublicKeyRequest;
import pro.gravit.launcher.base.request.websockets.StdWebSocketService;
import pro.gravit.launcher.server.ServerWrapper;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;

@ -1 +1 @@
Subproject commit 875cbb463dc432bc48300e4f3a7e5b3b21c6267e
Subproject commit c176ddfef61247e98e3832921877ec4a79cdcc63