2021-04-24 22:11:26 +02:00
|
|
|
/*
|
|
|
|
* This file is part of BlueMap, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
|
|
|
|
* Copyright (c) contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
package de.bluecolored.bluemap.core.map;
|
|
|
|
|
|
|
|
import com.flowpowered.math.vector.Vector2i;
|
2022-08-12 18:11:11 +02:00
|
|
|
import com.google.gson.GsonBuilder;
|
2022-08-07 20:32:55 +02:00
|
|
|
import de.bluecolored.bluemap.api.debug.DebugDump;
|
2022-08-01 12:53:58 +02:00
|
|
|
import de.bluecolored.bluemap.api.gson.MarkerGson;
|
2022-07-27 20:31:14 +02:00
|
|
|
import de.bluecolored.bluemap.api.markers.MarkerSet;
|
2021-04-24 22:11:26 +02:00
|
|
|
import de.bluecolored.bluemap.core.logger.Logger;
|
|
|
|
import de.bluecolored.bluemap.core.map.hires.HiresModelManager;
|
2022-08-07 20:32:55 +02:00
|
|
|
import de.bluecolored.bluemap.core.map.lowres.LowresTileManager;
|
2022-06-03 18:52:15 +02:00
|
|
|
import de.bluecolored.bluemap.core.resources.adapter.ResourcesGson;
|
2022-05-28 21:55:41 +02:00
|
|
|
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
|
2021-11-14 14:18:31 +01:00
|
|
|
import de.bluecolored.bluemap.core.storage.Storage;
|
2021-04-24 22:11:26 +02:00
|
|
|
import de.bluecolored.bluemap.core.world.Grid;
|
|
|
|
import de.bluecolored.bluemap.core.world.World;
|
|
|
|
|
2022-06-03 18:52:15 +02:00
|
|
|
import java.io.*;
|
2023-03-02 16:30:28 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-07-24 12:10:00 +02:00
|
|
|
import java.util.Map;
|
2021-04-24 22:11:26 +02:00
|
|
|
import java.util.Objects;
|
2021-11-06 16:14:14 +01:00
|
|
|
import java.util.Optional;
|
2022-07-24 12:10:00 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2021-05-24 14:48:31 +02:00
|
|
|
import java.util.function.Predicate;
|
2021-04-24 22:11:26 +02:00
|
|
|
|
2021-06-16 20:09:45 +02:00
|
|
|
@DebugDump
|
2021-04-24 22:11:26 +02:00
|
|
|
public class BmMap {
|
|
|
|
|
2022-12-13 16:54:31 +01:00
|
|
|
public static final String META_FILE_SETTINGS = "settings.json";
|
|
|
|
public static final String META_FILE_TEXTURES = "textures.json";
|
|
|
|
public static final String META_FILE_RENDER_STATE = ".rstate";
|
2022-12-29 16:06:53 +01:00
|
|
|
public static final String META_FILE_MARKERS = "live/markers.json";
|
2022-12-13 16:54:31 +01:00
|
|
|
public static final String META_FILE_PLAYERS = "live/players.json";
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
private final String id;
|
|
|
|
private final String name;
|
2022-04-20 22:54:27 +02:00
|
|
|
private final String worldId;
|
2021-09-19 22:15:50 +02:00
|
|
|
private final World world;
|
2021-11-06 16:14:14 +01:00
|
|
|
private final Storage storage;
|
2022-04-24 18:53:06 +02:00
|
|
|
private final MapSettings mapSettings;
|
2021-04-24 22:11:26 +02:00
|
|
|
|
2022-05-28 21:55:41 +02:00
|
|
|
private final ResourcePack resourcePack;
|
2021-09-19 22:15:50 +02:00
|
|
|
private final MapRenderState renderState;
|
2022-05-28 21:55:41 +02:00
|
|
|
private final TextureGallery textureGallery;
|
2021-04-24 22:11:26 +02:00
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
private final HiresModelManager hiresModelManager;
|
2022-08-07 20:32:55 +02:00
|
|
|
private final LowresTileManager lowresTileManager;
|
2021-04-24 22:11:26 +02:00
|
|
|
|
2022-07-30 13:24:07 +02:00
|
|
|
private final ConcurrentHashMap<String, MarkerSet> markerSets;
|
2022-07-24 12:10:00 +02:00
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
private Predicate<Vector2i> tileFilter;
|
2021-05-24 14:48:31 +02:00
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
private long renderTimeSumNanos;
|
|
|
|
private long tilesRendered;
|
2021-06-04 11:23:53 +02:00
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
public BmMap(String id, String name, String worldId, World world, Storage storage, ResourcePack resourcePack, MapSettings settings) throws IOException {
|
2021-09-19 22:15:50 +02:00
|
|
|
this.id = Objects.requireNonNull(id);
|
|
|
|
this.name = Objects.requireNonNull(name);
|
2022-04-20 22:54:27 +02:00
|
|
|
this.worldId = Objects.requireNonNull(worldId);
|
2021-09-19 22:15:50 +02:00
|
|
|
this.world = Objects.requireNonNull(world);
|
2021-11-06 16:14:14 +01:00
|
|
|
this.storage = Objects.requireNonNull(storage);
|
2022-05-28 21:55:41 +02:00
|
|
|
this.resourcePack = Objects.requireNonNull(resourcePack);
|
2022-06-06 22:51:26 +02:00
|
|
|
this.mapSettings = Objects.requireNonNull(settings);
|
2021-04-24 22:11:26 +02:00
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
this.renderState = new MapRenderState();
|
2022-05-28 21:55:41 +02:00
|
|
|
loadRenderState();
|
2021-04-24 22:11:26 +02:00
|
|
|
|
2022-05-28 21:55:41 +02:00
|
|
|
this.textureGallery = loadTextureGallery();
|
|
|
|
this.textureGallery.put(resourcePack);
|
|
|
|
saveTextureGallery();
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
this.hiresModelManager = new HiresModelManager(
|
2022-08-07 20:32:55 +02:00
|
|
|
storage.tileStorage(id, 0),
|
2022-05-28 21:55:41 +02:00
|
|
|
this.resourcePack,
|
|
|
|
this.textureGallery,
|
2021-09-19 22:15:50 +02:00
|
|
|
settings,
|
|
|
|
new Grid(settings.getHiresTileSize(), 2)
|
|
|
|
);
|
|
|
|
|
2022-08-07 20:32:55 +02:00
|
|
|
this.lowresTileManager = new LowresTileManager(
|
|
|
|
storage.mapStorage(id),
|
2022-08-12 18:11:11 +02:00
|
|
|
new Grid(settings.getLowresTileSize()),
|
|
|
|
settings.getLodCount(),
|
|
|
|
settings.getLodFactor()
|
2021-09-19 22:15:50 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
this.tileFilter = t -> true;
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
this.markerSets = new ConcurrentHashMap<>();
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
this.renderTimeSumNanos = 0;
|
|
|
|
this.tilesRendered = 0;
|
2022-06-06 22:51:26 +02:00
|
|
|
|
|
|
|
saveMapSettings();
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void renderTile(Vector2i tile) {
|
|
|
|
if (!tileFilter.test(tile)) return;
|
|
|
|
|
|
|
|
long start = System.nanoTime();
|
|
|
|
|
2022-08-15 00:19:24 +02:00
|
|
|
hiresModelManager.render(world, tile, lowresTileManager, mapSettings.isSaveHiresLayer());
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
long end = System.nanoTime();
|
|
|
|
long delta = end - start;
|
|
|
|
|
|
|
|
renderTimeSumNanos += delta;
|
|
|
|
tilesRendered ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void save() {
|
2022-08-07 20:32:55 +02:00
|
|
|
lowresTileManager.save();
|
2022-05-28 21:55:41 +02:00
|
|
|
saveRenderState();
|
2022-07-30 15:31:42 +02:00
|
|
|
saveMarkerState();
|
2023-03-02 16:30:28 +01:00
|
|
|
savePlayerState();
|
2022-08-14 20:36:50 +02:00
|
|
|
saveMapSettings();
|
2022-08-06 07:05:29 +02:00
|
|
|
|
2022-08-14 20:36:50 +02:00
|
|
|
// only save texture gallery if not present in storage
|
2022-08-06 07:05:29 +02:00
|
|
|
try {
|
2023-02-01 21:42:43 +01:00
|
|
|
if (storage.readMetaInfo(id, META_FILE_TEXTURES).isEmpty())
|
2022-08-06 07:05:29 +02:00
|
|
|
saveTextureGallery();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Logger.global.logError("Failed to read texture gallery", e);
|
|
|
|
}
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2022-05-28 21:55:41 +02:00
|
|
|
private void loadRenderState() throws IOException {
|
2022-12-13 16:54:31 +01:00
|
|
|
Optional<InputStream> rstateData = storage.readMeta(id, META_FILE_RENDER_STATE);
|
2022-05-28 21:55:41 +02:00
|
|
|
if (rstateData.isPresent()) {
|
2022-12-13 16:54:31 +01:00
|
|
|
try (InputStream in = rstateData.get()){
|
2022-05-28 21:55:41 +02:00
|
|
|
this.renderState.load(in);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Logger.global.logWarning("Failed to load render-state for map '" + getId() + "': " + ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 15:31:42 +02:00
|
|
|
public synchronized void saveRenderState() {
|
2022-12-13 16:54:31 +01:00
|
|
|
try (OutputStream out = storage.writeMeta(id, META_FILE_RENDER_STATE)) {
|
2021-11-06 16:14:14 +01:00
|
|
|
this.renderState.save(out);
|
2021-09-19 22:15:50 +02:00
|
|
|
} catch (IOException ex){
|
|
|
|
Logger.global.logError("Failed to save render-state for map: '" + this.id + "'!", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 21:55:41 +02:00
|
|
|
private TextureGallery loadTextureGallery() throws IOException {
|
|
|
|
TextureGallery gallery = null;
|
2022-12-13 16:54:31 +01:00
|
|
|
Optional<InputStream> texturesData = storage.readMeta(id, META_FILE_TEXTURES);
|
2022-05-28 21:55:41 +02:00
|
|
|
if (texturesData.isPresent()) {
|
2022-12-13 16:54:31 +01:00
|
|
|
try (InputStream in = texturesData.get()){
|
2022-05-28 21:55:41 +02:00
|
|
|
gallery = TextureGallery.readTexturesFile(in);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Logger.global.logError("Failed to load textures for map '" + getId() + "'!", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gallery != null ? gallery : new TextureGallery();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void saveTextureGallery() {
|
2022-12-13 16:54:31 +01:00
|
|
|
try (OutputStream out = storage.writeMeta(id, META_FILE_TEXTURES)) {
|
2023-12-11 12:35:12 +01:00
|
|
|
this.textureGallery.writeTexturesFile(out);
|
2022-05-28 21:55:41 +02:00
|
|
|
} catch (IOException ex) {
|
|
|
|
Logger.global.logError("Failed to save textures for map '" + getId() + "'!", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 20:38:34 +01:00
|
|
|
public synchronized void resetTextureGallery() {
|
|
|
|
this.textureGallery.clear();
|
|
|
|
this.textureGallery.put(this.resourcePack);
|
|
|
|
}
|
|
|
|
|
2022-06-03 18:52:15 +02:00
|
|
|
private void saveMapSettings() {
|
|
|
|
try (
|
2022-12-13 16:54:31 +01:00
|
|
|
OutputStream out = storage.writeMeta(id, META_FILE_SETTINGS);
|
2023-07-03 13:03:52 +02:00
|
|
|
Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)
|
2022-06-03 18:52:15 +02:00
|
|
|
) {
|
2022-08-12 18:11:11 +02:00
|
|
|
ResourcesGson.addAdapter(new GsonBuilder())
|
|
|
|
.registerTypeAdapter(BmMap.class, new MapSettingsSerializer())
|
|
|
|
.create()
|
|
|
|
.toJson(this, writer);
|
2022-06-03 18:52:15 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
Logger.global.logError("Failed to save settings for map '" + getId() + "'!", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 15:31:42 +02:00
|
|
|
public synchronized void saveMarkerState() {
|
|
|
|
try (
|
2022-12-13 16:54:31 +01:00
|
|
|
OutputStream out = storage.writeMeta(id, META_FILE_MARKERS);
|
2023-07-03 13:03:52 +02:00
|
|
|
Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)
|
2022-07-30 15:31:42 +02:00
|
|
|
) {
|
|
|
|
MarkerGson.INSTANCE.toJson(this.markerSets, writer);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
Logger.global.logError("Failed to save markers for map '" + getId() + "'!", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:30:28 +01:00
|
|
|
public synchronized void savePlayerState() {
|
|
|
|
try (
|
2023-06-18 13:46:41 +02:00
|
|
|
OutputStream out = storage.writeMeta(id, META_FILE_PLAYERS)
|
2023-03-02 16:30:28 +01:00
|
|
|
) {
|
|
|
|
out.write("{}".getBytes(StandardCharsets.UTF_8));
|
|
|
|
} catch (Exception ex) {
|
|
|
|
Logger.global.logError("Failed to save markers for map '" + getId() + "'!", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
public String getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
public String getWorldId() {
|
|
|
|
return worldId;
|
|
|
|
}
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
public World getWorld() {
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
|
2021-11-06 16:14:14 +01:00
|
|
|
public Storage getStorage() {
|
|
|
|
return storage;
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-24 18:53:06 +02:00
|
|
|
public MapSettings getMapSettings() {
|
|
|
|
return mapSettings;
|
|
|
|
}
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
public MapRenderState getRenderState() {
|
|
|
|
return renderState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HiresModelManager getHiresModelManager() {
|
|
|
|
return hiresModelManager;
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:32:55 +02:00
|
|
|
public LowresTileManager getLowresTileManager() {
|
|
|
|
return lowresTileManager;
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
public Map<String, MarkerSet> getMarkerSets() {
|
|
|
|
return markerSets;
|
|
|
|
}
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
public Predicate<Vector2i> getTileFilter() {
|
|
|
|
return tileFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTileFilter(Predicate<Vector2i> tileFilter) {
|
|
|
|
this.tileFilter = tileFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getAverageNanosPerTile() {
|
|
|
|
return renderTimeSumNanos / tilesRendered;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return id.hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (obj instanceof BmMap) {
|
|
|
|
BmMap that = (BmMap) obj;
|
|
|
|
|
|
|
|
return this.id.equals(that.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "BmMap{" +
|
|
|
|
"id='" + id + '\'' +
|
|
|
|
", name='" + name + '\'' +
|
|
|
|
", world=" + world +
|
2021-11-06 16:14:14 +01:00
|
|
|
", storage=" + storage +
|
2021-09-19 22:15:50 +02:00
|
|
|
'}';
|
|
|
|
}
|
2021-06-25 13:51:24 +02:00
|
|
|
|
2021-04-24 22:11:26 +02:00
|
|
|
}
|