[gnome-games] retro-runner: Turn '*_path' props into getter methods
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] retro-runner: Turn '*_path' props into getter methods
- Date: Fri, 25 Mar 2016 06:44:12 +0000 (UTC)
commit d1bc4efac0a1dba58fd55cf26f64e277dc2fcad4
Author: Adrien Plazas <kekun plazas laposte net>
Date: Tue Mar 22 19:15:50 2016 +0100
retro-runner: Turn '*_path' props into getter methods
This allows the evaluation of the paths to throw errors, which is needed
to allow lazy computation of the UID used in the paths as it can throw
errors, and hence helping to construct a RetroRunner earlier with no
performance impact.
This is needed to create a generic game type.
src/retro/retro-runner.vala | 95 +++++++++++++++++++++++++------------------
1 files changed, 55 insertions(+), 40 deletions(-)
---
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index 116b2e2..e8852f6 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -3,48 +3,17 @@
public class Games.RetroRunner : Object, Runner {
public bool can_resume {
get {
- var file = File.new_for_path (snapshot_path);
+ try {
+ var snapshot_path = get_snapshot_path ();
+ var file = File.new_for_path (snapshot_path);
- return file.query_exists ();
- }
- }
-
- private string _save_path;
- private string save_path {
- get {
- if (_save_path != null)
- return _save_path;
-
- var dir = Application.get_saves_dir ();
- _save_path = @"$dir/$uid.save";
-
- return _save_path;
- }
- }
-
- private string _snapshot_path;
- private string snapshot_path {
- get {
- if (_snapshot_path != null)
- return _snapshot_path;
-
- var dir = Application.get_snapshots_dir ();
- _snapshot_path = @"$dir/$uid.snapshot";
-
- return _snapshot_path;
- }
- }
-
- private string _screenshot_path;
- private string screenshot_path {
- get {
- if (_screenshot_path != null)
- return _screenshot_path;
-
- var dir = Application.get_snapshots_dir ();
- _screenshot_path = @"$dir/$uid.png";
+ return file.query_exists ();
+ }
+ catch (Error e) {
+ warning (e.message);
+ }
- return _screenshot_path;
+ return false;
}
}
@@ -60,6 +29,10 @@ public class Games.RetroRunner : Object, Runner {
private Gtk.EventBox widget;
+ private string save_path;
+ private string snapshot_path;
+ private string screenshot_path;
+
private string uid;
private bool _running;
@@ -225,6 +198,16 @@ public class Games.RetroRunner : Object, Runner {
save_screenshot ();
}
+ private string get_save_path () throws Error {
+ if (save_path != null)
+ return save_path;
+
+ var dir = Application.get_saves_dir ();
+ save_path = @"$dir/$uid.save";
+
+ return save_path;
+ }
+
private void save_ram () throws Error{
var save = core.get_memory (Retro.MemoryType.SAVE_RAM);
if (save.length == 0)
@@ -233,10 +216,14 @@ public class Games.RetroRunner : Object, Runner {
var dir = Application.get_saves_dir ();
try_make_dir (dir);
+ var save_path = get_save_path ();
+
FileUtils.set_data (save_path, save);
}
private void load_ram () throws Error {
+ var save_path = get_save_path ();
+
if (!FileUtils.test (save_path, FileTest.EXISTS))
return;
@@ -250,6 +237,16 @@ public class Games.RetroRunner : Object, Runner {
core.set_memory (Retro.MemoryType.SAVE_RAM, data);
}
+ private string get_snapshot_path () throws Error {
+ if (snapshot_path != null)
+ return snapshot_path;
+
+ var dir = Application.get_snapshots_dir ();
+ snapshot_path = @"$dir/$uid.snapshot";
+
+ return snapshot_path;
+ }
+
private void save_snapshot () throws Error {
var size = core.serialize_size ();
var buffer = new uint8[size];
@@ -260,10 +257,14 @@ public class Games.RetroRunner : Object, Runner {
var dir = Application.get_snapshots_dir ();
try_make_dir (dir);
+ var snapshot_path = get_snapshot_path ();
+
FileUtils.set_data (snapshot_path, buffer);
}
private void load_snapshot () throws Error {
+ var snapshot_path = get_snapshot_path ();
+
if (!FileUtils.test (snapshot_path, FileTest.EXISTS))
return;
@@ -278,15 +279,29 @@ public class Games.RetroRunner : Object, Runner {
throw new RetroError.COULDNT_LOAD_SNAPSHOT ("Couldn't load snapshot.");
}
+ private string get_screenshot_path () throws Error {
+ if (screenshot_path != null)
+ return screenshot_path;
+
+ var dir = Application.get_snapshots_dir ();
+ screenshot_path = @"$dir/$uid.png";
+
+ return screenshot_path;
+ }
+
private void save_screenshot () throws Error {
var pixbuf = video.pixbuf;
if (pixbuf == null)
return;
+ var screenshot_path = get_screenshot_path ();
+
pixbuf.save (screenshot_path, "png");
}
private void load_screenshot () throws Error {
+ var screenshot_path = get_screenshot_path ();
+
if (!FileUtils.test (screenshot_path, FileTest.EXISTS))
return;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]