1import os
2
3from fsgs.drivers.dolphindriver import DolphinDriver, DolphinInputMapper
4from fsgs.platform import Platform
5from fsgs.platforms.loader import SimpleLoader
6
7NGC_PLATFORM_NAME = "GameCube"
8NGC_CONTROLLER = {
9    "type": "gamepad",
10    "description": "Gamepad",
11    "mapping_name": "ngc",
12}
13
14
15class GameCubePlatform(Platform):
16    # FIXME: Move to init instead
17    PLATFORM_NAME = NGC_PLATFORM_NAME
18
19    def driver(self, fsgc):
20        return GameCubeDriver(fsgc)
21
22    def loader(self, fsgc):
23        return GameCubeLoader(fsgc)
24
25
26class GameCubeLoader(SimpleLoader):
27    pass
28
29
30class GameCubeDriver(DolphinDriver):
31    PORTS = [
32        {"description": "Controller 1", "types": [NGC_CONTROLLER]},
33        {"description": "Controller 2", "types": [NGC_CONTROLLER]},
34        {"description": "Controller 3", "types": [NGC_CONTROLLER]},
35        {"description": "Controller 4", "types": [NGC_CONTROLLER]},
36    ]
37
38    def __init__(self, fsgs):
39        super().__init__(fsgs)
40        self.helper = GameCubeHelper(self.options)
41
42    def dolphin_configure_core(self, f):
43        # find devices now (need to know how many devices to
44        # specify in dolphin.ini)
45        # devices = self.context.input.get_devices(1, 4)
46        # configure dolphin.ini
47        device_count = 0
48        for i, port in enumerate(self.ports):
49            if port.device:
50                device_count += 1
51        for i in range(device_count):
52            f.write("SIDevice{0} = 150994944\n".format(i))
53
54        # FIXME:
55        memcard_region = "USA"
56
57        f.write(
58            "MemcardA = {dir}/MemoryCardA.{region}.raw\n".format(
59                dir=self.get_state_dir(), region=memcard_region
60            )
61        )
62        f.write(
63            "MemcardB = {dir}/MemoryCardB.{region}.raw\n".format(
64                dir=self.get_state_dir(), region=memcard_region
65            )
66        )
67
68    def dolphin_configure_input(self):
69        # devices = self.context.input.get_devices(1, 4)
70        input_mapping = {
71            "A": "Buttons/A",
72            "B": "Buttons/B",
73            "X": "Buttons/X",
74            "Y": "Buttons/Y",
75            "Z": "Buttons/Z",
76            "START": "Buttons/Start",
77            "STICK_UP": "Main Stick/Up",
78            "STICK_DOWN": "Main Stick/Down",
79            "STICK_LEFT": "Main Stick/Left",
80            "STICK_RIGHT": "Main Stick/Right",
81            "C_UP": "C-Stick/Up",  # or z?
82            "C_DOWN": "C-Stick/Down",
83            "C_LEFT": "C-Stick/Left",
84            "C_RIGHT": "C-Stick/Right",
85            "L": "Triggers/L",
86            "R": "Triggers/R",
87            "DPAD_UP": "D-Pad/Up",
88            "DPAD_DOWN": "D-Pad/Down",
89            "DPAD_LEFT": "D-Pad/Left",
90            "DPAD_RIGHT": "D-Pad/Right",
91            "L_ANALOG": "Triggers/L-Analog",
92            "R_ANALOG": "Triggers/R-Analog",
93            # FIXME: ADD MODIFIER?
94        }
95        temp_dir = self.temp_dir("dolphin")
96        input_config_file = os.path.join(
97            temp_dir.path, "user", "Config", "GCPadNew.ini"
98        )
99        f = open(input_config_file, "w")
100        for i, port in enumerate(self.ports):
101            if not port.device:
102                continue
103            f.write("[GCPad{num}]\n".format(num=i + 1))
104            if port.device.is_keyboard():
105                f.write("Device = DInput/0/Keyboard Mouse\n")
106            else:
107                type_index = int(port.device.id.rsplit("#", 1)[1]) - 1
108                f.write(
109                    "Device = SDL/{index}/{name}\n".format(
110                        index=type_index, name=port.device.sdl_name
111                    )
112                )
113            mapper = DolphinInputMapper(port, input_mapping)
114            for key, value in mapper.items():
115                if isinstance(key, tuple):
116                    for k in key:
117                        f.write("{key} = {value}\n".format(key=k, value=value))
118                else:
119                    f.write("{key} = {value}\n".format(key=key, value=value))
120
121
122class GameCubeHelper:
123    def __init__(self, options):
124        self.options = options
125