[gnome-2048] Move saving to file in Grid.



commit 6df8d3e30c06de6114e0c1d10facbb1dd6eec8b6
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Nov 13 19:58:32 2019 +0100

    Move saving to file in Grid.

 src/game.vala | 21 ++-------------------
 src/grid.vala | 36 ++++++++++++++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 21 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index f341c0a..1150777 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -154,30 +154,13 @@ private class Game : Object
 
     internal void save_game ()
     {
-        string contents = _grid.save ();
-        try {
-            DirUtils.create_with_parents (Path.get_dirname (_saved_path), 0775);
-            FileUtils.set_contents (_saved_path, contents);
-            debug ("game saved successfully");
-        } catch (FileError e) {
-            warning ("Failed to save game: %s", e.message);
-        }
+        _grid.save_game (_saved_path);
     }
 
     internal bool restore_game (ref GLib.Settings settings)
     {
-        string contents;
-        try {
-            FileUtils.get_contents (_saved_path, out contents);
-        } catch (FileError e) {
-            return false;
-        }
-
-        if (!_grid.load (ref contents))
-        {
-            warning ("Failed to restore game from saved file");
+        if (!_grid.restore_game (_saved_path))
             return false;
-        }
 
         score = _grid.get_score ();
 
diff --git a/src/grid.vala b/src/grid.vala
index 0fe9d9d..3e9356f 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -490,7 +490,7 @@ private class Grid : Object
     * * saving
     \*/
 
-    internal string save ()
+    internal string save () // internal for tests
     {
         string ret_string = @"$_rows $_cols\n";
         _convert_to_string (ref _grid, ref ret_string);
@@ -529,7 +529,7 @@ private class Grid : Object
     * * restoring
     \*/
 
-    internal bool load (ref string content) // TODO transform into a constructor
+    internal bool load (ref string content) // TODO transform into a constructor    // internal for tests
     {
         uint8 [,] grid = {{}};   // garbage
         if (!_load_from_string (ref content, ref grid))
@@ -621,6 +621,38 @@ private class Grid : Object
 
         return false;
     }
+
+    /*\
+    * * saving and restoring from file
+    \*/
+
+    internal void save_game (string path)
+    {
+        string contents = save ();
+        try
+        {
+            DirUtils.create_with_parents (Path.get_dirname (path), 0775);
+            FileUtils.set_contents (path, contents);
+            debug ("game saved successfully");
+        }
+        catch (FileError e) {
+            warning ("Failed to save game: %s", e.message);
+        }
+    }
+
+    internal bool restore_game (string path)
+    {
+        string content;
+
+        try { FileUtils.get_contents (path, out content); }
+        catch (FileError e) { return false; }
+
+        if (load (ref content))
+            return true;
+
+        warning ("Failed to restore game from saved file");
+        return false;
+    }
 }
 
 private struct GridPosition


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