-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathMekHQ.java
629 lines (509 loc) · 21.9 KB
/
MekHQ.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
* MekHQ.java
*
* Copyright (c) 2009 - Jay Lawson (jaylawson39 at yahoo.com). All Rights Reserved.
* Copyright (c) 2020-2022 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MekHQ.
*
* MekHQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MekHQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MekHQ. If not, see <http://www.gnu.org/licenses/>.
*/
package mekhq;
import megamek.MegaMek;
import megamek.client.Client;
import megamek.client.generator.RandomNameGenerator;
import megamek.client.generator.RandomUnitGenerator;
import megamek.client.ui.preferences.PreferencesNode;
import megamek.client.ui.preferences.SuitePreferences;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.gameConnectionDialogs.ConnectDialog;
import megamek.client.ui.swing.gameConnectionDialogs.HostDialog;
import megamek.common.event.*;
import megamek.server.GameManager;
import megamek.server.Server;
import megameklab.MegaMekLab;
import mekhq.campaign.Campaign;
import mekhq.campaign.CampaignController;
import mekhq.campaign.ResolveScenarioTracker;
import mekhq.campaign.event.ScenarioResolvedEvent;
import mekhq.campaign.handler.XPHandler;
import mekhq.campaign.mission.AtBContract;
import mekhq.campaign.mission.AtBScenario;
import mekhq.campaign.mission.Scenario;
import mekhq.campaign.storyarc.StoryArc;
import mekhq.campaign.stratcon.StratconRulesManager;
import mekhq.campaign.unit.Unit;
import mekhq.gui.CampaignGUI;
import mekhq.gui.StoryArcEditorGUI;
import mekhq.gui.dialog.ResolveScenarioWizardDialog;
import mekhq.gui.dialog.RetirementDefectionDialog;
import mekhq.gui.panels.StartupScreenPanel;
import mekhq.gui.preferences.StringPreference;
import mekhq.gui.utilities.ObservableString;
import mekhq.service.AutosaveService;
import mekhq.service.IAutosaveService;
import org.apache.logging.log4j.LogManager;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.stream.Stream;
/**
* The main class of the application.
*/
public class MekHQ implements GameListener {
//region Variable Declarations
private static final SuitePreferences mhqPreferences = new SuitePreferences();
private static final MHQOptions mhqOptions = new MHQOptions();
private static final EventBus EVENT_BUS = new EventBus();
private static ObservableString selectedTheme;
// Deprecated directory options
private static ObservableString personnelDirectory;
private static ObservableString partsDirectory;
private static ObservableString planetsDirectory;
private static ObservableString starMapsDirectory;
private static ObservableString unitsDirectory;
private static ObservableString campaignsDirectory;
private static ObservableString scenarioTemplatesDirectory;
private static ObservableString financesDirectory;
// stuff related to MM games
private Server myServer = null;
private GameThread gameThread = null;
private Scenario currentScenario = null;
private Client client = null;
// the actual campaign - this is where the good stuff is
private CampaignController campaignController;
private CampaignGUI campaignGUI;
private StoryArcEditorGUI storyArcEditorGUI;
private IconPackage iconPackage = new IconPackage();
private final IAutosaveService autosaveService;
//endregion Variable Declarations
public static SuitePreferences getMHQPreferences() {
return mhqPreferences;
}
public static MHQOptions getMHQOptions() {
return mhqOptions;
}
public static ObservableString getSelectedTheme() {
return selectedTheme;
}
public static ObservableString getPersonnelDirectory() {
return personnelDirectory;
}
public static ObservableString getPartsDirectory() {
return partsDirectory;
}
public static ObservableString getPlanetsDirectory() {
return planetsDirectory;
}
public static ObservableString getStarMapsDirectory() {
return starMapsDirectory;
}
public static ObservableString getUnitsDirectory() {
return unitsDirectory;
}
public static ObservableString getCampaignsDirectory() {
return campaignsDirectory;
}
public static ObservableString getScenarioTemplatesDirectory() {
return scenarioTemplatesDirectory;
}
public static ObservableString getFinancesDirectory() {
return financesDirectory;
}
protected static MekHQ getInstance() {
return new MekHQ();
}
private MekHQ() {
this.autosaveService = new AutosaveService();
}
/**
* At startup create and show the main frame of the application.
*/
protected void startup() {
// Setup user preferences
MegaMek.getMMPreferences().loadFromFile(MHQConstants.MM_PREFERENCES_FILE);
MegaMekLab.getMMLPreferences().loadFromFile(MHQConstants.MML_PREFERENCES_FILE);
getMHQPreferences().loadFromFile(MHQConstants.MHQ_PREFERENCES_FILE);
setUserPreferences();
initEventHandlers();
// create a start-up frame and display it
new StartupScreenPanel(this).getFrame().setVisible(true);
}
/**
* restart back to the splash screen
*/
public void restart() {
// Actually close MHQ
if (campaignGUI != null) {
campaignGUI.getFrame().dispose();
}
new StartupScreenPanel(this).getFrame().setVisible(true);
}
@Deprecated // These need to be migrated to the Suite Constants / Suite Options Setup
private void setUserPreferences() {
try {
PreferencesNode preferences = MekHQ.getMHQPreferences().forClass(MekHQ.class);
// TODO: complete integration of Suite Preferences, including GUIPreferences
selectedTheme = new ObservableString("selectedTheme", GUIPreferences.UI_THEME);
selectedTheme.addPropertyChangeListener(new MekHqPropertyChangedListener());
preferences.manage(new StringPreference(selectedTheme));
personnelDirectory = new ObservableString("personnelDirectory", ".");
preferences.manage(new StringPreference(personnelDirectory));
partsDirectory = new ObservableString("partsDirectory", ".");
preferences.manage(new StringPreference(partsDirectory));
planetsDirectory = new ObservableString("planetsDirectory", ".");
preferences.manage(new StringPreference(planetsDirectory));
starMapsDirectory = new ObservableString("starMapsDirectory", ".");
preferences.manage(new StringPreference(starMapsDirectory));
unitsDirectory = new ObservableString("unitsDirectory", ".");
preferences.manage(new StringPreference(unitsDirectory));
campaignsDirectory = new ObservableString("campaignsDirectory", "./campaigns");
preferences.manage(new StringPreference(campaignsDirectory));
scenarioTemplatesDirectory = new ObservableString("scenarioTemplatesDirectory", ".");
preferences.manage(new StringPreference(scenarioTemplatesDirectory));
financesDirectory = new ObservableString("financesDirectory", ".");
preferences.manage(new StringPreference(financesDirectory));
} catch (Exception ex) {
LogManager.getLogger().error("Failed to set user preferences", ex);
}
}
public void exit() {
int savePrompt = JOptionPane.showConfirmDialog(null, "Do you want to save the game before quitting MekHQ?",
"Save First?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if ((savePrompt == JOptionPane.CANCEL_OPTION) || (savePrompt == JOptionPane.CLOSED_OPTION)) {
return;
} else if ((savePrompt == JOptionPane.YES_OPTION) && !getCampaigngui().saveCampaign(null)) {
// When the user did not actually save the game, don't close MHQ
return;
}
// Actually close MHQ
if (campaignGUI != null) {
campaignGUI.getFrame().dispose();
}
MegaMek.getMMPreferences().saveToFile(MHQConstants.MM_PREFERENCES_FILE);
MegaMekLab.getMMLPreferences().saveToFile(MHQConstants.MML_PREFERENCES_FILE);
getMHQPreferences().saveToFile(MHQConstants.MHQ_PREFERENCES_FILE);
System.exit(0);
}
public void showNewView() {
campaignGUI = new CampaignGUI(this);
}
public void showNewStoryArcEditor(StoryArc arc) {
storyArcEditorGUI = new StoryArcEditorGUI(this, arc);
}
/**
* Main method launching the application.
*/
public static void main(String... args) {
// First, create a global default exception handler
Thread.setDefaultUncaughtExceptionHandler((thread, t) -> {
LogManager.getLogger().error("Uncaught Exception Detected", t);
final String name = t.getClass().getName();
JOptionPane.showMessageDialog(null,
String.format("Uncaught %s detected. Please open up an issue containing all logs, campaign save file, and customs at https://github.com/MegaMek/mekhq/issues", name),
"Uncaught " + name, JOptionPane.ERROR_MESSAGE);
});
// Second, let's handle logging
MegaMek.initializeLogging(MHQConstants.PROJECT_NAME);
MegaMekLab.initializeLogging(MHQConstants.PROJECT_NAME);
MekHQ.initializeLogging(MHQConstants.PROJECT_NAME);
// Third, let's handle suite graphical setup initialization
MegaMek.initializeSuiteGraphicalSetups(MHQConstants.PROJECT_NAME);
// Finally, let's handle startup
SwingUtilities.invokeLater(() -> MekHQ.getInstance().startup());
}
public static void initializeLogging(final String originProject) {
LogManager.getLogger().info(getUnderlyingInformation(originProject));
}
/**
* @param originProject the project that launched MekHQ
* @return the underlying information for this launch of MekHQ
*/
public static String getUnderlyingInformation(final String originProject) {
return MegaMek.getUnderlyingInformation(originProject, MHQConstants.PROJECT_NAME);
}
public Server getMyServer() {
return myServer;
}
public Campaign getCampaign() {
return campaignController.getLocalCampaign();
}
public void setCampaign(Campaign c) {
campaignController = new CampaignController(c);
}
public CampaignController getCampaignController() {
return campaignController;
}
/**
* @return the campaigngui
*/
public CampaignGUI getCampaigngui() {
return campaignGUI;
}
/**
* @param campaigngui the campaigngui to set
*/
public void setCampaigngui(CampaignGUI campaigngui) {
this.campaignGUI = campaigngui;
}
public void joinGame(Scenario scenario, List<Unit> meks) {
ConnectDialog joinGameDialog = new ConnectDialog(campaignGUI.getFrame(), campaignGUI.getCampaign().getName());
joinGameDialog.setVisible(true);
if (!joinGameDialog.dataValidation("MegaMek.ConnectDialog.title")) {
return;
}
final String playerName = joinGameDialog.getPlayerName();
final String serverAddress = joinGameDialog.getServerAddress();
final int port = joinGameDialog.getPort();
joinGameDialog.dispose();
try {
client = new Client(playerName, serverAddress, port);
} catch (Exception e) {
LogManager.getLogger().error("Failed to connect to server properly", e);
return;
}
client.getGame().addGameListener(this);
currentScenario = scenario;
// Start the game thread
gameThread = new GameThread(playerName, client, this, meks, scenario, false);
gameThread.start();
}
public void startHost(Scenario scenario, boolean loadSavegame, List<Unit> meks) {
HostDialog hostDialog = new HostDialog(campaignGUI.getFrame(), getCampaign().getName());
hostDialog.setVisible(true);
if (!hostDialog.dataValidation("MegaMek.HostGameAlert.title")) {
stopHost();
return;
}
this.autosaveService.requestBeforeMissionAutosave(getCampaign());
final String playerName = hostDialog.getPlayerName();
final String password = hostDialog.getServerPass();
final int port = hostDialog.getPort();
final boolean register = hostDialog.isRegister();
final String metaserver = register ? hostDialog.getMetaserver() : "";
// Force cleanup of the current modal, since we are (possibly) about to display
// a new one and MacOS
// seems to struggle with that (see https://github.com/MegaMek/mekhq/issues/953)
hostDialog.dispose();
try {
myServer = new Server(password, port, new GameManager(), register, metaserver);
if (loadSavegame) {
FileDialog f = new FileDialog(campaignGUI.getFrame(), "Load Savegame");
f.setDirectory(System.getProperty("user.dir") + "/savegames");
f.setVisible(true);
if (null != f.getFile()) {
getMyServer().loadGame(new File(f.getDirectory(), f.getFile()));
} else {
stopHost();
return; // exceptions as flow control? no thanks.
}
}
} catch (FileNotFoundException ex) {
// The dialog was cancelled or the file not found
// Return to the UI
stopHost();
return;
} catch (Exception ex) {
LogManager.getLogger().error("Failed to start up server", ex);
stopHost();
return;
}
client = new Client(playerName, "127.0.0.1", port);
client.getGame().addGameListener(this);
currentScenario = scenario;
// Start the game thread
if (getCampaign().getCampaignOptions().isUseAtB() && (scenario instanceof AtBScenario)) {
gameThread = new AtBGameThread(playerName, password, client, this, meks, (AtBScenario) scenario);
} else {
gameThread = new GameThread(playerName, password, client, this, meks, scenario);
}
gameThread.start();
}
// Stop & send the close game event to the Server
public synchronized void stopHost() {
if (getMyServer() != null) {
getMyServer().die();
myServer = null;
}
currentScenario = null;
}
@Override
public void gameBoardChanged(GameBoardChangeEvent e) {
}
@Override
public void gameBoardNew(GameBoardNewEvent e) {
}
@Override
public void gameEnd(GameEndEvent e) {
}
@Override
public void gameEntityChange(GameEntityChangeEvent e) {
}
@Override
public void gameEntityNew(GameEntityNewEvent e) {
}
@Override
public void gameEntityNewOffboard(GameEntityNewOffboardEvent e) {
}
@Override
public void gameEntityRemove(GameEntityRemoveEvent e) {
}
@Override
public void gameMapQuery(GameMapQueryEvent e) {
}
@Override
public void gameNewAction(GameNewActionEvent e) {
}
@Override
public void gamePhaseChange(GamePhaseChangeEvent e) {
}
@Override
public void gameVictory(GameVictoryEvent gve) {
// Prevent double run
if (gameThread.stopRequested()) {
return;
}
try {
boolean control = JOptionPane.showConfirmDialog(campaignGUI.getFrame(),
"Did your side control the battlefield at the end of the scenario?", "Control of Battlefield?",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
ResolveScenarioTracker tracker = new ResolveScenarioTracker(currentScenario, getCampaign(), control);
tracker.setClient(gameThread.getClient());
tracker.setEvent(gve);
tracker.processGame();
ResolveScenarioWizardDialog resolveDialog = new ResolveScenarioWizardDialog(campaignGUI.getFrame(), true,
tracker);
resolveDialog.setVisible(true);
if (campaignGUI.getCampaign().getCampaignOptions().isUseAtB()
&& (campaignGUI.getCampaign().getMission(currentScenario.getMissionId()) instanceof AtBContract)
&& !campaignGUI.getCampaign().getRetirementDefectionTracker().getRetirees().isEmpty()) {
RetirementDefectionDialog rdd = new RetirementDefectionDialog(campaignGUI,
(AtBContract) campaignGUI.getCampaign().getMission(currentScenario.getMissionId()), false);
rdd.setVisible(true);
if (!rdd.wasAborted()) {
getCampaign().applyRetirement(rdd.totalPayout(), rdd.getUnitAssignments());
}
}
// we need to trigger ScenarioResolvedEvent before stopping the thread or currentScenario may become null
MekHQ.triggerEvent(new ScenarioResolvedEvent(currentScenario));
gameThread.requestStop();
// MegaMek dumps these in the deployment phase to free memory
if (getCampaign().getCampaignOptions().isUseAtB()) {
RandomUnitGenerator.getInstance();
RandomNameGenerator.getInstance();
}
// MegaMek creates some temporary files that MHQ needs to remove between runs
final File tempImageDirectory = new File("data/images/temp");
if (tempImageDirectory.isDirectory()) {
// This can't be null because of the above
Stream.of(tempImageDirectory.listFiles()).filter(file -> file.getName().endsWith(".png")).forEach(File::delete);
}
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
}
}
@Override
public void gamePlayerChange(GamePlayerChangeEvent e) {
}
@Override
public void gamePlayerChat(GamePlayerChatEvent e) {
}
@Override
public void gamePlayerConnected(GamePlayerConnectedEvent e) {
}
@Override
public void gamePlayerDisconnected(GamePlayerDisconnectedEvent e) {
}
@Override
public void gameReport(GameReportEvent e) {
}
@Override
public void gameSettingsChange(GameSettingsChangeEvent e) {
}
@Override
public void gameTurnChange(GameTurnChangeEvent e) {
}
@Override
public void gameClientFeedbackRequest(GameCFREvent e) {
}
public IconPackage getIconPackage() {
return iconPackage;
}
/*
* Access methods for event bus.
*/
static public void registerHandler(Object handler) {
EVENT_BUS.register(handler);
}
static public boolean triggerEvent(MMEvent event) {
return EVENT_BUS.trigger(event);
}
static public void unregisterHandler(Object handler) {
EVENT_BUS.unregister(handler);
}
/**
* TODO : This needs to be way more flexible, but it will do for now.
*/
private void initEventHandlers() {
EVENT_BUS.register(new XPHandler());
StratconRulesManager srm = new StratconRulesManager();
srm.startup();
EVENT_BUS.register(srm);
}
private static void setLookAndFeel(String themeName) {
Runnable runnable = () -> {
try {
UIManager.setLookAndFeel(themeName);
if (System.getProperty("os.name", "").startsWith("Mac OS X")) {
// Ensure OSX key bindings are used for copy, paste etc
addOSXKeyStrokes((InputMap) UIManager.get("EditorPane.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("FormattedTextField.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("TextField.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("TextPane.focusInputMap"));
addOSXKeyStrokes((InputMap) UIManager.get("TextArea.focusInputMap"));
}
for (final Frame frame : Frame.getFrames()) {
SwingUtilities.updateComponentTreeUI(frame);
}
for (Window window : Window.getWindows()) {
SwingUtilities.updateComponentTreeUI(window);
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
LogManager.getLogger().error("", e);
}
};
SwingUtilities.invokeLater(runnable);
}
private static class MekHqPropertyChangedListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource().equals(selectedTheme)) {
setLookAndFeel((String) evt.getNewValue());
}
}
}
private static void addOSXKeyStrokes(InputMap inputMap) {
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_DOWN_MASK), DefaultEditorKit.copyAction);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.META_DOWN_MASK), DefaultEditorKit.cutAction);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.META_DOWN_MASK), DefaultEditorKit.pasteAction);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.META_DOWN_MASK), DefaultEditorKit.selectAllAction);
}
}