Skip to content

Commit

Permalink
#21 It is now possible to import engine features. New class SharingMa…
Browse files Browse the repository at this point in the history
…nager this class currently contains functions to make it simpler to import things. Will soon contain functions to make it simpler to export things.
  • Loading branch information
LMH01 committed Mar 16, 2021
1 parent 10fd27b commit a0c342a
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 146 deletions.
114 changes: 41 additions & 73 deletions src/main/java/com/github/lmh01/mgt2mt/data_stream/SharingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.windows.WindowMain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -16,10 +17,8 @@

@SuppressWarnings("JavaDoc")
public class SharingHandler {
//Contains functions with which exports are written to file and imports are read and imported
private static final Logger LOGGER = LoggerFactory.getLogger(SharingHandler.class);
public static final String[] GENRE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.7.0", "1.7.1", "1.8.0"};
public static final String[] PUBLISHER_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.6.0", "1.7.0", "1.7.1", "1.8.0"};
public static final String[] THEME_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.7.1", "1.8.0"};//TODO Vor release, wenn tool version geändert 1.7.1 raus nehmen

/**
* Opens a gui where the user can select what should be exported. The selected entries are then exported using the exporter
Expand Down Expand Up @@ -188,7 +187,7 @@ public static String importGenre(String importFolderPath) throws IOException, Nu
}
}
boolean genreCanBeImported = false;
for(String string : GENRE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){
for(String string : SharingManager.GENRE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){
if(string.equals(map.get("MGT2MT VERSION"))){
genreCanBeImported = true;
}
Expand Down Expand Up @@ -291,7 +290,7 @@ public static String importPublisher(String importFolderPath) throws IOException
}
}
boolean publisherCanBeImported = false;
for(String string : PUBLISHER_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){
for(String string : SharingManager.PUBLISHER_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){
if(string.equals(map.get("MGT2MT VERSION"))){
publisherCanBeImported = true;
}
Expand Down Expand Up @@ -379,7 +378,7 @@ public static String importTheme(String importFolderPath) throws IOException{
}
}
boolean themeCanBeImported = false;
for(String string : THEME_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){
for(String string : SharingManager.THEME_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){
if(string.equals(map.get("MGT2MT VERSION"))){
themeCanBeImported = true;
}
Expand Down Expand Up @@ -446,6 +445,38 @@ public static boolean exportEngineFeature(String engineFeatureName){
return false;
}

public static String importEngineFeature(String importFolderPath) throws IOException{
AnalyzeExistingEngineFeatures.analyzeEngineFeatures();
File fileToImport = new File(importFolderPath + "\\engineFeature.txt");
Map<String, String> 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<String, String> existingEngineFeatures : AnalyzeExistingEngineFeatures.engineFeatures){
for(Map.Entry<String, String> 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";
}

/**
* Opens a GUI where the user can select what gameplay features should be exported. Exports these gameplay features.
*/
Expand Down Expand Up @@ -487,6 +518,10 @@ public static boolean exportGameplayFeature(String gameplayFeatureName){
return false;
}

public static String importGameplayFeature(String importFolderPath) throws IOException{
return null;
}

/**
* @param genreId The genre id from which the genre comb names should be transformed
* @return Returns a list of genre names
Expand Down Expand Up @@ -547,71 +582,4 @@ private static String getGenreNames(String genreNumbersRaw){
String.valueOf(genreNumbersRaw.charAt(1));
return genreNames.toString();
}

/**
* 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
* @return Returns the selected folder, ready for import
*/
public static ArrayList<String> getImportFolderPath(String fileName){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //set Look and Feel to Windows
JFileChooser fileChooser = new JFileChooser(); //Create a new GUI that will use the current(windows) Look and Feel
fileChooser.setDialogTitle("Choose the folder(s) where the " + fileName + " file is located.");
fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(true);
int return_value = fileChooser.showOpenDialog(null);
if(return_value == JFileChooser.APPROVE_OPTION){
File[] files = fileChooser.getSelectedFiles();
ArrayList<String> importFolders = new ArrayList<>();
for(int i=0; i<fileChooser.getSelectedFiles().length; i++){
String importFolder = files[i].getPath();
if(Utils.doesFolderContainFile(importFolder, fileName)){
File fileGenreToImport = new File(importFolder + "//" + fileName);
BufferedReader br = new BufferedReader(new FileReader(fileGenreToImport));
String currentLine = br.readLine();
br.close();
if(currentLine.contains("[MGT2MT VERSION]")){
LOGGER.info("File seams to be valid.");
importFolders.add(importFolder);
}else{
JOptionPane.showMessageDialog(null, "The selected folder does not contain a valid " + fileName + " file.\nPlease select the correct folder.");
}
}else{
JOptionPane.showMessageDialog(null, "The selected folder does not contain the " + fileName + " file.\nPlease select the correct folder.");
}
}
return importFolders;
}
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException | IOException e) {
e.printStackTrace();
}
return null;
}

/**
* Use this function to evaluate the return value from the respective import function.
* @param importName The name that is written is some JOptionPanes. Eg. genre, publisher, theme
* @param returnValue The return value from the import function
* @param compatibleModToolVersions A array containing the compatible mod tool versions for the import file
*/
public static void analyzeReturnValue(String importName, String returnValue, String[] compatibleModToolVersions){
try{
if(returnValue.equals("false")){
JOptionPane.showMessageDialog(null, "The selected " + importName + " already exists.", "Action unavailable", JOptionPane.ERROR_MESSAGE);
}else{
if(!returnValue.equals("true")){
StringBuilder supportedModToolVersions = new StringBuilder();
for(String string : compatibleModToolVersions){
supportedModToolVersions.append("[").append(string).append("]");
}
JOptionPane.showMessageDialog(null, returnValue + "\nSupported versions: " + supportedModToolVersions.toString(), "Action unavailable", JOptionPane.ERROR_MESSAGE);
}
}
}catch(NullPointerException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Unable to import " + importName + ":\nThe file is corrupted or not compatible with the current Mod Manager Version", "Action unavailable", JOptionPane.ERROR_MESSAGE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,8 @@ public static void addEngineFeature(){
newEngineFeature.put("GRAPHIC", spinnerGraphic.getValue().toString());
newEngineFeature.put("SOUND", spinnerSound.getValue().toString());
newEngineFeature.put("TECH", spinnerTech.getValue().toString());
for(Map.Entry<String, String> entry : newEngineFeature.entrySet()){
LOGGER.info("Key: " + entry.getKey() + " | " + entry.getValue());
}
String messageBody = "Your gameplay feature is ready:\n\n" +
"Name: " + newEngineFeature.get("NAME EN") + "\n" +
"Description: " + newEngineFeature.get("DESC EN") + "\n" +
"Unlock date: " + newEngineFeature.get("DATE") + "\n" +
"Type: " + getEngineFeatureNameByTypeId(Integer.parseInt(newEngineFeature.get("TYP"))) + "\n" +
"Research point cost: " + newEngineFeature.get("RES POINTS") + "\n" +
"Research cost " + newEngineFeature.get("PRICE") + "\n" +
"Development cost: " + newEngineFeature.get("DEV COSTS") + "\n" +
"Tech level: " + newEngineFeature.get("TECHLEVEL") + "\n" +
"\n*Points*\n\n" +
"Gameplay: " + newEngineFeature.get("GAMEPLAY") + "\n" +
"Graphic: " + newEngineFeature.get("GRAPHIC") + "\n" +
"Sound: " + newEngineFeature.get("SOUND") + "\n" +
"Tech: " + newEngineFeature.get("TECH") + "\n";
if(JOptionPane.showConfirmDialog(null, messageBody, "Add gameplay feature?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
boolean addFeature = Summaries.showEngineFeatureMessage(newEngineFeature);
if(addFeature){
EditEngineFeaturesFile.addEngineFeature(newEngineFeature);
JOptionPane.showMessageDialog(null, "Engine feature: [" + newEngineFeature.get("NAME EN") + "] has been added successfully!", "Engine feature added", JOptionPane.INFORMATION_MESSAGE);
ChangeLog.addLogEntry(27, newEngineFeature.get("NAME EN"));
Expand Down
113 changes: 113 additions & 0 deletions src/main/java/com/github/lmh01/mgt2mt/util/SharingManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.github.lmh01.mgt2mt.util;

import com.github.lmh01.mgt2mt.data_stream.SharingHandler;
import com.github.lmh01.mgt2mt.util.interfaces.ReturnValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class SharingManager {
//This class contains functions with which it is easy to export/import things
private static final Logger LOGGER = LoggerFactory.getLogger(SharingManager.class);
public static final String[] GENRE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.7.0", "1.7.1", "1.8.0"};
public static final String[] PUBLISHER_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.6.0", "1.7.0", "1.7.1", "1.8.0"};
public static final String[] THEME_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.7.1", "1.8.0"};//TODO Vor release, wenn tool version geändert 1.7.1 raus nehmen
public static final String[] ENGINE_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.7.1", "1.8.0"};//TODO Vor release, wenn tool version geändert 1.7.1 raus nehmen
public static final String[] GAMEPLAY_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS = {"1.7.1", "1.8.0"};//TODO Vor release, wenn tool version geändert 1.7.1 raus nehmen

/**
*
* @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 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){
try {
ArrayList<String> importFolders = getImportFolderPath(fileName);
try{
for(String importFolder : importFolders){
analyzeReturnValue(importName, exportFunction.getReturnValue(importFolder), compatibleModToolVersions);
}
}catch(NullPointerException ignored){

}
}catch(IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Unable to import " + importName + ":\nThe file is corrupted or not compatible with the current Mod Manager Version", "Action unavailable", JOptionPane.ERROR_MESSAGE);
}
}

/**
* 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
* @return Returns the selected folder, ready for import
*/
private static ArrayList<String> getImportFolderPath(String fileName){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //set Look and Feel to Windows
JFileChooser fileChooser = new JFileChooser(); //Create a new GUI that will use the current(windows) Look and Feel
fileChooser.setDialogTitle("Choose the folder(s) where the " + fileName + " file is located.");
fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(true);
int return_value = fileChooser.showOpenDialog(null);
if(return_value == JFileChooser.APPROVE_OPTION){
File[] files = fileChooser.getSelectedFiles();
ArrayList<String> importFolders = new ArrayList<>();
for(int i=0; i<fileChooser.getSelectedFiles().length; i++){
String importFolder = files[i].getPath();
if(Utils.doesFolderContainFile(importFolder, fileName)){
File fileGenreToImport = new File(importFolder + "//" + fileName);
BufferedReader br = new BufferedReader(new FileReader(fileGenreToImport));
String currentLine = br.readLine();
br.close();
if(currentLine.contains("[MGT2MT VERSION]")){
LOGGER.info("File seams to be valid.");
importFolders.add(importFolder);
}else{
JOptionPane.showMessageDialog(null, "The selected folder does not contain a valid " + fileName + " file.\nPlease select the correct folder.");
}
}else{
JOptionPane.showMessageDialog(null, "The selected folder does not contain the " + fileName + " file.\nPlease select the correct folder.");
}
}
return importFolders;
}
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException | IOException e) {
e.printStackTrace();
}
return null;
}

/**
* Use this function to evaluate the return value from the respective import function.
* @param importName The name that is written is some JOptionPanes. Eg. genre, publisher, theme
* @param returnValue The return value from the import function
* @param compatibleModToolVersions A array containing the compatible mod tool versions for the import file
*/
private static void analyzeReturnValue(String importName, String returnValue, String[] compatibleModToolVersions){
try{
if(returnValue.equals("false")){
JOptionPane.showMessageDialog(null, "The selected " + importName + " already exists.", "Action unavailable", JOptionPane.ERROR_MESSAGE);
}else{
if(!returnValue.equals("true")){
StringBuilder supportedModToolVersions = new StringBuilder();
for(String string : compatibleModToolVersions){
supportedModToolVersions.append("[").append(string).append("]");
}
JOptionPane.showMessageDialog(null, returnValue + "\nSupported versions: " + supportedModToolVersions.toString(), "Action unavailable", JOptionPane.ERROR_MESSAGE);
}
}
}catch(NullPointerException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Unable to import " + importName + ":\nThe file is corrupted or not compatible with the current Mod Manager Version", "Action unavailable", JOptionPane.ERROR_MESSAGE);
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/github/lmh01/mgt2mt/util/Summaries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.lmh01.mgt2mt.util;

import javax.swing.*;
import java.util.Map;

public class Summaries {
//Contains functions that show messages to the user, asking to confirm the import/addition of feature

/**
* Opens a message to the user asking to confirm the addition of engine feature
* @param map The map containing the values
* @return Returns true if user clicked ok. Returns false if user clicked something else
*/
public static boolean showEngineFeatureMessage(Map<String, String> map){
String messageBody = "Your engine feature is ready:\n\n" +
"Name: " + map.get("NAME EN") + "\n" +
"Description: " + map.get("DESC EN") + "\n" +
"Unlock date: " + map.get("DATE") + "\n" +
"Type: " + EngineFeatureHelper.getEngineFeatureNameByTypeId(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" +
"Tech level: " + map.get("TECHLEVEL") + "\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;
}
}
}
Loading

0 comments on commit a0c342a

Please sign in to comment.