[gnome-games/wip/exalm/runner-refactor: 18/58] core: Introduce SnapshotManager



commit 752e9767d2f4f72902430367b29df680619f3133
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Mar 6 16:03:29 2020 +0500

    core: Introduce SnapshotManager

 src/core/snapshot-manager.vala | 48 ++++++++++++++++++++++++++++++++++++++++++
 src/meson.build                |  1 +
 2 files changed, 49 insertions(+)
---
diff --git a/src/core/snapshot-manager.vala b/src/core/snapshot-manager.vala
new file mode 100644
index 00000000..9d3df0ec
--- /dev/null
+++ b/src/core/snapshot-manager.vala
@@ -0,0 +1,48 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+public class Games.SnapshotManager : Object {
+       private Game game;
+       private string core_id;
+
+       private Savestate[] snapshots;
+
+       public SnapshotManager (Game game, string core_id) throws Error {
+               this.game = game;
+               this.core_id = core_id;
+
+               var dir_path = get_snapshots_dir ();
+               var dir_file = File.new_for_path (dir_path);
+
+               if (!dir_file.query_exists ()) {
+                       dir_file.make_directory_with_parents ();
+
+                       snapshots = {};
+                       return;
+               }
+
+               var dir = Dir.open (dir_path);
+
+               snapshots = {};
+               string snapshot_name = null;
+
+               while ((snapshot_name = dir.read_name ()) != null) {
+                       var snapshot_path = Path.build_filename (dir_path, snapshot_name);
+                       snapshots += Savestate.load (game.platform, core_id, snapshot_path);
+               }
+
+               qsort_with_data (snapshots, sizeof (Savestate), Savestate.compare);
+       }
+
+       private string get_snapshots_dir () throws Error {
+               var uid = game.uid;
+               var core_id_prefix = core_id.replace (".libretro", "");
+
+               return Path.build_filename (Application.get_data_dir (),
+                                           "savestates",
+                                           @"$uid-$core_id_prefix");
+       }
+
+       public Savestate[] get_snapshots () {
+               return snapshots;
+       }
+}
diff --git a/src/meson.build b/src/meson.build
index 354dfee0..f5d53eee 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -39,6 +39,7 @@ vala_sources = [
   'core/runner-error.vala',
   'core/runner-factory.vala',
   'core/savestate.vala',
+  'core/snapshot-manager.vala',
   'core/title.vala',
   'core/uid.vala',
   'core/uri-game-factory.vala',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]