Skip to content

Commit 7209435

Browse files
Merge pull request #504 from MovingBlocks/noContinueFromOld
disable continue from different major version
2 parents f552884 + f4582dc commit 7209435

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

engine/src/main/java/org/destinationsol/Const.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ public class Const {
3737
public static final float CAM_VIEW_DIST_JOURNEY = 8.6f;
3838
public static final float DEFAULT_AI_SPD = 4f;
3939
public static final float BIG_AI_SPD = 2f;
40+
41+
public static final String SAVE_FILE_NAME = "prevShip.ini";
42+
public static final String MERC_SAVE_FILE = "mercenaries.json";
43+
public static final String WORLD_SAVE_FILE_NAME = "world.json";
4044
}

engine/src/main/java/org/destinationsol/game/SaveManager.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.gson.JsonObject;
2424
import com.google.gson.JsonParser;
2525
import com.google.gson.stream.JsonReader;
26+
import org.destinationsol.Const;
2627
import org.destinationsol.IniReader;
2728
import org.destinationsol.common.SolRandom;
2829
import org.destinationsol.files.HullConfigManager;
@@ -47,9 +48,6 @@
4748
import java.util.List;
4849

4950
public class SaveManager {
50-
protected static final String SAVE_FILE_NAME = "prevShip.ini";
51-
protected static final String MERC_SAVE_FILE = "mercenaries.json";
52-
protected static final String WORLD_SAVE_FILE_NAME = "world.json";
5351

5452
private static Logger logger = LoggerFactory.getLogger(SaveManager.class);
5553

@@ -66,7 +64,8 @@ public static void writeShips(HullConfig hull, float money, List<SolItem> itemsL
6664

6765
String waypoints = waypointsToString(hero.getWaypoints());
6866

69-
IniReader.write(SAVE_FILE_NAME, "hull", hullName, "money", (int) money, "items", items, "x", pos.x, "y", pos.y, "waypoints", waypoints);
67+
IniReader.write(Const.SAVE_FILE_NAME, "hull", hullName, "money", (int) money, "items", items,
68+
"x", pos.x, "y", pos.y, "waypoints", waypoints, "version", Const.VERSION);
7069
}
7170

7271
private static String waypointsToString(ArrayList<Waypoint> waypoints) {
@@ -156,7 +155,7 @@ private static void writeMercs(Hero hero, HullConfigManager hullConfigManager) {
156155
// Using PrintWriter because it truncates the file if it exists or creates a new one if it doesn't
157156
// And truncation is good because we don't want dead mercs respawning
158157
try {
159-
writer = new PrintWriter(getResourcePath(MERC_SAVE_FILE), "UTF-8");
158+
writer = new PrintWriter(getResourcePath(Const.MERC_SAVE_FILE), "UTF-8");
160159
writer.write(stringToWrite);
161160
writer.close();
162161
} catch (FileNotFoundException | UnsupportedEncodingException e) {
@@ -189,17 +188,23 @@ public static boolean resourceExists(String fileName) {
189188
}
190189

191190
/**
192-
* Tests is the game has a previous ship (a game to continue)
191+
* Tests if the game has a compatible previous ship (a game to continue)
193192
*/
194-
public static boolean hasPrevShip(String fileName) {
195-
return resourceExists(fileName);
193+
public static boolean hasPreviousCompatibleShip() {
194+
if (!resourceExists(Const.SAVE_FILE_NAME)) {
195+
return false;
196+
}
197+
IniReader reader = new IniReader(Const.SAVE_FILE_NAME, null);
198+
String saveMajorVersion = reader.getString("version", "").split("\\.")[0];
199+
String gameMajorVersion = Const.VERSION.split("\\.")[0];
200+
return saveMajorVersion.equals(gameMajorVersion);
196201
}
197202

198203
/**
199204
* Load last saved ship from file
200205
*/
201206
public static ShipConfig readShip(HullConfigManager hullConfigs, ItemManager itemManager) {
202-
IniReader ir = new IniReader(SAVE_FILE_NAME, null);
207+
IniReader ir = new IniReader(Const.SAVE_FILE_NAME, null);
203208

204209
String hullName = ir.getString("hull", null);
205210
if (hullName == null) {
@@ -229,7 +234,7 @@ public static ShipConfig readShip(HullConfigManager hullConfigs, ItemManager ite
229234
*/
230235
public static void saveWorld(int numberOfSystems) {
231236
Long seed = SolRandom.getSeed();
232-
String fileName = SaveManager.getResourcePath(WORLD_SAVE_FILE_NAME);
237+
String fileName = SaveManager.getResourcePath(Const.WORLD_SAVE_FILE_NAME);
233238

234239
JsonObject world = new JsonObject();
235240
world.addProperty("seed", seed);
@@ -257,11 +262,11 @@ public static void saveWorld(int numberOfSystems) {
257262
* Load the last saved world from file, or returns null if there is no file
258263
*/
259264
public static WorldConfig loadWorld() {
260-
if (SaveManager.resourceExists(WORLD_SAVE_FILE_NAME)) {
265+
if (SaveManager.resourceExists(Const.WORLD_SAVE_FILE_NAME)) {
261266
WorldConfig config = new WorldConfig();
262267
JsonReader reader = null;
263268
try {
264-
reader = new com.google.gson.stream.JsonReader(new FileReader(SaveManager.getResourcePath(WORLD_SAVE_FILE_NAME)));
269+
reader = new com.google.gson.stream.JsonReader(new FileReader(SaveManager.getResourcePath(Const.WORLD_SAVE_FILE_NAME)));
265270
reader.setLenient(true); // without this it will fail with strange errors
266271
JsonObject world = new JsonParser().parse(reader).getAsJsonObject();
267272

engine/src/main/java/org/destinationsol/menu/NewGameScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class NewGameScreen extends SolUiBaseScreen {
5858

5959
@Override
6060
public void onAdd(SolApplication solApplication) {
61-
continueControl.setEnabled(SaveManager.hasPrevShip("prevShip.ini"));
61+
continueControl.setEnabled(SaveManager.hasPreviousCompatibleShip());
6262
}
6363

6464
@Override

0 commit comments

Comments
 (0)