[FIX] ProfileProvider support overwrite profile

This commit is contained in:
Gravita 2024-07-25 22:28:26 +07:00
parent 9bffe07d36
commit af2dcec8cd
No known key found for this signature in database
GPG key ID: 543A8F335C9CD633
3 changed files with 24 additions and 18 deletions

View file

@ -9,10 +9,7 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
@ -40,8 +37,21 @@ public Set<ClientProfile> getProfiles() {
@Override
public void addProfile(ClientProfile profile) throws IOException {
Path profilesDirPath = Path.of(profilesDir);
Path target = IOHelper.resolveIncremental(profilesDirPath,
profile.getDir(), "json");
ClientProfile oldProfile;
Path target = null;
for(var e : profilesMap.entrySet()) {
if(e.getValue().getUUID().equals(profile.getUUID())) {
target = e.getKey();
}
}
if(target == null) {
target = IOHelper.resolveIncremental(profilesDirPath,
profile.getTitle(), "json");
oldProfile = profilesMap.get(target);
if(oldProfile != null && !oldProfile.getUUID().equals(profile.getUUID())) {
throw new FileAlreadyExistsException(target.toString());
}
}
try (BufferedWriter writer = IOHelper.newWriter(target)) {
Launcher.gsonManager.configGson.toJson(profile, writer);
}
@ -61,6 +71,13 @@ public void deleteProfile(ClientProfile profile) throws IOException {
}
private void addProfile(Path path, ClientProfile profile) {
for(var e : profilesMap.entrySet()) {
if(e.getValue().getUUID().equals(profile.getUUID())) {
profilesMap.remove(e.getKey());
profilesList.remove(e.getValue());
break;
}
}
profilesMap.put(path, profile);
profilesList.add(profile);
}
@ -83,10 +100,9 @@ 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.put(file, profile);
result.put(file.toAbsolutePath(), profile);
return super.visitFile(file, attrs);
}
}

View file

@ -44,7 +44,6 @@ public void invoke(String... args) throws Exception {
} catch (IllegalArgumentException ex) {
profile = server.config.profileProvider.getProfile(profileName);
}
server.config.profileProvider.deleteProfile(profile);
server.config.profileProvider.addProfile(profile);
}
server.syncProfilesDir();

View file

@ -18,7 +18,6 @@
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
@ -392,14 +391,6 @@ 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
}