Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions engine/src/main/java/org/destinationsol/SolApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ private void draw() {

public void play(boolean tut, String shipName, boolean isNewGame, WorldConfig worldConfig) {
ModuleManager moduleManager = appContext.getBean(ModuleManager.class);
moduleManager.loadEnvironment(worldConfig.getModules());
appContext.getBean(AssetHelper.class).switchEnvironment(moduleManager.getEnvironment());

gameContext = appContext.getNestedContainer(
new GameConfigurationServiceRegistry(worldConfig),
new EventReceiverServiceRegistry(moduleManager.getEnvironment()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public Set<ResourceUrn> listAssets(Class<? extends Asset<?>> type, String asset,
return list;
}

public void switchEnvironment(ModuleEnvironment environment) {
assetTypeManager.switchEnvironment(environment);
}

public void dispose() {
try {
assetTypeManager.unloadEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void unregisterModuleMusic() {
}

public void resetMusic() {
musicMap.put(GAME_MUSIC_SET, new ArrayList<>());
musicMap.clear();
}

public String getCurrentMusicSet() {
Expand Down
32 changes: 32 additions & 0 deletions engine/src/main/java/org/destinationsol/game/SaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
import org.destinationsol.game.item.SolItem;
import org.destinationsol.game.ship.SolShip;
import org.destinationsol.game.ship.hulls.HullConfig;
import org.destinationsol.modules.ModuleManager;
import org.destinationsol.ui.Waypoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.context.annotation.API;
import org.terasology.gestalt.module.Module;
import org.terasology.gestalt.naming.Name;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -50,8 +53,10 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@API
public class SaveManager {
Expand Down Expand Up @@ -263,6 +268,15 @@ public static void saveWorld(WorldConfig worldConfig) {
}
world.add("featureGenerators", featureGenerators);

JsonArray modulesArray = new JsonArray();
for (Name module : ModuleManager.getEnvironmentStatic().getModuleIdsOrderedByDependencies()) {
// Exclude built-in modules
if (module.compareTo("engine") != 0 && module.compareTo("nui") != 0) {
modulesArray.add(module.toString());
}
}
world.add("modules", modulesArray);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String stringToWrite = gson.toJson(world);

Expand Down Expand Up @@ -316,6 +330,24 @@ public static Optional<WorldConfig> loadWorld() {
config.setFeatureGenerators(featureGenerators);
}

if (world.has("modules")) {
Set<Module> modules = new HashSet<>();
for (JsonElement value : world.getAsJsonArray("modules")) {
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
Module module = ModuleManager.getEnvironmentStatic().get(new Name(value.getAsString()));
if (module != null) {
modules.add(module);
} else {
logger.warn("The module \"" + value.getAsString() + "\" is missing!");
}
}
}
config.setModules(modules);
} else {
// This is for compatibility with older saves, which always used all modules unconditionally.
config.setModules(new HashSet<>(ModuleManager.getEnvironmentStatic().getModulesOrderedByDependencies()));
}

logger.debug("Successfully loaded the world file");
return Optional.of(config);
} catch (FileNotFoundException e) {
Expand Down
17 changes: 16 additions & 1 deletion engine/src/main/java/org/destinationsol/game/WorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@
package org.destinationsol.game;

import org.destinationsol.game.planet.SystemsBuilder;
import org.terasology.gestalt.module.Module;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WorldConfig {
protected long seed;
protected int numberOfSystems;
private List<String> solarSystemGenerators;
private List<String> featureGenerators;
private Set<Module> modules;

public WorldConfig() {
seed = System.currentTimeMillis();
numberOfSystems = SystemsBuilder.DEFAULT_SYSTEM_COUNT;
solarSystemGenerators = new ArrayList<>();
featureGenerators = new ArrayList<>();
modules = new HashSet<>();
}

public WorldConfig(long seed, int numberOfSystems,
List<String> solarSystemGenerators,
List<String> featureGenerators) {
List<String> featureGenerators,
Set<Module> modules) {
this.seed = seed;
this.numberOfSystems = numberOfSystems;
this.solarSystemGenerators = solarSystemGenerators;
this.featureGenerators = featureGenerators;
this.modules = modules;
}

public long getSeed() {
Expand Down Expand Up @@ -73,4 +80,12 @@ public List<String> getFeatureGenerators() {
public void setSolarSystemGenerators(List<String> solarSystemGenerators) {
this.solarSystemGenerators = solarSystemGenerators;
}

public Set<Module> getModules() {
return modules;
}

public void setModules(Set<Module> modules) {
this.modules = modules;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static List<String> splitParameters(String paramStr) {
}

public void init(SolGame game) {

commandRegistry.clear();
for (Class commands : context.get(ModuleManager.class).getEnvironment().getTypesAnnotatedWith(RegisterCommands.class)) {
try {
Object commandsObject = commands.newInstance();
Expand Down
3 changes: 3 additions & 0 deletions engine/src/main/java/org/destinationsol/menu/MenuScreens.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.destinationsol.ui.nui.screens.mainMenu.InputMapScreen;
import org.destinationsol.ui.nui.screens.mainMenu.LoadingScreen;
import org.destinationsol.ui.nui.screens.mainMenu.MainMenuScreen;
import org.destinationsol.ui.nui.screens.mainMenu.ModulesScreen;
import org.destinationsol.ui.nui.screens.mainMenu.NewGameScreen;
import org.destinationsol.ui.nui.screens.mainMenu.NewShipScreen;
import org.destinationsol.ui.nui.screens.mainMenu.OptionsScreen;
Expand All @@ -36,6 +37,7 @@ public class MenuScreens {
public final LoadingScreen loading;
public final NewGameScreen newGame;
public final NewShipScreen newShip;
public final ModulesScreen modules;

public MenuScreens(SolLayouts layouts, boolean mobile, GameOptions gameOptions, NUIManager nuiManager) {
MenuLayout menuLayout = layouts.menuLayout;
Expand All @@ -47,5 +49,6 @@ public MenuScreens(SolLayouts layouts, boolean mobile, GameOptions gameOptions,
loading = (LoadingScreen) nuiManager.createScreen("engine:loadingScreen");
newGame = (NewGameScreen) nuiManager.createScreen("engine:newGameScreen");
newShip = (NewShipScreen) nuiManager.createScreen("engine:newShipScreen");
modules = (ModulesScreen) nuiManager.createScreen("engine:modulesScreen");
}
}
18 changes: 16 additions & 2 deletions engine/src/main/java/org/destinationsol/modules/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public class ModuleManager implements AutoCloseable {
private final FacadeModuleConfig moduleConfig;
protected ModuleRegistry registry;
protected Module engineModule;
private Set<Module> builtInModules;

@Inject
public ModuleManager(BeanContext beanContext, ModuleFactory moduleFactory, ModuleRegistry moduleRegistry,
Expand All @@ -240,9 +241,12 @@ public void init() throws Exception {
File modulesRoot = moduleConfig.getModulesPath();
scanner.scan(registry, modulesRoot);

builtInModules = Sets.newHashSet();
builtInModules.add(engineModule);
builtInModules.add(nuiModule);
registry.addAll(builtInModules);

Set<Module> requiredModules = Sets.newHashSet();
registry.add(engineModule);
registry.add(nuiModule);
requiredModules.addAll(registry);

loadEnvironment(requiredModules);
Expand All @@ -253,6 +257,8 @@ public void init() throws Exception {
}

public void loadEnvironment(Set<Module> modules) {
modules.addAll(builtInModules);

StandardPermissionProviderFactory permissionFactory = new StandardPermissionProviderFactory();
for (String api : API_WHITELIST) {
permissionFactory.getBasePermissionSet().addAPIPackage(api);
Expand Down Expand Up @@ -288,6 +294,10 @@ public ModuleEnvironment getEnvironment() {
return environment;
}

public Set<Module> getBuiltInModules() {
return builtInModules;
}

//TODO: REMOVE THIS
public static ModuleEnvironment getEnvironmentStatic() {
return environment;
Expand All @@ -299,6 +309,10 @@ public void printAvailableModules() {
}
}

public ModuleRegistry getRegistry() {
return registry;
}

public void dispose() {
environment.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,37 @@
import org.destinationsol.SolApplication;
import org.destinationsol.assets.music.OggMusicManager;
import org.destinationsol.game.WorldConfig;
import org.destinationsol.modules.ModuleManager;
import org.destinationsol.ui.nui.NUIManager;
import org.destinationsol.ui.nui.NUIScreenLayer;
import org.terasology.nui.Canvas;
import org.terasology.nui.widgets.UIButton;

import javax.inject.Inject;
import java.util.HashSet;

/**
* The main menu screen. This is the first screen shown when you open the game.
*/
public class MainMenuScreen extends NUIScreenLayer {

private final SolApplication solApplication;
private final ModuleManager moduleManager;
private UIButton tutorialButton;

@Inject
public MainMenuScreen(SolApplication solApplication) {
public MainMenuScreen(SolApplication solApplication, ModuleManager moduleManager) {
this.solApplication = solApplication;
this.moduleManager = moduleManager;
}

@Override
public void initialise() {
tutorialButton = find("tutorialButton", UIButton.class);
tutorialButton.subscribe(button -> {
solApplication.getMenuScreens().loading.setMode(true, "Imperial Small", true, new WorldConfig());
WorldConfig worldConfig = new WorldConfig();
worldConfig.setModules(new HashSet<>(moduleManager.getEnvironment().getModulesOrderedByDependencies()));
solApplication.getMenuScreens().loading.setMode(true, "Imperial Small", true, worldConfig);
nuiManager.setScreen(solApplication.getMenuScreens().loading);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2022 The Terasology Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.destinationsol.ui.nui.screens.mainMenu;

import org.destinationsol.SolApplication;
import org.destinationsol.modules.ModuleManager;
import org.destinationsol.ui.nui.NUIScreenLayer;
import org.terasology.gestalt.module.Module;
import org.terasology.nui.databinding.ReadOnlyBinding;
import org.terasology.nui.itemRendering.StringTextRenderer;
import org.terasology.nui.widgets.UIButton;
import org.terasology.nui.widgets.UIList;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* This screen is used to select the modules that should be active when playing a particular save.
* You can activate and de-activate modules only when initially creating a game.
* This is to prevent side-effects from new modules being introduced unexpectedly.
*/
public class ModulesScreen extends NUIScreenLayer {
private final SolApplication solApplication;
private final ModuleManager moduleManager;
private Set<Module> selectedModules;

@Inject
public ModulesScreen(SolApplication solApplication, ModuleManager moduleManager) {
this.solApplication = solApplication;
this.moduleManager = moduleManager;
}

@Override
public void initialise() {
selectedModules = new HashSet<>();

UIList<Module> moduleList = find("modulesList", UIList.class);
List<Module> modules = new ArrayList<>(moduleManager.getEnvironment().getModulesOrderedByDependencies());
modules.removeAll(moduleManager.getBuiltInModules());
moduleList.setList(modules);
moduleList.setItemRenderer(new StringTextRenderer<Module>() {
@Override
public String getString(Module value) {
if (!selectedModules.contains(value)) {
return value.getId().toString();
} else {
return value.getId().toString() + " (Active)";
}
}
});
moduleList.subscribe((list, module) -> {
if (selectedModules.contains(module)) {
selectedModules.remove(module);
} else {
selectedModules.add(module);
}
});

UIButton activateButton = find("activateButton", UIButton.class);
activateButton.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
Module selectedModule = moduleList.getSelection();
return selectedModule != null && !selectedModules.contains(selectedModule);
}
});
activateButton.subscribe(button -> selectedModules.add(moduleList.getSelection()));

UIButton deactivateButton = find("deactivateButton", UIButton.class);
deactivateButton.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
Module selectedModule = moduleList.getSelection();
return selectedModule != null && selectedModules.contains(selectedModule);
}
});
deactivateButton.subscribe(button -> selectedModules.remove(moduleList.getSelection()));

UIButton confirmButton = find("confirmButton", UIButton.class);
confirmButton.subscribe(button -> {
nuiManager.setScreen(solApplication.getMenuScreens().newShip);
});
}

public Set<Module> getSelectedModules() {
return selectedModules;
}

public void setSelectedModules(Set<Module> selectedModules) {
this.selectedModules = selectedModules;
}
}
Loading