aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/runtime/linux_music.c (6306B)


      1 #include "linux_music.h"
      2 #include "linux_audio.h"
      3 #include "linux_thread.h"
      4 #include "guest_heap.h"
      5 #include <pthread.h>
      6 
      7 /* Only this worker enters mmr's music controller and its pr player. */
      8 _Thread_local int linux_music_worker;
      9 enum music_operation { MUSIC_START, MUSIC_EVENT, MUSIC_ZONE, MUSIC_MUTE, MUSIC_VOLUME, MUSIC_SHUTDOWN };
     10 struct music_command {
     11     enum music_operation operation;
     12     uint32_t driver, combat, zone, event, volume, generation;
     13     uint8_t enabled, muted, force;
     14     struct music_command *next;
     15 };
     16 static struct music_command *head, *tail;
     17 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     18 static pthread_cond_t changed = PTHREAD_COND_INITIALIZER;
     19 static pthread_once_t once = PTHREAD_ONCE_INIT;
     20 
     21 static void call_music(uint32_t address, uint32_t object, unsigned count, const uint32_t *arguments)
     22 {
     23     uint32_t stack = g_esp;
     24     for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]);
     25     PUSH32(g_esp, RECOMP_RETADDR);
     26     g_ecx = object;
     27     recomp_func_t function = recomp_lookup(address);
     28     if (!function) abort();
     29     function();
     30     if (g_esp != stack) abort();
     31 }
     32 
     33 static void run_music(void)
     34 {
     35     linux_music_worker = 1;
     36     uint32_t manager = guest_malloc(0x10c), audio = guest_malloc(0x24), driver = 0;
     37     uint32_t volume = 0;
     38     int have_volume = 0;
     39     if (!manager || !audio) abort();
     40     for (;;) {
     41         if (pthread_mutex_lock(&mutex)) abort();
     42         while (!head)
     43             if (pthread_cond_wait(&changed, &mutex)) abort();
     44         struct music_command *command = head;
     45         head = command->next;
     46         if (!head) tail = NULL;
     47         if (pthread_mutex_unlock(&mutex)) abort();
     48 
     49         /* The private manager contains only music state; no queued game pointers. */
     50         int release_command = 1;
     51         if (driver != command->driver) {
     52             if (driver) {
     53                 uint32_t muted = 1;
     54                 call_music(0x3A004700, manager, 1, &muted);
     55                 linux_audio_release_driver(driver);
     56             }
     57             driver = command->driver;
     58             release_command = 0; /* Transfer the command's reference to the owner. */
     59             memset((void *)ADDR(manager), 0, 0x10c);
     60             memset((void *)ADDR(audio), 0, 0x24);
     61             MEM32(manager + 0x60) = audio;
     62             MEM32(manager + 0x74) = UINT32_MAX;
     63             MEM32(audio + 0xc) = driver;
     64             MEM8(audio + 0x10) = 1;
     65         }
     66         MEM8(manager + 4) = command->enabled;
     67         MEM32(manager + 0x94) = command->combat;
     68         MEM8(manager + 0x108) = command->muted;
     69         MEM8(manager + 0x109) = command->force;
     70 
     71         /* Preserve mmr's project, layer selection and pr's duration-based scheduler. */
     72         switch (command->operation) {
     73         case MUSIC_START:
     74             call_music(0x3A004AD0, manager, 0, NULL);
     75             break;
     76         case MUSIC_EVENT:
     77             call_music(0x3A004A10, manager, 1, &command->event);
     78             break;
     79         case MUSIC_ZONE: {
     80             uint32_t arguments[] = {command->zone, command->event};
     81             call_music(0x3A004C00, manager, 2, arguments);
     82             break;
     83         }
     84         case MUSIC_MUTE: {
     85             uint32_t muted = command->muted;
     86             call_music(0x3A004700, manager, 1, &muted);
     87             break;
     88         }
     89         case MUSIC_VOLUME:
     90             volume = command->volume;
     91             have_volume = 1;
     92             break;
     93         case MUSIC_SHUTDOWN: {
     94             uint32_t muted = 1;
     95             call_music(0x3A004700, manager, 1, &muted);
     96             linux_audio_release_driver(driver);
     97             driver = 0;
     98             break;
     99         }
    100         }
    101         if (have_volume && MEM32(0x3A00D2DC))
    102             call_music(MEM32(0x3A00A160), MEM32(0x3A00D2DC), 1, &volume);
    103         /* A mute invalidates earlier plays even when an unmute is already queued. */
    104         if (!command->muted && (command->operation == MUSIC_START ||
    105             command->operation == MUSIC_EVENT || command->operation == MUSIC_ZONE))
    106             linux_audio_enable_music(command->driver, command->generation);
    107         if (release_command) linux_audio_release_driver(command->driver);
    108         free(command);
    109     }
    110 }
    111 
    112 static void start_worker(void) { linux_thread_start_worker(run_music); }
    113 
    114 static int enqueue(uint32_t manager, enum music_operation operation, uint32_t zone, uint32_t event)
    115 {
    116     uint32_t audio = MEM32(manager + 0x60);
    117     if (!audio || !MEM8(audio + 0x10)) return 0;
    118     struct music_command *command = calloc(1, sizeof(*command));
    119     if (!command) abort();
    120     command->operation = operation;
    121     command->driver = MEM32(audio + 0xc);
    122     command->enabled = MEM8(manager + 4);
    123     command->combat = MEM32(manager + 0x94);
    124     command->muted = MEM8(manager + 0x108);
    125     command->force = MEM8(manager + 0x109);
    126     command->zone = zone;
    127     command->event = event;
    128     command->volume = MEM32(0x3A00D03C);
    129     linux_audio_retain_driver(command->driver);
    130     /* Mute immediately, including streams that an outstanding open will publish. */
    131     command->generation = linux_audio_mute_music(command->driver,
    132         operation == MUSIC_SHUTDOWN || command->muted);
    133     if (pthread_once(&once, start_worker)) abort();
    134     if (pthread_mutex_lock(&mutex)) abort();
    135     if (tail) tail->next = command;
    136     else head = command;
    137     tail = command;
    138     if (pthread_cond_signal(&changed)) abort();
    139     if (pthread_mutex_unlock(&mutex)) abort();
    140     return 1;
    141 }
    142 
    143 void linux_music_start(uint32_t manager) { enqueue(manager, MUSIC_START, 0, 0); }
    144 void linux_music_event(uint32_t manager, uint32_t event) { enqueue(manager, MUSIC_EVENT, 0, event); }
    145 
    146 void linux_music_zone(uint32_t manager, uint32_t zone, uint32_t event)
    147 {
    148     if (!MEM8(manager + 4)) {
    149         MEM32(manager + 0x64) = MEM32(manager + 0x74) = UINT32_MAX;
    150         return;
    151     }
    152     MEM32(manager + 0x64) = zone;
    153     enqueue(manager, MUSIC_ZONE, zone, event);
    154 }
    155 
    156 void linux_music_mute(uint32_t manager, uint8_t muted)
    157 {
    158     MEM8(manager + 0x108) = muted;
    159     if (muted) MEM32(manager + 0x74) = UINT32_MAX;
    160     enqueue(manager, MUSIC_MUTE, 0, 0);
    161 }
    162 
    163 void linux_music_volume(uint32_t manager)
    164 {
    165     if (!MEM8(0x3A00D040)) return;
    166     if (enqueue(manager, MUSIC_VOLUME, 0, 0)) MEM8(0x3A00D040) = 0;
    167 }
    168 
    169 void linux_music_shutdown(uint32_t manager) { enqueue(manager, MUSIC_SHUTDOWN, 0, 0); }