1# FSGS - Common functionality for Fengestad Game System.
2# Copyright (C) 2013-2019  Frode Solheim <frode@solheim.dev>
3#
4# This program is free software; you can redistribute it and/or modify it
5# under the terms of the GNU General Public License as published by the Free
6# Software Foundation; either version 2 of the License, or (at your option)
7# any later version.
8#
9# This program is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12# more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18import json
19import os
20
21from fsgs.option import Option
22from fsgs.platforms.loader import SimpleLoader
23
24ZXS_MODEL_48K = "spectrum"
25ZXS_MODEL_128 = "spectrum128"
26ZXS_MODEL_PLUS3 = "spectrum+3"
27
28
29class SpectrumLoader(SimpleLoader):
30    def load_files(self, values):
31        file_list = json.loads(values["file_list"])
32        if len(file_list) == 0:
33            self.config["x_variant_error"] = "Variant has empty file list"
34        elif len(file_list) > 1:
35            self.config["x_variant_error"] = "Unsupported multi-file variant"
36
37        name = file_list[0]["name"]
38        sha1 = file_list[0]["sha1"]
39        _, ext = os.path.splitext(name)
40        ext = ext.lower()
41        if ext in [".z80"]:
42            key = Option.SNAPSHOT_FILE
43        elif ext in [".tap", ".tzx"]:
44            key = Option.TAPE_DRIVE_0
45        elif ext in [".dsk", ".trd"]:
46            key = Option.FLOPPY_DRIVE_0
47        elif ext in [".rom"]:
48            key = Option.CARTRIDGE_SLOT
49        else:
50            return
51        self.config[key] = "sha1://{0}/{1}".format(sha1, name)
52
53    def load_extra(self, values):
54        self.config[Option.ZXS_MODEL] = values["model"]
55        if not self.config[Option.ZXS_MODEL]:
56            self.config[Option.ZXS_MODEL] = ZXS_MODEL_48K
57        self.config["model"] = ""
58