SDL: Use the CRC as part of the unique key for mappings (3d6d4)

From 3d6d4ca2cda9e8ecdcd320514d2b3dfe7f1c6612 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 17 Mar 2024 12:12:07 -0700
Subject: [PATCH] Use the CRC as part of the unique key for mappings

(cherry picked from commit 4697153d1e2d781e83f790fc12f3c1efce02aa52)
(cherry picked from commit 07f04bb35e54e3ad1e71b27c6344d5e87b82c4c8)
---
 src/joystick/sort_controllers.py | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/joystick/sort_controllers.py b/src/joystick/sort_controllers.py
index c354a22dccf56..c97559f553700 100755
--- a/src/joystick/sort_controllers.py
+++ b/src/joystick/sort_controllers.py
@@ -35,6 +35,15 @@ def find_element(prefix, bindings):
 
     return -1
 
+def get_crc_from_entry(entry):
+    crc = ""
+    line = "".join(entry)
+    bindings = line.split(",")
+    pos = find_element("crc:", bindings)
+    if pos >= 0:
+        crc = bindings[pos][4:]
+    return crc
+
 def save_controller(line):
     global controllers
     match = split_pattern.match(line)
@@ -85,7 +94,7 @@ def save_controller(line):
     entry.append(match.group(5))
     controllers.append(entry)
 
-    entry_id = entry[1] + entry[3]
+    entry_id = entry[1] + get_crc_from_entry(entry)
     if ',sdk' in line or ',hint:' in line:
         conditionals.append(entry_id)
 
@@ -94,7 +103,7 @@ def write_controllers():
     global controller_guids
     # Check for duplicates
     for entry in controllers:
-        entry_id = entry[1] + entry[3]
+        entry_id = entry[1] + get_crc_from_entry(entry)
         if (entry_id in controller_guids and entry_id not in conditionals):
             current_name = entry[2]
             existing_name = controller_guids[entry_id][2]