| 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 a Vala game's history file directory, where each category |
| 25 |
* of scores is saved together in the same file. This is the format used by |
| 26 |
* games that were converted to Vala before switching to libgnome-games-support. This |
| 27 |
* class should probably be used by Klotski, Mines, Swell Foop, Mahjongg, |
| 28 |
* Tetravex, Quadrapassel, and nothing else. |
| 29 |
*/ |
| 30 |
public class HistoryFileImporter : Importer |
| 31 |
{ |
| 32 |
/* A function provided by the game that converts a line in its history file |
| 33 |
* to a Score and Category we can use. |
| 34 |
*/ |
| 35 |
public delegate void HistoryConvertFunc (string line, out Score score, out Category category); |
| 36 |
private HistoryConvertFunc history_convert; |
| 37 |
|
| 38 |
public HistoryFileImporter (HistoryConvertFunc history_convert) |
| 39 |
{ |
| 40 |
set_history_convert_func (history_convert); |
| 41 |
} |
| 42 |
|
| 43 |
public void set_history_convert_func (HistoryConvertFunc history_convert) |
| 44 |
{ |
| 45 |
this.history_convert = (line, out score, out category) => { |
| 46 |
history_convert (line, out score, out category); |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
public static int64 parse_date (string date) |
| 51 |
{ |
| 52 |
TimeVal timeval = {}; |
| 53 |
var ret = timeval.from_iso8601 (date); |
| 54 |
if (!ret) |
| 55 |
warning ("Failed to parse date: %s", date); |
| 56 |
return timeval.tv_sec; |
| 57 |
} |
| 58 |
|
| 59 |
/* Each game uses a somewhat different format for its scores; one game might |
| 60 |
* use one column to store the category, while another might use two or |
| 61 |
* more. Pass the game one line of the history file at a time, and let the |
| 62 |
* game convert it to a Score and Category for us. |
| 63 |
*/ |
| 64 |
protected override void importOldScores (Context context, File new_scores_dir) throws GLib.Error |
| 65 |
{ |
| 66 |
var history_filename = Path.build_filename (new_scores_dir.get_path (), "..", "history", null); |
| 67 |
var stream = FileStream.open (history_filename, "r"); |
| 68 |
if (stream == null) |
| 69 |
return; |
| 70 |
|
| 71 |
debug ("Importing scores from %s", history_filename); |
| 72 |
|
| 73 |
string line; |
| 74 |
while ((line = stream.read_line ()) != null) |
| 75 |
{ |
| 76 |
Score score; |
| 77 |
Category category; |
| 78 |
history_convert (line, out score, out category); |
| 79 |
if (score == null || category == null) |
| 80 |
warning ("Failed to import from %s score line %s", history_filename, line); |
| 81 |
context.add_score_sync (score, category); |
| 82 |
} |
| 83 |
|
| 84 |
var history_file = File.new_for_path (history_filename); |
| 85 |
history_file.@delete (); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
} /* namespace Scores */ |
| 90 |
} /* namespace Games */ |