Class SaveManager

java.lang.Object
it.polimi.ingsw.gc14.SaveManager

public class SaveManager extends Object
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
    Modifier and Type
    Field
    Description
    private final Path
    Absolute path of the save file, resolved once at construction.
    private static final String
    Relative sub-path of the save file inside the JAR directory.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SaveManager(Class<?> anchorClass)
    Constructs a SaveManager whose save file is located relative to the JAR directory of the given anchor class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Deletes the save file.
    Deserializes the game model from disk.
    boolean
    save(Game game)
    Serializes the game model to disk, creating parent directories if needed.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SAVE_RELATIVE_PATH

      private static final String SAVE_RELATIVE_PATH
      Relative sub-path of the save file inside the JAR directory.
      See Also:
    • filePath

      private final Path filePath
      Absolute path of the save file, resolved once at construction.
  • Constructor Details

    • SaveManager

      public SaveManager(Class<?> anchorClass)
      Constructs a SaveManager whose 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

      public boolean save(Game game)
      Serializes the game model to disk, creating parent directories if needed.
      Parameters:
      game - the game model to persist.
      Returns:
      true if the save succeeded, false on I/O error.
    • load

      public Game load()
      Deserializes the game model from disk.
      Returns:
      the saved Game instance, or null if 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:
      true if the file was deleted, false otherwise (including when the file did not exist).