Add light-reload command to reload bluemap without reloading all block-resources

This commit is contained in:
Lukas Rieger (Blue) 2023-01-03 16:03:07 +01:00
parent 3b37be432c
commit 1fa8bb5e4c
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
3 changed files with 63 additions and 9 deletions

View File

@ -48,6 +48,7 @@
import de.bluecolored.bluemap.core.util.FileHelper;
import de.bluecolored.bluemap.core.world.World;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.gson.GsonConfigurationLoader;
@ -82,6 +83,14 @@ public class BlueMapService implements Closeable {
private ResourcePack resourcePack;
public BlueMapService(ServerInterface serverInterface, BlueMapConfigProvider configProvider, @Nullable ResourcePack preloadedResourcePack) {
this(serverInterface, configProvider);
if (preloadedResourcePack != null)
this.resourcePack = preloadedResourcePack;
}
public BlueMapService(ServerInterface serverInterface, BlueMapConfigProvider configProvider) {
this.serverInterface = serverInterface;
this.configs = configProvider;
@ -230,6 +239,7 @@ private synchronized void loadMapConfig(String id, MapConfig mapConfig) throws C
World world = worlds.get(worldId);
if (world == null) {
try {
Logger.global.logInfo("Loading world '" + worldId + "' (" + worldFolder.toAbsolutePath().normalize() + ")...");
world = new MCAWorld(worldFolder, mapConfig.getWorldSkyLight(), mapConfig.isIgnoreMissingLightData());
worlds.put(worldId, world);
} catch (IOException ex) {
@ -244,6 +254,7 @@ private synchronized void loadMapConfig(String id, MapConfig mapConfig) throws C
try {
Logger.global.logInfo("Loading map '" + name + "'...");
BmMap map = new BmMap(
id,
name,
@ -424,6 +435,10 @@ public synchronized ResourcePack getResourcePack() throws ConfigurationException
return resourcePack;
}
public Optional<ResourcePack> getResourcePackIfLoaded() {
return Optional.ofNullable(this.resourcePack);
}
private Collection<Path> getWorldFolders() {
Set<Path> folders = new HashSet<>();
for (MapConfig mapConfig : configs.getMapConfigs().values()) {

View File

@ -45,9 +45,11 @@
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.map.BmMap;
import de.bluecolored.bluemap.core.metrics.Metrics;
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.storage.Storage;
import de.bluecolored.bluemap.core.util.FileHelper;
import de.bluecolored.bluemap.core.world.World;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.gson.GsonConfigurationLoader;
import org.spongepowered.configurate.serialize.SerializationException;
@ -101,15 +103,19 @@ public Plugin(String implementationType, ServerInterface serverInterface) {
}
public void load() throws IOException {
load(null);
}
private void load(@Nullable ResourcePack preloadedResourcePack) throws IOException {
loadingLock.lock();
try {
loadingLock.lock();
synchronized (this) {
if (loaded) return;
unload(); //ensure nothing is left running (from a failed load or something)
//load configs
blueMap = new BlueMapService(serverInterface, new BlueMapConfigs(serverInterface));
blueMap = new BlueMapService(serverInterface, new BlueMapConfigs(serverInterface), preloadedResourcePack);
CoreConfig coreConfig = getConfigs().getCoreConfig();
WebserverConfig webserverConfig = getConfigs().getWebserverConfig();
WebappConfig webappConfig = getConfigs().getWebappConfig();
@ -333,8 +339,8 @@ public void run() {
}
public void unload() {
loadingLock.interruptAndLock();
try {
loadingLock.interruptAndLock();
synchronized (this) {
//save
save();
@ -401,6 +407,31 @@ public void reload() throws IOException {
load();
}
/**
* {@link #reload()} but without reloading the resourcepack (if it is loaded).
*/
public void lightReload() throws IOException {
loadingLock.lock();
try {
synchronized (this) {
if (!loaded) {
reload(); // reload normally
return;
}
// hold and reuse loaded resourcepack
ResourcePack preloadedResourcePack = this.blueMap.getResourcePackIfLoaded().orElse(null);
unload();
load(preloadedResourcePack);
}
} finally {
loadingLock.unlock();
}
}
public synchronized void save() {
if (pluginState != null) {
try {

View File

@ -96,9 +96,9 @@ public void init() {
LiteralCommandNode<S> versionCommand =
literal("version")
.requires(requirementsUnloaded("bluemap.version"))
.executes(this::versionCommand)
.build();
.requires(requirementsUnloaded("bluemap.version"))
.executes(this::versionCommand)
.build();
LiteralCommandNode<S> helpCommand =
literal("help")
@ -109,7 +109,11 @@ public void init() {
LiteralCommandNode<S> reloadCommand =
literal("reload")
.requires(requirementsUnloaded("bluemap.reload"))
.executes(this::reloadCommand)
.executes(context -> this.reloadCommand(context, false))
.then(literal("light")
.executes(context -> this.reloadCommand(context, true)))
.build();
LiteralCommandNode<S> debugCommand =
@ -380,14 +384,18 @@ public int helpCommand(CommandContext<S> context) {
return 1;
}
public int reloadCommand(CommandContext<S> context) {
public int reloadCommand(CommandContext<S> context, boolean light) {
CommandSource source = commandSourceInterface.apply(context.getSource());
source.sendMessage(Text.of(TextColor.GOLD, "Reloading BlueMap..."));
new Thread(() -> {
try {
plugin.reload();
if (light) {
plugin.lightReload();
} else {
plugin.reload();
}
if (plugin.isLoaded()) {
source.sendMessage(Text.of(TextColor.GREEN, "BlueMap reloaded!"));