[gnome-games/wip/exalm/3ds: 13/21] Implement global snapshot for cores that don't support savestates




commit 8a448f584438d709679b78a8eb91b81d89a5b71e
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sun Nov 8 16:04:52 2020 +0500

    Implement global snapshot for cores that don't support savestates
    
    This will be useful for cores like Citra.

 src/core/snapshot-manager.vala |  3 +++
 src/retro/retro-runner.vala    | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)
---
diff --git a/src/core/snapshot-manager.vala b/src/core/snapshot-manager.vala
index ceea640b..652a35d7 100644
--- a/src/core/snapshot-manager.vala
+++ b/src/core/snapshot-manager.vala
@@ -30,6 +30,9 @@ public class Games.SnapshotManager : Object {
                string snapshot_name = null;
 
                while ((snapshot_name = dir.read_name ()) != null) {
+                       if (snapshot_name == "global")
+                               continue;
+
                        var snapshot_path = Path.build_filename (dir_path, snapshot_name);
                        snapshots += Snapshot.load (game.platform, core_id, snapshot_path);
                }
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index 12114104..96bcbee8 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -218,7 +218,19 @@ public class Games.RetroRunner : Object, Runner {
                        var snapshot = snapshot_manager.get_latest_snapshot ();
 
                        tmp_save_dir = create_tmp_save_dir ();
-                       if (snapshot != null)
+                       if (!supports_snapshots) {
+                               var path = get_fallback_save_directory_path ();
+
+                               var save_ram_path = Path.build_filename (path, "save");
+                               var save_dir = File.new_for_path (Path.build_filename (path, "save-dir"));
+
+                               if (FileUtils.test (save_ram_path, FileTest.EXISTS) &&
+                                   core.get_memory_size (Retro.MemoryType.SAVE_RAM) > 0)
+                                       core.load_memory (Retro.MemoryType.SAVE_RAM, save_ram_path);
+
+                               FileOperations.copy_contents (save_dir, File.new_for_path (tmp_save_dir));
+                       }
+                       else if (snapshot != null)
                                snapshot.copy_save_dir_to (tmp_save_dir);
 
                        prepare_core ();
@@ -271,10 +283,34 @@ public class Games.RetroRunner : Object, Runner {
                running = false;
        }
 
+       private string get_fallback_save_directory_path () {
+               var uid = game.uid;
+               var core_id_prefix = get_core_id ().replace (".libretro", "");
+
+               return Path.build_filename (Application.get_data_dir (),
+                                           "savestates",
+                                           @"$uid-$core_id_prefix",
+                                           "global");
+       }
+
        public void stop () {
                if (!core_loaded)
                        return;
 
+               if (!supports_snapshots) {
+                       var path = get_fallback_save_directory_path ();
+
+                       if (core.get_memory_size (Retro.MemoryType.SAVE_RAM) > 0)
+                               core.save_memory (Retro.MemoryType.SAVE_RAM,
+                                                 Path.build_filename (path, "save"));
+
+                       var tmp_dir = File.new_for_path (tmp_save_dir);
+                       var dest_dir = File.new_for_path (Path.build_filename (path, "save-dir"));
+                       if (dest_dir.query_exists ())
+                               FileOperations.delete_files (dest_dir, {});
+                       FileOperations.copy_contents (tmp_dir, dest_dir);
+               }
+
                game.update_last_played ();
                deinit ();
                stopped ();


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