[libgames-support] scores: Make Importer an abstract class and add DirectoryImporter
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgames-support] scores: Make Importer an abstract class and add DirectoryImporter
- Date: Sat, 13 Feb 2016 21:12:12 +0000 (UTC)
commit 06a90ae62913892a9211a290f854a09d965492c7
Author: Michael Catanzaro <mcatanzaro igalia com>
Date: Sat Feb 13 15:10:51 2016 -0600
scores: Make Importer an abstract class and add DirectoryImporter
games/Makefile.am | 13 +++---
games/scores/directory-importer.vala | 81 ++++++++++++++++++++++++++++++++++
games/scores/importer.vala | 77 +++-----------------------------
3 files changed, 95 insertions(+), 76 deletions(-)
---
diff --git a/games/Makefile.am b/games/Makefile.am
index c34a444..b41dfd7 100644
--- a/games/Makefile.am
+++ b/games/Makefile.am
@@ -1,12 +1,13 @@
lib_LTLIBRARIES = libgames-support.la
libgames_support_la_SOURCES = \
- config.vapi \
- gridframe.vala \
- scores/category.vala \
- scores/context.vala \
- scores/dialog.vala \
- scores/importer.vala \
+ config.vapi \
+ gridframe.vala \
+ scores/category.vala \
+ scores/context.vala \
+ scores/dialog.vala \
+ scores/directory-importer.vala \
+ scores/importer.vala \
scores/score.vala
libgames_support_la_CFLAGS = \
diff --git a/games/scores/directory-importer.vala b/games/scores/directory-importer.vala
new file mode 100644
index 0000000..946a381
--- /dev/null
+++ b/games/scores/directory-importer.vala
@@ -0,0 +1,81 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * Copyright © 2016 Michael Catanzaro <mcatanzaro gnome org>
+ *
+ * This file is part of libgames-scores.
+ *
+ * libgames-scores is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * libgames-scores is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libgames-scores. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace Games {
+namespace Scores {
+
+/* Imports scores from an old scores directory, where each category of scores is
+ * saved in a separate file. This is the format used by old C games that were
+ * never converted to Vala before switching to libgames-support. This class
+ * should probably be used by Five or More, Nibbles, Robots, Tali, and nothing
+ * else.
+ */
+public class DirectoryImporter : Importer
+{
+ /* A function provided by the game that converts the old category key to a
+ * new key. If the keys have not been changed, this function should return
+ * the same string. If the key is invalid, it should return null, as this
+ * function will be called once for each file in the game's local data
+ * directory, and some of those files might not be valid categories.
+ */
+ public delegate string? CategoryConvertFunc (string old_key);
+ private CategoryConvertFunc category_convert;
+
+ public DirectoryImporter (CategoryConvertFunc category_convert)
+ {
+ this.category_convert = category_convert;
+ }
+
+ /* This scores format is mostly-compatible with the current format, the only
+ * differences are (a) the scores file nowadays has a column for the player
+ * name, (b) scores nowadays are kept under ~/.local/share/APPNAME/scores
+ * whereas they used to be saved one level up, and (c) some category names
+ * have changed. All we have to do here is copy the files from the parent
+ * directory to the subdirectory, and rename them according to the new
+ * category names. Context.load_scores_from_file handles the missing player
+ * name column by assuming it matches the current UNIX account if missing.
+ * Notice that we are importing only home directory scores, not any scores
+ * from /var/games, since it's been several years since scores were removed
+ * from there and most players will have lost them by now anyway.
+ */
+ protected override void importOldScores (File new_scores_dir) throws GLib.Error
+ {
+ var original_scores_dir = new_scores_dir.get_parent ();
+ assert (original_scores_dir != null);
+
+ var enumerator = original_scores_dir.enumerate_children (FileAttribute.STANDARD_NAME, 0);
+ FileInfo file_info;
+ while ((file_info = enumerator.next_file ()) != null)
+ {
+ var new_key = category_convert (file_info.get_name ());
+ if (new_key == null)
+ continue;
+
+ var new_file = new_scores_dir.get_child (new_key);
+ var original_file = original_scores_dir.resolve_relative_path (file_info.get_name ());
+ debug ("Moving scores from %s to %s", original_file.get_path (), new_file.get_path ());
+ original_file.copy (new_file, FileCopyFlags.NONE);
+ original_file delete ();
+ }
+ }
+}
+
+} /* namespace Scores */
+} /* namespace Games */
diff --git a/games/scores/importer.vala b/games/scores/importer.vala
index 80d6441..5341029 100644
--- a/games/scores/importer.vala
+++ b/games/scores/importer.vala
@@ -45,7 +45,8 @@ namespace Scores {
* because it turns out people share user accounts and want to record scores
* separately.
*
- * This importer exists so that we don't delete everyone's scores a second time.
+ * These importers exist so that we don't delete everyone's scores a second
+ * time.
*
* Some users have complained that scores are now saved in the home directory.
* Using /var/games was a clever setup and it's a bit of a shame to see it go;
@@ -57,78 +58,14 @@ namespace Scores {
* don't plan to spend more time on this, though.
*/
-/* Note that all file I/O here is performed synchronously, because the importer
- * is only used before scores are loaded, and the library ensures scores are
- * always loaded before showing the main window.
+/* Note that importers must operate synchronously. This is fine because the
+ * importer is only used before scores are loaded, and we ensure scores are
+ * loaded before showing the main window.
*/
-public class Importer
+public abstract class Importer
{
- public enum OldFormat
- {
- C_GAMES_MULTI_FILE_FORMAT,
- VALA_GAMES_SINGLE_FILE_FORMAT,
- }
-
- /* A function provided by the game that converts the old category key to a
- * new key. If the keys have not been changed, this function should return
- * the same string. If the key is invalid, it should return null, as this
- * function will be called once for each file in the game's local data
- * directory, and some of those files might not be valid categories.
- *
- * FIXME: Is this only useful for C_GAMES_MULTI_FILE_FORMAT?
- */
- public delegate string? CategoryConvertFunc (string old_key);
- private CategoryConvertFunc category_convert;
-
- private OldFormat format;
-
- public Importer (OldFormat format, CategoryConvertFunc category_convert)
- {
- this.format = format;
- this.category_convert = category_convert;
- }
-
- /* This scores format is mostly-compatible with the current format, the only
- * differences are (a) the scores file nowadays has a column for the player
- * name, (b) scores nowadays are kept under ~/.local/share/APPNAME/scores
- * whereas they used to be saved one level up, and (c) some category names
- * have changed. All we have to do here is copy the files from the parent
- * directory to the subdirectory, and rename them according to the new
- * category names. Context.load_scores_from_file handles the missing player
- * name column by assuming it matches the current UNIX account if missing.
- * Notice that we are importing only home directory scores, not any scores
- * from /var/games, since it's been several years since scores were removed
- * from there and most players will have lost them by now anyway.
- */
- private void importCMultiFileScores (File new_scores_dir) throws GLib.Error
- {
- var original_scores_dir = new_scores_dir.get_parent ();
- assert (original_scores_dir != null);
-
- var enumerator = original_scores_dir.enumerate_children (FileAttribute.STANDARD_NAME, 0);
- FileInfo file_info;
-
- while ((file_info = enumerator.next_file ()) != null)
- {
- var new_key = category_convert (file_info.get_name ());
- if (new_key == null)
- continue;
-
- var new_file = new_scores_dir.get_child (new_key);
- var original_file = original_scores_dir.resolve_relative_path (file_info.get_name ());
- debug ("Moving scores from %s to %s", original_file.get_path (), new_file.get_path ());
- original_file.copy (new_file, FileCopyFlags.NONE);
- original_file delete ();
- }
- }
-
- private void importOldScores (File new_scores_dir) throws GLib.Error
- /* TODO: Support importing scores from Vala games, bug #745489. */
- requires (format == OldFormat.C_GAMES_MULTI_FILE_FORMAT)
- {
- importCMultiFileScores (new_scores_dir);
- }
+ protected abstract void importOldScores (File new_scores_dir) throws GLib.Error;
internal void run (string new_scores_dir) throws GLib.Error
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]