Convert this file https://github.com/libretro/libretro-database/blob/master/metadat/genre/Nintendo%20-%20Game%20Boy%20Advance.dat to json with bash script
Last active
March 22, 2024 06:04
-
-
Save SMUsamaShah/e707e2e855016122e26883c8567b65b5 to your computer and use it in GitHub Desktop.
Bash script to convert libretro genre metadata .dat files to json (works on miyoo mini)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
input_file="gba.dat" | |
output_file="output.json" | |
# declare -A crc_to_genre | |
# declare -A name_to_genre | |
inside_game_block=false | |
crc_to_genre_json="" | |
name_to_genre_json="" | |
N=$'\n' | |
T=$'\t' | |
while IFS='' read -r line; do | |
case "$line" in | |
'game ('*) | |
inside_game_block=true | |
comment="" | |
genre="" | |
crc="" | |
;; | |
')'*) | |
inside_game_block=false | |
if [ ! -z "$comment" ]; then | |
crc_to_genre_json="$crc_to_genre_json$N\"$crc\":\"$genre\"," | |
name_to_genre_json="$name_to_genre_json$N\"$comment\":\"$genre\"," | |
# json_string="$json_string$N\"$crc\": {$N$T\"name\": \"$comment\",$N$T\"genre\": \"$genre\"$N}," | |
fi | |
;; | |
*) | |
if $inside_game_block; then | |
case "$line" in | |
*'comment '*) | |
comment="${line#*\"}" # remove everything before the first quote | |
comment="${comment%%\"*}" # remove everything after the first quote | |
#lower case, remove all symbols and spaces | |
comment=$(echo "$comment" | sed 's/[^a-zA-Z0-9]//g' | tr '[:upper:]' '[:lower:]') | |
;; | |
*'genre "'*) | |
genre="${line#*\"}" | |
genre="${genre%%\"*}" | |
;; | |
*'rom ( crc '*) | |
crc="${line#*crc }" # removes everything up to crc | |
crc="${crc%% )*}" # removes everything after the first space | |
# crc="${crc,,}" # to lower case | |
crc=$(echo "$crc" | tr '[:upper:]' '[:lower:]') | |
;; | |
esac | |
fi | |
;; | |
esac | |
done < "$input_file" | |
# Remove trailing comma, add brackets | |
jsonstr="{$N$crc_to_genre_json${name_to_genre_json%%,}$N}" | |
# write to file | |
echo "$jsonstr" > "$output_file" | |
#remove duplicate keys | |
jsonstr=$(jq 'to_entries | unique | from_entries' $output_file) | |
echo "$jsonstr" > "$output_file" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
genrejson="output.json" | |
for romfile in *.*; do | |
romfileNoExt="${romfile%.*}" | |
romfileNoBrackets="${romfileNoExt%%\(*}" | |
romfileNoSymbols=$(echo "$romfileNoBrackets" | sed 's/[^a-zA-Z0-9]//g' | tr '[:upper:]' '[:lower:]' ) | |
# echo $romfileNoSymbols | |
# Calculate CRC | |
case $romfile in | |
*.zip) | |
crc=$(unzip -p "$romfile" | xcrc /proc/self/fd/0) | |
;; | |
*.7z) | |
crc=$(7z e "$romfile" -so | xcrc /proc/self/fd/0) | |
;; | |
*.*) | |
crc=$(xcrc "$romfile") | |
;; | |
*) | |
continue | |
;; | |
esac | |
#first search by CRC | |
genre=$(jq -r '.["'"$crc"'"]' $genrejson); | |
#then search by name | |
if [ "$genre" = "null" ]; then | |
genre=$(jq -r 'to_entries[] | select(.key | contains("'"$romfileNoSymbols"'")) | .value' $genrejson | head -1) | |
fi | |
if [ -z "$genre" ]; then | |
echo "Genre not found: $romfile" | |
else | |
mkdir -p "$genre" | |
mv "$romfile" "$genre/" | |
echo "$genre <-- $romfile" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment