client/runtime/browser_host.c (9090B)
1 #include <emscripten/wasmfs.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 #include <fcntl.h> 7 #include <assert.h> 8 #include <errno.h> 9 #include <pthread.h> 10 #include <stdint.h> 11 #include <emscripten/proxying.h> 12 #include <emscripten/html5.h> 13 #include <emscripten/threading.h> 14 15 char *ao_asset_base_url(void); 16 void ao_asset_manifest_js(em_proxying_ctx *context, void *request); 17 char *ao_asset_manifest_parse(const char *json); 18 char *browser_preferences_directory(const char *base); 19 void browser_storage_available(em_proxying_ctx *context, void *available); 20 21 static char *preferences_directory; 22 static unsigned preferences_character = UINT32_MAX; 23 24 void browser_select_character(unsigned character_id) 25 { 26 if (character_id == preferences_character) return; 27 28 /* Mount the new character before native preferences load on world entry. */ 29 char persistent_path[4096]; 30 snprintf(persistent_path, sizeof(persistent_path), "%s/%u", preferences_directory, character_id); 31 assert(mkdir(persistent_path, 0755) == 0 || errno == EEXIST); 32 char character_path[128]; 33 snprintf(character_path, sizeof(character_path), "Prefs/localtest/Char%u", character_id); 34 assert(symlink(persistent_path, character_path) == 0); 35 36 /* Seed first-run settings once; native writers create the other files. */ 37 strcat(character_path, "/Char.cfg"); 38 int preferences_file = open(character_path, O_CREAT | O_EXCL | O_WRONLY, 0600); 39 if (preferences_file >= 0) { 40 const char first_run[] = "IsFirstTime 1\nIsTipEnabled 0\n"; 41 assert(write(preferences_file, first_run, sizeof(first_run) - 1) == sizeof(first_run) - 1); 42 assert(close(preferences_file) == 0); 43 } else assert(errno == EEXIST); 44 preferences_character = character_id; 45 } 46 47 static char *ao_asset_manifest(const char *base) 48 { 49 /* Fetch and retry on the page thread while this worker waits without JSPI. */ 50 struct { const char *base; char *listing; } request = {base, NULL}; 51 em_proxying_queue *queue = em_proxying_queue_create(); 52 assert(queue); 53 assert(emscripten_proxy_sync_with_ctx(queue, emscripten_main_runtime_thread_id(), 54 ao_asset_manifest_js, &request)); 55 em_proxying_queue_destroy(queue); 56 assert(request.listing); 57 char *listing = ao_asset_manifest_parse(request.listing); 58 free(request.listing); 59 return listing; 60 } 61 62 static em_proxying_queue *resource_queue; 63 static pthread_t resource_thread; 64 void browser_prefetch_resource_js(uint32_t kind, uint32_t identity); 65 66 void browser_prefetch_resource_ready(void) 67 { 68 resource_queue = em_proxying_queue_create(); 69 assert(resource_queue); 70 resource_thread = pthread_self(); 71 } 72 73 static void prefetch_resource(void *argument) 74 { 75 uint32_t *identity = argument; 76 browser_prefetch_resource_js(identity[0], identity[1]); 77 free(identity); 78 } 79 80 void browser_prefetch_resource(uint32_t kind, uint32_t identity) 81 { 82 /* Only bytes are prefetched; guest object construction keeps its ordering. */ 83 uint32_t *request = malloc(2 * sizeof(uint32_t)); 84 assert(request); 85 request[0] = kind; 86 request[1] = identity; 87 if (!emscripten_proxy_async(resource_queue, resource_thread, prefetch_resource, request)) 88 free(request); 89 } 90 91 char *browser_startup_assets(void); 92 void browser_prefetch_startup_js(const char *configuration, uint32_t inode); 93 struct startup_prefetch { char *configuration; uint32_t inode; }; 94 95 static void prefetch_startup(void *argument) 96 { 97 /* The fetch worker can read storage while guest initialization continues. */ 98 struct startup_prefetch *request = argument; 99 browser_prefetch_startup_js(request->configuration, request->inode); 100 free(request->configuration); 101 free(argument); 102 } 103 104 void browser_initialize(unsigned character_id) 105 { 106 /* FetchFS performs range requests on its own worker while game threads wait. */ 107 char *base = ao_asset_base_url(); 108 backend_t remote = wasmfs_create_fetch_backend(base, 1024 * 1024); 109 char *manifest = ao_asset_manifest(base); 110 assert(manifest); 111 assert(wasmfs_create_directory("/remote", 0755, remote) == 0); 112 assert(mkdir("/game", 0755) == 0); 113 assert(chdir("/game") == 0); 114 assert(mkdir("cd_image", 0755) == 0); 115 assert(mkdir("/proc", 0755) == 0); 116 assert(mkdir("/proc/self", 0755) == 0); 117 assert(symlink("/game/game.wasm", "/proc/self/exe") == 0); 118 char *copy_manifest = strdup(manifest); 119 assert(copy_manifest); 120 for (char *name = strtok(manifest, "\n"); name; name = strtok(NULL, "\n")) { 121 char path[4096]; 122 snprintf(path, sizeof(path), "/remote/%s", name); 123 size_t length = strlen(name); 124 int directory = name[length - 1] == '/'; 125 if (directory) { 126 path[strlen(path) - 1] = 0; 127 assert(mkdir(path, 0755) == 0); 128 } else { 129 int file = open(path, O_CREAT | O_RDONLY, 0644); 130 assert(file >= 0); 131 close(file); 132 } 133 /* Preferences and occlusion caches are private, writable browser data. */ 134 if (!strncmp(name, "Prefs/", 6) || !strncmp(name, "cd_image/OCC/", 13)) { 135 if (directory) { 136 name[length - 1] = 0; 137 assert(mkdir(name, 0755) == 0); 138 } 139 } else if (!strncmp(name, "cd_image/", 9) && name[9]) { 140 /* Link the other immediate children to their HTTP-backed asset tree. */ 141 const char *slash = strchr(name + 9, '/'); 142 if (!slash || slash == name + length - 1) { 143 if (directory) name[length - 1] = 0; 144 assert(symlink(path, name) == 0); 145 } 146 } 147 } 148 free(manifest); 149 150 char *startup = browser_startup_assets(); 151 if (startup) { 152 struct stat info; 153 assert(stat("/remote/cd_image/data/db/ResourceDatabase.dat", &info) == 0); 154 assert(info.st_ino <= UINT32_MAX); 155 struct startup_prefetch *request = malloc(sizeof(*request)); 156 assert(request); 157 *request = (struct startup_prefetch){startup, info.st_ino}; 158 assert(emscripten_proxy_async(resource_queue, resource_thread, prefetch_startup, request)); 159 } 160 161 /* Register all remote files first so startup reads can prefetch their siblings. */ 162 for (char *name = strtok(copy_manifest, "\n"); name; name = strtok(NULL, "\n")) { 163 if (name[strlen(name) - 1] == '/' || 164 (strncmp(name, "Prefs/", 6) && strncmp(name, "cd_image/OCC/", 13))) continue; 165 char path[4096]; 166 snprintf(path, sizeof(path), "/remote/%s", name); 167 FILE *source = fopen(path, "rb"), *destination = fopen(name, "wb"); 168 assert(source && destination); 169 unsigned char buffer[8192]; 170 size_t size; 171 while ((size = fread(buffer, 1, sizeof(buffer), source))) 172 assert(fwrite(buffer, 1, size, destination) == size); 173 assert(!ferror(source)); 174 fclose(source); 175 fclose(destination); 176 } 177 free(copy_manifest); 178 /* Private browsing can deny OPFS; native preferences still need writable files. */ 179 int storage_available = -1; 180 em_proxying_queue *queue = em_proxying_queue_create(); 181 assert(queue); 182 assert(emscripten_proxy_sync_with_ctx(queue, emscripten_main_runtime_thread_id(), 183 browser_storage_available, &storage_available)); 184 em_proxying_queue_destroy(queue); 185 assert(storage_available >= 0); 186 backend_t persistent = storage_available ? wasmfs_create_opfs_backend() : wasmfs_create_memory_backend(); 187 assert(wasmfs_create_directory("/persistent", 0755, persistent) == 0); 188 preferences_directory = browser_preferences_directory(base); 189 free(base); 190 assert(mkdir(preferences_directory, 0755) == 0 || errno == EEXIST); 191 browser_select_character(character_id); 192 193 /* Start the guest with the viewport size, including nonstandard aspect ratios. */ 194 int width, height; 195 assert(emscripten_get_canvas_element_size("#canvas", &width, &height) == EMSCRIPTEN_RESULT_SUCCESS); 196 FILE *preferences = fopen("Prefs/Main.cfg", "r"); 197 FILE *resized = fopen("Prefs/Main.cfg.new", "w"); 198 assert(preferences && resized); 199 char line[4096]; 200 while (fgets(line, sizeof(line), preferences)) { 201 if (!strncmp(line, "DisplayWidth ", 13)) fprintf(resized, "DisplayWidth %d\n", width); 202 else if (!strncmp(line, "DisplayHeight ", 14)) fprintf(resized, "DisplayHeight %d\n", height); 203 else fputs(line, resized); 204 } 205 assert(!ferror(preferences)); 206 fclose(preferences); 207 assert(fclose(resized) == 0); 208 assert(rename("Prefs/Main.cfg.new", "Prefs/Main.cfg") == 0); 209 assert(symlink("/remote/Setupf", "Setupf") == 0); 210 assert(symlink("/remote/fonts", "fonts") == 0); 211 assert(symlink("/remote/version.id", "version.id") == 0); 212 213 /* The native creator loads its camera paths from the game directory. */ 214 assert(symlink("/remote/CharCreateCamera.dat", "CharCreateCamera.dat") == 0); 215 setenv("ANARCHY_CD", "cd_image", 1); 216 setenv("ANARCHY_SETUP", "Setupf", 1); 217 setenv("ANARCHY", ".", 1); 218 }