From 9b99dee07fcc2a85b76046fd34de13268009c7e5 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 11 Jul 2021 15:54:06 -0500 Subject: [PATCH] added dynamic loading of the json color file --- site/js/js_events.js | 39 ++++++++++++ src/main.onyx | 138 +++++++++++++++++++++++++++++------------- src/test_console.onyx | 6 +- 3 files changed, 138 insertions(+), 45 deletions(-) diff --git a/site/js/js_events.js b/site/js/js_events.js index 4ff47d3..546bec2 100644 --- a/site/js/js_events.js +++ b/site/js/js_events.js @@ -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; + }, }); + diff --git a/src/main.onyx b/src/main.onyx index f38c138..7e32498 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -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; +} diff --git a/src/test_console.onyx b/src/test_console.onyx index a3d6681..b738107 100644 --- a/src/test_console.onyx +++ b/src/test_console.onyx @@ -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"); } -- 2.25.1