aorecomp

Anarchy Online classic in the browser

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


client/runtime/browser_testing.c (16305B)


      1 #define _GNU_SOURCE
      2 #include "recomp_types.h"
      3 #include "linux_graphics.h"
      4 #include <stdatomic.h>
      5 #include <emscripten.h>
      6 
      7 static _Atomic unsigned requested;
      8 static unsigned frame;
      9 static unsigned active, pick_sequence;
     10 static uint32_t pick_camera, pick_type, pick_id;
     11 static int pick_position[2];
     12 void ao_test_publish(const char *json);
     13 
     14 /* Requests cross threads; guest objects are read only after a complete game frame. */
     15 EMSCRIPTEN_KEEPALIVE void browser_test_request(void)
     16 {
     17     atomic_store(&requested, 1);
     18 }
     19 
     20 void browser_test_pick_completed(uint32_t camera)
     21 {
     22     if (!active) return;
     23     uint32_t view = MEM32(0x2b0ee640);
     24     if (!view) return;
     25 
     26     /* ActionView_t's mode-2 detector defines the camera's normalized coordinates. */
     27     uint32_t detector = MEM32(view + 0x10);
     28     int left = MEM32(detector + 0x2c), top = MEM32(detector + 0x30);
     29     int width = (int)MEM32(detector + 0x34) - left;
     30     int height = (int)MEM32(detector + 0x38) - top;
     31     float position[2];
     32     memcpy(position, (void *)ADDR(camera + 0x108), sizeof(position));
     33     pick_position[0] = left + lroundf((position[0] + 1) * (width / 2));
     34     pick_position[1] = top + lroundf((1 - position[1]) * (height / 2));
     35     pick_camera = camera;
     36     pick_type = MEM32(camera + 0xf0);
     37     pick_id = MEM32(camera + 0xf4);
     38     ++pick_sequence;
     39 }
     40 
     41 static void vector(FILE *output, uint32_t address, unsigned count)
     42 {
     43     fputc('[', output);
     44     for (unsigned i = 0; i < count; ++i) {
     45         float value;
     46         memcpy(&value, (void *)ADDR(address + i * 4), 4);
     47         if (i) fputc(',', output);
     48         if (isfinite(value)) fprintf(output, "%.9g", value);
     49         else fputs("null", output);
     50     }
     51     fputc(']', output);
     52 }
     53 
     54 static void inventory(FILE *output, uint32_t actor)
     55 {
     56     uint32_t attributes = MEM32(actor + 0x150);
     57     fputc('[', output);
     58     if (attributes) {
     59         /* KKR inventory proxies: flags/count, identity, QL and template endpoints. */
     60         uint32_t slots = MEM32(MEM32(MEM32(attributes + 12) + 4) + 4);
     61         unsigned count = 0;
     62         for (unsigned slot = 0; slot < 78; ++slot) {
     63             uint32_t item = MEM32(slots + slot * 4);
     64             if (!item) continue;
     65             if (count++) fputc(',', output);
     66             fprintf(output, "{\"slot\":%u,\"type\":%u,\"id\":%u,\"quality\":%u,"
     67                 "\"templates\":[%u,%u],\"count\":%u}", slot, MEM32(item + 8),
     68                 MEM32(item + 12), MEM32(item + 16), MEM32(item + 20),
     69                 MEM32(item + 24), MEM32(item + 4) >> 16);
     70         }
     71     }
     72     fputc(']', output);
     73 }
     74 
     75 static void icon_grids(FILE *output, uint32_t head)
     76 {
     77     fputc('[', output);
     78     unsigned count = 0;
     79     for (uint32_t node = head ? MEM32(head) : 0; node && node != head; node = MEM32(node)) {
     80         uint32_t slider = MEM32(node + 8);
     81         /* ccr.dll!IconGrid_t::IconGrid_t binds even inactive scrollbars through
     82            SliderButton_t::SetCallback. Check its class before reading those fields. */
     83         if (MEM32(slider) != 0x27034524 || MEM32(slider + 0xb4) != 0x27011540) continue;
     84         uint32_t grid = MEM32(slider + 0xb8);
     85         if (!grid || !MEM8(grid + 0x35)) continue;
     86         if (count++) fputc(',', output);
     87         uint32_t list = MEM32(grid), cells = MEM32(list + 4);
     88         fprintf(output, "{\"position\":[%d,%d],\"cellSize\":[%d,%d],\"columns\":%u,\"rows\":%u,"
     89             "\"firstRow\":%d,\"totalCells\":%u,\"dragType\":%u,\"cells\":[",
     90             (int32_t)MEM32(grid + 8), (int32_t)MEM32(grid + 12),
     91             (int32_t)MEM32(grid + 0x18), (int32_t)MEM32(grid + 0x1c),
     92             MEM32(grid + 0x10), MEM32(grid + 0x14), (int32_t)MEM32(grid + 0x2c),
     93             MEM32(list + 8), MEM32(grid + 0x3c));
     94 
     95         /* IconGrid_t::Reposition walks this list in visual order, independently
     96            of each DragObject_t identity. Dummy cells include pending/empty icons. */
     97         unsigned index = 0, visible = 0;
     98         for (uint32_t cell = MEM32(cells); cell != cells; cell = MEM32(cell), ++index) {
     99             uint32_t object = MEM32(cell + 8);
    100             if (!MEM8(object + 8)) continue;
    101             if (visible++) fputc(',', output);
    102             fprintf(output, "{\"index\":%u,\"identity\":[%u,%u],\"bounds\":[%d,%d,%d,%d],\"dummy\":%s}",
    103                 index, MEM32(object + 0x54), MEM32(object + 0x58),
    104                 (int32_t)MEM32(object + 0x2c), (int32_t)MEM32(object + 0x30),
    105                 (int32_t)MEM32(object + 0x34), (int32_t)MEM32(object + 0x38),
    106                 MEM8(object + 0x70) ? "true" : "false");
    107         }
    108         fputs("]}", output);
    109     }
    110     fputc(']', output);
    111 }
    112 
    113 static void mission_waypoints(FILE *output)
    114 {
    115     /* BBR NewMapViewSprite_t::SetWaypoint stores one uploaded mission. */
    116     uint32_t view = MEM32(0x2b0eeb14), sprite = view ? MEM32(view + 0x5c) : 0;
    117     fputs("{\"local\":[", output);
    118     if (sprite && MEM8(sprite + 0xdc))
    119         fprintf(output, "[%u,%u]", MEM32(sprite + 0xc0), MEM32(sprite + 0xc4));
    120     fputs("],\"planet\":", output);
    121 
    122     /* DDR PlanetMapModule and BottomBarModule retain their own last upload. */
    123     if (MEM8(0x2b0ef07c))
    124         fprintf(output, "[%u,%u]", MEM32(0x2b0ef070), MEM32(0x2b0ef074));
    125     else fputs("null", output);
    126     uint32_t bottom = MEM32(0x2b0ef0a4), compass = bottom ? MEM32(bottom + 0x3c) : 0;
    127     fputs(",\"compass\":", output);
    128     if (compass && MEM8(compass + 0x30))
    129         fprintf(output, "[%u,%u]", MEM32(compass + 0x14), MEM32(compass + 0x18));
    130     else fputs("null", output);
    131     fputc('}', output);
    132 }
    133 
    134 static void actor(FILE *output, uint32_t object, uint32_t type, uint32_t id, int local)
    135 {
    136     uint32_t vehicle = MEM32(object + 0x60);
    137     fprintf(output, "{\"type\":%u,\"id\":%u,\"position\":", type, id);
    138     /* Vehicle global coordinates differ from local coordinates inside scene sections. */
    139     vector(output, vehicle + (MEM32(vehicle + 0x28) ? 0xe8 : 0x54), 3);
    140     float position[3], screen[3];
    141     memcpy(position, (void *)ADDR(vehicle + (MEM32(vehicle + 0x28) ? 0xe8 : 0x54)), sizeof(position));
    142     fputs(",\"screenPosition\":", output);
    143     if (linux_graphics_project(position, screen))
    144         fprintf(output, "[%.9g,%.9g,%.9g]", screen[0], screen[1], screen[2]);
    145     else fputs("null", output);
    146     fputs(",\"rotation\":", output);
    147     vector(output, vehicle + 0x7c, 4);
    148     fprintf(output, ",\"playfield\":%u,\"deleting\":%s", MEM32(object + 0x78),
    149         MEM8(object + 0x94) ? "true" : "false");
    150     if (type == 51016) {
    151         /* KKR Door_t embeds n3Dynel at +0x8c; BBR retains animation time on
    152            the VisualMesh's root RRefFrame. Read without advancing animation. */
    153         uint32_t door = object - 0x8c, mesh = MEM32(object + 0xc4);
    154         uint32_t frame = mesh ? MEM32(mesh + 0x10) : 0;
    155         fprintf(output, ",\"door\":{\"flags\":%u,\"latched\":%s,\"duration\":%.9g,\"time\":%.9g}",
    156             MEM32(door + 0x34), MEM8(door + 0x1c5) ? "true" : "false",
    157             mesh ? MEMF(mesh + 0x24) : 0, frame ? MEMF(frame + 0x12c) : 0);
    158     }
    159     if (type == 50000) {
    160         /* KKR's stat dictionary has 1024 raw entries; modifiers are separate. */
    161         uint32_t stats = MEM32(MEM32(object + 0xe8) + 4);
    162         uint32_t combat = MEM32(object + 0x16c);
    163         fprintf(output, ",\"fighting\":%u,\"combatTarget\":%u,\"stats\":[",
    164             combat ? MEM32(combat + 0x50) : 0, combat ? MEM32(combat + 0x5c) : 0);
    165         for (unsigned i = 0; i < 1024; ++i) {
    166             if (i) fputc(',', output);
    167             fprintf(output, "%d", (int32_t)MEM32(stats + i * 4));
    168         }
    169         /* KKR sub_3606DFB0 mode3 / sub_36075CD0: raw AC plus signed modifiers.
    170            Read the modifier tree without calling getters that may insert keys. */
    171         uint32_t skills = MEM32(object + 0x154);
    172         uint32_t modifiers = skills ? MEM32(skills + 8) : 0;
    173         uint32_t head = modifiers ? MEM32(modifiers + 4) : 0;
    174         fputs("],\"effectiveArmor\":[", output);
    175         for (unsigned stat = 90; stat < 98; ++stat) {
    176             uint32_t value = MEM32(stats + stat * 4);
    177             uint32_t node = head ? MEM32(head + 4) : 0;
    178             while (node && node != head) {
    179                 uint32_t key = MEM32(node + 12);
    180                 if (stat == key) {
    181                     value += MEM32(node + 16);
    182                     break;
    183                 }
    184                 node = MEM32(node + (stat < key ? 0 : 8));
    185             }
    186             if (stat != 90) fputc(',', output);
    187             fprintf(output, "%d", (int32_t)value);
    188         }
    189         fputs("],\"weapons\":[", output);
    190         unsigned weapons = 0;
    191         if (combat) {
    192             /* KKR sub_360AB5A0 consumes these real results at animation impact. */
    193             for (unsigned slot = 0; slot < 16; ++slot) {
    194                 uint32_t weapon = MEM32(combat + 0x0c + slot * 4);
    195                 if (!weapon) continue;
    196                 if (weapons++) fputc(',', output);
    197                 /* KKR sub_3607A660 uses the controller's bound WeaponItem at +0x20;
    198                    sub_36003000 reads that item's stat container directly. */
    199                 uint32_t weapon_item = MEM32(weapon + 0x20);
    200                 int32_t loaded_ammo = -1;
    201                 if (weapon_item) {
    202                     uint32_t weapon_stats = MEM32(MEM32(weapon_item + 4) + 4);
    203                     loaded_ammo = (int32_t)MEM32(weapon_stats + 26 * 4);
    204                 }
    205                 uint32_t read = MEM32(weapon + 0x18), write = MEM32(weapon + 0x14);
    206                 uint32_t results = MEM32(MEM32(weapon + 0x10) + 4);
    207                 fprintf(output, "{\"slot\":%u,\"loadedAmmo\":%d,\"read\":%u,\"write\":%u,\"pending\":[",
    208                     slot, loaded_ammo, read, write);
    209                 for (uint32_t index = read; index != write; index = (index + 1) % 50) {
    210                     if (index != read) fputc(',', output);
    211                     fprintf(output, "[%d,%u]", (int32_t)MEM32(results + index * 8), MEM32(results + index * 8 + 4));
    212                 }
    213                 fputs("]}", output);
    214             }
    215         }
    216         fputc(']', output);
    217         /* kkr.dll/lifted-0026.c::sub_36052A20 / sub_36052790 enumerate action types. */
    218         uint32_t actions = MEM32(object + 0x184);
    219         fputs(",\"actions\":[", output);
    220         unsigned count = 0;
    221         if (actions) {
    222             for (int i = 0; i < (int)MEM32(actions + 4); ++i) {
    223                 uint32_t action = MEM32(MEM32(MEM32(actions + 8) + 4) + i * 4);
    224                 if (!action) continue;
    225                 if (count++) fputc(',', output);
    226                 fprintf(output, "%d", (int32_t)MEM32(action + 0x18));
    227             }
    228         }
    229         fputc(']', output);
    230         if (local) {
    231             fputs(",\"inventory\":", output);
    232             inventory(output, object);
    233         }
    234     }
    235     fputc('}', output);
    236 }
    237 
    238 static void directory(FILE *output, uint32_t node, uint32_t nil, uint32_t local, unsigned *count, unsigned *local_id)
    239 {
    240     if (node == nil) return;
    241     directory(output, MEM32(node), nil, local, count, local_id);
    242     uint32_t type = MEM32(node + 12), id = MEM32(node + 16), object = MEM32(node + 20);
    243     if ((type == 50000 || type == 51005 || type == 51016 || type == 51035 || type == 51050 || type == 51059 ||
    244             type == 56001 || type == 56006) && MEM32(object + 0x60)) {
    245         if ((*count)++) fputc(',', output);
    246         actor(output, object, type, id, object == local);
    247         if (object == local) *local_id = id;
    248     }
    249     directory(output, MEM32(node + 8), nil, local, count, local_id);
    250 }
    251 
    252 void browser_test_frame(void)
    253 {
    254     ++frame;
    255     if (!atomic_exchange(&requested, 0)) return;
    256     active = 1;
    257 
    258     char *json;
    259     size_t length;
    260     FILE *output = open_memstream(&json, &length);
    261     if (!output) abort();
    262     uint32_t engine = MEM32(0x2d01752c), local = engine ? MEM32(engine + 0x88) : 0;
    263     /* KKR sub_36072000 owns selection in the local actor's command helper. */
    264     uint32_t commands = local ? MEM32(local + 0x168) : 0;
    265     /* ccr.dll!DragAndDrop_t::GetDragObject returns the mouse-held object. */
    266     uint32_t drag = MEM32(0x27048b6c), dragged = drag ? MEM32(drag + 0x20) : 0;
    267     fprintf(output, "{\"frame\":%u,\"screen\":[%u,%u],\"hover\":[%u,%u],\"selected\":[%u,%u],\"dragged\":[%u,%u],",
    268         frame, MEM32(0x27048be8), MEM32(0x27048bec),
    269         engine ? MEM32(engine + 0xd8) : 0, engine ? MEM32(engine + 0xdc) : 0,
    270         commands ? MEM32(commands + 0x70) : 0, commands ? MEM32(commands + 0x74) : 0,
    271         dragged ? MEM32(dragged + 0x54) : 0, dragged ? MEM32(dragged + 0x58) : 0);
    272 
    273     /* CCR IconMenu_t owns the action hotspots and selected command. */
    274     uint32_t menu = MEM32(0x27048b98);
    275     fputs("\"itemMenu\":", output);
    276     if (menu) {
    277         fprintf(output, "{\"identity\":[%u,%u],\"actions\":[", MEM32(menu), MEM32(menu + 4));
    278         for (unsigned i = 0; i < MEM32(menu + 0x10); ++i) {
    279             uint32_t button = MEM32(menu + 0x14 + i * 4);
    280             if (i) fputc(',', output);
    281             fprintf(output, "{\"command\":%u,\"bounds\":[%d,%d,%d,%d]}", MEM32(button + 0x5c),
    282                 (int32_t)MEM32(button + 0x68), (int32_t)MEM32(button + 0x6c),
    283                 (int32_t)MEM32(button + 0x68) + 32, (int32_t)MEM32(button + 0x6c) + 32);
    284         }
    285         fputs("]}", output);
    286     } else fputs("null", output);
    287     fputc(',', output);
    288 
    289     /* CCR's pointer owner and artwork table expose the native click hotspot. */
    290     uint32_t cursor = MEM32(0x27048bb4);
    291     fputs("\"cursor\":", output);
    292     if (cursor) {
    293         uint32_t entry = 0x270485e0 + MEM32(cursor + 8) * 20;
    294         uint32_t sprite = MEM32(entry + 16);
    295         fprintf(output, "{\"shape\":%u,\"visible\":%s,\"hotspot\":[%u,%u],\"drawSize\":",
    296             MEM32(cursor + 8), MEM8(cursor + 0x10) ? "true" : "false", MEM32(entry), MEM32(entry + 4));
    297         vector(output, sprite + 0x3c, 2);
    298         fputc('}', output);
    299     } else fputs("null", output);
    300     fputc(',', output);
    301 
    302     uint32_t scene = MEM32(0x3806ec04), camera = scene ? MEM32(scene + 0x80) : 0;
    303     fputs("\"camera\":", output);
    304     if (camera) {
    305         fprintf(output, "{\"firstPerson\":%s,\"angles\":", MEM32(camera + 0x19c) ? "false" : "true");
    306         vector(output, camera + 0xc4, 2);
    307         uint32_t vehicle = MEM32(camera + 0x60);
    308         fputs(",\"position\":", output);
    309         vector(output, vehicle + (MEM32(vehicle + 0x28) ? 0xe8 : 0x54), 3);
    310         fputc('}', output);
    311     } else fputs("null", output);
    312 
    313     fputs(",\"pick\":", output);
    314     if (pick_sequence && camera == pick_camera)
    315         fprintf(output, "{\"sequence\":%u,\"position\":[%d,%d],\"identity\":[%u,%u]}",
    316             pick_sequence, pick_position[0], pick_position[1], pick_type, pick_id);
    317     else fputs("null", output);
    318 
    319     /* ddr.dll SceneBase_t::GetSceneID and NameScene_t::SetState. */
    320     uint32_t creation = MEM32(0x2b0ee7d0);
    321     fputs(",\"creation\":", output);
    322     if (creation) {
    323         fprintf(output, "{\"scene\":%u,\"hover\":%u,\"nameState\":%u,\"ready\":%s}",
    324             MEM32(creation + 0x40), MEM32(creation + 0x5c),
    325             creation == MEM32(0x2b0ee7e0) ? MEM32(creation + 0xa4) : 0,
    326             MEM32(0x2b0e42a8) ? "false" : "true");
    327     } else fputs("null", output);
    328 
    329     fputs(",\"actors\":[", output);
    330     uint32_t actors = MEM32(0x3806c110);
    331     /* Resolve the engine's local actor pointer through the same directory. */
    332     unsigned count = 0, local_id = 0;
    333     if (actors) directory(output, MEM32(MEM32(actors + 4) + 4), MEM32(actors + 8), local, &count, &local_id);
    334     fprintf(output, "],\"localId\":%u,\"regions\":[", local_id);
    335     /* CCR sub_27010890 walks this list in native hit-test order. */
    336     uint32_t module = MEM32(0x27048b7c);
    337     uint32_t head = module && MEM8(module + 4) ? MEM32(MEM32(module) + 4) : 0;
    338     count = 0;
    339     for (uint32_t node = head ? MEM32(head) : 0; node && node != head; node = MEM32(node)) {
    340         uint32_t region = MEM32(node + 8);
    341         if (!MEM8(region + 8)) continue;
    342         if (count++) fputc(',', output);
    343         fprintf(output, "[%d,%d,%d,%d]", (int32_t)MEM32(region + 0x2c),
    344             (int32_t)MEM32(region + 0x30), (int32_t)MEM32(region + 0x34), (int32_t)MEM32(region + 0x38));
    345     }
    346     fputs("],\"grids\":", output);
    347     icon_grids(output, head);
    348     fputs(",\"missionWaypoints\":", output);
    349     mission_waypoints(output);
    350     fputc('}', output);
    351     if (fclose(output)) abort();
    352     ao_test_publish(json);
    353     free(json);
    354 }