From: Brendan Hansen Date: Tue, 21 Sep 2021 22:59:54 +0000 (-0500) Subject: abstracted the feature system X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=e4c6a52f2d380019e903e72662755516bd33818e;p=onyx-wasm-analyzer.git abstracted the feature system --- diff --git a/src/app/app.onyx b/src/app/app.onyx index 2434d89..ff771aa 100644 --- a/src/app/app.onyx +++ b/src/app/app.onyx @@ -110,33 +110,7 @@ init :: () { } }; - // Dynamically load things in the binary - { - use type_info; - - // Look through all the types in the program - for type: type_table { - if type.kind != .Struct do continue; - - ts := cast(^Type_Info_Struct) type; - if !string.starts_with(ts.name, "Feature_") do continue; - - // Any types that are a structure and start with "Feature_" will be dynamically loaded - - debug_log(.Info, "Found feature '{}'", string.advance(ts.name, 8)); - - for ^member: ts.members { - Hook_Function_Type :: #type () -> void; - - if member.name == "setup" { - assert(member.type == Hook_Function_Type, "setup has the wrong type."); - assert(member.default != null, "setup has no default function."); - - (*(cast(^Hook_Function_Type) member.default))(); - } - } - } - } + invoke_feature_function("setup"); load_background_tile_texture :: () { background_tile_texture = gfx.load_texture(32, 32, #file_contents "res/images/background_tile.data", gl.RGB, gl.RGB); @@ -451,3 +425,39 @@ open_tool_opener :: macro () { gfx.set_texture(); } } + +@Relocate // This should be moved to somewhere else when the code base. +invoke_feature_function :: macro (name: str, Hook_Type := #type () -> void, call := #code f()) { + use type_info; + + #persist features : [..] ^type_info.Type_Info_Struct; + if features.data == null { + debug_log(.Info, "Getting list of loaded features."); + + array.init(^features); + + for type: type_table { + if type.kind != .Struct do continue; + + feature := cast(^Type_Info_Struct) type; + if !string.starts_with(feature.name, "Feature_") do continue; + + features << feature; + } + } + + for feature: features { + for ^member: feature.members { + if member.name == name { + #if config.DEBUG { + buffer: [256] u8; + assert(member.type == Hook_Type, conv.str_format(buffer, "'{}' has the wrong type.", name)); + assert(member.default != null, conv.str_format(buffer, "'{}' has no default function.", name)); + } + + f := *(cast(^Hook_Type) member.default); + #insert call; + } + } + } +} diff --git a/src/app/colors.onyx b/src/app/colors.onyx index 3e7d2e1..a0cb148 100644 --- a/src/app/colors.onyx +++ b/src/app/colors.onyx @@ -10,10 +10,10 @@ use package core config :: package config wasm :: package wasm_utils debug :: package debug -} -use package debug { init as debug_init, debug_log, draw_debug_log } -use package core.intrinsics.onyx { __initialize } + use package debug { init as debug_init, debug_log, draw_debug_log } + use package core.intrinsics.onyx { __initialize } +} Colorscheme :: enum { Undefined; diff --git a/src/app/window_switcher.onyx b/src/app/window_switcher.onyx index a50ce0d..06a50d8 100644 --- a/src/app/window_switcher.onyx +++ b/src/app/window_switcher.onyx @@ -1,10 +1,10 @@ package app #private_file { - ui :: package ui - gfx :: package immediate_mode + ui :: package ui + gfx :: package immediate_mode config :: package config - math :: package core.math + math :: package core.math string :: package core.string memory :: package core.memory