Skip to content

Instantly share code, notes, and snippets.

@miabrahams
Created February 25, 2025 02:16
Show Gist options
  • Save miabrahams/69d524627b3cd6e5af233538602e1e08 to your computer and use it in GitHub Desktop.
Save miabrahams/69d524627b3cd6e5af233538602e1e08 to your computer and use it in GitHub Desktop.
Custom Music Extension for Rift Wizard 2
import pygame
import os
import random
from pathlib import Path
import logging
import inspect
# This is a simple mod to add custom music tracks.
# Put your music files in the same folder as this script.
# Only .wav files are supported at the moment.
# List of included battle tracks that we don't want played. Defaults to 4 and 5 in the initial release.
DISABLED_TRACKS = [4, 5]
# DISABLED_TRACKS = range(2, 11) # Disable all built-in tracks
# Set to the name of the track you want to start with on level 1. If None, a random track will be chosen.
INITIAL_TRACK = 'battle_1'
# INITIAL_TRACK = None
# .mp3 doesn't work, maybe this will change!
SUPPORTED_FORMATS = (".wav")
# Enable verbose logging
DEBUG = False
# DEBUG = True
logger = logging.getLogger("CustomMusic")
logger.setLevel(logging.DEBUG if DEBUG else logging.INFO)
logger.addHandler(logging.StreamHandler())
logger.debug("-- Loading CustomMusic Mod --")
class MusicManager():
track_queue = None
started = False
def __init__(self):
# Load music files from CustomMusic folder
mod_music_folder = Path(__file__).parent
mod_tracks = []
if os.path.exists(mod_music_folder):
for file in os.listdir(mod_music_folder):
if file.endswith(SUPPORTED_FORMATS):
logger.debug("Found additional track: %s", file)
# Add a ":"
mod_tracks.append(":" + os.path.join(mod_music_folder, file))
# Shuffle w/ default tracks
default_tracks = [f"battle_{i}" for i in range(2, 11) if i not in DISABLED_TRACKS]
self.all_tracks = default_tracks + mod_tracks
random.shuffle(self.all_tracks)
logger.debug("Playlist: %s", '\n'.join(self.all_tracks))
def first_track(self, level_num=-1):
if INITIAL_TRACK and level_num == 1:
return INITIAL_TRACK
else:
return self.next_song()
def next_song(self, level_num=-1):
if self.track_queue is None:
self.track_queue = self.all_tracks.copy()
return self.first_track(level_num)
elif len(self.track_queue) == 0:
self.track_queue = self.all_tracks.copy()
return self.track_queue.pop()
MM = MusicManager()
def custom_battle_music(self, num=None):
self.play_music(MM.next_song(self.game.level_num))
# slight modification of original play_music to handle our new tracks
def custom_play_music(self, track_name, fade_ms=0):
logger.debug("Playing music track %s", track_name)
if not self.can_play_sound:
return
if track_name[0] == ':': # Mod track
track_path = str(track_name[1:])
else:
track_path = os.path.join('rl_data', 'music', track_name + '.wav')
pygame.mixer.music.load(track_path)
self.adjust_volume(0, 'music')
pygame.mixer.music.play()
# Monkey patch
frm = inspect.stack()[-1]
RiftWizard2 = inspect.getmodule(frm[0])
RiftWizard2.PyGameView.play_battle_music = custom_battle_music
RiftWizard2.PyGameView.play_music = custom_play_music
logger.debug("-- CustomMusic Mod Loaded --")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment