Skip to content

Commit

Permalink
Fix unable to exit in beryl console. Fix deleting old version.txt
Browse files Browse the repository at this point in the history
r4979
2019-08-27

## DISCUSSION
1. #208
2. #216

## NEW
1. Add "0. Exit" in the beryl console for exiting mars-sim.

## CHANGE
1. Modify selectMode() in InteractiveTerm to enable exiting
   mars-sim.
2. Modify the constructor in SimulationConfigEditor to enable
   exiting mars-sim.

## FIX
1. Correct not being able to delete old version.txt in loadConfig.
   - Call versionFile.delete() prior to deleting the xml folder.
2. Fix unable to exit in beryl console.
2. Fix the ability to exit mars-sim by clicking the top right
   corner "X" button in SimulationConfigEditor.
  • Loading branch information
mokun committed Aug 27, 2019
1 parent 5dc4173 commit 4f6ae63
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,16 @@ public static void loadConfig() {
// if the "xml" directory exists, back up everything inside and clean the directory
if (existed && xmlLocation.isDirectory()) {
LogConsolidated.log(Level.CONFIG, 0, sourceName,
"'xml' folder already existed. Backing it up into 'backup' folder & Creating a new 'xml' folder with new xml files.");
"'xml' folder already existed.");

if (versionFile.exists()) {
BufferedReader brTest;
try {
brTest = new BufferedReader(new FileReader(versionFile));
String buildText = brTest.readLine();
if (buildText.equals(Simulation.BUILD)) {
LogConsolidated.log(Level.CONFIG, 0, sourceName,
"The version.txt shows the existing xml file set already has the most current BUILD tag.");
sameBuild = true;
}
} catch (FileNotFoundException e) {
Expand All @@ -301,7 +303,16 @@ public static void loadConfig() {
// may use FileUtils.checksum(file, checksum)

try {
LogConsolidated.log(Level.CONFIG, 0, sourceName,
"Backing up the existing xml files into the 'backup' folder. Deleting the xml folder.");

// copy everything in the xml folder
FileUtils.copyDirectoryToDirectory(xmlLocation, backupLocation);

// delete the version.txt file
versionFile.delete();

// delete everything in the xml folder
FileUtils.deleteDirectory(xmlLocation);

} catch (IOException e) {
Expand All @@ -314,21 +325,21 @@ public static void loadConfig() {
existed = xmlLocation.exists();

if (!sameBuild) {

// the "xml" folder does NOT exist

// Create the xml folder
System.out.println("Can it write ? " + xmlLocation.canWrite());
LogConsolidated.log(Level.CONFIG, 0, sourceName,
"'xml' folder does not exist. Creating it.");
// System.out.println("path is " + Files.createDirectories(path));
if (!versionFile.getParentFile().exists()) {
if (!xmlLocation.exists()) {
LogConsolidated.log(Level.CONFIG, 0, sourceName,
"'xml' folder does not exist. Creating it.");
// Create the xml folder
versionFile.getParentFile().mkdirs();
List<String> lines = Arrays.asList(Simulation.BUILD);
try {
Files.write(versionPath, lines, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
}

List<String> lines = Arrays.asList(Simulation.BUILD);
try {
// Create the version.txt file
Files.write(versionPath, lines, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,28 @@ public void startModeSelection() {


public void selectMode() {
terminal.print("1. Command Mode "
terminal.print(
"0. Exit "
+ System.lineSeparator()
+ "1. Command Mode "
+ System.lineSeparator()
+ "2. Sandbox Mode "
+ System.lineSeparator()
+ System.lineSeparator()
);

handler.addStringTask("input", "Select the Game Mode:", false).addChoices("1", "2").constrainInputToChoices();
handler.addStringTask("input", "Select the Game Mode:", false).addChoices("0", "1", "2").constrainInputToChoices();
handler.executeOneTask();

if (GameManager.input.equals("1")) {
if (GameManager.input.equals("0")) {
Simulation sim = Simulation.instance();
sim.endSimulation();
sim.getSimExecutor().shutdownNow();
sim.getMasterClock().exitProgram();
logger.info("Exiting the Simulation.");
System.exit(0);
}
else if (GameManager.input.equals("1")) {

// Set the Game Mode to Command Mode in GameManager
GameManager.mode = GameMode.COMMAND;
Expand Down
2 changes: 1 addition & 1 deletion mars-sim-core/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ Simulation.log.saveError = Error in saving the si
Simulation.log.saveSimTo = Saving simulation to
Simulation.thread.masterClock = Master Clock
Simulation.version = 3.1.0-b2
Simulation.build = 4978
Simulation.build = 4979

SimulationConfigEditor.button.add = Add
SimulationConfigEditor.button.crewEditor = Edit Crew
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public SimulationConfigEditor(SimulationConfig config, MainWindow mainWindow) {

f = new JFrame();

f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
System.exit(0);
destroy();
}
});

f.setSize(HORIZONTAL_SIZE, 360);
f.setTitle(Msg.getString("SimulationConfigEditor.title")); //$NON-NLS-1$

Expand Down

0 comments on commit 4f6ae63

Please sign in to comment.