aorecomp

Anarchy Online classic in the browser

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


client/tests/native_input.c (8158B)


      1 #include "recomp_types.h"
      2 #include <assert.h>
      3 
      4 void game_data_ccr_dll(void);
      5 void sub_27003930(void);
      6 void sub_27003C60(void);
      7 void sub_27003E70(void);
      8 void sub_27003ED0(void);
      9 void sub_27003F00(void);
     10 void sub_270040C0(void);
     11 void sub_27004370(void);
     12 void sub_270046E0(void);
     13 void sub_270047F0(void);
     14 void sub_27004EA0(void);
     15 void sub_27004EF0(void);
     16 void sub_270051A0(void);
     17 void sub_27005500(void);
     18 void sub_27005540(void);
     19 
     20 static unsigned hotspot_count, command_count;
     21 static uint32_t hotspot_points[8], command_references[8];
     22 
     23 /* ccr.dll::InputConfig_t runs FramePreProcess before FrameProcess/ProcessInput. */
     24 /* Scene hit testing and command execution are outside the input-frame contract. */
     25 static void check_hotspot(void)
     26 {
     27     /* The controlled scene uses x as its void reference; zero is empty space. */
     28     uint32_t x = MEM32(g_esp + 4);
     29     assert(hotspot_count < 8);
     30     hotspot_points[hotspot_count++] = x;
     31     MEM32(0x500000c4) = x;
     32     g_esp += 16;
     33 }
     34 
     35 static void execute_command(void)
     36 {
     37     assert(g_ecx == 0x50000900);
     38     assert(command_count < 8);
     39     command_references[command_count++] = MEM32(g_esp + 12);
     40     g_esp += 20;
     41 }
     42 
     43 static void timer_instance(void) { g_eax = 0x50000f00; g_esp += 4; }
     44 static void hotspot_instance(void) { g_eax = 0x50000e00; g_esp += 4; }
     45 static void no_display(void) { g_eax = 0; g_esp += 4; }
     46 static void no_camera(void) { g_esp += 4; }
     47 static void no_modifiers(void) { g_eax = 0; g_esp += 8; }
     48 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
     49 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     50 recomp_func_t recomp_lookup(uint32_t address)
     51 {
     52     switch (address) {
     53     case 0x27003930: return sub_27003930;
     54     case 0x27003E70: return sub_27003E70;
     55     case 0x27003ED0: return sub_27003ED0;
     56     case 0x27004370: return sub_27004370;
     57     case 0x270046E0: return sub_270046E0;
     58     case 0x270047F0: return sub_270047F0;
     59     case 0x27004EA0: return sub_27004EA0;
     60     case 0x27004EF0: return sub_27004EF0;
     61     case 0x270051A0: return sub_270051A0;
     62     case 0x27005500: return sub_27005500;
     63     case 0x27005540: return sub_27005540;
     64     case 0x27001570: return execute_command;
     65     case 0x270029e0: return hotspot_instance;
     66     case 0x27010770: return check_hotspot;
     67     case 0x270041f0: return no_camera;
     68     case 0x11110000: return timer_instance;
     69     case 0x11110004: return no_display;
     70     case 0x11110008: return no_modifiers;
     71     default: fprintf(stderr, "unexpected input call: %08x\n", address); abort();
     72     }
     73 }
     74 
     75 static void initialize_input(void)
     76 {
     77     memset((void *)ADDR(0x50000000), 0, 0x1000);
     78     g_esp = 0x5000ff00;
     79     hotspot_count = command_count = 0;
     80 
     81     /* Reserve input-vector capacity so AddUserInput never needs an allocator. */
     82     MEM32(0x500000dc) = 0x50000400;
     83     MEM32(0x50000404) = MEM32(0x50000408) = 0x50000600;
     84     MEM32(0x5000040c) = 0x50000680;
     85 
     86     /* Keep press/release events below the native mouse-hold threshold. */
     87     MEMF(0x50000014) = 0.3f;
     88     MEMF(0x50000138) = MEMF(0x5000013c) = MEMF(0x50000140) = 10;
     89     MEM32(0x50000f14) = 0x50000f80;
     90     MEMF(0x50000f80) = 0.016f;
     91 
     92     /* Two commands consume the current hotspot reference on right press/release. */
     93     MEM32(0x50000010) = 0x50000420;
     94     MEM32(0x50000424) = 0x50000440;
     95     MEM32(0x50000428) = 2;
     96     MEM32(0x50000440) = 0x50000460;
     97     MEM32(0x50000460) = 0x50000480;
     98     MEM32(0x50000468) = 0x50000500;
     99     MEM32(0x50000480) = 0x50000440;
    100     MEM32(0x50000488) = 0x50000540;
    101     MEM32(0x50000508) = 5;
    102     MEM32(0x50000548) = 8;
    103     MEM32(0x5000050c) = MEM32(0x5000054c) = 4;
    104     MEM32(0x50000510) = MEM32(0x50000550) = 0x50000900;
    105 }
    106 
    107 static void queue_input(uint32_t action, uint32_t x)
    108 {
    109     MEM32(0x50000800) = action;
    110     MEM32(0x50000804) = action != 0;
    111     MEM32(0x50000808) = x;
    112     MEM32(0x5000080c) = 20;
    113     g_ecx = 0x50000000;
    114     MEM32(g_esp + 4) = 0x50000800;
    115     sub_27003C60();
    116     assert(g_esp == 0x5000ff08);
    117     g_esp -= 8;
    118 }
    119 
    120 static void process_frame(void)
    121 {
    122     g_ecx = 0x50000000;
    123     sub_27003F00();
    124     assert(g_esp == 0x5000ff04);
    125     g_esp -= 4;
    126     g_ecx = 0x50000000;
    127     sub_270040C0();
    128     assert(g_esp == 0x5000ff04);
    129     g_esp -= 4;
    130     assert(MEM32(0x50000408) == 0x50000600);
    131 }
    132 
    133 static void clicks_in_separate_frames_use_their_own_hotspots(void)
    134 {
    135     initialize_input();
    136     queue_input(5, 100);
    137     queue_input(8, 100);
    138 
    139     process_frame();
    140     queue_input(5, 200);
    141     queue_input(8, 200);
    142     process_frame();
    143 
    144     assert(hotspot_count == 4 && command_count == 4);
    145     assert(command_references[0] == 100 && command_references[1] == 100);
    146     assert(command_references[2] == 200 && command_references[3] == 200);
    147 }
    148 
    149 static void clicks_in_one_frame_use_the_final_hotspot(void)
    150 {
    151     initialize_input();
    152     queue_input(5, 100);
    153     queue_input(8, 100);
    154     queue_input(5, 200);
    155     queue_input(8, 200);
    156 
    157     process_frame();
    158 
    159     assert(hotspot_count == 4 && command_count == 4);
    160     assert(hotspot_points[0] == 100 && hotspot_points[1] == 100);
    161     assert(hotspot_points[2] == 200 && hotspot_points[3] == 200);
    162     assert(command_references[0] == 200 && command_references[1] == 200);
    163     assert(command_references[2] == 200 && command_references[3] == 200);
    164 }
    165 
    166 static void later_empty_click_is_visible_to_earlier_click_commands(void)
    167 {
    168     initialize_input();
    169     queue_input(5, 100);
    170     queue_input(8, 100);
    171     queue_input(5, 0);
    172     queue_input(8, 0);
    173 
    174     process_frame();
    175 
    176     assert(hotspot_count == 4 && command_count == 4);
    177     assert(hotspot_points[0] == 100 && hotspot_points[1] == 100);
    178     assert(hotspot_points[2] == 0 && hotspot_points[3] == 0);
    179     assert(command_references[0] == 0 && command_references[1] == 0);
    180     assert(command_references[2] == 0 && command_references[3] == 0);
    181 }
    182 
    183 static void camera_release_ignores_held_modifiers(void)
    184 {
    185     for (unsigned modifiers = 0; modifiers < 8; ++modifiers) {
    186         initialize_input();
    187         MEM8(0x50000064) = 1; /* MouseLookMode in the persistent mode array. */
    188         queue_input(8 | (modifiers << 17), 100);
    189 
    190         process_frame();
    191 
    192         assert(command_count == 1);
    193         assert(command_references[0] == 100);
    194     }
    195 }
    196 
    197 static void modified_release_outside_camera_keeps_its_binding(void)
    198 {
    199     initialize_input();
    200     MEM32(0x50000548) = 0x20008; /* Shift + RightRelease. */
    201     queue_input(0x20008, 100);
    202 
    203     process_frame();
    204 
    205     assert(command_count == 1);
    206     assert(command_references[0] == 100);
    207 }
    208 
    209 static void laptop_shortcuts_release_after_shift(void)
    210 {
    211     const unsigned keys[][2] = {
    212         {32, 74}, {33, 76}, {34, 72}, {35, 78}, {112, 75}, {114, 77}, {117, 70},
    213     };
    214     for (unsigned i = 0; i < sizeof(keys) / sizeof(keys[0]); ++i) {
    215         initialize_input();
    216         MEM8(0x5000017c + keys[i][1]) = 1;
    217         MEM32(0x50000508) = keys[i][1];
    218         MEM32(0x50000548) = keys[i][1] | 0x100000;
    219         queue_input(keys[i][0] | 0x20000, 100);
    220         MEM8(0x50000604) = 0; /* Keyboard input. */
    221 
    222         process_frame();
    223         queue_input(keys[i][0] | 0x100000, 100);
    224         MEM8(0x50000604) = 0;
    225         process_frame();
    226 
    227         assert(command_count == 2);
    228     }
    229 }
    230 
    231 static void laptop_shortcuts_preserve_text_input(void)
    232 {
    233     initialize_input();
    234     MEM8(0x5000005e) = 1; /* Native text-entry mode 3. */
    235     MEM32(0x50000508) = 70;
    236     queue_input(0x20075, 100); /* Shift-0 must remain text, not autorun. */
    237     MEM8(0x50000604) = 0;
    238 
    239     process_frame();
    240 
    241     assert(command_count == 0);
    242     assert(MEM32(0x50000600) == 0x20075);
    243 }
    244 
    245 int main(void)
    246 {
    247     assert(guest_map("native input fixture", 0x50000000, 0x10000));
    248     game_data_ccr_dll();
    249     MEM32(0x270291e4) = 0x11110000;
    250     MEM32(0x27029274) = 0x11110004;
    251     MEM32(0x27029158) = 0x11110008;
    252     clicks_in_separate_frames_use_their_own_hotspots();
    253     clicks_in_one_frame_use_the_final_hotspot();
    254     later_empty_click_is_visible_to_earlier_click_commands();
    255     camera_release_ignores_held_modifiers();
    256     modified_release_outside_camera_keeps_its_binding();
    257     laptop_shortcuts_release_after_shift();
    258     laptop_shortcuts_preserve_text_input();
    259     puts("native input: event coordinates, two-pass frame dispatch and shared hotspot references passed");
    260 }