summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2022-10-06 17:17:46 +0600
committerilotterytea <iltsu@alright.party>2022-10-06 17:17:46 +0600
commite8ab2aff11aa1da59968c9f06f96194e2f5e501e (patch)
tree2b7d723a41e97568144e85ff7c5abe817066395f
parent54594c18eebb0f6a2d758cb920f71ff007eef436 (diff)
new game data system that based on json
-rw-r--r--core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java98
1 files changed, 81 insertions, 17 deletions
diff --git a/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java b/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java
index a10d98a..3e64c45 100644
--- a/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java
+++ b/core/src/com/ilotterytea/maxoning/utils/serialization/GameDataSystem.java
@@ -1,35 +1,99 @@
package com.ilotterytea.maxoning.utils.serialization;
+import com.badlogic.gdx.utils.Null;
+import com.google.gson.Gson;
import com.ilotterytea.maxoning.MaxonConstants;
-import com.ilotterytea.maxoning.player.MaxonPlayer;
+import com.ilotterytea.maxoning.player.MaxonSavegame;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.*;
+import java.util.ArrayList;
+/**
+ * External game data system control.
+ * @author NotDankEnough
+ * @since Alpha 1.0
+ */
public class GameDataSystem {
private static final File dir = new File(MaxonConstants.GAME_SAVEGAME_FOLDER);
- private static final File file = new File(dir.getPath() + "/savegame.sav");
+ private static final Gson gson = new Gson();
+ private static final Logger log = LoggerFactory.getLogger(GameDataSystem.class.getSimpleName());
- public static boolean exists() { return file.exists(); }
+ /**
+ * Get all savefiles from savegame directory (/.Maxoning/savegames/)
+ * @return Array of MaxonSavegames
+ * @see MaxonSavegame
+ */
+ public static ArrayList<MaxonSavegame> getSavegames() {
+ ArrayList<MaxonSavegame> saves = new ArrayList<>();
+ File[] files = dir.listFiles();
- public static void SaveData(MaxonPlayer player) throws IOException {
- if (!dir.exists()) {
- dir.mkdirs();
+ assert files != null;
+ for (File file : files) {
+ try {
+
+ FileInputStream fis = new FileInputStream(file);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+
+ MaxonSavegame sav = gson.fromJson(ois.readUTF(), MaxonSavegame.class);
+ saves.add(sav);
+
+ ois.close();
+ fis.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
- FileOutputStream fo = new FileOutputStream(file);
- ObjectOutputStream out = new ObjectOutputStream(fo);
- out.writeObject(player);
- out.close();
+ return saves;
+ }
+
+ /**
+ * Convert <b>MaxonSavegame</b> class to <b>JSON</b> string and write in UTF-8 encoding (I'm sorry, encryption enjoyers).
+ * @param savegame Save game object.
+ * @param file_name File name.
+ * @see MaxonSavegame
+ */
+ public static void save(@NotNull MaxonSavegame savegame, @NotNull String file_name) {
+ try {
+ log.info("Saving the game...");
+ FileOutputStream fos = new FileOutputStream(String.format("%s/%s", dir.getAbsolutePath(), file_name));
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+
+ oos.writeUTF(gson.toJson(savegame));
+ oos.close();
+ log.info(String.format("Success! Savegame located at %s/%s", dir.getAbsolutePath(), file_name));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
- public static MaxonPlayer LoadData() throws IOException, ClassNotFoundException {
- FileInputStream fi = new FileInputStream(file);
- ObjectInputStream oi = new ObjectInputStream(fi);
+ /**
+ * Reading a <b>JSON</b> string from the file and convert to <b>MaxonSavegame</b> class.
+ * @param file_name File name. If null - it will get the first file by last modified time.
+ * @return Filled <b>MaxonSavegame</b> class
+ * @see MaxonSavegame
+ */
+ public static MaxonSavegame load(@Null String file_name) {
+ MaxonSavegame sav = new MaxonSavegame();
+
+ if (new File(dir.getAbsolutePath() + "/" + file_name).exists()) {
+ try {
+ log.info(String.format("Trying to get the savegame at %s/%s...", dir.getAbsolutePath(), file_name));
+ FileInputStream fis = new FileInputStream(String.format("%s/%s", dir.getAbsolutePath(), file_name));
+ ObjectInputStream oos = new ObjectInputStream(fis);
- MaxonPlayer pl = (MaxonPlayer) oi.readObject();
+ sav = gson.fromJson(oos.readUTF(), MaxonSavegame.class);
+ oos.close();
+
+ log.info(String.format("Successfully loaded the savegame from %s/%s!", dir.getAbsolutePath(), file_name));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
- oi.close();
- fi.close();
- return pl;
+ return sav;
}
}