abstracted the feature system
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Sep 2021 22:59:54 +0000 (17:59 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Sep 2021 22:59:54 +0000 (17:59 -0500)
src/app/app.onyx
src/app/colors.onyx
src/app/window_switcher.onyx

index 2434d89b9a7fc6807ab1994b4840a0cc10315d3f..ff771aa2bfee6d262845ee6c6e3c87db5d0594cc 100644 (file)
@@ -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;
+            }
+        }
+    }
+}
index 3e7d2e1a4e6f4bb798b98f478b795e9ad7cc8d7c..a0cb148de10970e933d5710c848947e1d3693016 100644 (file)
@@ -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;
index a50ce0d3287f5c068775c4e417438be31a494e10..06a50d8eb182c9f64038a60b2623c60c2c22d71b 100644 (file)
@@ -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