diff --git a/.github/workflows/generate-sounds.yml b/.github/workflows/generate-sounds.yml new file mode 100644 index 000000000..2d9e9631a --- /dev/null +++ b/.github/workflows/generate-sounds.yml @@ -0,0 +1,34 @@ +name: Generate sounds.json + +on: + workflow_dispatch: + pull_request_target: + types: [opened, synchronize, reopened, ready_for_review] + merge_group: + branches: [main, development] + +jobs: + generate-sounds: + if: '!github.event.pull_request.draft' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout the Repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + token: ${{ secrets.GITHUB_TOKEN }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Generate sounds.json + run: | + chmod +x ./utils/process_sounds.sh + ./utils/process_sounds.sh ./src/main/resources/assets/wynnvp/sounds.json + + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_user_name: 'VoWBot' + commit_user_email: 'admin@voicesofwynn.com' + commit_message: 'ci: generate sounds.json' \ No newline at end of file diff --git a/build.gradle b/build.gradle index dae7f5f6a..533d2477a 100644 --- a/build.gradle +++ b/build.gradle @@ -55,7 +55,7 @@ allprojects { } json { target "src/**/*.json" - targetExclude "src/**/sound_info.json" + targetExclude "src/**/sound_info.json", "src/**/sounds.json" gson() .indentWithSpaces(2) .sortByKeys() diff --git a/utils/process_sounds.sh b/utils/process_sounds.sh new file mode 100755 index 000000000..c1bf2d4fc --- /dev/null +++ b/utils/process_sounds.sh @@ -0,0 +1,64 @@ +#!/bin/sh + +# This script processes the sound_info.json file and creates a sounds.json file, +# which is used by Minecraft to load and play sounds. + +# Check if the sound_info.json file is provided +if [ "$#" -ne 1 ]; then + echo "Usage: $0 path/to/sound_info.json" + exit 1 +fi + +sound_info_file=$1 + +# Check if the provided file exists +if [ ! -f "$sound_info_file" ]; then + echo "File not found: $sound_info_file" + exit 1 +fi + +# Get the directory of the provided sound_info.json file +sound_info_dir=$(dirname "$sound_info_file") + +# Define the output sounds.json file path +sounds_json_file="$sound_info_dir/sounds.json" + +# Process the sound_info.json file and create the sounds.json file +# NOTE: This script can only generate the fileOverride field for the sounds.json file, +# if the NPC name and line info are provided in the sound_info.json file. +# If either of these data is not available, the script fails. Use a unique fileOverride value for these cases. +jq '[ + .content | to_entries | map( + .key as $content | .value.contexts | to_entries | map( + .key as $context | .value.dialogues | map( + .fileOverride as $fileOverride | + { + "fileName": ($fileOverride // ( + ($content | gsub("[^a-zA-Z0-9]"; "") | ascii_downcase) + "-" + + ($context | gsub("[^a-zA-Z0-9]"; "") | ascii_downcase) + "-" + + (.lineInfo.npc | gsub("[^a-zA-Z0-9]"; "") | ascii_downcase) + "-" + + (.lineInfo.line.current | tostring) + )) + } | { + (.fileName): { + "category": "voice", + "sounds": [ + { + "name": "wynnvp:\(.fileName)", + "stream": true + } + ] + } + } + ) + ) + ) | flatten | add +] | add | to_entries | sort_by(.key) | from_entries' "$sound_info_file" > "$sounds_json_file" + +# Check if the sounds.json file was created successfully +if [ $? -eq 0 ]; then + echo "sounds.json file has been created successfully at $sounds_json_file" +else + echo "An error occurred while creating the sounds.json file" + exit 1 +fi