Class package refactoring

This commit is contained in:
Anuken
2018-07-14 12:11:48 -04:00
parent 59ade6cf09
commit cfecdae970
47 changed files with 318 additions and 333 deletions
-45
View File
@@ -1,45 +0,0 @@
package io.anuke.mindustry.io;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.ucore.function.Supplier;
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 MapMeta meta;
/**
* Supplies a new input stream with the data of this map.
*/
public final Supplier<InputStream> stream;
/**
* Preview texture.
*/
public Texture texture;
public Map(String name, MapMeta meta, boolean custom, Supplier<InputStream> streamSupplier){
this.name = name;
this.custom = custom;
this.meta = meta;
this.stream = streamSupplier;
}
public Map(String unknownName, int width, int height){
this(unknownName, new MapMeta(0, new ObjectMap<>(), width, height, null), true, () -> null);
}
public String getDisplayName(){
return meta.tags.get("name", name);
}
}
+5 -2
View File
@@ -8,8 +8,11 @@ import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectMap.Entry;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.io.MapTileData.DataPosition;
import io.anuke.mindustry.io.MapTileData.TileDataMarker;
import io.anuke.mindustry.maps.Map;
import io.anuke.mindustry.maps.MapMeta;
import io.anuke.mindustry.maps.MapTileData;
import io.anuke.mindustry.maps.MapTileData.DataPosition;
import io.anuke.mindustry.maps.MapTileData.TileDataMarker;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.ColorMapper;
@@ -1,35 +0,0 @@
package io.anuke.mindustry.io;
import com.badlogic.gdx.utils.IntIntMap;
import com.badlogic.gdx.utils.ObjectMap;
public class MapMeta{
public final int version;
public final ObjectMap<String, String> tags;
public final int width, height;
public final IntIntMap blockMap;
public MapMeta(int version, ObjectMap<String, String> tags, int width, int height, IntIntMap blockMap){
this.version = version;
this.tags = tags;
this.width = width;
this.height = height;
this.blockMap = blockMap;
}
public String author(){
return tags.get("author", "unknown");
}
public String description(){
return tags.get("description", "unknown");
}
public String name(){
return tags.get("name", "unknown");
}
public boolean hasOreGen(){
return !tags.get("oregen", "0").equals("0");
}
}
@@ -1,139 +0,0 @@
package io.anuke.mindustry.io;
import com.badlogic.gdx.utils.IntIntMap;
import io.anuke.ucore.util.Bits;
import java.nio.ByteBuffer;
public class MapTileData{
/**
* Tile size: 4 bytes. <br>
* 0: ground tile <br>
* 1: wall tile <br>
* 2: rotation + team <br>
* 3: link (x/y) <br>
* 4: elevation <br>
*/
private final static int TILE_SIZE = 5;
private final ByteBuffer buffer;
private final int width, height;
private final boolean readOnly;
private IntIntMap map;
public MapTileData(int width, int height){
this.width = width;
this.height = height;
this.map = null;
this.readOnly = false;
buffer = ByteBuffer.allocate(width * height * TILE_SIZE);
}
public MapTileData(byte[] bytes, int width, int height, IntIntMap mapping, boolean readOnly){
buffer = ByteBuffer.wrap(bytes);
this.width = width;
this.height = height;
this.map = mapping;
this.readOnly = readOnly;
if(mapping != null && !readOnly){
for(int i = 0; i < width * height; i++){
TileDataMarker marker = new TileDataMarker();
read(marker);
buffer.position(i * TILE_SIZE);
write(marker);
}
buffer.position(0);
this.map = null;
}
}
public byte[] toArray(){
return buffer.array();
}
public int width(){
return width;
}
public int height(){
return height;
}
/**
* Write a byte to a specific position.
*/
public void write(int x, int y, DataPosition position, byte data){
buffer.put((x + width * y) * TILE_SIZE + position.ordinal(), data);
}
/**
* Gets a byte at a specific position.
*/
public byte read(int x, int y, DataPosition position){
return buffer.get((x + width * y) * TILE_SIZE + position.ordinal());
}
/**
* Reads and returns the next tile data.
*/
public TileDataMarker read(TileDataMarker marker){
marker.read(buffer);
return marker;
}
/**
* Writes this tile data marker.
*/
public void write(TileDataMarker marker){
marker.write(buffer);
}
/**
* Sets read position to the specified coordinates
*/
public void position(int x, int y){
buffer.position((x + width * y) * TILE_SIZE);
}
public TileDataMarker newDataMarker(){
return new TileDataMarker();
}
public enum DataPosition{
floor, wall, link, rotationTeam, elevation
}
public class TileDataMarker{
public byte floor, wall;
public byte link;
public byte rotation;
public byte team;
public byte elevation;
public void read(ByteBuffer buffer){
floor = buffer.get();
wall = buffer.get();
link = buffer.get();
byte rt = buffer.get();
elevation = buffer.get();
rotation = Bits.getLeftByte(rt);
team = Bits.getRightByte(rt);
if(map != null){
floor = (byte) map.get(floor, floor);
wall = (byte) map.get(wall, wall);
}
}
public void write(ByteBuffer buffer){
if(readOnly) throw new IllegalArgumentException("This data is read-only.");
buffer.put(floor);
buffer.put(wall);
buffer.put(link);
buffer.put(Bits.packByte(rotation, team));
buffer.put(elevation);
}
}
}
-223
View File
@@ -1,223 +0,0 @@
package io.anuke.mindustry.io;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.ucore.core.Settings;
import io.anuke.ucore.function.Supplier;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.ThreadArray;
import java.io.*;
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;
/**
* 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 ThreadArray<>();
/**
* Temporary array used for returning things.
*/
private Array<Map> returnArray = new ThreadArray<>();
/**
* Used for storing a list of custom map names for GWT.
*/
private Array<String> customMapNames;
public Maps(){
}
/**
* Returns a list of all maps, including custom ones.
*/
public Array<Map> all(){
return allMaps;
}
/**
* 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;
}
/**
* 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;
}
/**
* Returns map by internal name.
*/
public Map getByName(String name){
return maps.get(name);
}
/**
* Load all maps. Should be called at application start.
*/
public void load(){
try{
for(String name : defaultMapNames){
FileHandle file = Gdx.files.internal("maps/" + name + "." + mapExtension);
loadMap(file.nameWithoutExtension(), file::read, false);
}
}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){
try{
if(!gwt){
FileHandle file = customMapDirectory.child(name + "." + mapExtension);
MapIO.writeMap(file.write(false), tags, data);
}else{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
MapIO.writeMap(stream, tags, data);
Settings.putString("map-data-" + name, new String(Base64Coder.encode(stream.toByteArray())));
if(!customMapNames.contains(name, false)){
customMapNames.add(name);
Settings.putJson("custom-maps", customMapNames);
}
Settings.save();
}
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));
if(!headless){
map.texture = new Texture(MapIO.generatePixmap(data));
}
allMaps.add(map);
maps.put(name, map);
}catch(IOException e){
throw new RuntimeException(e);
}
}
/**
* 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);
if(!gwt){
customMapDirectory.child(map.name + "." + mapExtension).delete();
}else{
customMapNames.removeValue(map.name, false);
Settings.putString("map-data-" + map.name, "");
Settings.putJson("custom-maps", customMapNames);
Settings.save();
}
}
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);
if(!headless){
map.texture = new Texture(MapIO.generatePixmap(MapIO.readTileData(ds, meta, true)));
}
maps.put(map.name, map);
allMaps.add(map);
}
}
private void loadCustomMaps(){
if(!gwt){
for(FileHandle file : customMapDirectory.list()){
try{
if(file.extension().equalsIgnoreCase(mapExtension)){
loadMap(file.nameWithoutExtension(), file::read, true);
}
}catch(Exception e){
Log.err("Failed to load custom map file '{0}'!", file);
Log.err(e);
}
}
}else{
customMapNames = Settings.getJson("custom-maps", Array.class);
for(String name : customMapNames){
try{
String data = Settings.getString("map-data-" + name, "");
byte[] bytes = Base64Coder.decode(data);
loadMap(name, () -> new ByteArrayInputStream(bytes), true);
}catch(Exception e){
Log.err("Failed to load custom map '{0}'!", name);
Log.err(e);
}
}
}
}
/**
* Returns an input stream supplier for a given map name.
*/
private Supplier<InputStream> getStreamFor(String name){
if(!gwt){
return customMapDirectory.child(name + "." + mapExtension)::read;
}else{
String data = Settings.getString("map-data-" + name, "");
byte[] bytes = Base64Coder.decode(data);
return () -> new ByteArrayInputStream(bytes);
}
}
@Override
public void dispose(){
}
}
@@ -3,6 +3,7 @@ package io.anuke.mindustry.io;
import io.anuke.mindustry.core.Platform;
import io.anuke.mindustry.game.Difficulty;
import io.anuke.mindustry.game.GameMode;
import io.anuke.mindustry.maps.Map;
import java.util.Date;
-199
View File
@@ -1,199 +0,0 @@
package io.anuke.mindustry.io;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.game.Difficulty;
import io.anuke.mindustry.game.EventType.StateChangeEvent;
import io.anuke.mindustry.game.GameMode;
import io.anuke.ucore.core.Events;
import io.anuke.ucore.core.Settings;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.util.ThreadArray;
import java.io.IOException;
import static io.anuke.mindustry.Vars.*;
public class Saves{
private int nextSlot;
private Array<SaveSlot> saves = new ThreadArray<>();
private SaveSlot current;
private boolean saving;
private float time;
public Saves(){
Events.on(StateChangeEvent.class, (prev, state) -> {
if(state == State.menu){
threads.run(() -> current = null);
}
});
}
public void load(){
saves.clear();
for(int i = 0; i < saveSlots; i++){
if(SaveIO.isSaveValid(i)){
SaveSlot slot = new SaveSlot(i);
saves.add(slot);
slot.meta = SaveIO.getData(i);
nextSlot = i + 1;
}
}
}
public SaveSlot getCurrent(){
return current;
}
public void update(){
if(!state.is(State.menu) && !state.gameOver && current != null && current.isAutosave()){
time += Timers.delta();
if(time > Settings.getInt("saveinterval") * 60){
saving = true;
Timers.run(2f, () -> {
try{
SaveIO.saveToSlot(current.index);
current.meta = SaveIO.getData(current.index);
}catch(Exception e){
e.printStackTrace();
}
saving = false;
});
time = 0;
}
}else{
time = 0;
}
}
public void resetSave(){
current = null;
}
public boolean isSaving(){
return saving;
}
public boolean canAddSave(){
return nextSlot < saveSlots;
}
public void addSave(String name){
SaveSlot slot = new SaveSlot(nextSlot);
nextSlot++;
slot.setName(name);
saves.add(slot);
SaveIO.saveToSlot(slot.index);
slot.meta = SaveIO.getData(slot.index);
current = slot;
}
public SaveSlot importSave(FileHandle file) throws IOException{
SaveSlot slot = new SaveSlot(nextSlot);
slot.importFile(file);
nextSlot++;
slot.setName(file.nameWithoutExtension());
saves.add(slot);
slot.meta = SaveIO.getData(slot.index);
current = slot;
return slot;
}
public Array<SaveSlot> getSaveSlots(){
return saves;
}
public class SaveSlot{
public final int index;
SaveMeta meta;
public SaveSlot(int index){
this.index = index;
}
public void load(){
SaveIO.loadFromSlot(index);
meta = SaveIO.getData(index);
current = this;
}
public void save(){
SaveIO.saveToSlot(index);
meta = SaveIO.getData(index);
current = this;
}
public String getDate(){
return meta.date;
}
public Map getMap(){
return meta.map;
}
public String getName(){
return Settings.getString("save-" + index + "-name", "untittled");
}
public void setName(String name){
Settings.putString("save-" + index + "-name", name);
Settings.save();
}
public int getBuild(){
return meta.build;
}
public int getWave(){
return meta.wave;
}
public Difficulty getDifficulty(){
return meta.difficulty;
}
public GameMode getMode(){
return meta.mode;
}
public boolean isAutosave(){
return Settings.getBool("save-" + index + "-autosave", !gwt);
}
public void setAutosave(boolean save){
Settings.putBool("save-" + index + "-autosave", save);
Settings.save();
}
public void importFile(FileHandle file) throws IOException{
try{
file.copyTo(SaveIO.fileFor(index));
}catch(Exception e){
throw new IOException(e);
}
}
public void exportFile(FileHandle file) throws IOException{
try{
if(!file.extension().equals(saveExtension)){
file = file.parent().child(file.nameWithoutExtension() + "." + saveExtension);
}
SaveIO.fileFor(index).copyTo(file);
}catch(Exception e){
throw new IOException(e);
}
}
public void delete(){
SaveIO.fileFor(index).delete();
saves.removeValue(this, true);
if(this == current){
current = null;
}
}
}
}
+1 -3
View File
@@ -27,9 +27,7 @@ import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.*;
/**
* Class for specifying read/write methods for code generation.
*/
/** Class for specifying read/write methods for code generation.*/
public class TypeIO{
@WriteClass(Player.class)
@@ -1,35 +0,0 @@
package io.anuke.mindustry.io;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.PropertiesUtils;
import io.anuke.ucore.util.Strings;
import java.io.IOException;
public class Version{
public static String name;
public static String type;
public static String code;
public static int build = 0;
public static String buildName;
public static void init(){
try{
FileHandle file = Gdx.files.internal("version.properties");
ObjectMap<String, String> map = new ObjectMap<>();
PropertiesUtils.load(map, file.reader());
name = map.get("name");
type = map.get("version");
code = map.get("code");
build = Strings.canParseInt(map.get("build")) ? Integer.parseInt(map.get("build")) : -1;
buildName = build == -1 ? map.get("build") : "build " + build;
}catch(IOException e){
throw new RuntimeException(e);
}
}
}
@@ -9,9 +9,9 @@ import io.anuke.mindustry.entities.traits.TypeTrait;
import io.anuke.mindustry.game.Difficulty;
import io.anuke.mindustry.game.GameMode;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.io.Map;
import io.anuke.mindustry.maps.Map;
import io.anuke.mindustry.io.SaveFileVersion;
import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.game.Version;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.BlockPart;