Removed useless name mapping / Cleanup

This commit is contained in:
Anuken
2019-03-11 11:43:23 -04:00
parent ad059cbc38
commit 6c8d80777c
13 changed files with 251 additions and 212 deletions

View File

@@ -2,40 +2,35 @@ package io.anuke.mindustry.maps;
import io.anuke.arc.Core;
import io.anuke.arc.collection.ObjectMap;
import io.anuke.arc.function.Supplier;
import io.anuke.arc.files.FileHandle;
import io.anuke.arc.graphics.Texture;
import java.io.InputStream;
public class Map{
/** Internal map name. This is the filename, without any extensions.*/
public final String name;
/** Whether this is a custom map.*/
public final boolean custom;
/** Metadata. Author description, display name, etc.*/
public final ObjectMap<String, String> tags;
/** Supplies a new input stream with the data of this map.*/
public final Supplier<InputStream> stream;
/** Base file of this map.*/
public final FileHandle file;
/**Map width/height, shorts.*/
public int width, height;
/** Preview texture.*/
public Texture texture;
public Map(String name, int width, int height, ObjectMap<String, String> tags, boolean custom, Supplier<InputStream> streamSupplier){
this.name = name;
public Map(FileHandle file, int width, int height, ObjectMap<String, String> tags, boolean custom){
this.custom = custom;
this.tags = tags;
this.stream = streamSupplier;
this.file = file;
this.width = width;
this.height = height;
}
public Map(String name, int width, int height){
this(name, width, height, new ObjectMap<>(), true, () -> null);
public String fileName(){
return file.nameWithoutExtension();
}
public String getDisplayName(){
return tags.get("name", name);
return tags.get("name", fileName());
}
public String author(){
@@ -57,7 +52,7 @@ public class Map{
@Override
public String toString(){
return "Map{" +
"name='" + name + '\'' +
"file='" + file + '\'' +
", custom=" + custom +
", tags=" + tags +
'}';

View File

@@ -1,166 +1,125 @@
package io.anuke.mindustry.maps;
import io.anuke.arc.Core;
import io.anuke.arc.collection.Array;
import io.anuke.arc.files.FileHandle;
import io.anuke.arc.graphics.Texture;
import io.anuke.arc.collection.Array;
import io.anuke.arc.util.Disposable;
import io.anuke.arc.collection.ObjectMap;
import io.anuke.mindustry.io.MapIO;
import io.anuke.arc.function.Supplier;
import io.anuke.arc.util.Log;
import io.anuke.mindustry.io.MapIO;
import io.anuke.mindustry.world.Tile;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import static io.anuke.mindustry.Vars.*;
public class Maps implements Disposable{
/**List of all built-in maps.*/
private static final String[] defaultMapNames = {};
/**Tile format version.*/
private static final int version = 0;
/** List of all built-in maps. */
private static final String[] defaultMapNames = {"impact0079"};
/** All maps stored in an ordered array. */
private Array<Map> maps = new Array<>();
/**Maps map names to the real maps.*/
private ObjectMap<String, Map> maps = new ObjectMap<>();
/**All maps stored in an ordered array.*/
private Array<Map> allMaps = new Array<>();
/**Temporary array used for returning things.*/
private Array<Map> returnArray = new Array<>();
/**Returns a list of all maps, including custom ones.*/
/** Returns a list of all maps, including custom ones. */
public Array<Map> all(){
return allMaps;
return maps;
}
/**Returns a list of only custom maps.*/
/** Returns a list of only custom maps. */
public Array<Map> customMaps(){
returnArray.clear();
for(Map map : allMaps){
if(map.custom) returnArray.add(map);
}
return returnArray;
return maps.select(m -> m.custom);
}
/**Returns a list of only default maps.*/
/** Returns a list of only default maps. */
public Array<Map> defaultMaps(){
returnArray.clear();
for(Map map : allMaps){
if(!map.custom) returnArray.add(map);
}
return returnArray;
return maps.select(m -> !m.custom);
}
/**Returns map by internal name.*/
public Map getByName(String name){
return maps.get(name);
}
/**Loads a map from the map folder and returns it. Should only be used for zone maps.
* Does not add this map to the map list.*/
/**
* Loads a map from the map folder and returns it. Should only be used for zone maps.
* Does not add this map to the map list.
*/
public Map loadInternalMap(String name){
FileHandle file = Core.files.internal("maps/" + name + "." + mapExtension);
try(DataInputStream ds = new DataInputStream(file.read())) {
return new Map(name, MapIO.readMapMeta(ds), false, file::read);
try{
return MapIO.readMap(file, false);
}catch(IOException e){
throw new RuntimeException(e);
}
}
/**Load all maps. Should be called at application start.*/
/** Load all maps. Should be called at application start. */
public void load(){
try{
for (String name : defaultMapNames) {
for(String name : defaultMapNames){
FileHandle file = Core.files.internal("maps/" + name + "." + mapExtension);
loadMap(file.nameWithoutExtension(), file::read, false);
loadMap(file, false);
}
}catch (IOException e){
}catch(IOException e){
throw new RuntimeException(e);
}
loadCustomMaps();
}
/**Save a map. This updates all values and stored data necessary.*/
public void saveMap(String name, MapTileData data, ObjectMap<String, String> tags){
/** Save a map. This updates all values and stored data necessary. */
public void saveMap(Map map, Tile[][] data){
try{
//create copy of tags to prevent mutation later
ObjectMap<String, String> newTags = new ObjectMap<>();
newTags.putAll(tags);
tags = newTags;
FileHandle file = customMapDirectory.child(name + "." + mapExtension);
MapIO.writeMap(file.write(false), tags, data);
if(maps.containsKey(name)){
if(maps.get(name).texture != null) {
maps.get(name).texture.dispose();
maps.get(name).texture = null;
}
allMaps.removeValue(maps.get(name), true);
}
Map map = new Map(name, new MapMeta(version, tags, data.width(), data.height(), null), true, getStreamFor(name));
MapIO.writeMap(map.file.write(false), map, data);
if(!headless){
map.texture = new Texture(MapIO.generatePixmap(data));
map.texture = new Texture(MapIO.generatePreview(data));
}
allMaps.add(map);
maps.put(name, map);
}catch (IOException e){
maps.add(map);
}catch(IOException e){
throw new RuntimeException(e);
}
}
/**Removes a map completely.*/
/** Import a map, then save it. This updates all values and stored data necessary. */
public void importMap(FileHandle file, Map map){
file.copyTo(customMapDirectory.child(file.name()));
if(!headless){
map.texture = new Texture(MapIO.generatePreview(map));
}
maps.add(map);
}
/** Removes a map completely. */
public void removeMap(Map map){
if(map.texture != null){
map.texture.dispose();
map.texture = null;
}
maps.remove(map.name);
allMaps.removeValue(map, true);
customMapDirectory.child(map.name + "." + mapExtension).delete();
maps.remove(map);
map.file.delete();
}
private void loadMap(String name, Supplier<InputStream> supplier, boolean custom) throws IOException{
try(DataInputStream ds = new DataInputStream(supplier.get())) {
MapMeta meta = MapIO.readMapMeta(ds);
Map map = new Map(name, meta, custom, supplier);
private void loadMap(FileHandle file, boolean custom) throws IOException{
Map map = MapIO.readMap(file, custom);
if (!headless){
map.texture = new Texture(MapIO.generatePixmap(MapIO.readTileData(ds, meta, true)));
}
maps.put(map.name, map);
allMaps.add(map);
if(!headless){
map.texture = new Texture(MapIO.generatePreview(map));
}
maps.add(map);
}
private void loadCustomMaps(){
for(FileHandle file : customMapDirectory.list()){
try{
if(file.extension().equalsIgnoreCase(mapExtension)){
loadMap(file.nameWithoutExtension(), file::read, true);
loadMap(file, true);
}
}catch (Exception e){
}catch(Exception e){
Log.err("Failed to load custom map file '{0}'!", file);
Log.err(e);
}
}
}
/**Returns an input stream supplier for a given map name.*/
private Supplier<InputStream> getStreamFor(String name){
return customMapDirectory.child(name + "." + mapExtension)::read;
}
@Override
public void dispose() {
public void dispose(){
}
}