Engine provider

This commit is contained in:
Daniel Mills 2021-08-09 02:12:36 -04:00
parent 79b3d7359a
commit eb4b42c089
3 changed files with 96 additions and 26 deletions

View File

@ -28,8 +28,10 @@ import com.volmit.iris.engine.object.common.IrisWorld;
import com.volmit.iris.engine.object.dimensional.IrisDimension;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.hunk.Hunk;
import com.volmit.iris.util.io.IO;
import com.volmit.iris.util.io.ReactiveFolder;
import com.volmit.iris.util.parallel.MultiBurst;
import com.volmit.iris.util.scheduling.J;
import com.volmit.iris.util.scheduling.PrecisionStopwatch;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -50,8 +52,7 @@ import java.util.concurrent.atomic.AtomicReference;
public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChunkGenerator {
private static final BlockData ERROR_BLOCK = Material.RED_GLAZED_TERRACOTTA.createBlockData();
private final AtomicReference<CompletableFuture<Engine>> engine = new AtomicReference<>();
private final AtomicBoolean busy = new AtomicBoolean(false);
private final EngineProvider provider;
private final IrisWorld world;
private final File dataLocation;
private final String dimensionKey;
@ -67,18 +68,13 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
this.dataLocation = dataLocation;
this.dimensionKey = dimensionKey;
this.folder = new ReactiveFolder(dataLocation, (_a, _b, _c) -> initialize());
this.provider = new EngineProvider();
initialize();
}
public Engine getEngine()
{
try {
return engine.get().get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
return provider.getEngine();
}
@Override
@ -88,7 +84,7 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
@Override
public void close() {
getEngine().close();
provider.close();
}
@Override
@ -104,17 +100,10 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
private void initialize()
{
engine.set(MultiBurst.burst.completeValue(() -> createEngine(world, dimensionKey, dataLocation)));
}
private Engine createEngine(IrisWorld world, String dimension, File dataLocation) {
IrisData data = new IrisData(dataLocation);
IrisDimension realDimension = data.getDimensionLoader().load(dimension);
EngineTarget target = new EngineTarget(world, realDimension, data);
Engine engine = new IrisEngine(target, isStudio());
provider.provideEngine(world, dimensionKey, dataLocation, isStudio(), (e) -> {
populators.clear();
populators.add((BlockPopulator) engine);
return engine;
populators.add((BlockPopulator) e);
});
}
@Override
@ -126,7 +115,7 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
Hunk<BlockData> blocks = Hunk.view((ChunkData) tc);
Hunk<Biome> biomes = Hunk.view((BiomeGrid) tc);
this.world.bind(world);
engine.get().get().generate(x * 16, z * 16, blocks, biomes, true);
getEngine().generate(x * 16, z * 16, blocks, biomes, true);
return tc.getRaw();
}

View File

@ -0,0 +1,75 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.engine.platform;
import com.volmit.iris.core.project.loader.IrisData;
import com.volmit.iris.engine.IrisEngine;
import com.volmit.iris.engine.framework.Engine;
import com.volmit.iris.engine.framework.EngineTarget;
import com.volmit.iris.engine.object.common.IrisWorld;
import com.volmit.iris.engine.object.dimensional.IrisDimension;
import com.volmit.iris.util.parallel.MultiBurst;
import org.bukkit.generator.BlockPopulator;
import java.io.File;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
public class EngineProvider {
private final AtomicReference<CompletableFuture<Engine>> engine = new AtomicReference<>();
public void provideEngine(IrisWorld world, String dimension, File dataLocation, boolean studio, Consumer<Engine> post) {
close();
engine.set(MultiBurst.burst.completeValue(() -> {
IrisData data = new IrisData(dataLocation);
IrisDimension realDimension = data.getDimensionLoader().load(dimension);
EngineTarget target = new EngineTarget(world, realDimension, data);
Engine engine = new IrisEngine(target, studio);
post.accept(engine);
return engine;
}));
}
public Engine getEngine()
{
try {
return engine.get().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
public void close() {
if(engine.get() != null && engine.get().isDone())
{
Engine e = getEngine();
if(e != null)
{
e.close();
}
}
}
}

View File

@ -54,13 +54,14 @@ public class HeadlessGenerator implements PlatformChunkGenerator {
private final HeadlessWorld world;
private final NBTWorld writer;
private final MultiBurst burst;
private final Engine engine;
private final EngineProvider provider;
public HeadlessGenerator(HeadlessWorld world) {
this.world = world;
burst = new MultiBurst("Iris Headless Generator", 9, IrisSettings.getThreadCount(IrisSettings.get().getConcurrency().getPregenThreadCount()));
writer = new NBTWorld(world.getWorld().worldFolder());
engine = new IrisEngine(new EngineTarget(world.getWorld(),world.getDimension(), world.getDimension().getLoader()), isStudio());
provider = new EngineProvider();
provider.provideEngine(world.getWorld(), world.getDimension().getLoadKey(), world.getDimension().getLoader().getDataFolder(), isStudio(), (e) -> {});
}
@ChunkCoordinates
@ -76,7 +77,7 @@ public class HeadlessGenerator implements PlatformChunkGenerator {
.injector((xx, yy, zz, biomeBase) -> chunk.setBiomeAt(ox + xx, yy, oz + zz,
INMS.get().getTrueBiomeBaseId(biomeBase)))
.build();
engine.generate(x * 16, z * 16,
getEngine().generate(x * 16, z * 16,
Hunk.view((ChunkGenerator.ChunkData) tc), Hunk.view((ChunkGenerator.BiomeGrid) tc),
false);
} catch (Throwable e) {
@ -132,7 +133,7 @@ public class HeadlessGenerator implements PlatformChunkGenerator {
public void close() {
burst.shutdownAndAwait();
engine.close();
provider.close();
writer.close();
}
@ -151,6 +152,11 @@ public class HeadlessGenerator implements PlatformChunkGenerator {
return EMPTYPOINTS;
}
@Override
public Engine getEngine() {
return provider.getEngine();
}
@Override
public boolean isHeadless() {
return true;