Skip to content

Commit

Permalink
Star Citizen now collapses profiles into groupings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexeh committed Jan 21, 2024
1 parent 3fc3599 commit 90d4e85
Showing 1 changed file with 72 additions and 15 deletions.
87 changes: 72 additions & 15 deletions joystick_diagrams/plugins/star_citizen_plugin/star_citizen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Star Citizen XML Parser for use with Joystick Diagrams"""
"""Star Citizen XML Parser for use with Joystick Diagrams."""
import logging
import os
from pathlib import Path
from typing import Union
from xml.dom import minidom

from joystick_diagrams.input.profile_collection import ProfileCollection
Expand All @@ -11,6 +12,49 @@

HAT_FORMAT_LOOKUP = {"up": "U", "down": "D", "left": "L", "right": "R"}

PROFILE_MAPPINGS = {
"seat_general": "Spaceship",
"spaceship_general": "Spaceship",
"spaceship_view": "Spaceship",
"spaceship_movement": "Spaceship",
"spaceship_quantum": "Spaceship",
"spaceship_docking": "Spaceship",
"spaceship_targeting": "Weapons",
"spaceship_targeting_advanced": "Weapons",
"spaceship_target_hailing": "Weapons",
"spaceship_radar": "Spaceship Scanning",
"spaceship_scanning": "Spaceship Scanning",
"spaceship_mining": "Spaceship Mining",
"spaceship_salvage": "Spaceship Salavage",
"turret_movement": "Weapons",
"turret_advanced": "Weapons",
"spaceship_weapons": "Weapons",
"spaceship_missiles": "Weapons",
"spaceship_defensive": "Weapons",
"vehicle_capacitor_assignment": "Spaceship",
"spaceship_auto_weapons": "Weapons",
"spaceship_power": "Spaceship",
"spaceship_hud": "Spaceship",
"lights_controller": "Spaceship",
"stopwatch": "Spaceship",
"player": "Spaceship",
"prone": "Spaceship",
"mapui": "Spaceship",
"tractor_beam": "Spaceship",
"zero_gravity_eva": "Spaceship",
"zero_gravity_traversal": "Spaceship",
"vehicle_general": "Spaceship",
"vehicle_driver": "Spaceship",
"spectator": "Spaceship",
"default": "Spaceship",
"player_emotes": "Spaceship",
"player_input_optical_tracking": "Spaceship",
"player_choice": "Spaceship",
"view_director_mode": "Spaceship",
"remoterigidentitycontroller": "Spaceship",
"server_renderer": "Spaceship",
}


class StarCitizen:
def __init__(self, file_path):
Expand Down Expand Up @@ -484,19 +528,19 @@ def __load_file(self) -> str:
try:
self.__validate_file(data)
except Exception:
raise Exception("File is not a valid Star Citizen XML")
raise Exception("File is not a valid Star Citizen XML") # TODO remove base exception
else:
return data
else:
raise Exception("File must be an XML file")
raise Exception("File must be an XML file") # TODO remove base exception
else:
raise FileNotFoundError("File not found")

def __validate_file(self, data) -> bool:
try:
parsed_xml = minidom.parseString(data)
except ValueError:
raise Exception("File is not a valid Star Citizen XML")
raise Exception("File is not a valid Star Citizen XML") # TODO remove base exception
else:
if (
len(parsed_xml.getElementsByTagName("ActionMaps")) == 1
Expand Down Expand Up @@ -564,7 +608,8 @@ def parse(self) -> ProfileCollection:

# For each Profile get the binds for the devices
for profile in profiles:
profile_obj = profile_collection.create_profile(profile.getAttribute("name"))
profile_name = get_profile_name_map(profile.getAttribute("name"))
profile_obj = profile_collection.create_profile(profile_name)

# Get all the action nodes for a profile
actions = profile.getElementsByTagName("action")
Expand All @@ -581,8 +626,6 @@ def parse(self) -> ProfileCollection:
for bind in binds:
bind_input = bind.getAttribute("input")

if bind_input == "js1_rctrl+button15":
print("")
resolved_input = resolve_input(bind_input)

if not resolved_input: # No binding available
Expand All @@ -607,6 +650,21 @@ def parse(self) -> ProfileCollection:
return profile_collection


def get_profile_name_map(name: str) -> str:
"""Return a mapped profile name for a given name.
Allows multiple profiles to be grouped into one
"""
_name = PROFILE_MAPPINGS.get(name)

# Handle unexpected new mappings with default
if _name is None:
_logger.warning("No map found for a Star Citizen profile {name}. This should be raised as a bug.")
_name = "Spaceship"

return _name


def extract_modifiers(bind_str: str) -> str | None:
"""Extract modifiers from an input string.
Expand All @@ -618,7 +676,7 @@ def extract_modifiers(bind_str: str) -> str | None:
return None


def resolve_bind(bind_str: str) -> list[str | None, str]:
def resolve_bind(bind_str: str) -> tuple[Union[str, None], str]:
"""Determine bind type and return."""
_modifiers = extract_modifiers(bind_str)

Expand All @@ -629,7 +687,7 @@ def resolve_bind(bind_str: str) -> list[str | None, str]:

control = find_control_type(_control_input)

return [_modifiers, control]
return (_modifiers, control)


def find_control_type(control_input: str) -> str:
Expand Down Expand Up @@ -670,7 +728,10 @@ def find_control_type(control_input: str) -> str:


def resolve_input(input_str: str) -> tuple[str, str, str | None] | None:
"""Resolve an INPUT string to the a device/binding."""
"""Resolve an INPUT string to the a device/binding.
Returns (device id, bind string, modifiers)
"""
input_str = input_str.strip()

_device_id, _binding = input_str[0:3], input_str[4:]
Expand All @@ -684,8 +745,4 @@ def resolve_input(input_str: str) -> tuple[str, str, str | None] | None:


if __name__ == "__main__":
inst = StarCitizen("C:\\Users\\RCox\\Downloads\\layout_BK_DualVKB_3-22_exported.xml")

# resolve_input("js2_hat1_up")
profiles = inst.parse()
print(profiles)
pass

0 comments on commit 90d4e85

Please sign in to comment.