216 lines
6.8 KiB
HTML
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"> package it.polimi.ingsw.gc14;
|
|
|
|
import it.polimi.ingsw.gc14.Model.Game;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.net.URISyntaxException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
|
|
/**
|
|
* Handles persistence of the game model to and from disk.
|
|
*
|
|
* <p>The save file is stored at {@code 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.
|
|
*
|
|
* <p>All three operations ({@link #save}, {@link #load}, {@link #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.
|
|
*/
|
|
public class SaveManager {
|
|
|
|
/** Relative sub-path of the save file inside the JAR directory. */
|
|
private static final String SAVE_RELATIVE_PATH = "GameSaves/save.dat";
|
|
|
|
/** Absolute path of the save file, resolved once at construction. */
|
|
private final Path filePath;
|
|
|
|
/**
|
|
* Constructs a {@code SaveManager} whose save file is located relative
|
|
* to the JAR directory of the given anchor class.
|
|
*
|
|
* @param 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.
|
|
*/
|
|
<b class="nc"> public SaveManager(Class<?> anchorClass) {</b>
|
|
try {
|
|
<b class="nc"> Path jarDir = Paths.get(</b>
|
|
<b class="nc"> anchorClass.getProtectionDomain().getCodeSource().getLocation().toURI()</b>
|
|
<b class="nc"> ).getParent();</b>
|
|
<b class="nc"> this.filePath = jarDir.resolve(SAVE_RELATIVE_PATH);</b>
|
|
} catch (URISyntaxException e) {
|
|
<b class="nc"> throw new RuntimeException("Could not resolve save file path", e);</b>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Serializes the game model to disk, creating parent directories if needed.
|
|
*
|
|
* @param game the game model to persist.
|
|
* @return {@code true} if the save succeeded, {@code false} on I/O error.
|
|
*/
|
|
public boolean save(Game game) {
|
|
try {
|
|
<b class="nc"> Files.createDirectories(filePath.getParent());</b>
|
|
<b class="nc"> try (ObjectOutputStream oos = new ObjectOutputStream(</b>
|
|
<b class="nc"> new BufferedOutputStream(new FileOutputStream(filePath.toFile())))) {</b>
|
|
<b class="nc"> oos.writeObject(game);</b>
|
|
<b class="nc"> return true;</b>
|
|
}
|
|
} catch (IOException e) {
|
|
<b class="nc"> e.printStackTrace();</b>
|
|
<b class="nc"> return false;</b>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deserializes the game model from disk.
|
|
*
|
|
* @return the saved {@link Game} instance, or {@code 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).
|
|
*/
|
|
public Game load() {
|
|
<b class="nc"> try (ObjectInputStream ois = new ObjectInputStream(</b>
|
|
<b class="nc"> new BufferedInputStream(new FileInputStream(filePath.toFile())))) {</b>
|
|
<b class="nc"> return (Game) ois.readObject();</b>
|
|
} catch (FileNotFoundException e) {
|
|
<b class="nc"> return null;</b>
|
|
} catch (IOException e) {
|
|
<b class="nc"> e.printStackTrace();</b>
|
|
<b class="nc"> return null;</b>
|
|
} catch (ClassNotFoundException e) {
|
|
<b class="nc"> throw new RuntimeException("Save file references an unknown class", e);</b>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes the save file.
|
|
*
|
|
* @return {@code true} if the file was deleted, {@code false} otherwise
|
|
* (including when the file did not exist).
|
|
*/
|
|
public boolean delete() {
|
|
try {
|
|
<b class="nc"> return Files.deleteIfExists(filePath);</b>
|
|
} catch (IOException e) {
|
|
<b class="nc"> return false;</b>
|
|
}
|
|
}
|
|
}
|
|
</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>
|