Class SaveManager
java.lang.Object
it.polimi.ingsw.gc14.SaveManager
Handles persistence of the game model to and from disk.
The save file is stored at GameSaves/save.dat relative to the
directory containing the running JAR. The anchor class passed to the
constructor is used to resolve that directory at construction time.
All three operations (save(Game), load(), delete())
are independent and safe to call in any order; missing files are treated
as a normal condition (no save present) rather than an error.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSaveManager(Class<?> anchorClass) Constructs aSaveManagerwhose save file is located relative to the JAR directory of the given anchor class. -
Method Summary
-
Field Details
-
SAVE_RELATIVE_PATH
Relative sub-path of the save file inside the JAR directory.- See Also:
-
filePath
Absolute path of the save file, resolved once at construction.
-
-
Constructor Details
-
SaveManager
Constructs aSaveManagerwhose save file is located relative to the JAR directory of the given anchor class.- Parameters:
anchorClass- the class whose code-source location is used as the base directory for the save file.- Throws:
RuntimeException- if the JAR path cannot be resolved.
-
-
Method Details
-
save
Serializes the game model to disk, creating parent directories if needed.- Parameters:
game- the game model to persist.- Returns:
trueif the save succeeded,falseon I/O error.
-
load
Deserializes the game model from disk.- Returns:
- the saved
Gameinstance, ornullif no save file exists or an I/O error prevents reading. - Throws:
RuntimeException- if the serialized class cannot be found on the classpath (indicates a deployment mismatch).
-
delete
public boolean delete()Deletes the save file.- Returns:
trueif the file was deleted,falseotherwise (including when the file did not exist).
-