2323import com .google .gson .JsonObject ;
2424import com .google .gson .JsonParser ;
2525import com .google .gson .stream .JsonReader ;
26+ import org .destinationsol .Const ;
2627import org .destinationsol .IniReader ;
2728import org .destinationsol .common .SolRandom ;
2829import org .destinationsol .files .HullConfigManager ;
4748import java .util .List ;
4849
4950public 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
0 commit comments