Fix and improve checking if profile is loaded
Other changes: - Improved readability of logging - Improved deleting profiles - Added some comments - Forgot to remove old classes in imports
This commit is contained in:
parent
2ed45b5b99
commit
8435350ee0
3 changed files with 45 additions and 37 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
package com.axolotlmaid.optionsprofiles.gui;
|
package com.axolotlmaid.optionsprofiles.gui;
|
||||||
|
|
||||||
import com.axolotlmaid.optionsprofiles.profiles.OldProfiles;
|
|
||||||
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
|
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
|
||||||
import net.minecraft.ChatFormatting;
|
import net.minecraft.ChatFormatting;
|
||||||
import net.minecraft.client.gui.GuiGraphics;
|
import net.minecraft.client.gui.GuiGraphics;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package com.axolotlmaid.optionsprofiles.gui;
|
package com.axolotlmaid.optionsprofiles.gui;
|
||||||
|
|
||||||
import com.axolotlmaid.optionsprofiles.profiles.OldProfiles;
|
|
||||||
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
|
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
|
||||||
import net.minecraft.client.gui.GuiGraphics;
|
import net.minecraft.client.gui.GuiGraphics;
|
||||||
import net.minecraft.client.gui.components.Button;
|
import net.minecraft.client.gui.components.Button;
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,15 @@ package com.axolotlmaid.optionsprofiles.profiles;
|
||||||
import com.axolotlmaid.optionsprofiles.OptionsProfilesMod;
|
import com.axolotlmaid.optionsprofiles.OptionsProfilesMod;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.Comparator;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
public class Profiles {
|
public class Profiles {
|
||||||
private static final Path PROFILES_DIRECTORY = Paths.get("options-profiles");
|
private static final Path PROFILES_DIRECTORY = Paths.get("options-profiles");
|
||||||
|
|
@ -24,6 +24,7 @@ public class Profiles {
|
||||||
String profileName = "Profile 1";
|
String profileName = "Profile 1";
|
||||||
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
||||||
|
|
||||||
|
// Increases the number in 'Profile 1' if it already exists
|
||||||
for (int i = 1; Files.exists(profile); i++) {
|
for (int i = 1; Files.exists(profile); i++) {
|
||||||
profileName = "Profile " + i;
|
profileName = "Profile " + i;
|
||||||
profile = Paths.get(PROFILES_DIRECTORY.toString(), profileName);
|
profile = Paths.get(PROFILES_DIRECTORY.toString(), profileName);
|
||||||
|
|
@ -33,13 +34,13 @@ public class Profiles {
|
||||||
Files.createDirectory(profile);
|
Files.createDirectory(profile);
|
||||||
|
|
||||||
if (Files.exists(profile)) {
|
if (Files.exists(profile)) {
|
||||||
OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: created");
|
OptionsProfilesMod.LOGGER.info("[Profile '{}']: created", profileName);
|
||||||
writeProfile(profileName);
|
writeProfile(profileName);
|
||||||
} else {
|
} else {
|
||||||
OptionsProfilesMod.LOGGER.warn("[Profile '" + profileName + "']: Profile already exists?");
|
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile already exists?", profileName);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when creating a profile", e);
|
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when creating a profile", profileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,7 +51,7 @@ public class Profiles {
|
||||||
try {
|
try {
|
||||||
Files.copy(options, profileOptions);
|
Files.copy(options, profileOptions);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
OptionsProfilesMod.LOGGER.error("[Profile '" + profile.getFileName().toString() + "']: Unable to copy '" + options.getFileName().toString() + "'", e);
|
OptionsProfilesMod.LOGGER.error("[Profile '{}']: Unable to copy '{}'", profile.getFileName().toString(), options.getFileName().toString(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -59,9 +60,10 @@ public class Profiles {
|
||||||
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Removes old option files
|
||||||
FileUtils.cleanDirectory(profile.toFile());
|
FileUtils.cleanDirectory(profile.toFile());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when clearing old options files", e);
|
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when clearing old options files", profileName, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
copyOptionFile(profile, OPTIONS_FILE);
|
copyOptionFile(profile, OPTIONS_FILE);
|
||||||
|
|
@ -70,25 +72,36 @@ public class Profiles {
|
||||||
copyOptionFile(profile, SODIUM_EXTRA_OPTIONS_FILE);
|
copyOptionFile(profile, SODIUM_EXTRA_OPTIONS_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean compareFileContent(Path file1, Path file2) throws IOException {
|
|
||||||
return
|
|
||||||
Files.exists(file1)
|
|
||||||
&& Files.exists(file2)
|
|
||||||
&& Files.readAllLines(file1).equals(Files.readAllLines(file2));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isProfileLoaded(String profileName) {
|
public static boolean isProfileLoaded(String profileName) {
|
||||||
|
boolean profileLoaded = false;
|
||||||
|
|
||||||
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
||||||
|
|
||||||
|
List<Path> optionFiles = new ArrayList<>();
|
||||||
|
optionFiles.add(OPTIONS_FILE);
|
||||||
|
|
||||||
|
// The next few lines check if the specified file exists. If so, it adds it to the optionFiles ArrayList.
|
||||||
|
Optional.of(OPTIFINE_OPTIONS_FILE).filter(Files::exists).ifPresent(optionFiles::add);
|
||||||
|
Optional.of(SODIUM_OPTIONS_FILE).filter(Files::exists).ifPresent(optionFiles::add);
|
||||||
|
Optional.of(SODIUM_EXTRA_OPTIONS_FILE).filter(Files::exists).ifPresent(optionFiles::add);
|
||||||
|
|
||||||
|
// Check if the original option file and the profile option file have the same content
|
||||||
try {
|
try {
|
||||||
return compareFileContent(OPTIONS_FILE, profile.resolve(OPTIONS_FILE))
|
for (Path optionFile : optionFiles) {
|
||||||
|| compareFileContent(OPTIFINE_OPTIONS_FILE, profile.resolve(OPTIFINE_OPTIONS_FILE))
|
Path profileOptions = profile.resolve(optionFile.getFileName());
|
||||||
|| compareFileContent(SODIUM_OPTIONS_FILE, profile.resolve(SODIUM_OPTIONS_FILE))
|
|
||||||
|| compareFileContent(SODIUM_EXTRA_OPTIONS_FILE, profile.resolve(SODIUM_EXTRA_OPTIONS_FILE));
|
if (FileUtils.contentEquals(optionFile.toFile(), profileOptions.toFile())) {
|
||||||
|
profileLoaded = true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when checking if the profile is loaded", e);
|
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when checking if the profile is loaded", profileName, e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return profileLoaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void loadOptionFile(String profileName, Path options) {
|
private static void loadOptionFile(String profileName, Path options) {
|
||||||
|
|
@ -97,10 +110,11 @@ public class Profiles {
|
||||||
|
|
||||||
if (Files.exists(profileOptions)) {
|
if (Files.exists(profileOptions)) {
|
||||||
try {
|
try {
|
||||||
|
// Replaces the original option file with the profile option file
|
||||||
Files.copy(profileOptions, options, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(profileOptions, options, StandardCopyOption.REPLACE_EXISTING);
|
||||||
OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: loaded");
|
OptionsProfilesMod.LOGGER.info("[Profile '{}']: '{}' loaded", profileName, options.getFileName());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when loading the profile", e);
|
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when loading the profile", profileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +125,7 @@ public class Profiles {
|
||||||
|
|
||||||
if (Files.exists(profileOptions)) {
|
if (Files.exists(profileOptions)) {
|
||||||
loader.accept(profileOptions);
|
loader.accept(profileOptions);
|
||||||
OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: loaded");
|
OptionsProfilesMod.LOGGER.info("[Profile '{}']: '{}' loaded", profileName, options.getFileName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,7 +133,7 @@ public class Profiles {
|
||||||
loadOptionFile(profileName, OPTIONS_FILE);
|
loadOptionFile(profileName, OPTIONS_FILE);
|
||||||
loadOptionFile(profileName, OPTIFINE_OPTIONS_FILE);
|
loadOptionFile(profileName, OPTIFINE_OPTIONS_FILE);
|
||||||
loadOptionFile(profileName, SODIUM_OPTIONS_FILE, SodiumConfigLoader::load);
|
loadOptionFile(profileName, SODIUM_OPTIONS_FILE, SodiumConfigLoader::load);
|
||||||
// loadOptionFile(profileName, SODIUM_EXTRA_OPTIONS_FILE, SodiumExtraConfigLoader::load);
|
loadOptionFile(profileName, SODIUM_EXTRA_OPTIONS_FILE, SodiumExtraConfigLoader::load);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void renameProfile(String profileName, String newProfileName) {
|
public static void renameProfile(String profileName, String newProfileName) {
|
||||||
|
|
@ -127,30 +141,26 @@ public class Profiles {
|
||||||
Path newProfile = PROFILES_DIRECTORY.resolve(newProfileName);
|
Path newProfile = PROFILES_DIRECTORY.resolve(newProfileName);
|
||||||
|
|
||||||
if (Files.exists(newProfile)) {
|
if (Files.exists(newProfile)) {
|
||||||
OptionsProfilesMod.LOGGER.warn("[Profile '" + profileName + "']: A profile with that name already exists!");
|
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: A profile with that name already exists!", profileName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Files.move(profile, newProfile);
|
Files.move(profile, newProfile);
|
||||||
OptionsProfilesMod.LOGGER.info("[Profile '" + newProfileName + "']: renamed. Old name: " + profileName);
|
OptionsProfilesMod.LOGGER.info("[Profile '{}']: renamed. Old name: {}", newProfileName, profileName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: An error occurred when renaming the profile", e);
|
OptionsProfilesMod.LOGGER.error("[Profile '{}']: An error occurred when renaming the profile", profileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void deleteProfile(String profileName) {
|
public static void deleteProfile(String profileName) {
|
||||||
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
Path profile = PROFILES_DIRECTORY.resolve(profileName);
|
||||||
|
|
||||||
try (Stream<Path> files = Files.walk(profile)) {
|
try {
|
||||||
files
|
FileUtils.deleteDirectory(profile.toFile());
|
||||||
.sorted(Comparator.reverseOrder())
|
OptionsProfilesMod.LOGGER.info("[Profile '{}']: deleted", profileName);
|
||||||
.map(Path::toFile)
|
|
||||||
.forEach(File::delete);
|
|
||||||
|
|
||||||
OptionsProfilesMod.LOGGER.info("[Profile '" + profileName + "']: deleted");
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
OptionsProfilesMod.LOGGER.error("[Profile '" + profileName + "']: Profile was not deleted", e);
|
OptionsProfilesMod.LOGGER.error("[Profile '{}']: Profile was not deleted", profileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue