added dynamic loading of the json color file
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 11 Jul 2021 20:54:06 +0000 (15:54 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 11 Jul 2021 20:54:06 +0000 (15:54 -0500)
site/js/js_events.js
src/main.onyx
src/test_console.onyx

index 4ff47d315c9fc0f38cb45a305b9a21848ea5a797..546bec2225c05e1ffcacf2b1c86ddeb8605cb9b2 100644 (file)
@@ -19,6 +19,8 @@ function push_event_to_buffer(esp, event_size, event_kind, data) {
     }
 }
 
+var requested_file_data = {};
+
 window.ONYX_MODULES.push({
     module_name: "js_events",
 
@@ -109,4 +111,41 @@ window.ONYX_MODULES.push({
             return false;
         };
     },
+
+    request_file(esp, event_size, filename_ptr, filename_len, fileid) {
+        esp /= 4;
+
+        var path_memory = new Uint8Array(ONYX_MEMORY.buffer, filename_ptr, filename_len);
+        var path = new TextDecoder("utf-8").decode(path_memory);
+        console.log(path);
+
+        fetch(path)
+            .then(response => response.arrayBuffer())
+            .then(array_buffer => {
+                requested_file_data[fileid] = array_buffer;
+
+                push_event_to_buffer(esp, event_size, 0x09, [ 0x01, fileid, array_buffer.byteLength ]);
+            })
+            .catch((error) => {
+                push_event_to_buffer(esp, event_size, 0x09, [ 0x02, fileid, 0 ]);
+            });
+    },
+
+    get_requested_file_data(fileid, bufferptr, bufferlen) {
+        var file_data = requested_file_data[fileid];
+        if (file_data == null) return 0;
+
+        if (bufferlen < file_data.byteLength) return 0;
+
+        let WASM_U8 = new Uint8Array(ONYX_MEMORY.buffer);
+        var u8_data = new Uint8Array(file_data);
+
+        WASM_U8.set(u8_data, bufferptr);
+
+        requested_file_data[fileid] = null;
+        delete requested_file_data[fileid];
+
+        return 1;
+    },
 });
+
index f38c138200e6bdc6efb1be483b69e05ad8fa2619..7e3249808e406e3c3a618cb21fa1f5e666a60dbc 100644 (file)
@@ -11,6 +11,8 @@ search_buffer: string.String_Buffer;
 
 background_tile_texture : gfx.Texture;
 
+on_file_load_callbacks : map.Map(u32, (file_event: ^events.Event) -> void);
+
 main :: (args: [] cstr) {
     init();
 
@@ -22,10 +24,14 @@ init :: () {
     gl.init("main_canvas");
     events.init();
     gfx.immediate_renderer_init();
-    load_colors();
 
     ui.init_ui();
 
+    map.init(^on_file_load_callbacks);
+
+    color_file := events.request_file("/res/colors_dark.json");
+    map.put(^on_file_load_callbacks, color_file, load_colors);
+
     search_buffer = string.buffer_make(memory.make_slice(u8, 256));
 
     gl.enable(gl.BLEND);
@@ -45,48 +51,6 @@ init :: () {
 
     test_window.size     = .{ 800, 800 };
     test_window.border_width = 8.0f;
-    test_window.border_color = config.Colors.primary;
-    test_window.background_color = config.Colors.dark_background;
-    test_window.active_color = config.Colors.background;
-
-    load_colors :: () {
-        json :: package json
-
-        @DynamicLoading // Currently, this file is just baked into the binary. When a mechanism
-        // is added to easily ask JS to make a fetch request for a file, that can be used here.
-        colors_json := #file_contents "res/colors_dark.json";
-
-        arena := alloc.arena.make(context.allocator, 4096);
-        defer alloc.arena.free(^arena);
-        colors := json.decode(colors_json, alloc.arena.make_allocator(^arena));
-        defer json.free(colors);
-
-        config.Colors.dark_background = decode_color(colors.root["dark_background"]);
-        config.Colors.background      = decode_color(colors.root["background"]);
-        config.Colors.foreground      = decode_color(colors.root["foreground"]);
-
-        config.Colors.keyword         = decode_color(colors.root["keyword"]);
-        config.Colors.value           = decode_color(colors.root["value"]);
-        config.Colors.jumppoint       = decode_color(colors.root["jumppoint"]);
-
-        config.Colors.primary         = decode_color(colors.root["primary"]);
-        config.Colors.primary_light   = decode_color(colors.root["primary_light"]);
-        config.Colors.primary_dark    = decode_color(colors.root["primary_dark"]);
-        config.Colors.primary_text    = decode_color(colors.root["primary_text"]);
-
-        config.Colors.secondary       = decode_color(colors.root["secondary"]);
-        config.Colors.secondary_light = decode_color(colors.root["secondary_light"]);
-        config.Colors.secondary_dark  = decode_color(colors.root["secondary_dark"]);
-        config.Colors.secondary_text  = decode_color(colors.root["secondary_text"]);
-
-        decode_color :: (v: ^json.Value) -> gfx.Color4 {
-            return .{
-                r = ~~v[0]->as_float(),
-                g = ~~v[1]->as_float(),
-                b = ~~v[2]->as_float(),
-            };
-        }
-    }
 }
 
 
@@ -168,6 +132,35 @@ poll_events :: () -> bool {
                 gl.setSize(event.resize.width, event.resize.height);
                 gfx.set_window_size(event.resize.width, event.resize.height);
             }
+
+            case .FileRequest {
+                @Bug // When debugging I tried to do this
+                //
+                printf("{}\n", event.file);
+                //
+                // And it looked like it was printing correctly. However, it destroyed the data in the event,
+                // which made the below code fail. Without printing this, everything works just fine. This
+                // probably has to do with the "any" type for printf.
+                //
+                // Actually, I tried this:
+                //
+                // println(event.file.file_id);
+                //
+                // And it also breaks it... Hmm
+                //
+                // Another wrinkle: When I copied this case-body to a different function, it worked perfectly.
+                // Definitely something wrong with the stack allocation when in case statements.
+                //
+                // events.consume() was returning a pointer to a local variable on the stack.... UGH
+                // Some kind of shadowing warning would be nice
+
+                if f := map.get(^on_file_load_callbacks, event.file.file_id); f != null_proc {
+                    f(event);
+
+                } else {
+                    printf("Warning: No callback set for file id {}.\n", event.file.file_id);
+                }
+            }
         }
     }
 
@@ -176,6 +169,11 @@ poll_events :: () -> bool {
 
 update :: (dt: f32) {
     t += dt;
+
+    if ui.is_key_just_down(~~#char "L") {
+        color_file := events.request_file("/res/colors_light.json");
+        map.put(^on_file_load_callbacks, color_file, load_colors);
+    }
 }
 
 t := 0.0f;
@@ -263,3 +261,57 @@ draw_background_lines :: (width: f32, height: f32, line_color := gfx.Color4.{0.2
     gfx.set_texture(); 
     gfx.pop_matrix();
 }
+
+
+@Relocate
+#private_file
+load_colors :: (event: ^events.Event) {
+    json :: package json
+
+    assert(event.kind == .FileRequest, "Bad event type");
+    assert(event.file.status == .Success, "Failed to load color file");
+
+    color_data := memory.make_slice(u8, event.file.size);
+    defer if color_data.count > 0 do cfree(color_data.data);
+    events.get_requested_file_data(event.file.file_id, color_data);
+
+    @DynamicLoading // Currently, this file is just baked into the binary. When a mechanism
+    // is added to easily ask JS to make a fetch request for a file, that can be used here.
+    // colors_json := #file_contents "res/colors_light.json";
+
+    arena := alloc.arena.make(context.allocator, 4096);
+    defer alloc.arena.free(^arena);
+    colors := json.decode(color_data, alloc.arena.make_allocator(^arena));
+    defer json.free(colors);
+
+    config.Colors.dark_background = decode_color(colors.root["dark_background"]);
+    config.Colors.background      = decode_color(colors.root["background"]);
+    config.Colors.foreground      = decode_color(colors.root["foreground"]);
+
+    config.Colors.keyword         = decode_color(colors.root["keyword"]);
+    config.Colors.value           = decode_color(colors.root["value"]);
+    config.Colors.jumppoint       = decode_color(colors.root["jumppoint"]);
+
+    config.Colors.primary         = decode_color(colors.root["primary"]);
+    config.Colors.primary_light   = decode_color(colors.root["primary_light"]);
+    config.Colors.primary_dark    = decode_color(colors.root["primary_dark"]);
+    config.Colors.primary_text    = decode_color(colors.root["primary_text"]);
+
+    config.Colors.secondary       = decode_color(colors.root["secondary"]);
+    config.Colors.secondary_light = decode_color(colors.root["secondary_light"]);
+    config.Colors.secondary_dark  = decode_color(colors.root["secondary_dark"]);
+    config.Colors.secondary_text  = decode_color(colors.root["secondary_text"]);
+
+    decode_color :: (v: ^json.Value) -> gfx.Color4 {
+        return .{
+            r = ~~v[0]->as_float(),
+            g = ~~v[1]->as_float(),
+            b = ~~v[2]->as_float(),
+        };
+    }
+
+    @Temporary
+    test_window.border_color = config.Colors.primary;
+    test_window.background_color = config.Colors.dark_background;
+    test_window.active_color = config.Colors.background;
+}
index a3d668189178204ec3e7359b60341a48b0ce0324..b738107bbda6e89cc65d4118bcb989110cba883f 100644 (file)
@@ -8,7 +8,9 @@ use package core
 // Testing running on the console
 main :: (args: [] cstr) {
     
-    wasm_data := #file_contents "data/test.wasm";
+    // wasm_data := #file_contents "data/test.wasm";
+    assert(args.count > 1, "Expected WASM file path as argument");
+    wasm_data := io.get_contents(args[1] |> string.from_cstr());
     
     wasm_binary := wasm.load(wasm_data);
     for ^entry: wasm_binary.sections.entries {
@@ -23,7 +25,7 @@ main :: (args: [] cstr) {
         i += 1;
         printf("Function {}\n", i);
         for instr: wasm.instruction_iterator(^wasm_binary, code) {
-            printf("{}\n", instr);
+            printf("{p}\n", instr);
         }
         print("\n");
     }