Skip to content

Commit

Permalink
Unified settings/save file for Gradle and IntelliJ
Browse files Browse the repository at this point in the history
  • Loading branch information
vampcat committed Jun 28, 2017
1 parent de1bba3 commit 6bc267e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 134 deletions.
17 changes: 13 additions & 4 deletions desktop/src/main/java/org/destinationsol/desktop/SolDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ public static void main(String[] argv) {
private static class MyReader implements SolFileReader {
@Override
public Path create(String fileName, List<String> lines) {
String path;
if (DebugOptions.DEV_ROOT_PATH != null) {
fileName = DebugOptions.DEV_ROOT_PATH + fileName;
path = DebugOptions.DEV_ROOT_PATH;
} else {
path = "src/main/resources/";
}
Path file = Paths.get(fileName);
path += fileName;

Path file = Paths.get(path);
try {
java.nio.file.Files.write(file, lines, Charset.forName("UTF-8"));
} catch (IOException e) {
Expand All @@ -98,14 +103,18 @@ public Path create(String fileName, List<String> lines) {

@Override
public List<String> read(String fileName) {
String path;
if (DebugOptions.DEV_ROOT_PATH != null) {
fileName = DebugOptions.DEV_ROOT_PATH + fileName;
path = DebugOptions.DEV_ROOT_PATH;
} else {
path = "src/main/resources/";
}
path += fileName;

ArrayList<String> lines = new ArrayList<>();

try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
Expand Down
4 changes: 2 additions & 2 deletions engine/src/main/java/org/destinationsol/GameOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public class GameOptions {
private int controllerButtonUp;
private int controllerButtonDown;

private SortedSet<String> supportedResolutions = new TreeSet<String>();
private SortedSet<String> supportedResolutions = new TreeSet<>();
private Iterator<String> resolutionIterator = null;

public GameOptions(boolean mobile, SolFileReader reader) {
IniReader r = new IniReader(FILE_NAME, reader, false);
IniReader r = new IniReader(FILE_NAME, reader);
x = r.getInt("x", 800);
y = r.getInt("y", 600);
fullscreen = r.getBoolean("fullscreen", false);
Expand Down
39 changes: 27 additions & 12 deletions engine/src/main/java/org/destinationsol/IniReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
package org.destinationsol;

import com.badlogic.gdx.files.FileHandle;
import org.destinationsol.files.FileManager;
import org.destinationsol.game.DebugOptions;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class IniReader {

private final HashMap<String, String> myVals;

public IniReader(String fileName, SolFileReader reader, boolean readOnly) {
public IniReader(String fileName, SolFileReader reader) {
myVals = new HashMap<>();
List<String> lines = reader != null ? reader.read(fileName) : fileToLines(fileName, readOnly);
List<String> lines = reader != null ? reader.read(fileName) : fileToLines(fileName);
initValueMap(lines);
}

Expand All @@ -57,7 +58,16 @@ public static void write(String fileName, Object... keysVals) {
sb.append(second ? '\n' : '=');
second = !second;
}
FileHandle file = FileManager.getInstance().getDynamicFile(fileName);

String path;
if (DebugOptions.DEV_ROOT_PATH != null) {
path = DebugOptions.DEV_ROOT_PATH;
} else {
path = "src/main/resources/";
}
path += fileName;

FileHandle file = new FileHandle(Paths.get(path).toFile());
file.writeString(sb.toString(), false);
}

Expand All @@ -77,18 +87,23 @@ private void initValueMap(List<String> lines) {
}
}

private List<String> fileToLines(String fileName, boolean readOnly) {
FileManager.FileLocation accessType = readOnly ? FileManager.FileLocation.STATIC_FILES : FileManager.FileLocation.DYNAMIC_FILES;
FileHandle fh = FileManager.getInstance().getFile(fileName, accessType);
private List<String> fileToLines(String fileName) {
String path;
if (DebugOptions.DEV_ROOT_PATH != null) {
path = DebugOptions.DEV_ROOT_PATH;
} else {
path = "src/main/resources/";
}
path += fileName;

FileHandle file = new FileHandle(Paths.get(path).toFile());

ArrayList<String> res = new ArrayList<>();
if (!fh.exists()) {
if (!file.exists()) {
return res;
}

for (String s : fh.readString().split("\n")) {
res.add(s);
}
Collections.addAll(res, file.readString().split("\n"));

return res;
}
Expand Down
112 changes: 0 additions & 112 deletions engine/src/main/java/org/destinationsol/files/FileManager.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class DebugOptions {
public static MissingResourceAction MISSING_PHYSICS_ACTION;

public static void read(SolFileReader reader) {
IniReader r = new IniReader("debugOptions.ini", reader, true);
IniReader r = new IniReader("debugOptions.ini", reader);

EMULATE_MOBILE = r.getBoolean("emulateMobile", EMULATE_MOBILE);
SPAWN_PLACE = r.getString("spawnPlace", SPAWN_PLACE);
Expand Down
15 changes: 12 additions & 3 deletions engine/src/main/java/org/destinationsol/game/SaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.destinationsol.game;

import com.badlogic.gdx.files.FileHandle;
import org.destinationsol.IniReader;
import org.destinationsol.files.FileManager;
import org.destinationsol.files.HullConfigManager;
import org.destinationsol.game.item.Gun;
import org.destinationsol.game.item.ItemManager;
import org.destinationsol.game.item.SolItem;
import org.destinationsol.game.ship.hulls.HullConfig;

import java.nio.file.Paths;
import java.util.ArrayList;

public class SaveManager {
Expand All @@ -49,11 +50,19 @@ public static void writeShip(HullConfig hull, float money, ArrayList<SolItem> it
}

public static boolean hasPrevShip() {
return FileManager.getInstance().getDynamicFile(FILE_NAME).exists();
String path;
if (DebugOptions.DEV_ROOT_PATH != null) {
path = DebugOptions.DEV_ROOT_PATH;
} else {
path = "src/main/resources/";
}
path += FILE_NAME;

return new FileHandle(Paths.get(path).toFile()).exists();
}

public static ShipConfig readShip(HullConfigManager hullConfigs, ItemManager itemManager) {
IniReader ir = new IniReader(FILE_NAME, null, false);
IniReader ir = new IniReader(FILE_NAME, null);

String hullName = ir.getString("hull", null);
if (hullName == null) {
Expand Down

0 comments on commit 6bc267e

Please sign in to comment.