Formatting

This commit is contained in:
Anuken
2018-07-12 20:37:15 -04:00
parent a3bda3a941
commit baaeb229cf
379 changed files with 17470 additions and 16215 deletions
@@ -13,7 +13,7 @@ import java.util.Locale;
import static io.anuke.mindustry.Vars.headless;
public class BundleLoader {
public class BundleLoader{
public static void load(){
Settings.defaults("locale", "default");
@@ -27,10 +27,10 @@ public class BundleLoader {
return Locale.getDefault();
}else{
Locale lastLocale;
if (loc.contains("_")) {
if(loc.contains("_")){
String[] split = loc.split("_");
lastLocale = new Locale(split[0], split[1]);
} else {
}else{
lastLocale = new Locale(loc);
}
@@ -40,7 +40,7 @@ public class BundleLoader {
private static void loadBundle(){
I18NBundle.setExceptionOnMissingKey(false);
try {
try{
//try loading external bundle
FileHandle handle = Gdx.files.local("bundle");
@@ -51,7 +51,7 @@ public class BundleLoader {
if(!headless){
Timers.run(10f, () -> Vars.ui.showInfo("Note: You have successfully loaded an external translation bundle."));
}
}catch (Throwable e){
}catch(Throwable e){
//no external bundle found
FileHandle handle = Gdx.files.internal("bundles/bundle");
@@ -8,7 +8,7 @@ import io.anuke.ucore.function.Consumer;
import static io.anuke.mindustry.Vars.releasesURL;
public class Changelogs {
public class Changelogs{
public static void getChangelog(Consumer<Array<VersionInfo>> success, Consumer<Throwable> fail){
Net.http(releasesURL, "GET", result -> {
@@ -30,7 +30,7 @@ public class Changelogs {
public final String name, description;
public final int id, build;
public VersionInfo(String name, String description, int id, int build) {
public VersionInfo(String name, String description, int id, int build){
this.name = name;
this.description = description;
this.id = id;
@@ -38,7 +38,7 @@ public class Changelogs {
}
@Override
public String toString() {
public String toString(){
return "VersionInfo{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
+16 -6
View File
@@ -6,16 +6,26 @@ import io.anuke.ucore.function.Supplier;
import java.io.InputStream;
public class Map {
/**Internal map name. This is the filename, without any extensions.*/
public class Map{
/**
* Internal map name. This is the filename, without any extensions.
*/
public final String name;
/**Whether this is a custom map.*/
/**
* Whether this is a custom map.
*/
public final boolean custom;
/**Metadata. Author description, display name, etc.*/
/**
* Metadata. Author description, display name, etc.
*/
public final MapMeta meta;
/**Supplies a new input stream with the data of this map.*/
/**
* Supplies a new input stream with the data of this map.
*/
public final Supplier<InputStream> stream;
/**Preview texture.*/
/**
* Preview texture.
*/
public Texture texture;
public Map(String name, MapMeta meta, boolean custom, Supplier<InputStream> streamSupplier){
+28 -20
View File
@@ -18,8 +18,10 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**Reads and writes map files.*/
public class MapIO {
/**
* Reads and writes map files.
*/
public class MapIO{
private static final int version = 0;
private static IntIntMap defaultBlockMap = new IntIntMap();
@@ -36,8 +38,8 @@ public class MapIO {
TileDataMarker marker = data.newDataMarker();
Color color = new Color();
for(int y = 0; y < data.height(); y ++){
for(int x = 0; x < data.width(); x ++){
for(int y = 0; y < data.height(); y++){
for(int x = 0; x < data.width(); x++){
data.read(marker);
Block floor = Block.getByID(marker.floor);
Block wall = Block.getByID(marker.wall);
@@ -45,7 +47,7 @@ public class MapIO {
if(wallc == 0 && (wall.update || wall.solid || wall.breakable)) wallc = Team.all[marker.team].intColor;
wallc = wallc == 0 ? ColorMapper.getBlockColor(floor) : wallc;
if(marker.elevation > 0){
float scaling = 1f + marker.elevation/8f;
float scaling = 1f + marker.elevation / 8f;
color.set(wallc);
color.mul(scaling, scaling, scaling, 1f);
wallc = Color.rgba8888(color);
@@ -62,17 +64,17 @@ public class MapIO {
public static MapTileData readPixmap(Pixmap pixmap){
MapTileData data = new MapTileData(pixmap.getWidth(), pixmap.getHeight());
for(int x = 0; x < data.width(); x ++){
for(int y = 0; y < data.height(); y ++){
for(int x = 0; x < data.width(); x++){
for(int y = 0; y < data.height(); y++){
Block block = ColorMapper.getByColor(pixmap.getPixel(y, pixmap.getWidth() - 1 - x));
if(block == null){
data.write(x, y, DataPosition.floor, (byte)Blocks.stone.id);
data.write(x, y, DataPosition.floor, (byte) Blocks.stone.id);
}else{
data.write(x, y, DataPosition.floor, (byte)block.id);
data.write(x, y, DataPosition.floor, (byte) block.id);
}
data.write(x, y, DataPosition.wall, (byte)Blocks.air.id);
data.write(x, y, DataPosition.wall, (byte) Blocks.air.id);
}
}
@@ -94,25 +96,31 @@ public class MapIO {
ds.close();
}
/**Reads tile data, skipping meta.*/
public static MapTileData readTileData(DataInputStream stream, boolean readOnly) throws IOException {
/**
* Reads tile data, skipping meta.
*/
public static MapTileData readTileData(DataInputStream stream, boolean readOnly) throws IOException{
MapMeta meta = readMapMeta(stream);
return readTileData(stream, meta, readOnly);
}
/**Does not skip meta. Call after reading meta.*/
public static MapTileData readTileData(DataInputStream stream, MapMeta meta, boolean readOnly) throws IOException {
/**
* Does not skip meta. Call after reading meta.
*/
public static MapTileData readTileData(DataInputStream stream, MapMeta meta, boolean readOnly) throws IOException{
byte[] bytes = new byte[stream.available()];
stream.readFully(bytes);
return new MapTileData(bytes, meta.width, meta.height, meta.blockMap, readOnly);
}
/**Reads tile data, skipping meta tags.*/
/**
* Reads tile data, skipping meta tags.
*/
public static MapTileData readTileData(Map map, boolean readOnly){
try (DataInputStream ds = new DataInputStream(map.stream.get())){
try(DataInputStream ds = new DataInputStream(map.stream.get())){
return MapIO.readTileData(ds, readOnly);
}catch (IOException e){
}catch(IOException e){
throw new RuntimeException(e);
}
}
@@ -125,14 +133,14 @@ public class MapIO {
byte tagAmount = stream.readByte();
for(int i = 0; i < tagAmount; i ++){
for(int i = 0; i < tagAmount; i++){
String name = stream.readUTF();
String value = stream.readUTF();
tags.put(name, value);
}
short blocks = stream.readShort();
for(int i = 0; i < blocks; i ++){
for(int i = 0; i < blocks; i++){
short id = stream.readShort();
String name = stream.readUTF();
Block block = Block.getByName(name);
@@ -151,7 +159,7 @@ public class MapIO {
public static void writeMapMeta(DataOutputStream stream, MapMeta meta) throws IOException{
stream.writeInt(meta.version);
stream.writeByte((byte)meta.tags.size);
stream.writeByte((byte) meta.tags.size);
for(Entry<String, String> entry : meta.tags.entries()){
stream.writeUTF(entry.key);
+2 -2
View File
@@ -3,13 +3,13 @@ package io.anuke.mindustry.io;
import com.badlogic.gdx.utils.IntIntMap;
import com.badlogic.gdx.utils.ObjectMap;
public class MapMeta {
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) {
public MapMeta(int version, ObjectMap<String, String> tags, int width, int height, IntIntMap blockMap){
this.version = version;
this.tags = tags;
this.width = width;
+24 -12
View File
@@ -5,13 +5,15 @@ import io.anuke.ucore.util.Bits;
import java.nio.ByteBuffer;
public class MapTileData {
/**Tile size: 4 bytes. <br>
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>*/
* 4: elevation <br>
*/
private final static int TILE_SIZE = 5;
private final ByteBuffer buffer;
@@ -36,7 +38,7 @@ public class MapTileData {
this.readOnly = readOnly;
if(mapping != null && !readOnly){
for(int i = 0; i < width * height; i ++){
for(int i = 0; i < width * height; i++){
TileDataMarker marker = new TileDataMarker();
read(marker);
buffer.position(i * TILE_SIZE);
@@ -59,28 +61,38 @@ public class MapTileData {
return height;
}
/**Write a byte to a specific position.*/
/**
* 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.*/
/**
* 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.*/
/**
* Reads and returns the next tile data.
*/
public TileDataMarker read(TileDataMarker marker){
marker.read(buffer);
return marker;
}
/**Writes this tile data marker.*/
/**
* Writes this tile data marker.
*/
public void write(TileDataMarker marker){
marker.write(buffer);
}
/**Sets read position to the specified coordinates*/
/**
* Sets read position to the specified coordinates
*/
public void position(int x, int y){
buffer.position((x + width * y) * TILE_SIZE);
}
@@ -93,7 +105,7 @@ public class MapTileData {
floor, wall, link, rotationTeam, elevation
}
public class TileDataMarker {
public class TileDataMarker{
public byte floor, wall;
public byte link;
public byte rotation;
@@ -110,8 +122,8 @@ public class MapTileData {
team = Bits.getRightByte(rt);
if(map != null){
floor = (byte)map.get(floor, floor);
wall = (byte)map.get(wall, wall);
floor = (byte) map.get(floor, floor);
wall = (byte) map.get(wall, wall);
}
}
+149 -121
View File
@@ -17,145 +17,171 @@ 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;
/**
* 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;
/**
* 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(){
public Maps(){
}
/**Returns a list of all maps, including custom ones.*/
public Array<Map> all(){
return allMaps;
}
/**
* 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 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 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);
}
/**
* 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);
}
/**
* 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();
}
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();
}
/**
* 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);
}
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);
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);
}
}
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;
}
/**
* 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);
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();
}
}
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())) {
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)));
}
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){
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){
}catch(Exception e){
Log.err("Failed to load custom map file '{0}'!", file);
Log.err(e);
}
@@ -169,7 +195,7 @@ public class Maps implements Disposable{
String data = Settings.getString("map-data-" + name, "");
byte[] bytes = Base64Coder.decode(data);
loadMap(name, () -> new ByteArrayInputStream(bytes), true);
}catch (Exception e){
}catch(Exception e){
Log.err("Failed to load custom map '{0}'!", name);
Log.err(e);
}
@@ -177,19 +203,21 @@ public class Maps implements Disposable{
}
}
/**Returns an input stream supplier for a given map name.*/
/**
* 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);
}
}
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() {
@Override
public void dispose(){
}
}
}
@@ -6,7 +6,7 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public abstract class SaveFileVersion {
public abstract class SaveFileVersion{
public final int version;
public SaveFileVersion(int version){
@@ -23,5 +23,6 @@ public abstract class SaveFileVersion {
}
public abstract void read(DataInputStream stream) throws IOException;
public abstract void write(DataOutputStream stream) throws IOException;
}
+129 -129
View File
@@ -15,148 +15,148 @@ import java.util.zip.InflaterInputStream;
import static io.anuke.mindustry.Vars.*;
public class SaveIO{
public static final IntMap<SaveFileVersion> versions = new IntMap<>();
public static final Array<SaveFileVersion> versionArray = Array.with(
new Save16()
);
public static final IntMap<SaveFileVersion> versions = new IntMap<>();
public static final Array<SaveFileVersion> versionArray = Array.with(
new Save16()
);
static{
for(SaveFileVersion version : versionArray){
versions.put(version.version, version);
}
}
static{
for(SaveFileVersion version : versionArray){
versions.put(version.version, version);
}
}
public static void saveToSlot(int slot){
if(gwt){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
write(stream);
Settings.putString("save-"+slot+"-data", new String(Base64Coder.encode(stream.toByteArray())));
Settings.save();
}else{
FileHandle file = fileFor(slot);
boolean exists = file.exists();
if(exists) file.moveTo(file.sibling(file.name() + "-backup." + file.extension()));
try {
write(fileFor(slot));
}catch (Exception e){
if(exists) file.sibling(file.name() + "-backup." + file.extension()).moveTo(file);
throw new RuntimeException(e);
}
}
}
public static void loadFromSlot(int slot){
if(gwt){
String string = Settings.getString("save-"+slot+"-data", "");
ByteArrayInputStream stream = new ByteArrayInputStream(Base64Coder.decode(string));
load(stream);
}else{
load(fileFor(slot));
}
}
public static void saveToSlot(int slot){
if(gwt){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
write(stream);
Settings.putString("save-" + slot + "-data", new String(Base64Coder.encode(stream.toByteArray())));
Settings.save();
}else{
FileHandle file = fileFor(slot);
boolean exists = file.exists();
if(exists) file.moveTo(file.sibling(file.name() + "-backup." + file.extension()));
try{
write(fileFor(slot));
}catch(Exception e){
if(exists) file.sibling(file.name() + "-backup." + file.extension()).moveTo(file);
throw new RuntimeException(e);
}
}
}
public static DataInputStream getSlotStream(int slot){
if(gwt){
String string = Settings.getString("save-"+slot+"-data", "");
byte[] bytes = Base64Coder.decode(string);
return new DataInputStream(new ByteArrayInputStream(bytes));
}else{
return new DataInputStream(new InflaterInputStream(fileFor(slot).read()));
}
}
public static boolean isSaveValid(int slot){
try {
return isSaveValid(getSlotStream(slot));
}catch (Exception e){
return false;
}
}
public static void loadFromSlot(int slot){
if(gwt){
String string = Settings.getString("save-" + slot + "-data", "");
ByteArrayInputStream stream = new ByteArrayInputStream(Base64Coder.decode(string));
load(stream);
}else{
load(fileFor(slot));
}
}
public static boolean isSaveValid(FileHandle file){
return isSaveValid(new DataInputStream(new InflaterInputStream(file.read())));
}
public static DataInputStream getSlotStream(int slot){
if(gwt){
String string = Settings.getString("save-" + slot + "-data", "");
byte[] bytes = Base64Coder.decode(string);
return new DataInputStream(new ByteArrayInputStream(bytes));
}else{
return new DataInputStream(new InflaterInputStream(fileFor(slot).read()));
}
}
public static boolean isSaveValid(DataInputStream stream){
public static boolean isSaveValid(int slot){
try{
return isSaveValid(getSlotStream(slot));
}catch(Exception e){
return false;
}
}
try{
int version = stream.readInt();
SaveFileVersion ver = versions.get(version);
ver.getData(stream);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public static boolean isSaveValid(FileHandle file){
return isSaveValid(new DataInputStream(new InflaterInputStream(file.read())));
}
public static SaveMeta getData(int slot){
return getData(getSlotStream(slot));
}
public static SaveMeta getData(DataInputStream stream){
try{
int version = stream.readInt();
SaveMeta meta = versions.get(version).getData(stream);
stream.close();
return meta;
}catch (IOException e){
throw new RuntimeException(e);
}
}
public static FileHandle fileFor(int slot){
return saveDirectory.child(slot + "." + Vars.saveExtension);
}
public static boolean isSaveValid(DataInputStream stream){
public static void write(FileHandle file){
write(file.write(false));
}
try{
int version = stream.readInt();
SaveFileVersion ver = versions.get(version);
ver.getData(stream);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public static void write(OutputStream os){
DataOutputStream stream;
try{
stream = new DataOutputStream(new DeflaterOutputStream(os));
getVersion().write(stream);
stream.close();
}catch (Exception e){
throw new RuntimeException(e);
}
}
public static SaveMeta getData(int slot){
return getData(getSlotStream(slot));
}
public static void load(FileHandle file){
try {
load(new InflaterInputStream(file.read()));
}catch (RuntimeException e){
e.printStackTrace();
FileHandle backup = file.sibling(file.name() + "-backup." + file.extension());
if(backup.exists()){
load(new InflaterInputStream(backup.read()));
}
}
}
public static SaveMeta getData(DataInputStream stream){
public static void load(InputStream is){
logic.reset();
try{
int version = stream.readInt();
SaveMeta meta = versions.get(version).getData(stream);
stream.close();
return meta;
}catch(IOException e){
throw new RuntimeException(e);
}
}
DataInputStream stream;
try{
stream = new DataInputStream(is);
int version = stream.readInt();
SaveFileVersion ver = versions.get(version);
public static FileHandle fileFor(int slot){
return saveDirectory.child(slot + "." + Vars.saveExtension);
}
ver.read(stream);
public static void write(FileHandle file){
write(file.write(false));
}
stream.close();
}catch (Exception e){
throw new RuntimeException(e);
}
}
public static void write(OutputStream os){
DataOutputStream stream;
public static SaveFileVersion getVersion(){
return versionArray.peek();
}
try{
stream = new DataOutputStream(new DeflaterOutputStream(os));
getVersion().write(stream);
stream.close();
}catch(Exception e){
throw new RuntimeException(e);
}
}
public static void load(FileHandle file){
try{
load(new InflaterInputStream(file.read()));
}catch(RuntimeException e){
e.printStackTrace();
FileHandle backup = file.sibling(file.name() + "-backup." + file.extension());
if(backup.exists()){
load(new InflaterInputStream(backup.read()));
}
}
}
public static void load(InputStream is){
logic.reset();
DataInputStream stream;
try{
stream = new DataInputStream(is);
int version = stream.readInt();
SaveFileVersion ver = versions.get(version);
ver.read(stream);
stream.close();
}catch(Exception e){
throw new RuntimeException(e);
}
}
public static SaveFileVersion getVersion(){
return versionArray.peek();
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ import java.util.Date;
import static io.anuke.mindustry.Vars.world;
public class SaveMeta {
public class SaveMeta{
public int version;
public String date;
public GameMode mode;
+14 -14
View File
@@ -13,7 +13,7 @@ import java.io.IOException;
import static io.anuke.mindustry.Vars.*;
public class Saves {
public class Saves{
private int nextSlot;
private Array<SaveSlot> saves = new ThreadArray<>();
private SaveSlot current;
@@ -22,7 +22,7 @@ public class Saves {
public void load(){
saves.clear();
for(int i = 0; i < saveSlots; i ++){
for(int i = 0; i < saveSlots; i++){
if(SaveIO.isSaveValid(i)){
SaveSlot slot = new SaveSlot(i);
saves.add(slot);
@@ -32,7 +32,7 @@ public class Saves {
}
}
public SaveSlot getCurrent() {
public SaveSlot getCurrent(){
return current;
}
@@ -43,14 +43,14 @@ public class Saves {
if(!state.is(State.menu) && !state.gameOver && current != null && current.isAutosave()){
time += Timers.delta();
if(time > Settings.getInt("saveinterval")*60) {
if(time > Settings.getInt("saveinterval") * 60){
saving = true;
Timers.run(2f, () -> {
try {
try{
SaveIO.saveToSlot(current.index);
current.meta = SaveIO.getData(current.index);
}catch (Exception e){
}catch(Exception e){
e.printStackTrace();
}
saving = false;
@@ -77,7 +77,7 @@ public class Saves {
public void addSave(String name){
SaveSlot slot = new SaveSlot(nextSlot);
nextSlot ++;
nextSlot++;
slot.setName(name);
saves.add(slot);
SaveIO.saveToSlot(slot.index);
@@ -88,7 +88,7 @@ public class Saves {
public SaveSlot importSave(FileHandle file) throws IOException{
SaveSlot slot = new SaveSlot(nextSlot);
slot.importFile(file);
nextSlot ++;
nextSlot++;
slot.setName(file.nameWithoutExtension());
saves.add(slot);
slot.meta = SaveIO.getData(slot.index);
@@ -129,11 +129,11 @@ public class Saves {
}
public String getName(){
return Settings.getString("save-"+index+"-name", "untittled");
return Settings.getString("save-" + index + "-name", "untittled");
}
public void setName(String name){
Settings.putString("save-"+index+"-name", name);
Settings.putString("save-" + index + "-name", name);
Settings.save();
}
@@ -150,18 +150,18 @@ public class Saves {
}
public boolean isAutosave(){
return Settings.getBool("save-"+index+"-autosave", !gwt);
return Settings.getBool("save-" + index + "-autosave", !gwt);
}
public void setAutosave(boolean save){
Settings.putBool("save-"+index + "-autosave", 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){
}catch(Exception e){
throw new IOException(e);
}
}
@@ -172,7 +172,7 @@ public class Saves {
file = file.parent().child(file.nameWithoutExtension() + "." + saveExtension);
}
SaveIO.fileFor(index).copyTo(file);
}catch (Exception e){
}catch(Exception e){
throw new IOException(e);
}
}
+32 -30
View File
@@ -27,14 +27,16 @@ import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.*;
/**Class for specifying read/write methods for code generation.*/
public class TypeIO {
/**
* Class for specifying read/write methods for code generation.
*/
public class TypeIO{
@WriteClass(Player.class)
public static void writePlayer(ByteBuffer buffer, Player player){
if(player == null){
buffer.putInt(-1);
}else {
}else{
buffer.putInt(player.id);
}
}
@@ -47,7 +49,7 @@ public class TypeIO {
@WriteClass(Unit.class)
public static void writeUnit(ByteBuffer buffer, Unit unit){
buffer.put((byte)unit.getGroup().getID());
buffer.put((byte) unit.getGroup().getID());
buffer.putInt(unit.getID());
}
@@ -55,12 +57,12 @@ public class TypeIO {
public static Unit readUnit(ByteBuffer buffer){
byte gid = buffer.get();
int id = buffer.getInt();
return (Unit)Entities.getGroup(gid).getByID(id);
return (Unit) Entities.getGroup(gid).getByID(id);
}
@WriteClass(ShooterTrait.class)
public static void writeShooter(ByteBuffer buffer, ShooterTrait trait){
buffer.put((byte)trait.getGroup().getID());
buffer.put((byte) trait.getGroup().getID());
buffer.putInt(trait.getID());
}
@@ -85,10 +87,10 @@ public class TypeIO {
@WriteClass(CarriableTrait.class)
public static void writeCarriable(ByteBuffer buffer, CarriableTrait unit){
if(unit == null){
buffer.put((byte)-1);
buffer.put((byte) -1);
return;
}
buffer.put((byte)unit.getGroup().getID());
buffer.put((byte) unit.getGroup().getID());
buffer.putInt(unit.getID());
}
@@ -99,16 +101,16 @@ public class TypeIO {
return null;
}
int id = buffer.getInt();
return (CarriableTrait)Entities.getGroup(gid).getByID(id);
return (CarriableTrait) Entities.getGroup(gid).getByID(id);
}
@WriteClass(CarryTrait.class)
public static void writeCarry(ByteBuffer buffer, CarryTrait unit){
if(unit == null){
buffer.put((byte)-1);
buffer.put((byte) -1);
return;
}
buffer.put((byte)unit.getGroup().getID());
buffer.put((byte) unit.getGroup().getID());
buffer.putInt(unit.getID());
}
@@ -119,12 +121,12 @@ public class TypeIO {
return null;
}
int id = buffer.getInt();
return (CarryTrait)Entities.getGroup(gid).getByID(id);
return (CarryTrait) Entities.getGroup(gid).getByID(id);
}
@WriteClass(BaseUnit.class)
public static void writeBaseUnit(ByteBuffer buffer, BaseUnit unit){
buffer.put((byte)unitGroups[unit.getTeam().ordinal()].getID());
buffer.put((byte) unitGroups[unit.getTeam().ordinal()].getID());
buffer.putInt(unit.getID());
}
@@ -132,7 +134,7 @@ public class TypeIO {
public static BaseUnit writeBaseUnit(ByteBuffer buffer){
byte gid = buffer.get();
int id = buffer.getInt();
return (BaseUnit)Entities.getGroup(gid).getByID(id);
return (BaseUnit) Entities.getGroup(gid).getByID(id);
}
@WriteClass(Tile.class)
@@ -147,7 +149,7 @@ public class TypeIO {
@WriteClass(Block.class)
public static void writeBlock(ByteBuffer buffer, Block block){
buffer.put((byte)block.id);
buffer.put((byte) block.id);
}
@ReadClass(Block.class)
@@ -157,7 +159,7 @@ public class TypeIO {
@WriteClass(KickReason.class)
public static void writeKick(ByteBuffer buffer, KickReason reason){
buffer.put((byte)reason.ordinal());
buffer.put((byte) reason.ordinal());
}
@ReadClass(KickReason.class)
@@ -167,7 +169,7 @@ public class TypeIO {
@WriteClass(Team.class)
public static void writeTeam(ByteBuffer buffer, Team reason){
buffer.put((byte)reason.ordinal());
buffer.put((byte) reason.ordinal());
}
@ReadClass(Team.class)
@@ -177,7 +179,7 @@ public class TypeIO {
@WriteClass(AdminAction.class)
public static void writeAction(ByteBuffer buffer, AdminAction reason){
buffer.put((byte)reason.ordinal());
buffer.put((byte) reason.ordinal());
}
@ReadClass(AdminAction.class)
@@ -187,7 +189,7 @@ public class TypeIO {
@WriteClass(Effect.class)
public static void writeEffect(ByteBuffer buffer, Effect effect){
buffer.putShort((short)effect.id);
buffer.putShort((short) effect.id);
}
@ReadClass(Effect.class)
@@ -227,7 +229,7 @@ public class TypeIO {
@WriteClass(Liquid.class)
public static void writeLiquid(ByteBuffer buffer, Liquid liquid){
buffer.put((byte)liquid.id);
buffer.put((byte) liquid.id);
}
@ReadClass(Liquid.class)
@@ -247,7 +249,7 @@ public class TypeIO {
@WriteClass(BulletType.class)
public static void writeBulletType(ByteBuffer buffer, BulletType type){
buffer.put((byte)type.id);
buffer.put((byte) type.id);
}
@ReadClass(BulletType.class)
@@ -257,7 +259,7 @@ public class TypeIO {
@WriteClass(Item.class)
public static void writeItem(ByteBuffer buffer, Item item){
buffer.put((byte)item.id);
buffer.put((byte) item.id);
}
@ReadClass(Item.class)
@@ -267,7 +269,7 @@ public class TypeIO {
@WriteClass(Recipe.class)
public static void writeRecipe(ByteBuffer buffer, Recipe recipe){
buffer.put((byte)recipe.id);
buffer.put((byte) recipe.id);
}
@ReadClass(Recipe.class)
@@ -277,19 +279,19 @@ public class TypeIO {
@WriteClass(String.class)
public static void writeString(ByteBuffer buffer, String string){
if(string != null) {
if(string != null){
byte[] bytes = string.getBytes();
buffer.putShort((short) bytes.length);
buffer.put(bytes);
}else{
buffer.putShort((short)-1);
buffer.putShort((short) -1);
}
}
@ReadClass(String.class)
public static String readString(ByteBuffer buffer){
short length = buffer.getShort();
if(length != -1) {
if(length != -1){
byte[] bytes = new byte[length];
buffer.get(bytes);
return new String(bytes);
@@ -300,7 +302,7 @@ public class TypeIO {
@WriteClass(byte[].class)
public static void writeBytes(ByteBuffer buffer, byte[] bytes){
buffer.putShort((short)bytes.length);
buffer.putShort((short) bytes.length);
buffer.put(bytes);
}
@@ -315,10 +317,10 @@ public class TypeIO {
@WriteClass(TraceInfo.class)
public static void writeTrace(ByteBuffer buffer, TraceInfo info){
buffer.putInt(info.playerid);
buffer.putShort((short)info.ip.getBytes().length);
buffer.putShort((short) info.ip.getBytes().length);
buffer.put(info.ip.getBytes());
buffer.put(info.modclient ? (byte)1 : 0);
buffer.put(info.android ? (byte)1 : 0);
buffer.put(info.modclient ? (byte) 1 : 0);
buffer.put(info.android ? (byte) 1 : 0);
buffer.putInt(info.totalBlocksBroken);
buffer.putInt(info.structureBlocksBroken);
+3 -3
View File
@@ -8,7 +8,7 @@ import io.anuke.ucore.util.Strings;
import java.io.IOException;
public class Version {
public class Version{
public static String name;
public static String type;
public static String code;
@@ -16,7 +16,7 @@ public class Version {
public static String buildName;
public static void init(){
try {
try{
FileHandle file = Gdx.files.internal("version.properties");
ObjectMap<String, String> map = new ObjectMap<>();
@@ -28,7 +28,7 @@ public class Version {
build = Strings.canParseInt(map.get("build")) ? Integer.parseInt(map.get("build")) : -1;
buildName = build == -1 ? map.get("build") : "build " + build;
}catch (IOException e){
}catch(IOException e){
throw new RuntimeException(e);
}
}
@@ -27,14 +27,14 @@ import java.io.IOException;
import static io.anuke.mindustry.Vars.state;
import static io.anuke.mindustry.Vars.world;
public class Save16 extends SaveFileVersion {
public class Save16 extends SaveFileVersion{
public Save16(){
super(16);
}
@Override
public void read(DataInputStream stream) throws IOException {
public void read(DataInputStream stream) throws IOException{
/*long loadTime = */
stream.readLong();
@@ -61,7 +61,7 @@ public class Save16 extends SaveFileVersion {
IntMap<Block> blockMap = new IntMap<>();
for(int i = 0; i < blocksize; i ++){
for(int i = 0; i < blocksize; i++){
String name = stream.readUTF();
int id = stream.readShort();
@@ -72,9 +72,9 @@ public class Save16 extends SaveFileVersion {
byte groups = stream.readByte();
for (int i = 0; i < groups; i++) {
for(int i = 0; i < groups; i++){
int amount = stream.readInt();
for (int j = 0; j < amount; j++) {
for(int j = 0; j < amount; j++){
byte typeid = stream.readByte();
SaveTrait trait = (SaveTrait) TypeTrait.getTypeByID(typeid).get();
trait.readSave(stream);
@@ -94,8 +94,8 @@ public class Save16 extends SaveFileVersion {
Tile[][] tiles = world.createTiles(width, height);
for (int i = 0; i < width * height; i++) {
int x = i % width, y = i /width;
for(int i = 0; i < width * height; i++){
int x = i % width, y = i / width;
byte floorid = stream.readByte();
byte wallid = stream.readByte();
byte elevation = stream.readByte();
@@ -103,9 +103,9 @@ public class Save16 extends SaveFileVersion {
Tile tile = new Tile(x, y, floorid, wallid);
tile.elevation = elevation;
if (wallid == Blocks.blockpart.id) {
if(wallid == Blocks.blockpart.id){
tile.link = stream.readByte();
}else if (tile.entity != null) {
}else if(tile.entity != null){
byte tr = stream.readByte();
short health = stream.readShort();
@@ -118,10 +118,10 @@ public class Save16 extends SaveFileVersion {
tile.entity.health = health;
tile.setRotation(rotation);
if (tile.entity.items != null) tile.entity.items.read(stream);
if (tile.entity.power != null) tile.entity.power.read(stream);
if (tile.entity.liquids != null) tile.entity.liquids.read(stream);
if (tile.entity.cons != null) tile.entity.cons.read(stream);
if(tile.entity.items != null) tile.entity.items.read(stream);
if(tile.entity.power != null) tile.entity.power.read(stream);
if(tile.entity.liquids != null) tile.entity.liquids.read(stream);
if(tile.entity.cons != null) tile.entity.cons.read(stream);
tile.entity.read(stream);
@@ -132,7 +132,7 @@ public class Save16 extends SaveFileVersion {
}else if(wallid == 0){
int consecutives = stream.readUnsignedByte();
for (int j = i + 1; j < i + 1 + consecutives; j++) {
for(int j = i + 1; j < i + 1 + consecutives; j++){
int newx = j % width, newy = j / width;
Tile newTile = new Tile(newx, newy, floorid, wallid);
newTile.elevation = elevation;
@@ -149,7 +149,7 @@ public class Save16 extends SaveFileVersion {
}
@Override
public void write(DataOutputStream stream) throws IOException {
public void write(DataOutputStream stream) throws IOException{
//--META--
stream.writeInt(version); //version id
stream.writeLong(TimeUtils.millis()); //last saved
@@ -168,7 +168,7 @@ public class Save16 extends SaveFileVersion {
stream.writeInt(Block.all().size);
for(int i = 0; i < Block.all().size; i ++){
for(int i = 0; i < Block.all().size; i++){
Block block = Block.all().get(i);
stream.writeUTF(block.name);
stream.writeShort(block.id);
@@ -181,7 +181,7 @@ public class Save16 extends SaveFileVersion {
for(EntityGroup<?> group : Entities.getAllGroups()){
if(!group.isEmpty() && group.all().get(0) instanceof SaveTrait){
groups ++;
groups++;
}
}
@@ -191,8 +191,8 @@ public class Save16 extends SaveFileVersion {
if(!group.isEmpty() && group.all().get(0) instanceof SaveTrait){
stream.writeInt(group.size());
for(Entity entity : group.all()){
stream.writeByte(((SaveTrait)entity).getTypeID());
((SaveTrait)entity).writeSave(stream);
stream.writeByte(((SaveTrait) entity).getTypeID());
((SaveTrait) entity).writeSave(stream);
}
}
}
@@ -205,7 +205,7 @@ public class Save16 extends SaveFileVersion {
stream.writeShort(world.width());
stream.writeShort(world.height());
for (int i = 0; i < world.width() * world.height(); i++) {
for(int i = 0; i < world.width() * world.height(); i++){
Tile tile = world.tile(i);
stream.writeByte(tile.getFloorID());
@@ -216,7 +216,7 @@ public class Save16 extends SaveFileVersion {
stream.writeByte(tile.link);
}else if(tile.entity != null){
stream.writeByte(Bits.packByte(tile.getTeamID(), tile.getRotation())); //team + rotation
stream.writeShort((short)tile.entity.health); //health
stream.writeShort((short) tile.entity.health); //health
if(tile.entity.items != null) tile.entity.items.write(stream);
if(tile.entity.power != null) tile.entity.power.write(stream);
@@ -227,14 +227,14 @@ public class Save16 extends SaveFileVersion {
}else if(tile.getWallID() == 0){
int consecutives = 0;
for (int j = i + 1; j < world.width() * world.height() && consecutives < 255; j++) {
for(int j = i + 1; j < world.width() * world.height() && consecutives < 255; j++){
Tile nextTile = world.tile(j);
if(nextTile.getFloorID() != tile.getFloorID() || nextTile.getWallID() != 0 || nextTile.elevation != tile.elevation){
break;
}
consecutives ++;
consecutives++;
}
stream.writeByte(consecutives);