diff --git a/src/main/java/com/github/lmh01/mgt2mt/data_stream/SharingHandler.java b/src/main/java/com/github/lmh01/mgt2mt/data_stream/SharingHandler.java index f8dead61..19dd4890 100644 --- a/src/main/java/com/github/lmh01/mgt2mt/data_stream/SharingHandler.java +++ b/src/main/java/com/github/lmh01/mgt2mt/data_stream/SharingHandler.java @@ -3,6 +3,8 @@ import com.github.lmh01.mgt2mt.MadGamesTycoon2ModTool; import com.github.lmh01.mgt2mt.util.*; import com.github.lmh01.mgt2mt.util.interfaces.Exporter; +import com.github.lmh01.mgt2mt.util.interfaces.FreeId; +import com.github.lmh01.mgt2mt.util.interfaces.Importer; import com.github.lmh01.mgt2mt.windows.WindowMain; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -447,34 +449,15 @@ public static boolean exportEngineFeature(String engineFeatureName){ public static String importEngineFeature(String importFolderPath) throws IOException{ AnalyzeExistingEngineFeatures.analyzeEngineFeatures(); - File fileToImport = new File(importFolderPath + "\\engineFeature.txt"); - Map map = Utils.parseDataFile(fileToImport).get(0); - map.put("ID", Integer.toString(AnalyzeExistingEngineFeatures.getFreeEngineFeatureId())); - boolean CanBeImported = false; - for(String string : SharingManager.ENGINE_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){ - if(string.equals(map.get("MGT2MT VERSION"))){ - CanBeImported = true; - } - } - if(!CanBeImported){ - return "Engine feature [" + map.get("NAME EN") + "] could not be imported:\nThe engine feature is not with the current mod tool version compatible\nEngine feature was exported in version: " + map.get("MGT2MT VERSION"); - } - for(Map existingEngineFeatures : AnalyzeExistingEngineFeatures.engineFeatures){ - for(Map.Entry entry : existingEngineFeatures.entrySet()){ - if(entry.getValue().equals(map.get("NAME EN"))){ - LOGGER.info("Engine feature already exists - The engine feature name is already taken"); - return "false"; - } - } - } - boolean addFeature = Summaries.showEngineFeatureMessage(map); - if(addFeature){ - EditEngineFeaturesFile.addEngineFeature(map); - ChangeLog.addLogEntry(32, map.get("NAME EN")); - JOptionPane.showMessageDialog(null, "Engine feature [" + map.get("NAME EN") + "] has been added successfully"); - WindowMain.checkActionAvailability(); - } - return "true"; + String returnValue = SharingManager.importGeneral("engineFeature.txt", + "Engine feature", + importFolderPath, + SharingManager.ENGINE_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS, + (map) -> EditEngineFeaturesFile.addEngineFeature(map), + () -> AnalyzeExistingEngineFeatures.getFreeEngineFeatureId(), + 30, + (map) -> Summaries.showEngineFeatureMessage(map)); + return returnValue; } /** @@ -506,8 +489,8 @@ public static boolean exportGameplayFeature(String gameplayFeatureName){ bw.write("[GRAPHIC]" + map.get("GRAPHIC") + System.getProperty("line.separator")); bw.write("[SOUND]" + map.get("SOUND") + System.getProperty("line.separator")); bw.write("[TECH]" + map.get("TECH") + System.getProperty("line.separator")); - bw.write("[BAD]" + map.get("BAD") + System.getProperty("line.separator")); - bw.write("[GOOD]" + map.get("GOOD") + System.getProperty("line.separator")); + bw.write("[BAD]" + getGenreNames(map.get("BAD")) + System.getProperty("line.separator")); + bw.write("[GOOD]" + getGenreNames(map.get("GOOD")) + System.getProperty("line.separator")); bw.close(); ChangeLog.addLogEntry(29, map.get("NAME EN")); return true; @@ -519,7 +502,16 @@ public static boolean exportGameplayFeature(String gameplayFeatureName){ } public static String importGameplayFeature(String importFolderPath) throws IOException{ - return null; + AnalyzeExistingGameplayFeatures.analyzeGameplayFeatures(); + String returnValue = SharingManager.importGeneral("gameplayFeature.txt", + "Gameplay feature", + importFolderPath, + SharingManager.GAMEPLAY_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS, + EditGameplayFeaturesFile::addGameplayFeature, + AnalyzeExistingGameplayFeatures::getFreeGameplayFeatureId, + 30, + Summaries::showGameplayFeatureMessage); + return returnValue; } /** diff --git a/src/main/java/com/github/lmh01/mgt2mt/util/GameplayFeatureHelper.java b/src/main/java/com/github/lmh01/mgt2mt/util/GameplayFeatureHelper.java index f53a5960..e4c23614 100644 --- a/src/main/java/com/github/lmh01/mgt2mt/util/GameplayFeatureHelper.java +++ b/src/main/java/com/github/lmh01/mgt2mt/util/GameplayFeatureHelper.java @@ -75,7 +75,7 @@ public static void addGameplayFeature(){ JComboBox comboBoxFeatureType = new JComboBox(); comboBoxFeatureType.setToolTipText("Select what type your gameplay feature should be"); comboBoxFeatureType.setModel(new DefaultComboBoxModel<>(new String[]{"Graphic", "Sound", "Physics", "Gameplay", "Controls", "Multiplayer"})); - comboBoxFeatureType.setSelectedItem("Multiplayer"); + comboBoxFeatureType.setSelectedItem("Graphic"); panelType.add(labelSelectType); panelType.add(comboBoxFeatureType); @@ -249,50 +249,9 @@ public static void addGameplayFeature(){ newGameplayFeature.put("TECH", spinnerTech.getValue().toString()); newGameplayFeature.put("GOOD", Utils.transformArrayListToString(goodGenreIds[0])); newGameplayFeature.put("BAD", Utils.transformArrayListToString(badGenreIds[0])); - StringBuilder badGenresFeatures = new StringBuilder(); - boolean firstBadFeature = true; - if(newGameplayFeature.get("BAD").equals("")){ - badGenresFeatures.append("None"); - }else{ - for(Integer integer : badGenreIds[0]){ - if(!firstBadFeature){ - badGenresFeatures.append(", "); - }else{ - firstBadFeature = false; - } - badGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(integer)); - } - } - StringBuilder goodGenresFeatures = new StringBuilder(); - boolean firstGoodFeature = true; - if(newGameplayFeature.get("GOOD").equals("")){ - goodGenresFeatures.append("None"); - }else{ - for(Integer integer : goodGenreIds[0]){ - if(!firstGoodFeature){ - goodGenresFeatures.append(", "); - }else{ - firstGoodFeature = false; - } - goodGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(integer)); - } - } - String messageBody = "Your gameplay feature is ready:\n\n" + - "Name: " + newGameplayFeature.get("NAME EN") + "\n" + - "Description: " + newGameplayFeature.get("DESC EN") + "\n" + - "Unlock date: " + newGameplayFeature.get("DATE") + "\n" + - "Type: " + getGameplayFeatureNameByTypeId(Integer.parseInt(newGameplayFeature.get("TYP"))) + "\n" + - "Research point cost: " + newGameplayFeature.get("RES POINTS") + "\n" + - "Research cost " + newGameplayFeature.get("PRICE") + "\n" + - "Development cost: " + newGameplayFeature.get("DEV COSTS") + "\n" + - "\n*Bad genres*\n\n" + badGenresFeatures.toString() + "\n" + - "\n*Good genres*\n\n" + goodGenresFeatures.toString() + "\n" + - "\n*Points*\n\n" + - "Gameplay: " + newGameplayFeature.get("GAMEPLAY") + "\n" + - "Graphic: " + newGameplayFeature.get("GRAPHIC") + "\n" + - "Sound: " + newGameplayFeature.get("SOUND") + "\n" + - "Tech: " + newGameplayFeature.get("TECH") + "\n"; - if(JOptionPane.showConfirmDialog(null, messageBody, "Add gameplay feature?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ + + boolean addFeature = Summaries.showGameplayFeatureMessage(newGameplayFeature); + if(addFeature) { EditGameplayFeaturesFile.addGameplayFeature(newGameplayFeature); JOptionPane.showMessageDialog(null, "Gameplay feature: [" + newGameplayFeature.get("NAME EN") + "] has been added successfully!", "Gameplay feature added", JOptionPane.INFORMATION_MESSAGE); break; diff --git a/src/main/java/com/github/lmh01/mgt2mt/util/SharingManager.java b/src/main/java/com/github/lmh01/mgt2mt/util/SharingManager.java index ea149df0..d3c1ee88 100644 --- a/src/main/java/com/github/lmh01/mgt2mt/util/SharingManager.java +++ b/src/main/java/com/github/lmh01/mgt2mt/util/SharingManager.java @@ -1,7 +1,13 @@ package com.github.lmh01.mgt2mt.util; +import com.github.lmh01.mgt2mt.data_stream.AnalyzeExistingEngineFeatures; +import com.github.lmh01.mgt2mt.data_stream.ChangeLog; import com.github.lmh01.mgt2mt.data_stream.SharingHandler; +import com.github.lmh01.mgt2mt.util.interfaces.FreeId; +import com.github.lmh01.mgt2mt.util.interfaces.Importer; import com.github.lmh01.mgt2mt.util.interfaces.ReturnValue; +import com.github.lmh01.mgt2mt.util.interfaces.Summary; +import com.github.lmh01.mgt2mt.windows.WindowMain; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,6 +17,7 @@ import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.Map; public class SharingManager { //This class contains functions with which it is easy to export/import things @@ -25,15 +32,15 @@ public class SharingManager { * * @param fileName This is the file the tool will search for in the folder. Eg. genre.txt or publisher.txt * @param importName The name that is written is some JOptionPanes. Eg. genre, publisher, theme - * @param exportFunction The function that exports the files + * @param importFunction The function that imports the files * @param compatibleModToolVersions A array containing the compatible mod tool versions for the import file */ - public static void importThings(String fileName, String importName, ReturnValue exportFunction, String[] compatibleModToolVersions){ + public static void importThings(String fileName, String importName, ReturnValue importFunction, String[] compatibleModToolVersions){ try { ArrayList importFolders = getImportFolderPath(fileName); try{ for(String importFolder : importFolders){ - analyzeReturnValue(importName, exportFunction.getReturnValue(importFolder), compatibleModToolVersions); + analyzeReturnValue(importName, importFunction.getReturnValue(importFolder), compatibleModToolVersions); } }catch(NullPointerException ignored){ @@ -44,6 +51,49 @@ public static void importThings(String fileName, String importName, ReturnValue } } + /** + * Loads the contents of import file into a map and imports feature with this map + * @param importFile This is the file the tool will search for in the folder. Eg. genre.txt or publisher.txt + * @param importName The name that is written is some JOptionPanes. Eg. Engine feature, Gameplay feature + * @param importFolderPath The folder where the importFile is located. + * @param compatibleModToolVersions A array containing the compatible mod tool versions for the import file + * @param importFunction The function that edits the file + * @param freeId The function that returns the free id + * @param changelogId The id that should be used when the changelog file is being edited + * @param summary The summary function that should be used + * @return + */ + public static String importGeneral(String importFile, String importName, String importFolderPath, String[] compatibleModToolVersions, Importer importFunction, FreeId freeId, int changelogId, Summary summary) throws IOException{ + File fileToImport = new File(importFolderPath + "\\" + importFile); + Map map = Utils.parseDataFile(fileToImport).get(0); + map.put("ID", Integer.toString(freeId.getFreeId())); + boolean CanBeImported = false; + for(String string : compatibleModToolVersions){ + if(string.equals(map.get("MGT2MT VERSION"))){ + CanBeImported = true; + } + } + if(!CanBeImported){ + return importName + " [" + map.get("NAME EN") + "] could not be imported:\n" + importName + " is not with the current mod tool version compatible\n" + importName + " was exported in version: " + map.get("MGT2MT VERSION"); + } + for(Map existingEngineFeatures : AnalyzeExistingEngineFeatures.engineFeatures){ + for(Map.Entry entry : existingEngineFeatures.entrySet()){ + if(entry.getValue().equals(map.get("NAME EN"))){ + LOGGER.info(importName + " already exists - " + importName + " name is already taken"); + return "false"; + } + } + } + boolean addFeature = summary.showSummary(map); + if(addFeature){ + importFunction.importer(map); + JOptionPane.showMessageDialog(null, importName + " [" + map.get("NAME EN") + "] has been added successfully"); + ChangeLog.addLogEntry(changelogId, map.get("NAME EN")); + WindowMain.checkActionAvailability(); + } + return "true"; + } + /** * This function will prompt the user to choose a folder where the files to import are located * @param fileName This is the file the tool will search for in the folder. Eg. genre.txt or publisher.txt diff --git a/src/main/java/com/github/lmh01/mgt2mt/util/Summaries.java b/src/main/java/com/github/lmh01/mgt2mt/util/Summaries.java index 3bde523e..8db9b120 100644 --- a/src/main/java/com/github/lmh01/mgt2mt/util/Summaries.java +++ b/src/main/java/com/github/lmh01/mgt2mt/util/Summaries.java @@ -1,6 +1,10 @@ package com.github.lmh01.mgt2mt.util; +import com.github.lmh01.mgt2mt.data_stream.AnalyzeExistingGenres; +import com.github.lmh01.mgt2mt.data_stream.EditGameplayFeaturesFile; + import javax.swing.*; +import java.util.ArrayList; import java.util.Map; public class Summaries { @@ -32,4 +36,71 @@ public static boolean showEngineFeatureMessage(Map map){ return false; } } + + public static boolean showGameplayFeatureMessage(Map map){ + if(!map.get("BAD").matches(".*\\d.*")){ + ArrayList badGenreNames = Utils.getEntriesFromString(map.get("BAD")); + ArrayList goodGenreNames = Utils.getEntriesFromString(map.get("GOOD")); + ArrayList badGenreIds = new ArrayList<>(); + ArrayList goodGenreIds = new ArrayList<>(); + for(String string : badGenreNames){ + badGenreIds.add(AnalyzeExistingGenres.getGenreIdByName(string)); + } + for(String string : goodGenreNames){ + goodGenreIds.add(AnalyzeExistingGenres.getGenreIdByName(string)); + } + map.remove("BAD"); + map.remove("GOOD"); + map.put("BAD", Utils.transformArrayListToString(badGenreIds)); + map.put("GOOD", Utils.transformArrayListToString(goodGenreIds)); + } + StringBuilder badGenresFeatures = new StringBuilder(); + boolean firstBadFeature = true; + if(map.get("BAD").equals("")){ + badGenresFeatures.append("None"); + }else{ + for(String string : Utils.getEntriesFromString(map.get("BAD"))){ + if(!firstBadFeature){ + badGenresFeatures.append(", "); + }else{ + firstBadFeature = false; + } + badGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(Integer.parseInt(string))); + } + } + StringBuilder goodGenresFeatures = new StringBuilder(); + boolean firstGoodFeature = true; + if(map.get("GOOD").equals("")){ + goodGenresFeatures.append("None"); + }else{ + for(String string : Utils.getEntriesFromString(map.get("GOOD"))){ + if(!firstGoodFeature){ + goodGenresFeatures.append(", "); + }else{ + firstGoodFeature = false; + } + goodGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(Integer.parseInt(string))); + } + } + String messageBody = "Your gameplay feature is ready:\n\n" + + "Name: " + map.get("NAME EN") + "\n" + + "Description: " + map.get("DESC EN") + "\n" + + "Unlock date: " + map.get("DATE") + "\n" + + "Type: " + GameplayFeatureHelper.getGameplayFeatureNameByTypeId(Integer.parseInt(map.get("TYP"))) + "\n" + + "Research point cost: " + map.get("RES POINTS") + "\n" + + "Research cost " + map.get("PRICE") + "\n" + + "Development cost: " + map.get("DEV COSTS") + "\n" + + "\n*Bad genres*\n\n" + badGenresFeatures.toString() + "\n" + + "\n*Good genres*\n\n" + goodGenresFeatures.toString() + "\n" + + "\n*Points*\n\n" + + "Gameplay: " + map.get("GAMEPLAY") + "\n" + + "Graphic: " + map.get("GRAPHIC") + "\n" + + "Sound: " + map.get("SOUND") + "\n" + + "Tech: " + map.get("TECH") + "\n"; + if(JOptionPane.showConfirmDialog(null, messageBody, "Add gameplay feature?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ + return true; + }else{ + return false; + } + } } diff --git a/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/FreeId.java b/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/FreeId.java new file mode 100644 index 00000000..e652c404 --- /dev/null +++ b/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/FreeId.java @@ -0,0 +1,6 @@ +package com.github.lmh01.mgt2mt.util.interfaces; + +@FunctionalInterface +public interface FreeId { + int getFreeId(); +} diff --git a/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/Importer.java b/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/Importer.java new file mode 100644 index 00000000..7b7987ea --- /dev/null +++ b/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/Importer.java @@ -0,0 +1,9 @@ +package com.github.lmh01.mgt2mt.util.interfaces; + +import java.io.IOException; +import java.util.Map; + +@FunctionalInterface +public interface Importer { + void importer(Map map) throws IOException; +} diff --git a/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/Summary.java b/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/Summary.java new file mode 100644 index 00000000..e3877b6e --- /dev/null +++ b/src/main/java/com/github/lmh01/mgt2mt/util/interfaces/Summary.java @@ -0,0 +1,7 @@ +package com.github.lmh01.mgt2mt.util.interfaces; + +import java.util.Map; + +public interface Summary { + boolean showSummary(Map map); +}