Skip to content

Commit

Permalink
fix: When writing configfiles, encapsulate every element of String Li…
Browse files Browse the repository at this point in the history
…sts in `"` in order to avoid problems described in issue #71. Fixes and closes issue #71.
  • Loading branch information
Griefed committed Aug 29, 2021
1 parent 7c490a3 commit 0e029ec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
* 30.{@link #readStringArray()}<br>
* 31.{@link #buildString(String...)}<br>
* 32.{@link #readBoolean()}<br>
* 33.{@link #writeConfigToFile(String, String, String, boolean, String, String, String, String, boolean, boolean, boolean, boolean, String, File, boolean)}<br>
* 33.{@link #writeConfigToFile(String, List, List, boolean, String, String, String, String, boolean, boolean, boolean, boolean, String, File, boolean)}<br>
* 34.{@link #getConfigurationAsList(ConfigurationModel)}
* <p>
* Requires an instance of {@link CurseCreateModpack} in order to create a modpack from scratch should the specified modpackDir
Expand Down Expand Up @@ -193,7 +193,7 @@ public ObjectMapper getObjectMapper() {
/**
* Getter for creator.conf.
* @author Griefed
* @return File. Returns the creator.conf-file for use in {@link #writeConfigToFile(String, String, String, boolean, String, String, String, String, boolean, boolean, boolean, boolean, String, File, boolean)}
* @return File. Returns the creator.conf-file for use in {@link #writeConfigToFile(String, List, List, boolean, String, String, String, String, boolean, boolean, boolean, boolean, String, File, boolean)}
*/
public File getOldConfigFile() {
return FILE_CONFIG_OLD;
Expand All @@ -203,7 +203,7 @@ public File getOldConfigFile() {
* Getter for serverpackcreator.conf.
* @author Griefed
* @return File. Returns the serverpackcreator.conf-file.
* {@link #writeConfigToFile(String, String, String, boolean, String, String, String, String, boolean, boolean, boolean, boolean, String, File, boolean)}
* {@link #writeConfigToFile(String, List, List, boolean, String, String, String, String, boolean, boolean, boolean, boolean, String, File, boolean)}
*/
public File getConfigFile() {
return FILE_CONFIG;
Expand Down Expand Up @@ -661,8 +661,8 @@ boolean isCurse(ConfigurationModel configurationModel) {

writeConfigToFile(
configurationModel.getModpackDir(),
buildString(configurationModel.getClientMods().toString()),
buildString(configurationModel.getCopyDirs().toString()),
configurationModel.getClientMods(),
configurationModel.getCopyDirs(),
configurationModel.getIncludeServerInstallation(),
configurationModel.getJavaPath(),
configurationModel.getMinecraftVersion(),
Expand Down Expand Up @@ -1647,8 +1647,8 @@ void createConfigurationFile() {
//-----------------------------------------------------------------------------------------WRITE CONFIG TO FILE---------
if (writeConfigToFile(
modpackDir,
buildString(Arrays.toString(tmpClientMods)),
buildString(Arrays.toString(tmpCopyDirs)),
Arrays.asList(tmpClientMods),
Arrays.asList(tmpCopyDirs),
includeServerInstallation,
javaPath,
minecraftVersion,
Expand Down Expand Up @@ -1701,6 +1701,27 @@ public String buildString(String... args) {
return stringBuilder.toString();
}

/**
* Encapsulate every element of the passed String List in quotes. Returns the list as <code>["element1","element2","element3"</code> etc.
* @author Griefed
* @param listToEncapsulate The String List of which to encapsulate every element in.
* @return String. Returns a concatenated String with all elements of the passed list encapsulated.
*/
public String encapsulateListElements(List<String> listToEncapsulate) {

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("[\"").append(listToEncapsulate.get(0).replace("\\", "/")).append("\"");

for (String element : listToEncapsulate) {
stringBuilder.append(",\"").append(element.replace("\\", "/")).append("\"");
}

stringBuilder.append("]");

return stringBuilder.toString();
}

/**
* A helper method for {@link #createConfigurationFile()}. Prompts the user to enter values which will then be
* converted to booleans, either <code>TRUE</code> or <code>FALSE</code>. This prevents any non-boolean values
Expand Down Expand Up @@ -1759,8 +1780,8 @@ private boolean readBoolean() {
* @return Boolean. Returns true if the configuration file has been successfully written and old ones replaced.
*/
public boolean writeConfigToFile(String modpackDir,
String clientMods,
String copyDirs,
List<String> clientMods,
List<String> copyDirs,
boolean includeServer,
String javaPath,
String minecraftVersion,
Expand All @@ -1783,8 +1804,8 @@ public boolean writeConfigToFile(String modpackDir,
//Griefed: What the fuck. This reads like someone having a stroke. What have I created here?
String configString = String.format(
"%s\nmodpackDir = \"%s\"\n\n" +
"%s\nclientMods = [%s]\n\n" +
"%s\ncopyDirs =[%s]\n\n" +
"%s\nclientMods = %s\n\n" +
"%s\ncopyDirs = %s\n\n" +
"%s\nincludeServerInstallation = %b\n\n" +
"%s\njavaPath = \"%s\"\n\n" +
"%s\nminecraftVersion = \"%s\"\n\n" +
Expand All @@ -1796,8 +1817,8 @@ public boolean writeConfigToFile(String modpackDir,
"%s\nincludeZipCreation = %b\n\n" +
"%s\njavaArgs = \"%s\"\n",
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.modpackdir"), modpackDir.replace("\\","/"),
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.clientmods"), clientMods,
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.copydirs"), copyDirs.replace("\\","/"),
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.clientmods"), encapsulateListElements(clientMods),
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.copydirs"), encapsulateListElements(copyDirs),
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.includeserverinstallation"), includeServer,
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.javapath"), javaPath.replace("\\","/"),
LOCALIZATIONMANAGER.getLocalizedString("configuration.writeconfigtofile.minecraftversion"), minecraftVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -992,10 +993,13 @@ void saveConfig(File configFile, boolean temporary) {
javaArgs = "empty";
}

List<String> tempClientMods = new ArrayList<>(Arrays.asList(TEXTFIELD_CLIENTSIDEMODS.getText().replace(", ", ",").split(",")));
List<String> tempCopyDirs = new ArrayList<>(Arrays.asList(TEXTFIELD_COPYDIRECTORIES.getText().replace(", ", ",").split(",")));

CONFIGURATIONHANDLER.writeConfigToFile(
TEXTFIELD_MODPACKDIRECTORY.getText(),
TEXTFIELD_CLIENTSIDEMODS.getText(),
TEXTFIELD_COPYDIRECTORIES.getText(),
tempClientMods,
tempCopyDirs,
checkBoxServer.isSelected(),
TEXTFIELD_JAVAPATH.getText(),
chosenMinecraftVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1104,8 +1104,8 @@ void writeConfigToFileTestForge() {

Assertions.assertTrue(CONFIGURATIONHANDLER.writeConfigToFile(
"./backend/test/resources/forge_tests",
CONFIGURATIONHANDLER.buildString(clientMods.toString()),
CONFIGURATIONHANDLER.buildString(copyDirs.toString()),
clientMods,
copyDirs,
true,
javaPath,
"1.16.5",
Expand Down Expand Up @@ -1169,8 +1169,8 @@ void writeConfigToFileTestFabric() {

Assertions.assertTrue(CONFIGURATIONHANDLER.writeConfigToFile(
"./backend/test/resources/fabric_tests",
CONFIGURATIONHANDLER.buildString(clientMods.toString()),
CONFIGURATIONHANDLER.buildString(copyDirs.toString()),
clientMods,
copyDirs,
true,
javaPath,
"1.16.5",
Expand Down

0 comments on commit 0e029ec

Please sign in to comment.