| 1 |
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 |
* |
| 3 |
* Copyright © 2016 Michael Catanzaro <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of libgnome-games-support. |
| 6 |
* |
| 7 |
* libgnome-games-support is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* libgnome-games-support is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with libgnome-games-support. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
namespace Games { |
| 22 |
namespace Scores { |
| 23 |
|
| 24 |
/* Imports scores from an old scores directory, where each category of scores is |
| 25 |
* saved in a separate file. This is the format used by old C games that were |
| 26 |
* never converted to Vala before switching to libgnome-games-support. This class |
| 27 |
* should probably be used by Five or More, Nibbles, Robots, Tali, and nothing |
| 28 |
* else. |
| 29 |
*/ |
| 30 |
public class DirectoryImporter : Importer |
| 31 |
{ |
| 32 |
/* A function provided by the game that converts the old category key to a |
| 33 |
* new key. If the keys have not been changed, this function should return |
| 34 |
* the same string. If the key is invalid, it should return null, as this |
| 35 |
* function will be called once for each file in the game's local data |
| 36 |
* directory, and some of those files might not be valid categories. |
| 37 |
*/ |
| 38 |
public delegate string? CategoryConvertFunc (string old_key); |
| 39 |
private CategoryConvertFunc? category_convert; |
| 40 |
|
| 41 |
public DirectoryImporter () |
| 42 |
{ |
| 43 |
/* Default converter for games that don't require category migration. */ |
| 44 |
set_category_convert_func ((old_key) => { return old_key; }); |
| 45 |
} |
| 46 |
|
| 47 |
public DirectoryImporter.with_convert_func (CategoryConvertFunc category_convert) |
| 48 |
{ |
| 49 |
set_category_convert_func (category_convert); |
| 50 |
} |
| 51 |
|
| 52 |
public void set_category_convert_func (CategoryConvertFunc category_convert) |
| 53 |
{ |
| 54 |
this.category_convert = (old_key) => { return category_convert (old_key); }; |
| 55 |
} |
| 56 |
|
| 57 |
/* This scores format is mostly-compatible with the current format, the only |
| 58 |
* differences are (a) the scores file nowadays has a column for the player |
| 59 |
* name, (b) scores nowadays are kept under ~/.local/share/APPNAME/scores |
| 60 |
* whereas they used to be saved one level up, and (c) some category names |
| 61 |
* have changed. All we have to do here is copy the files from the parent |
| 62 |
* directory to the subdirectory, and rename them according to the new |
| 63 |
* category names. Context.load_scores_from_file handles the missing player |
| 64 |
* name column by assuming it matches the current UNIX account if missing. |
| 65 |
* Notice that we are importing only home directory scores, not any scores |
| 66 |
* from /var/games, since it's been several years since scores were removed |
| 67 |
* from there and most players will have lost them by now anyway. |
| 68 |
*/ |
| 69 |
protected override void importOldScores (Context context, File new_scores_dir) throws Error |
| 70 |
{ |
| 71 |
var original_scores_dir = new_scores_dir.get_parent (); |
| 72 |
assert (original_scores_dir != null); |
| 73 |
|
| 74 |
var enumerator = original_scores_dir.enumerate_children (FileAttribute.STANDARD_NAME, 0); |
| 75 |
FileInfo file_info; |
| 76 |
while ((file_info = enumerator.next_file ()) != null) |
| 77 |
{ |
| 78 |
/* We just created this.... */ |
| 79 |
if (file_info.get_name () == "scores") |
| 80 |
continue; |
| 81 |
|
| 82 |
var new_key = category_convert (file_info.get_name ()); |
| 83 |
if (new_key == null) |
| 84 |
continue; |
| 85 |
|
| 86 |
var new_file = new_scores_dir.get_child (new_key); |
| 87 |
var original_file = original_scores_dir.resolve_relative_path (file_info.get_name ()); |
| 88 |
debug ("Moving scores from %s to %s", original_file.get_path (), new_file.get_path ()); |
| 89 |
original_file.copy (new_file, FileCopyFlags.NONE); |
| 90 |
original_file.@delete (); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
} /* namespace Scores */ |
| 96 |
} /* namespace Games */ |