load all plugins from relative directory (need to not hardcode it)

plugins
Patrick Cleavelin 2024-01-05 14:58:25 -06:00
parent 8891213a0a
commit d29b032251
3 changed files with 26 additions and 13 deletions

View File

@ -14,7 +14,7 @@ BufferIndex :: p.BufferIndex;
@export
OnInitialize :: proc "c" (plugin: Plugin) {
context = runtime.default_context();
fmt.println("Hello from the Odin Highlighter Plugin!");
fmt.println("builtin highlighter plugin initialized!");
plugin.register_highlighter(plugin.state, ".odin", color_buffer_odin);
plugin.register_highlighter(plugin.state, ".rs", color_buffer_rust);

View File

@ -186,14 +186,25 @@ register_default_input_actions :: proc(input_map: ^core.InputMap) {
register_default_go_actions(&(&input_map.key_actions[.G]).action.(core.InputMap));
}
load_plugins :: proc(state: ^State) -> core.Error {
if loaded_plugin, succ := plugin.try_load_plugin("bin/highlighter.dylib"); succ {
load_plugin :: proc(info: os.File_Info, in_err: os.Errno, state: rawptr) -> (err: os.Errno, skip_dir: bool) {
state := cast(^State)state;
relative_file_path, rel_error := filepath.rel(state.directory, info.fullpath);
extension := filepath.ext(info.fullpath);
if extension == ".dylib" || extension == ".dll" || extension == ".so" {
if loaded_plugin, succ := plugin.try_load_plugin(info.fullpath); succ {
append(&state.plugins, loaded_plugin);
fmt.println("Loaded Odin Highlighter plugin");
return core.no_error();
if rel_error == .None {
fmt.println("Loaded", relative_file_path);
} else {
fmt.println("Loaded", info.fullpath);
}
}
}
return core.make_error(.PluginLoadError, fmt.aprintf("failed to load Odin Highligher plugin"));
return in_err, skip_dir;
}
main :: proc() {
@ -496,9 +507,7 @@ main :: proc() {
}
// Load plugins
if err := load_plugins(&state); err.type != .None {
fmt.println(err.msg);
}
filepath.walk(filepath.join({ state.directory, "bin" }), load_plugin, transmute(rawptr)&state);
for plugin in state.plugins {
if plugin.on_initialize != nil {

View File

@ -76,7 +76,6 @@ load_proc_address :: proc(lib_path: string, library: dynlib.Library, symbol: str
where intrinsics.type_is_proc(ProcType)
{
if address, found := dynlib.symbol_address(library, symbol); found {
fmt.println("The symbol", symbol, "was found at the address", address);
return transmute(ProcType)address;
} else {
fmt.println("Could not find symbol", symbol, "in library", lib_path);
@ -91,9 +90,14 @@ try_load_plugin :: proc(lib_path: string) -> (plugin: Interface, success: bool)
return {}, false;
}
return Interface {
interface := Interface {
on_initialize = load_proc_address(lib_path, library, "OnInitialize", OnInitializeProc),
on_exit = load_proc_address(lib_path, library, "OnExit", OnExitProc),
on_draw = load_proc_address(lib_path, library, "OnDraw", OnDrawProc),
}, true;
};
if interface.on_initialize == nil do return interface, false;
if interface.on_exit == nil do return interface, false;
return interface, true
}