Skip to content

Commit 4f6ae63

Browse files
committed
Fix unable to exit in beryl console. Fix deleting old version.txt
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.
1 parent 5dc4173 commit 4f6ae63

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

mars-sim-core/src/main/java/org/mars_sim/msp/core/SimulationConfig.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,16 @@ public static void loadConfig() {
274274
// if the "xml" directory exists, back up everything inside and clean the directory
275275
if (existed && xmlLocation.isDirectory()) {
276276
LogConsolidated.log(Level.CONFIG, 0, sourceName,
277-
"'xml' folder already existed. Backing it up into 'backup' folder & Creating a new 'xml' folder with new xml files.");
277+
"'xml' folder already existed.");
278278

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

303305
try {
306+
LogConsolidated.log(Level.CONFIG, 0, sourceName,
307+
"Backing up the existing xml files into the 'backup' folder. Deleting the xml folder.");
308+
309+
// copy everything in the xml folder
304310
FileUtils.copyDirectoryToDirectory(xmlLocation, backupLocation);
311+
312+
// delete the version.txt file
313+
versionFile.delete();
314+
315+
// delete everything in the xml folder
305316
FileUtils.deleteDirectory(xmlLocation);
306317

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

316327
if (!sameBuild) {
328+
317329
// the "xml" folder does NOT exist
318-
319-
// Create the xml folder
320-
System.out.println("Can it write ? " + xmlLocation.canWrite());
321-
LogConsolidated.log(Level.CONFIG, 0, sourceName,
322-
"'xml' folder does not exist. Creating it.");
323-
// System.out.println("path is " + Files.createDirectories(path));
324-
if (!versionFile.getParentFile().exists()) {
330+
if (!xmlLocation.exists()) {
331+
LogConsolidated.log(Level.CONFIG, 0, sourceName,
332+
"'xml' folder does not exist. Creating it.");
333+
// Create the xml folder
325334
versionFile.getParentFile().mkdirs();
326-
List<String> lines = Arrays.asList(Simulation.BUILD);
327-
try {
328-
Files.write(versionPath, lines, StandardCharsets.UTF_8);
329-
} catch (IOException e) {
330-
e.printStackTrace();
331-
}
335+
}
336+
337+
List<String> lines = Arrays.asList(Simulation.BUILD);
338+
try {
339+
// Create the version.txt file
340+
Files.write(versionPath, lines, StandardCharsets.UTF_8);
341+
} catch (IOException e) {
342+
e.printStackTrace();
332343
}
333344
}
334345

mars-sim-core/src/main/java/org/mars_sim/msp/core/terminal/InteractiveTerm.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,28 @@ public void startModeSelection() {
102102

103103

104104
public void selectMode() {
105-
terminal.print("1. Command Mode "
105+
terminal.print(
106+
"0. Exit "
107+
+ System.lineSeparator()
108+
+ "1. Command Mode "
106109
+ System.lineSeparator()
107110
+ "2. Sandbox Mode "
108111
+ System.lineSeparator()
109112
+ System.lineSeparator()
110113
);
111114

112-
handler.addStringTask("input", "Select the Game Mode:", false).addChoices("1", "2").constrainInputToChoices();
115+
handler.addStringTask("input", "Select the Game Mode:", false).addChoices("0", "1", "2").constrainInputToChoices();
113116
handler.executeOneTask();
114117

115-
if (GameManager.input.equals("1")) {
118+
if (GameManager.input.equals("0")) {
119+
Simulation sim = Simulation.instance();
120+
sim.endSimulation();
121+
sim.getSimExecutor().shutdownNow();
122+
sim.getMasterClock().exitProgram();
123+
logger.info("Exiting the Simulation.");
124+
System.exit(0);
125+
}
126+
else if (GameManager.input.equals("1")) {
116127

117128
// Set the Game Mode to Command Mode in GameManager
118129
GameManager.mode = GameMode.COMMAND;

mars-sim-core/src/main/resources/messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ Simulation.log.saveError = Error in saving the si
854854
Simulation.log.saveSimTo = Saving simulation to
855855
Simulation.thread.masterClock = Master Clock
856856
Simulation.version = 3.1.0-b2
857-
Simulation.build = 4978
857+
Simulation.build = 4979
858858

859859
SimulationConfigEditor.button.add = Add
860860
SimulationConfigEditor.button.crewEditor = Edit Crew

mars-sim-ui/src/main/java/org/mars_sim/msp/ui/swing/configeditor/SimulationConfigEditor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ public SimulationConfigEditor(SimulationConfig config, MainWindow mainWindow) {
125125

126126
f = new JFrame();
127127

128+
f.addWindowListener(new WindowAdapter() {
129+
@Override
130+
public void windowClosing(WindowEvent event) {
131+
System.exit(0);
132+
destroy();
133+
}
134+
});
135+
128136
f.setSize(HORIZONTAL_SIZE, 360);
129137
f.setTitle(Msg.getString("SimulationConfigEditor.title")); //$NON-NLS-1$
130138

0 commit comments

Comments
 (0)