[gnome-games/wip/exalm/runner-refactor: 28/56] snapshot-manager: Move snapshot creation from RetroRunner



commit f1fe06ecd1011ec3106f96a21867a0686e94b588
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Mar 6 17:55:00 2020 +0500

    snapshot-manager: Move snapshot creation from RetroRunner

 src/core/snapshot-manager.vala | 81 +++++++++++++++++++++++++++++++++++
 src/retro/retro-runner.vala    | 95 +-----------------------------------------
 2 files changed, 82 insertions(+), 94 deletions(-)
---
diff --git a/src/core/snapshot-manager.vala b/src/core/snapshot-manager.vala
index 9d3df0ec..6371849e 100644
--- a/src/core/snapshot-manager.vala
+++ b/src/core/snapshot-manager.vala
@@ -1,6 +1,10 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 public class Games.SnapshotManager : Object {
+       private const int MAX_AUTOSAVES = 5;
+
+       public delegate void SnapshotFunc (Savestate snapshot) throws Error;
+
        private Game game;
        private string core_id;
 
@@ -45,4 +49,81 @@ public class Games.SnapshotManager : Object {
        public Savestate[] get_snapshots () {
                return snapshots;
        }
+
+       private void trim_autosaves () {
+               int n_autosaves = 1;
+
+               foreach (var snapshot in snapshots) {
+                       if (!snapshot.is_automatic)
+                               continue;
+
+                       if (n_autosaves < MAX_AUTOSAVES) {
+                               n_autosaves++;
+                               continue;
+                       }
+
+                       snapshot.delete_from_disk ();
+               }
+       }
+
+       private string create_new_snapshot_name () throws Error {
+               var list = new List<int> ();
+               var regex = new Regex (_("New snapshot %s").printf ("([1-9]\\d*)"));
+
+               foreach (var snapshot in snapshots) {
+                       if (snapshot.is_automatic)
+                               continue;
+
+                       MatchInfo match_info = null;
+
+                       if (regex.match (snapshot.name, 0, out match_info)) {
+                               var number = match_info.fetch (1);
+                               list.prepend (int.parse (number));
+                       }
+               }
+
+               list.sort ((a, b) => a - b);
+
+               // Find the next available name for a new manual snapshot
+               int next_number = 1;
+               foreach (var number in list) {
+                       if (number != next_number)
+                               break;
+
+                       next_number++;
+               }
+
+               return _("New snapshot %s").printf (next_number.to_string ());
+       }
+
+       public Savestate create_snapshot (bool is_automatic, SnapshotFunc save_callback) throws Error {
+               // Make room for the new automatic snapshot
+               if (is_automatic)
+                       trim_autosaves ();
+
+               var creation_date = new DateTime.now ();
+               var path = Path.build_filename (get_snapshots_dir (),
+                                               creation_date.to_string ());
+
+               var snapshot = Savestate.create_empty (game, core_id, path);
+
+               snapshot.is_automatic = is_automatic;
+               snapshot.name = is_automatic ? null : create_new_snapshot_name ();
+               snapshot.creation_date = creation_date;
+
+               save_callback (snapshot);
+               snapshot.write_metadata ();
+
+               snapshot = snapshot.move_to (path);
+
+               Savestate[] new_snapshots = {};
+
+               new_snapshots += snapshot;
+               foreach (var s in snapshots)
+                       new_snapshots += s;
+
+               snapshots = new_snapshots;
+
+               return snapshot;
+       }
 }
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index 4ee4e56d..4be554e6 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -1,8 +1,6 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 public class Games.RetroRunner : Object, Runner {
-       private const int MAX_AUTOSAVES = 5;
-
        public bool can_fullscreen {
                get { return true; }
        }
@@ -440,17 +438,6 @@ public class Games.RetroRunner : Object, Runner {
                }
        }
 
-       private string get_game_savestates_dir_path () throws Error {
-               // Get the savestates directory of the game
-               var data_dir_path = Application.get_data_dir ();
-               var savestates_dir_path = Path.build_filename (data_dir_path, "savestates");
-               var uid = game.uid;
-               var core_id = get_core_id ();
-               var core_id_prefix = core_id.replace (".libretro", "");
-
-               return Path.build_filename (savestates_dir_path, @"$uid-$core_id_prefix");
-       }
-
        // Returns the created Savestate or null if the Savestate couldn't be created
        // Currently the callers are the DisplayView and the SavestatesList
        // In the future we might want to throw Errors from here in case there is
@@ -465,7 +452,7 @@ public class Games.RetroRunner : Object, Runner {
                        new_savestate_created ();
 
                try {
-                       return create_savestate (is_automatic);
+                       return snapshot_manager.create_snapshot (is_automatic, save_to_snapshot);
                }
                catch (Error e) {
                        critical ("Failed to create snapshot: %s", e.message);
@@ -474,39 +461,6 @@ public class Games.RetroRunner : Object, Runner {
                }
        }
 
-       private Savestate create_savestate (bool is_automatic) throws Error {
-               // Make room for the new automatic savestate
-               if (is_automatic)
-                       trim_autosaves ();
-
-               var creation_date = new DateTime.now ();
-               var path = Path.build_filename (get_game_savestates_dir_path (),
-                                               creation_date.to_string ());
-
-               var snapshot = Savestate.create_empty (game, get_core_id (), path);
-
-               snapshot.is_automatic = is_automatic;
-               snapshot.name = is_automatic ? null : create_new_savestate_name ();
-               snapshot.creation_date = creation_date;
-
-               save_to_snapshot (snapshot);
-               snapshot.write_metadata ();
-
-               snapshot = snapshot.move_to (path);
-
-               // Update the game_savestates array
-               // Insert the new savestate at the beginning of the array since it's the latest savestate
-               Savestate[] new_game_savestates = {};
-
-               new_game_savestates += snapshot;
-               foreach (var existing_savestate in game_savestates)
-                       new_game_savestates += existing_savestate;
-
-               game_savestates = new_game_savestates;
-
-               return snapshot;
-       }
-
        public void delete_savestate (Savestate savestate) {
                Savestate[] new_game_savestates = {};
 
@@ -545,53 +499,6 @@ public class Games.RetroRunner : Object, Runner {
                return core;
        }
 
-       private string create_new_savestate_name () throws Error {
-               var list = new List<int>();
-               var regex = new Regex (_("New snapshot %s").printf ("([1-9]\\d*)"));
-
-               foreach (var savestate in game_savestates) {
-                       if (savestate.is_automatic)
-                               continue;
-
-                       MatchInfo match_info = null;
-
-                       if (regex.match (savestate.name, 0, out match_info)) {
-                               var number = match_info.fetch (1);
-                               list.prepend (int.parse (number));
-                       }
-               }
-
-               list.sort ((a, b) => a - b);
-
-               // Find the next available name for a new manual savestate
-               var next_number = 1;
-               foreach (var number in list) {
-                       if (number == next_number)
-                               next_number++;
-                       else
-                               break;
-               }
-
-               return _("New snapshot %s").printf (next_number.to_string ());
-       }
-
-       // Decide if there are too many automatic savestates and delete the
-       // last ones if so
-       private void trim_autosaves () {
-               // A new automatic savestate will be created right after this call,
-               // so counter starts from 1
-               int autosaves_counter = 1;
-
-               foreach (var savestate in game_savestates) {
-                       if (savestate.is_automatic) {
-                               if (autosaves_counter < MAX_AUTOSAVES)
-                                       autosaves_counter++;
-                               else
-                                       savestate.delete_from_disk ();
-                       }
-               }
-       }
-
        private void save_screenshot (string path) throws Error {
                var pixbuf = current_state_pixbuf;
                if (pixbuf == null)


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