forked from MovingBlocks/Terasology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerasologyEngine.java
683 lines (589 loc) · 28.3 KB
/
TerasologyEngine.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.core;
import com.badlogic.gdx.physics.bullet.Bullet;
import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Queues;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.config.Config;
import org.terasology.engine.context.Context;
import org.terasology.engine.context.internal.ContextImpl;
import org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler;
import org.terasology.engine.core.modes.GameState;
import org.terasology.engine.core.module.ExternalApiWhitelist;
import org.terasology.engine.core.module.ModuleManager;
import org.terasology.engine.core.subsystem.DisplayDevice;
import org.terasology.engine.core.subsystem.EngineSubsystem;
import org.terasology.engine.core.subsystem.RenderingSubsystemFactory;
import org.terasology.engine.core.subsystem.common.CommandSubsystem;
import org.terasology.engine.core.subsystem.common.ConfigurationSubsystem;
import org.terasology.engine.core.subsystem.common.GameSubsystem;
import org.terasology.engine.core.subsystem.common.MonitoringSubsystem;
import org.terasology.engine.core.subsystem.common.NetworkSubsystem;
import org.terasology.engine.core.subsystem.common.PhysicsSubsystem;
import org.terasology.engine.core.subsystem.common.TelemetrySubSystem;
import org.terasology.engine.core.subsystem.common.TimeSubsystem;
import org.terasology.engine.core.subsystem.common.WorldGenerationSubsystem;
import org.terasology.engine.entitySystem.prefab.Prefab;
import org.terasology.engine.entitySystem.prefab.internal.PojoPrefab;
import org.terasology.engine.i18n.I18nSubsystem;
import org.terasology.engine.input.InputSystem;
import org.terasology.engine.logic.behavior.asset.BehaviorTree;
import org.terasology.engine.monitoring.Activity;
import org.terasology.engine.monitoring.PerformanceMonitor;
import org.terasology.engine.network.NetworkSystem;
import org.terasology.engine.persistence.typeHandling.TypeHandlerLibraryImpl;
import org.terasology.engine.recording.CharacterStateEventPositionMap;
import org.terasology.engine.recording.DirectionAndOriginPosRecorderList;
import org.terasology.engine.recording.RecordAndReplayCurrentStatus;
import org.terasology.engine.recording.RecordAndReplayUtils;
import org.terasology.engine.registry.CoreRegistry;
import org.terasology.engine.rendering.gltf.ByteBufferAsset;
import org.terasology.engine.version.TerasologyVersion;
import org.terasology.engine.world.block.loader.BlockFamilyDefinition;
import org.terasology.engine.world.block.loader.BlockFamilyDefinitionData;
import org.terasology.engine.world.block.loader.BlockFamilyDefinitionFormat;
import org.terasology.engine.world.block.shapes.BlockShape;
import org.terasology.engine.world.block.shapes.BlockShapeImpl;
import org.terasology.engine.world.block.sounds.BlockSounds;
import org.terasology.engine.world.block.tiles.BlockTile;
import org.terasology.gestalt.assets.AssetType;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.gestalt.assets.module.ModuleAwareAssetTypeManager;
import org.terasology.gestalt.assets.module.autoreload.AutoReloadAssetTypeManager;
import org.terasology.nui.UIWidget;
import org.terasology.nui.asset.UIElement;
import org.terasology.nui.skin.UISkinAsset;
import org.terasology.persistence.typeHandling.TypeHandler;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.reflection.ModuleTypeRegistry;
import org.terasology.reflection.TypeRegistry;
import org.terasology.reflection.copy.CopyStrategyLibrary;
import org.terasology.reflection.reflect.ReflectFactory;
import org.terasology.reflection.reflect.ReflectionReflectFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
/**
* <p>
* This GameEngine implementation is the heart of Terasology.
* </p>
* <p>
* It first takes care of making a number of application-wide initializations (see init() method). It then provides a
* main game loop (see run() method) characterized by a number of mutually exclusive {@link GameState}s. The current
* GameState is updated each frame, and a change of state (see changeState() method) can be requested at any time - the
* switch will occur cleanly between frames. Interested parties can be notified of GameState changes by using the
* subscribeToStateChange() method.
* </p>
* <p>
* At this stage the engine also provides a number of utility methods (see submitTask() and hasMouseFocus() to name a
* few) but they might be moved elsewhere.
* </p>
* <p>
* Special mention must be made in regard to EngineSubsystems. An {@link EngineSubsystem} is a pluggable low-level
* component of the engine, that is processed every frame - like rendering or audio. A list of EngineSubsystems is
* provided in input to the engine's constructor. Different sets of Subsystems can significantly change the behaviour of
* the engine, i.e. providing a "no-frills" server in one case or a full-graphics client in another.
* </p>
*/
public class TerasologyEngine implements GameEngine {
private static final Logger logger = LoggerFactory.getLogger(TerasologyEngine.class);
private static final int ONE_MEBIBYTE = 1024 * 1024;
/**
* Subsystem classes that automatically make their classpath part of the engine module.
* <p>
* You don't want to add to this! If you need a module, make a module!
*/
private static final Set<String> LEGACY_ENGINE_MODULE_POLLUTERS = Set.of(
"org.terasology.subsystem.discordrpc.DiscordRPCSubSystem"
);
private final List<Class<?>> classesOnClasspathsToAddToEngine = new ArrayList<>();
private GameState currentState;
private GameState pendingState;
private Set<StateChangeSubscriber> stateChangeSubscribers = Sets.newLinkedHashSet();
private EngineStatus status = StandardGameStatus.UNSTARTED;
private final List<EngineStatusSubscriber> statusSubscriberList = new CopyOnWriteArrayList<>();
private volatile boolean shutdownRequested;
private volatile boolean running;
private TimeSubsystem timeSubsystem;
private Deque<EngineSubsystem> allSubsystems;
private ModuleAwareAssetTypeManager assetTypeManager;
private boolean initialisedAlready;
/**
* Contains objects that live for the duration of this engine.
*/
private Context rootContext;
/**
* This constructor initializes the engine by initializing its systems, subsystems and managers. It also verifies
* that some required systems are up and running after they have been initialized.
*
* @param timeSubsystem the timer subsystem
* @param subsystems other typical subsystems, e.g., graphics, audio and input subsystems.
*/
public TerasologyEngine(TimeSubsystem timeSubsystem, Collection<EngineSubsystem> subsystems) {
// configure native paths
PathManager.getInstance();
Bullet.init(true, false);
this.rootContext = new ContextImpl();
rootContext.put(GameEngine.class, this);
this.timeSubsystem = timeSubsystem;
//Record and Replay classes
RecordAndReplayCurrentStatus recordAndReplayCurrentStatus = new RecordAndReplayCurrentStatus();
rootContext.put(RecordAndReplayCurrentStatus.class, recordAndReplayCurrentStatus);
RecordAndReplayUtils recordAndReplayUtils = new RecordAndReplayUtils();
rootContext.put(RecordAndReplayUtils.class, recordAndReplayUtils);
CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap();
rootContext.put(CharacterStateEventPositionMap.class, characterStateEventPositionMap);
DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = new DirectionAndOriginPosRecorderList();
rootContext.put(DirectionAndOriginPosRecorderList.class, directionAndOriginPosRecorderList);
/*
* We can't load the engine without core registry yet.
* e.g. the statically created MaterialLoader needs the CoreRegistry to get the AssetManager.
* And the engine loads assets while it gets created.
*/
// TODO: Remove
CoreRegistry.setContext(rootContext);
this.allSubsystems = Queues.newArrayDeque();
this.allSubsystems.add(new ConfigurationSubsystem());
this.allSubsystems.add(timeSubsystem);
this.allSubsystems.addAll(subsystems);
this.allSubsystems.add(new PhysicsSubsystem());
this.allSubsystems.add(new CommandSubsystem());
this.allSubsystems.add(new NetworkSubsystem());
this.allSubsystems.add(new WorldGenerationSubsystem());
this.allSubsystems.add(new GameSubsystem());
this.allSubsystems.add(new I18nSubsystem());
this.allSubsystems.add(new TelemetrySubSystem());
this.allSubsystems.add(new MonitoringSubsystem());
for (EngineSubsystem subsystem : allSubsystems) {
if (LEGACY_ENGINE_MODULE_POLLUTERS.contains(subsystem.getClass().getName())) {
// add subsystem as engine module part. (needed for ECS classes loaded from external subsystems)
addToClassesOnClasspathsToAddToEngine(subsystem.getClass());
}
}
// the TypeHandlerLibrary is technically not a subsystem (although it lives in the subsystem space)
// therefore, we have to manually register the type handler classes with the engine module
addToClassesOnClasspathsToAddToEngine(TypeHandler.class);
// register NUI classes with engine module
addToClassesOnClasspathsToAddToEngine(UIWidget.class);
// register gestalt asset classes with engine module
addToClassesOnClasspathsToAddToEngine(ResourceUrn.class);
}
/**
* Provide ability to set additional engine classpath locations. This must be called before initialize() or
* run().
*
* @param clazz any class that appears in the resource location to treat as an engine classpath.
*/
protected void addToClassesOnClasspathsToAddToEngine(Class<?> clazz) {
classesOnClasspathsToAddToEngine.add(clazz);
}
public void initialize() {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Stopwatch totalInitTime = Stopwatch.createStarted();
if (!initialisedAlready) {
try {
logger.info("Initializing Terasology...");
logEnvironmentInfo();
// TODO: Need to get everything thread safe and get rid of the concept of "GameThread" as much as
// possible.
GameThread.setToCurrentThread();
preInitSubsystems();
initManagers();
initSubsystems();
changeStatus(TerasologyEngineStatus.INITIALIZING_ASSET_MANAGEMENT);
initAssets();
EnvironmentSwitchHandler environmentSwitcher = new EnvironmentSwitchHandler();
rootContext.put(EnvironmentSwitchHandler.class, environmentSwitcher);
environmentSwitcher.handleSwitchToGameEnvironment(rootContext);
postInitSubsystems();
verifyInitialisation();
/*
* Prevent objects being put in engine context after init phase. Engine states should use/create a
* child context.
*/
CoreRegistry.setContext(null);
initialisedAlready = true;
} catch (RuntimeException e) {
logger.error("Failed to initialise Terasology", e);
cleanup();
throw e;
}
}
double seconds = 0.001 * totalInitTime.elapsed(TimeUnit.MILLISECONDS);
logger.info("Initialization completed in {}sec.", String.format("%.2f", seconds));
}
private void verifyInitialisation() {
verifyRequiredSystemIsRegistered(Time.class);
verifyRequiredSystemIsRegistered(DisplayDevice.class);
verifyRequiredSystemIsRegistered(RenderingSubsystemFactory.class);
}
/**
* Logs software, environment and hardware information.
*/
private void logEnvironmentInfo() {
logger.info("{}", TerasologyVersion.getInstance());
logger.info("Home path: {}", PathManager.getInstance().getHomePath());
logger.info("Install path: {}", PathManager.getInstance().getInstallPath());
logger.info("Java: {} in {}", System.getProperty("java.version"), System.getProperty("java.home"));
logger.info("Java VM: {}, version: {}", System.getProperty("java.vm.name"), System.getProperty("java.vm" +
".version"));
logger.info("OS: {}, arch: {}, version: {}", System.getProperty("os.name"), System.getProperty("os.arch"),
System.getProperty("os.version"));
logger.info("Max. Memory: {} MiB", Runtime.getRuntime().maxMemory() / ONE_MEBIBYTE);
logger.info("Processors: {}", Runtime.getRuntime().availableProcessors());
if (NonNativeJVMDetector.JVM_ARCH_IS_NONNATIVE) {
logger.warn("Running on a 32-bit JVM on a 64-bit system. This may limit performance.");
}
}
/**
* Gives a chance to subsystems to do something BEFORE managers and Time are initialized.
*/
private void preInitSubsystems() {
changeStatus(TerasologyEngineStatus.PREPARING_SUBSYSTEMS);
for (EngineSubsystem subsystem : getSubsystems()) {
changeStatus(() -> "Pre-initialising " + subsystem.getName() + " subsystem");
subsystem.preInitialise(rootContext);
}
}
private void initSubsystems() {
changeStatus(TerasologyEngineStatus.INITIALIZING_SUBSYSTEMS);
for (EngineSubsystem subsystem : getSubsystems()) {
changeStatus(() -> "Initialising " + subsystem.getName() + " subsystem");
subsystem.initialise(this, rootContext);
}
}
/**
* Gives a chance to subsystems to do something AFTER managers and Time are initialized.
*/
private void postInitSubsystems() {
for (EngineSubsystem subsystem : getSubsystems()) {
changeStatus(() -> "Post-Initialising " + subsystem.getName() + " subsystem");
subsystem.postInitialise(rootContext);
}
}
/**
* Verifies that a required class is available through the core registry.
*
* @param clazz The required type, i.e. Time.class
* @throws IllegalStateException Details the required system that has not been registered.
*/
private void verifyRequiredSystemIsRegistered(Class<?> clazz) {
if (rootContext.get(clazz) == null) {
throw new IllegalStateException(clazz.getSimpleName() + " not registered as a core system.");
}
}
private void initManagers() {
changeStatus(TerasologyEngineStatus.INITIALIZING_MODULE_MANAGER);
TypeRegistry.WHITELISTED_CLASSES =
ExternalApiWhitelist.CLASSES.stream().map(Class::getName).collect(Collectors.toSet());
TypeRegistry.WHITELISTED_PACKAGES = ExternalApiWhitelist.PACKAGES;
ModuleManager moduleManager = new ModuleManager(rootContext.get(Config.class),
classesOnClasspathsToAddToEngine);
ModuleTypeRegistry typeRegistry = new ModuleTypeRegistry(moduleManager.getEnvironment());
rootContext.put(ModuleTypeRegistry.class, typeRegistry);
rootContext.put(TypeRegistry.class, typeRegistry);
rootContext.put(ModuleManager.class, moduleManager);
changeStatus(TerasologyEngineStatus.INITIALIZING_LOWLEVEL_OBJECT_MANIPULATION);
ReflectFactory reflectFactory = new ReflectionReflectFactory();
rootContext.put(ReflectFactory.class, reflectFactory);
CopyStrategyLibrary copyStrategyLibrary = new CopyStrategyLibrary(reflectFactory);
rootContext.put(CopyStrategyLibrary.class, copyStrategyLibrary);
rootContext.put(TypeHandlerLibrary.class, TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager,
typeRegistry));
changeStatus(TerasologyEngineStatus.INITIALIZING_ASSET_TYPES);
assetTypeManager = new AutoReloadAssetTypeManager();
rootContext.put(ModuleAwareAssetTypeManager.class, assetTypeManager);
rootContext.put(AssetManager.class, assetTypeManager.getAssetManager());
}
private void initAssets() {
// cast lambdas explicitly to avoid inconsistent compiler behavior wrt. type inference
assetTypeManager.createAssetType(Prefab.class, PojoPrefab::new, "prefabs");
assetTypeManager.createAssetType(BlockShape.class, BlockShapeImpl::new, "shapes");
assetTypeManager.createAssetType(BlockSounds.class, BlockSounds::new, "blockSounds");
assetTypeManager.createAssetType(BlockTile.class, BlockTile::new, "blockTiles");
AssetType<BlockFamilyDefinition, BlockFamilyDefinitionData> blockFamilyDefinitionAssetType =
assetTypeManager.createAssetType(BlockFamilyDefinition.class, BlockFamilyDefinition::new, "blocks");
assetTypeManager.getAssetFileDataProducer(blockFamilyDefinitionAssetType).addAssetFormat(
new BlockFamilyDefinitionFormat(assetTypeManager.getAssetManager()));
assetTypeManager.createAssetType(UISkinAsset.class, UISkinAsset::new, "skins");
assetTypeManager.createAssetType(BehaviorTree.class, BehaviorTree::new, "behaviors");
assetTypeManager.createAssetType(UIElement.class, UIElement::new, "ui");
assetTypeManager.createAssetType(ByteBufferAsset.class, ByteBufferAsset::new, "mesh");
for (EngineSubsystem subsystem : allSubsystems) {
subsystem.registerCoreAssetTypes(assetTypeManager);
}
}
@Override
public EngineStatus getStatus() {
return status;
}
@Override
public void subscribe(EngineStatusSubscriber subscriber) {
statusSubscriberList.add(subscriber);
}
@Override
public void unsubscribe(EngineStatusSubscriber subscriber) {
statusSubscriberList.remove(subscriber);
}
private void changeStatus(EngineStatus newStatus) {
status = newStatus;
for (EngineStatusSubscriber subscriber : statusSubscriberList) {
subscriber.onEngineStatusChanged(newStatus);
}
}
/**
* Runs the engine, including its main loop. This method is called only once per application startup, which is the
* reason the GameState provided is the -initial- state rather than a generic game state.
*
* @param initialState In at least one context (the PC facade) the GameState implementation provided as
* input may vary, depending if the application has or hasn't been started headless.
*/
@Override
public synchronized void run(GameState initialState) {
initializeRun(initialState);
runMain();
}
@Override
public synchronized void initializeRun(GameState initialState) {
Preconditions.checkState(!running);
running = true;
changeStatus(StandardGameStatus.INITIALIZING);
initialize();
try {
rootContext.put(GameEngine.class, this);
changeState(initialState);
} catch (Throwable e) {
logger.error("Uncaught exception, attempting clean game shutdown", e);
try {
cleanup();
} catch (RuntimeException t) {
logger.error("Clean game shutdown after an uncaught exception failed", t);
}
running = false;
shutdownRequested = false;
changeStatus(StandardGameStatus.UNSTARTED);
throw e;
}
}
@Override
public synchronized void runMain() {
Preconditions.checkState(running);
changeStatus(StandardGameStatus.RUNNING);
try {
mainLoop(); // -THE- MAIN LOOP. Most of the application time and resources are spent here.
} catch (Throwable e) {
logger.error("Uncaught exception, attempting clean game shutdown", e);
throw e;
} finally {
try {
cleanup();
} catch (RuntimeException t) {
logger.error("Clean game shutdown after an uncaught exception failed", t);
}
running = false;
shutdownRequested = false;
changeStatus(StandardGameStatus.UNSTARTED);
}
}
/**
* The main loop runs until the EngineState is set back to INITIALIZED by shutdown() or until the OS requests the
* application's window to be closed. Engine cleanup and disposal occur afterwards.
*/
@SuppressWarnings("checkstyle:EmptyBlock")
private void mainLoop() {
// PerformanceMonitor.startActivity("Other");
long tickStart = timeSubsystem.getEngineTime().getRealTimeInMs();
// MAIN GAME LOOP
while (tick()) {
/* do nothing */
long tickDuration = timeSubsystem.getEngineTime().getRealTimeInMs() - tickStart;
if (tickDuration > 100) {
logger.warn("Tick took a long time to execute ({}ms).", tickDuration);
}
tickStart = timeSubsystem.getEngineTime().getRealTimeInMs();
}
// PerformanceMonitor.endActivity();
}
/**
* Runs a single "tick" of the engine
*
* @return true if the loop requesting a tick should continue running
*/
public boolean tick() {
if (shutdownRequested) {
return false;
}
if (assetTypeManager instanceof AutoReloadAssetTypeManager) {
((AutoReloadAssetTypeManager) assetTypeManager).reloadChangedAssets();
}
processPendingState();
if (currentState == null) {
shutdown();
return false;
}
PerformanceMonitor.startActivity("TerasologyEngine::tick");
long startTime = timeSubsystem.getEngineTime().getRealTimeInMs();
Iterator<Float> updateCycles = timeSubsystem.getEngineTime().tick();
CoreRegistry.setContext(currentState.getContext());
rootContext.get(NetworkSystem.class).setContext(currentState.getContext());
for (EngineSubsystem subsystem : allSubsystems) {
try (Activity ignored = PerformanceMonitor.startActivity(subsystem.getName() + " PreUpdate")) {
subsystem.preUpdate(currentState, timeSubsystem.getEngineTime().getRealDelta());
}
}
while (updateCycles.hasNext()) {
float updateDelta = updateCycles.next(); // gameTime gets updated here!
try (Activity ignored = PerformanceMonitor.startActivity("Main Update")) {
currentState.update(updateDelta);
}
}
try (Activity ignored = PerformanceMonitor.startActivity("GameThread::processWaitingProcesses")) {
// Waiting processes are set by modules via GameThread.a/synch() methods.
GameThread.processWaitingProcesses();
}
for (EngineSubsystem subsystem : getSubsystems()) {
try (Activity ignored = PerformanceMonitor.startActivity(subsystem.getName() + " Subsystem postUpdate")) {
subsystem.postUpdate(currentState, timeSubsystem.getEngineTime().getRealDelta());
}
}
try (Activity ignored = PerformanceMonitor.startActivity("Dispose Unused Assets")) {
assetTypeManager.disposedUnusedAssets();
}
PerformanceMonitor.endActivity();
long tickDuration = timeSubsystem.getEngineTime().getRealTimeInMs() - startTime;
if (tickDuration > 100) {
logger.info("Begin abnormal activity stats (tick took {}ms):", tickDuration);
AtomicLong totalTime = new AtomicLong();
PerformanceMonitor.getTickExecutionStats().forEachEntry((activity, duration) -> {
if (duration > 1) {
logger.info("\t{}: {}", activity, duration);
}
totalTime.addAndGet(duration);
return true;
});
logger.info("End abnormal activity stats (recorded total: {}ms).", totalTime);
}
PerformanceMonitor.rollCycle();
// PerformanceMonitor.startActivity("Other");
return true;
}
public void cleanup() {
logger.info("Shutting down Terasology...");
changeStatus(StandardGameStatus.SHUTTING_DOWN);
if (currentState != null) {
currentState.dispose(true);
currentState = null;
}
Iterator<EngineSubsystem> preshutdownIter = allSubsystems.descendingIterator();
while (preshutdownIter.hasNext()) {
EngineSubsystem subsystem = preshutdownIter.next();
try {
subsystem.preShutdown();
} catch (RuntimeException e) {
logger.error("Error preparing to shutdown {} subsystem", subsystem.getName(), e);
}
}
Iterator<EngineSubsystem> shutdownIter = allSubsystems.descendingIterator();
while (shutdownIter.hasNext()) {
EngineSubsystem subsystem = shutdownIter.next();
try {
subsystem.shutdown();
} catch (RuntimeException e) {
logger.error("Error shutting down {} subsystem", subsystem.getName(), e);
}
}
}
/**
* Causes the main loop to stop at the end of the current frame, cleanly ending the current GameState, all running
* task threads and disposing subsystems.
*/
@Override
public void shutdown() {
shutdownRequested = true;
}
/**
* Changes the game state, i.e. to switch from the MainMenu to Ingame via Loading screen (each is a GameState). The
* change can be immediate, if there is no current game state set, or scheduled, when a current state exists and the
* new state is stored as pending. That been said, scheduled changes occurs in the main loop through the call
* processStateChanges(). As such, from a user perspective in normal circumstances, scheduled changes are likely to
* be perceived as immediate.
*/
@Override
public void changeState(GameState newState) {
if (currentState != null) {
pendingState = newState; // scheduled change
} else {
switchState(newState); // immediate change
}
}
private void processPendingState() {
if (pendingState != null) {
switchState(pendingState);
pendingState = null;
}
}
private void switchState(GameState newState) {
if (currentState != null) {
currentState.dispose();
}
CoreRegistry.setContext(newState.getContext());
currentState = newState;
LoggingContext.setGameState(newState);
newState.init(this);
stateChangeSubscribers.forEach(StateChangeSubscriber::onStateChange);
InputSystem inputSystem = rootContext.get(InputSystem.class);
if (inputSystem != null) {
inputSystem.drainQueues();
}
}
@Override
public boolean hasPendingState() {
return pendingState != null;
}
@Override
public GameState getState() {
return currentState;
}
@Override
public boolean isRunning() {
return running;
}
public Iterable<EngineSubsystem> getSubsystems() {
return allSubsystems;
}
@Override
public void subscribeToStateChange(StateChangeSubscriber subscriber) {
stateChangeSubscribers.add(subscriber);
}
@Override
public void unsubscribeToStateChange(StateChangeSubscriber subscriber) {
stateChangeSubscribers.remove(subscriber);
}
@Override
public Context createChildContext() {
return new ContextImpl(rootContext);
}
/**
* Allows it to obtain objects directly from the context of the game engine. It exists only for situations in which
* no child context exists yet. If there is a child context then it automatically contains the objects of the engine
* context. Thus normal code should just work with the (child) context that is available to it instead of using this
* method.
*
* @return a object directly from the context of the game engine
*/
public <T> T getFromEngineContext(Class<? extends T> type) {
return rootContext.get(type);
}
}