aorecomp

Anarchy Online classic in the browser

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


commit 394c28d72e0ab490cbef294566c95e5500f335df
parent d98939939be18cf0676409acd5c72da81bb96875
Author: anon <anon@example.com>
Date:   Wed, 11 Nov 2026 02:12:44 +0000

Decode browser PNG textures directly with libpng

Diffstat:
Mclient/Makefile.wasm | 10++++++++--
Mclient/game/bbr.dll/lifted-0000.c | 9+++++++++
Aclient/runtime/browser_png.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aclient/runtime/browser_png.h | 7+++++++
Aclient/tests/browser_png.c | 159+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 283 insertions(+), 2 deletions(-)

diff --git a/client/Makefile.wasm b/client/Makefile.wasm @@ -5,7 +5,7 @@ CFLAGS := -Oz -flto=thin -fno-strict-aliasing -pthread -sSUPPORT_LONGJMP=wasm PORT_FLAGS := -sUSE_SDL=2 -sUSE_FREETYPE=1 -sUSE_ZLIB=1 RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_music.c runtime/browser_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/browser_network.c runtime/browser_asset_cache.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c runtime/browser_host.c runtime/browser_loop.c runtime/browser_resolution.c runtime/browser_profile.c GAME_SOURCES := $(wildcard game/*/*.c) game/dispatch.c -RUNTIME_SOURCES += runtime/browser_testing.c +RUNTIME_SOURCES += runtime/browser_testing.c runtime/browser_png.c DATA_SOURCES := $(wildcard data/*.c) VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c) OBJECTS := $(patsubst %.c,build/wasm/objects/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(VENDOR_SOURCES) $(RUNTIME_SOURCES)) @@ -85,7 +85,7 @@ build/wasm/web/context-test.js: tests/browser_context.c runtime/linux_graphics.c build/wasm/web/game.js build/wasm/web/game.wasm &: $(OBJECTS) runtime/browser_network.js runtime/browser_host.js runtime/browser_fetch.js runtime/browser_audio.js runtime/browser_profile.js ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a Makefile.wasm @mkdir -p $(@D) - $(CC) $(CFLAGS) $(PORT_FLAGS) $(OBJECTS) ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a -sUSE_MPG123=1 -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=8 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sOFFSCREENCANVAS_SUPPORT -sOFFSCREEN_FRAMEBUFFER -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=268435456 -sSTACK_SIZE=1048576 --js-library runtime/browser_network.js --js-library runtime/browser_host.js --js-library runtime/browser_fetch.js --js-library runtime/browser_audio.js --js-library runtime/browser_profile.js -sBINARYEN_EXTRA_PASSES=merge-similar-functions -Wl,--thinlto-jobs=8 -Wl,--thinlto-cache-dir=build/wasm/lto-cache --emit-symbol-map -o build/wasm/web/game.js + $(CC) $(CFLAGS) $(PORT_FLAGS) -sUSE_LIBPNG=1 $(OBJECTS) ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a -sUSE_MPG123=1 -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=8 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sOFFSCREENCANVAS_SUPPORT -sOFFSCREEN_FRAMEBUFFER -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=268435456 -sSTACK_SIZE=1048576 --js-library runtime/browser_network.js --js-library runtime/browser_host.js --js-library runtime/browser_fetch.js --js-library runtime/browser_audio.js --js-library runtime/browser_profile.js -sBINARYEN_EXTRA_PASSES=merge-similar-functions -Wl,--thinlto-jobs=8 -Wl,--thinlto-cache-dir=build/wasm/lto-cache --emit-symbol-map -o build/wasm/web/game.js build/wasm/web/index.html: browser/index.html @mkdir -p $(@D) @@ -94,6 +94,7 @@ build/wasm/web/index.html: browser/index.html build/wasm/objects/runtime/linux_audio.o: CPPFLAGS += -I../local/wasm/deps/include build/wasm/objects/runtime/linux_gdi.o: CFLAGS += $(PORT_FLAGS) build/wasm/objects/runtime/gl_renderer.o: CFLAGS += -sUSE_ZLIB=1 +build/wasm/objects/runtime/browser_png.o: CFLAGS += -O2 -sUSE_LIBPNG=1 build/wasm/objects/runtime/linux_graphics.o build/wasm/objects/runtime/linux_window.o build/wasm/objects/runtime/linux_audio.o build/wasm/objects/runtime/browser_audio.o: CFLAGS += -sUSE_SDL=2 objects: $(OBJECTS) @@ -261,3 +262,8 @@ build/wasm/reference-persistence.js: tests/reference_persistence.c game/or.dll/l build/wasm/player-voice.js: tests/player_voice.c game/mmr.dll/lifted-0000.c game/mmr.dll/lifted-0002.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + +# Exercise the browser decoder's guest stream, pixel layout and ownership. +build/wasm/png-decoding.js: tests/browser_png.c runtime/browser_png.c runtime/browser_png.h runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -sUSE_LIBPNG=1 $(filter %.c,$^) -o $@ diff --git a/client/game/bbr.dll/lifted-0000.c b/client/game/bbr.dll/lifted-0000.c @@ -5,6 +5,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#ifdef __EMSCRIPTEN__ +#include "browser_png.h" +#endif void sub_25001000(void) { int _fpu_cmp = 0; @@ -1369,6 +1372,12 @@ L_25001829: } void sub_25001830(void) { +#ifdef __EMSCRIPTEN__ + g_cur_func = 0x25001830u; + g_eax = browser_png_load(g_ecx, MEM32(g_esp + 4)); + g_esp += 8; + return; +#endif int _fpu_cmp = 0; uint32_t _cf = 0; int _df = 1; diff --git a/client/runtime/browser_png.c b/client/runtime/browser_png.c @@ -0,0 +1,100 @@ +#include "browser_png.h" +#include "browser_profile.h" +#include "guest_heap.h" +#include "recomp_types.h" +#include <png.h> + +struct png_load { + uint32_t stream, scratch, pixels, palette; + png_bytep *rows; +}; + +static void read_stream(png_structp png, png_bytep output, png_size_t size) +{ + struct png_load *load = png_get_io_ptr(png); + while (size) { + unsigned count = size > 8192 ? 8192 : (unsigned)size; + g_ecx = load->stream; + PUSH32(g_esp, count); + PUSH32(g_esp, load->scratch); + PUSH32(g_esp, RECOMP_RETADDR); + recomp_lookup(MEM32(MEM32(load->stream)))(); + memcpy(output, (void *)ADDR(load->scratch), count); + output += count; + size -= count; + } +} + +int browser_png_load(uint32_t image, uint32_t stream) +{ + double start = ao_profile_begin("PNG decode"); + png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + png_infop info = png ? png_create_info_struct(png) : NULL; + struct png_load *load = calloc(1, sizeof(*load)); + int result = -1; + if (!png || !info || !load) goto done; + load->stream = stream; + load->scratch = guest_malloc(8192); + if (!load->scratch) goto done; + if (setjmp(png_jmpbuf(png))) goto done; + + /* Match bbr.dll!sub_25001830: unpack, truncate 16-bit samples and use BGR. + * Palette indices and transparency chunks retain the guest's semantics. */ + png_set_read_fn(png, load, read_stream); + png_read_info(png, info); + int depth = png_get_bit_depth(png, info); + int color = png_get_color_type(png, info); + if (depth < 8) png_set_packing(png); + if (color == PNG_COLOR_TYPE_GRAY && depth < 8) png_set_expand(png); + if (depth == 16) png_set_strip_16(png); + png_set_bgr(png); + png_set_interlace_handling(png); + png_read_update_info(png, info); + + /* Guest image owners free these buffers through the guest heap. */ + unsigned width = png_get_image_width(png, info); + unsigned height = png_get_image_height(png, info); + size_t stride = png_get_rowbytes(png, info); + if (!height || stride > UINT32_MAX / height) png_error(png, "PNG image too large"); + load->pixels = guest_malloc(stride * height); + load->rows = malloc(height * sizeof(*load->rows)); + if (!load->pixels || !load->rows) png_error(png, "PNG allocation failed"); + for (unsigned y = 0; y < height; ++y) + load->rows[y] = (void *)ADDR(load->pixels + y * stride); + + /* The guest stores palettes as BGRX and supplies a ramp for grayscale. */ + png_colorp palette; + int colors; + if (png_get_PLTE(png, info, &palette, &colors)) { + load->palette = guest_malloc(colors * 4); + if (!load->palette) png_error(png, "PNG palette allocation failed"); + for (int i = 0; i < colors; ++i) + MEM32(load->palette + i * 4) = palette[i].blue | + (uint32_t)palette[i].green << 8 | (uint32_t)palette[i].red << 16; + } else if (color == PNG_COLOR_TYPE_GRAY) { + load->palette = guest_malloc(1024); + if (!load->palette) png_error(png, "PNG palette allocation failed"); + for (unsigned i = 0; i < 256; ++i) MEM32(load->palette + i * 4) = i * 0x010101; + } + png_read_image(png, load->rows); + png_read_end(png, NULL); + MEM32(image + 0x110) = width; + MEM32(image + 0x114) = height; + MEM32(image + 0x10c) = png_get_channels(png, info) * 8; + MEM32(image + 0x118) = load->pixels; + MEM32(image + 0x11c) = load->palette; + load->pixels = load->palette = 0; + result = 0; + +done: + png_destroy_read_struct(&png, &info, NULL); + if (load) { + guest_free(load->scratch); + guest_free(load->pixels); + guest_free(load->palette); + free(load->rows); + free(load); + } + ao_profile_end("PNG decode", start, NULL); + return result; +} diff --git a/client/runtime/browser_png.h b/client/runtime/browser_png.h @@ -0,0 +1,7 @@ +#ifndef BROWSER_PNG_H +#define BROWSER_PNG_H + +#include <stdint.h> +int browser_png_load(uint32_t image, uint32_t stream); + +#endif diff --git a/client/tests/browser_png.c b/client/tests/browser_png.c @@ -0,0 +1,159 @@ +#include "browser_png.h" +#include "guest_heap.h" +#include "recomp_types.h" +#include <assert.h> +#include <png.h> + +#ifdef __EMSCRIPTEN__ +double ao_profile_begin(const char *name) { return 0; } +void ao_profile_end(const char *name, double start, const char *detail) {} +#endif + +static unsigned char encoded[65536]; +static size_t encoded_size, read_offset; +static uint32_t image, stream; + +static void write_png(png_structp png, png_bytep bytes, png_size_t count) +{ + assert(encoded_size + count <= sizeof(encoded)); + memcpy(encoded + encoded_size, bytes, count); + encoded_size += count; +} + +static void read_guest_stream(void) +{ + assert(g_ecx == stream); + unsigned count = MEM32(g_esp + 8); + assert(read_offset + count <= encoded_size); + memcpy((void *)ADDR(MEM32(g_esp + 4)), encoded + read_offset, count); + read_offset += count; + g_esp += 12; +} + +recomp_func_t recomp_lookup(uint32_t address) +{ + assert(address == 0x12345678); + return read_guest_stream; +} + +static void encode(int depth, int color, int interlace, const unsigned char *pixels, unsigned stride) +{ + encoded_size = read_offset = 0; + png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + png_infop info = png_create_info_struct(png); + assert(png && info); + if (setjmp(png_jmpbuf(png))) abort(); + png_set_write_fn(png, NULL, write_png, NULL); + png_set_IHDR(png, info, 2, 2, depth, color, interlace, 0, 0); + if (color == PNG_COLOR_TYPE_PALETTE) { + png_color palette[] = {{10, 20, 30}, {40, 50, 60}}; + unsigned char alpha[] = {0, 128}; + png_set_PLTE(png, info, palette, 2); + png_set_tRNS(png, info, alpha, 2, NULL); + } + png_set_gAMA(png, info, 0.7); + png_write_info(png, info); + png_bytep rows[] = {(png_bytep)pixels, (png_bytep)pixels + stride}; + png_write_image(png, rows); + png_write_end(png, info); + png_destroy_write_struct(&png, &info); +} + +static void check_pixels(unsigned bits, const unsigned char *expected, unsigned size) +{ + assert(MEM32(image + 0x110) == 2 && MEM32(image + 0x114) == 2); + assert(MEM32(image + 0x10c) == bits); + assert(!memcmp((void *)ADDR(MEM32(image + 0x118)), expected, size)); + assert(g_esp == 0x50010000); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + assert(read_offset == encoded_size); + guest_free(MEM32(image + 0x118)); + guest_free(MEM32(image + 0x11c)); +} + +static void rgba(void) +{ + const unsigned char pixels[] = {10,20,30,0, 40,50,60,1, 70,80,90,128, 100,110,120,255}; + encode(8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_ADAM7, pixels, 8); + + int result = browser_png_load(image, stream); + + const unsigned char expected[] = {30,20,10,0, 60,50,40,1, 90,80,70,128, 120,110,100,255}; + assert(result == 0); + check_pixels(32, expected, sizeof(expected)); +} + +static void rgb16(void) +{ + const unsigned char pixels[] = {10,1,20,2,30,3, 40,4,50,5,60,6, 70,7,80,8,90,9, 100,10,110,11,120,12}; + encode(16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, pixels, 12); + + int result = browser_png_load(image, stream); + + const unsigned char expected[] = {30,20,10, 60,50,40, 90,80,70, 120,110,100}; + assert(result == 0); + check_pixels(24, expected, sizeof(expected)); +} + +static void palette(void) +{ + const unsigned char pixels[] = {0x40, 0x80}; + encode(1, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, pixels, 1); + + int result = browser_png_load(image, stream); + + const unsigned char expected[] = {0,1,1,0}; + assert(result == 0); + uint32_t colors = MEM32(image + 0x11c); + assert(MEM32(colors) == 0x000a141e && MEM32(colors + 4) == 0x0028323c); + check_pixels(8, expected, sizeof(expected)); +} + +static void grayscale(void) +{ + const unsigned char pixels[] = {0x05, 0xaf}; + encode(4, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, pixels, 1); + + int result = browser_png_load(image, stream); + + const unsigned char expected[] = {0,85,170,255}; + assert(result == 0); + uint32_t colors = MEM32(image + 0x11c); + assert(MEM32(colors + 85 * 4) == 0x00555555); + assert(MEM32(colors + 255 * 4) == 0x00ffffff); + check_pixels(8, expected, sizeof(expected)); +} + +static void invalid_png(void) +{ + memset(encoded, 0, 8); + encoded_size = 8; + read_offset = 0; + MEM32(image + 0x118) = 0xabcdef; + + int result = browser_png_load(image, stream); + + assert(result == -1); + assert(MEM32(image + 0x118) == 0xabcdef); + assert(g_esp == 0x50010000); +} + +int main(void) +{ + assert(guest_map("PNG test stack", 0x50000000, 0x20000)); + g_esp = 0x50010000; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + image = guest_malloc(0x120); + stream = guest_malloc(8); + MEM32(stream) = stream + 4; + MEM32(stream + 4) = 0x12345678; + rgba(); + rgb16(); + palette(); + grayscale(); + invalid_png(); + guest_free(image); + guest_free(stream); + puts("PNG decoding passed"); + return 0; +}