Files
2026-06-19 22:57:43 +02:00

216 lines
6.8 KiB
HTML

<!DOCTYPE html>
<html id="htmlId">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Coverage Report > SaveManager</title>
<style type="text/css">
@import "../../css/coverage.css";
@import "../../css/idea.min.css";
</style>
<script type="text/javascript" src="../../js/highlight.min.js"></script>
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>
<body>
<div class="content">
<div class="breadCrumbs">
Current scope: <a href="../../index.html">all classes</a>
<span class="separator">|</span>
<a href="../index.html">it.polimi.ingsw.gc14</a>
</div>
<h1>Coverage Summary for Class: SaveManager (it.polimi.ingsw.gc14)</h1>
<table class="coverageStats">
<tr>
<th class="name">Class</th>
<th class="coverageStat
">
Class, %
</th>
<th class="coverageStat
">
Method, %
</th>
<th class="coverageStat
">
Line, %
</th>
</tr>
<tr>
<td class="name">SaveManager</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/1)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/4)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/22)
</span>
</td>
</tr>
</table>
<br/>
<br/>
<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package it.polimi.ingsw.gc14;
&nbsp;
&nbsp;import it.polimi.ingsw.gc14.Model.Game;
&nbsp;
&nbsp;import java.io.BufferedInputStream;
&nbsp;import java.io.BufferedOutputStream;
&nbsp;import java.io.FileInputStream;
&nbsp;import java.io.FileNotFoundException;
&nbsp;import java.io.FileOutputStream;
&nbsp;import java.io.IOException;
&nbsp;import java.io.ObjectInputStream;
&nbsp;import java.io.ObjectOutputStream;
&nbsp;import java.net.URISyntaxException;
&nbsp;import java.nio.file.Files;
&nbsp;import java.nio.file.Path;
&nbsp;import java.nio.file.Paths;
&nbsp;
&nbsp;/**
&nbsp; * Handles persistence of the game model to and from disk.
&nbsp; *
&nbsp; * &lt;p&gt;The save file is stored at {@code GameSaves/save.dat} relative to the
&nbsp; * directory containing the running JAR. The anchor class passed to the
&nbsp; * constructor is used to resolve that directory at construction time.
&nbsp; *
&nbsp; * &lt;p&gt;All three operations ({@link #save}, {@link #load}, {@link #delete})
&nbsp; * are independent and safe to call in any order; missing files are treated
&nbsp; * as a normal condition (no save present) rather than an error.
&nbsp; */
&nbsp;public class SaveManager {
&nbsp;
&nbsp; /** Relative sub-path of the save file inside the JAR directory. */
&nbsp; private static final String SAVE_RELATIVE_PATH = &quot;GameSaves/save.dat&quot;;
&nbsp;
&nbsp; /** Absolute path of the save file, resolved once at construction. */
&nbsp; private final Path filePath;
&nbsp;
&nbsp; /**
&nbsp; * Constructs a {@code SaveManager} whose save file is located relative
&nbsp; * to the JAR directory of the given anchor class.
&nbsp; *
&nbsp; * @param anchorClass the class whose code-source location is used as
&nbsp; * the base directory for the save file.
&nbsp; * @throws RuntimeException if the JAR path cannot be resolved.
&nbsp; */
<b class="nc">&nbsp; public SaveManager(Class&lt;?&gt; anchorClass) {</b>
&nbsp; try {
<b class="nc">&nbsp; Path jarDir = Paths.get(</b>
<b class="nc">&nbsp; anchorClass.getProtectionDomain().getCodeSource().getLocation().toURI()</b>
<b class="nc">&nbsp; ).getParent();</b>
<b class="nc">&nbsp; this.filePath = jarDir.resolve(SAVE_RELATIVE_PATH);</b>
&nbsp; } catch (URISyntaxException e) {
<b class="nc">&nbsp; throw new RuntimeException(&quot;Could not resolve save file path&quot;, e);</b>
&nbsp; }
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Serializes the game model to disk, creating parent directories if needed.
&nbsp; *
&nbsp; * @param game the game model to persist.
&nbsp; * @return {@code true} if the save succeeded, {@code false} on I/O error.
&nbsp; */
&nbsp; public boolean save(Game game) {
&nbsp; try {
<b class="nc">&nbsp; Files.createDirectories(filePath.getParent());</b>
<b class="nc">&nbsp; try (ObjectOutputStream oos = new ObjectOutputStream(</b>
<b class="nc">&nbsp; new BufferedOutputStream(new FileOutputStream(filePath.toFile())))) {</b>
<b class="nc">&nbsp; oos.writeObject(game);</b>
<b class="nc">&nbsp; return true;</b>
&nbsp; }
&nbsp; } catch (IOException e) {
<b class="nc">&nbsp; e.printStackTrace();</b>
<b class="nc">&nbsp; return false;</b>
&nbsp; }
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Deserializes the game model from disk.
&nbsp; *
&nbsp; * @return the saved {@link Game} instance, or {@code null} if no save file
&nbsp; * exists or an I/O error prevents reading.
&nbsp; * @throws RuntimeException if the serialized class cannot be found on
&nbsp; * the classpath (indicates a deployment mismatch).
&nbsp; */
&nbsp; public Game load() {
<b class="nc">&nbsp; try (ObjectInputStream ois = new ObjectInputStream(</b>
<b class="nc">&nbsp; new BufferedInputStream(new FileInputStream(filePath.toFile())))) {</b>
<b class="nc">&nbsp; return (Game) ois.readObject();</b>
&nbsp; } catch (FileNotFoundException e) {
<b class="nc">&nbsp; return null;</b>
&nbsp; } catch (IOException e) {
<b class="nc">&nbsp; e.printStackTrace();</b>
<b class="nc">&nbsp; return null;</b>
&nbsp; } catch (ClassNotFoundException e) {
<b class="nc">&nbsp; throw new RuntimeException(&quot;Save file references an unknown class&quot;, e);</b>
&nbsp; }
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Deletes the save file.
&nbsp; *
&nbsp; * @return {@code true} if the file was deleted, {@code false} otherwise
&nbsp; * (including when the file did not exist).
&nbsp; */
&nbsp; public boolean delete() {
&nbsp; try {
<b class="nc">&nbsp; return Files.deleteIfExists(filePath);</b>
&nbsp; } catch (IOException e) {
<b class="nc">&nbsp; return false;</b>
&nbsp; }
&nbsp; }
&nbsp;}
</code>
</pre>
</div>
<script type="text/javascript">
(function() {
var msie = false, msie9 = false;
/*@cc_on
msie = true;
@if (@_jscript_version >= 9)
msie9 = true;
@end
@*/
if (!msie || msie && msie9) {
hljs.highlightAll()
hljs.initLineNumbersOnLoad();
}
})();
</script>
<div class="footer">
<div style="float:right;">generated on 2026-06-19 22:53</div>
</div>
</body>
</html>