aorecomp

Anarchy Online classic in the browser

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


client/tests/browser_pacing.c (4987B)


      1 #include "linux_graphics.h"
      2 #include "linux_window.h"
      3 #include "guest_memory.h"
      4 #include <SDL2/SDL.h>
      5 #include <assert.h>
      6 #include <stdint.h>
      7 #include <stdio.h>
      8 #include <string.h>
      9 #include <time.h>
     10 #include <emscripten.h>
     11 #include <emscripten/threading.h>
     12 
     13 static _Thread_local int checking_wait;
     14 
     15 int __real_clock_nanosleep(clockid_t clock, int flags, const struct timespec *deadline, struct timespec *remaining);
     16 
     17 int __wrap_clock_nanosleep(clockid_t clock, int flags, const struct timespec *deadline, struct timespec *remaining)
     18 {
     19     assert(!checking_wait);
     20     return __real_clock_nanosleep(clock, flags, deadline, remaining);
     21 }
     22 
     23 recomp_func_t recomp_lookup(uint32_t address)
     24 {
     25     assert(address == 0x11110000);
     26     return linux_window_lookup("user32.dll", "DefWindowProcA");
     27 }
     28 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     29 
     30 static recomp_func_t virtual_method(uint32_t object, unsigned slot)
     31 {
     32     return linux_graphics_virtual(MEM32(MEM32(object) + slot * 4));
     33 }
     34 
     35 static uint32_t invoke(recomp_func_t function, unsigned count, const uint32_t *arguments)
     36 {
     37     uint32_t stack = g_esp, caller = g_cur_func;
     38     for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]);
     39     PUSH32(g_esp, RECOMP_RETADDR);
     40     g_ebx = 0x12345678; g_esi = 0x23456789;
     41     g_edi = 0x3456789a; g_ebp = 0x456789ab;
     42     function();
     43     assert(g_esp == stack && g_cur_func == caller);
     44     assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
     45     assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
     46     return g_eax;
     47 }
     48 
     49 int main(void)
     50 {
     51     assert(guest_map("browser pacing test", 0x50000000, 0x10000));
     52     assert(!emscripten_is_main_browser_thread());
     53     g_esp = 0x5000ff00;
     54     const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
     55     memcpy((void *)ADDR(0x50001000), draw7, sizeof(draw7));
     56     const uint32_t create[] = {0, 0x50001010, 0x50001000, 0};
     57     assert(!invoke(linux_graphics_lookup("DirectDrawCreateEx"), 4, create));
     58     uint32_t object = MEM32(0x50001010);
     59     assert(!invoke(virtual_method(object, 20), 3, (uint32_t[]){object, 0, 0x808}));
     60     uint32_t arguments[] = {object, 1, 0};
     61     recomp_func_t wait = virtual_method(object, 22);
     62     assert(wait);
     63 
     64     /* A real hidden guest window and primary blit exercise the skipped-copy path. */
     65     uint32_t record = 0x50002000, text = 0x50002100, desc = 0x50002200;
     66     MEM32(record + 4) = 0x11110000;
     67     MEM32(record + 16) = 1;
     68     MEM32(record + 36) = text;
     69     strcpy((void *)ADDR(text), "AO pacing test");
     70     uint32_t atom = invoke(linux_window_lookup("user32.dll", "RegisterClassA"), 1, &record);
     71     assert(atom);
     72     uint32_t window_arguments[] = {0, atom, text, 0, 0, 0, 32, 16, 0, 0, 1, 0};
     73     uint32_t window = invoke(linux_window_lookup("user32.dll", "CreateWindowExA"), 12, window_arguments);
     74     assert(window && (SDL_GetWindowFlags(linux_window_native(window)) & SDL_WINDOW_HIDDEN));
     75     assert(!invoke(virtual_method(object, 4), 4, (uint32_t[]){object, 0, 0x50001010, 0}));
     76     uint32_t clipper = MEM32(0x50001010);
     77     assert(!invoke(virtual_method(clipper, 8), 3, (uint32_t[]){clipper, 0, window}));
     78 
     79     MEM32(desc) = 124;
     80     MEM32(desc + 4) = 1;
     81     MEM32(desc + 104) = 0x200;
     82     assert(!invoke(virtual_method(object, 6), 4, (uint32_t[]){object, desc, 0x50001010, 0}));
     83     uint32_t primary = MEM32(0x50001010);
     84     assert(!invoke(virtual_method(primary, 28), 2, (uint32_t[]){primary, clipper}));
     85     MEM32(desc + 4) = 7;
     86     MEM32(desc + 8) = 16;
     87     MEM32(desc + 12) = 32;
     88     MEM32(desc + 104) = 0x840;
     89     assert(!invoke(virtual_method(object, 6), 4, (uint32_t[]){object, desc, 0x50001010, 0}));
     90     uint32_t source = MEM32(0x50001010), rectangle = 0x50002300;
     91     memcpy((void *)ADDR(rectangle), (uint32_t[]){0, 0, 32, 16}, 16);
     92     EM_ASM({
     93         Module.refreshTimes = [];
     94         const refresh = globalThis.requestAnimationFrame;
     95         globalThis.requestAnimationFrame = callback => refresh(time => {
     96             Module.refreshTimes.push(time);
     97             callback(time);
     98         });
     99     });
    100 
    101     for (unsigned i = 0; i < 5; ++i) {
    102         if (i == 2)
    103             invoke(linux_window_lookup("user32.dll", "ShowWindow"), 2, (uint32_t[]){window, 1});
    104         if (i == 4)
    105             memcpy((void *)ADDR(rectangle), (uint32_t[]){10000, 10000, 10032, 10016}, 16);
    106         checking_wait = 1;
    107         assert(!invoke(wait, 3, arguments));
    108         checking_wait = 0;
    109         assert(EM_ASM_INT({ return Module.refreshTimes.length; }) == 0);
    110         assert(!invoke(virtual_method(primary, 5), 6,
    111             (uint32_t[]){primary, rectangle, source, 0, 0x200, 0}));
    112         assert(EM_ASM_INT({ return Module.refreshTimes.length; }) == 0);
    113     }
    114 
    115     assert(EM_ASM_INT({ return Module.refreshTimes.length === 0; }));
    116     arguments[1] = 0;
    117     assert(invoke(wait, 3, arguments) == 0x80070057);
    118     linux_graphics_shutdown();
    119     puts("PASS: hidden, visible and clipped primary blits return without scheduling or waiting; guest ABI preserved");
    120 }