Adiciona tinyblocks-api, tinyblocks-libman e a tela "Escolha uma Extensão"
tinyblocks-api (gestor publico de modulos/libraries): - registry de modulos (embutidos + .so), load/unload/reload via dlopen, hot reload por mtime e introspeccao de tipos de bloco. - substitui a logica de hotlib duplicada no editor. tinyblocks-libman (gestor/buscador de libraries): - varre library.toml (libraries/ e ~/.tinyblocks/libraries), le metadados de forma RECURSIVA e resolve o caminho do .so/.a. - busca por texto e filtro por categoria; consulta o estado de carga na api. Editor: tela cheia "Escolha uma Extensão" (UI desenhada pelo editor, como no Scratch): botao Bibliotecas / Ctrl+L, busca, chips de categoria que quebram linha, grade de cartoes com faixa colorida e selo "carregada"; clicar carrega o .so via api. Respeito a interfaces publicas entre libraries: - libman deixa de incluir o ast.h (interno) do tinyblocks-toml e passa a usar uma API publica e opaca: tb_toml_open/close/str/int/int_array. - core ganha accessors publicos: tb_rt_type_count/at, tb_rt_library_count/at, tb_library_name/category/block_count; api e editor usam-nos em vez de ler campos de struct do core diretamente. Testes: make test-libman (novo) + step no harness que abre o libman, busca, carrega um .so e confirma a categoria na paleta. test-editor/toml/blueprint/ plugin seguem OK.
This commit is contained in:
1 parent
f616503caa
commit
dcc20985c5
20 files changed
+1143
-112
No files matched your search
@@ -0,0 +1,29 @@
|
||||
# Makefile da library api (gestor publico de modulos/libraries)
|
||||
# Constroi build/libtinyblocks-api.a e build/libtinyblocks-api.so
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
|
||||
|
||||
OBJ := $(BUILD)/tb_api.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-api.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-api.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(OBJ): src/tb_api.c include/tb_api.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJ)
|
||||
$(CC) -shared -o $@ $^ -ldl
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* TinyBlocks - tb_api.h
|
||||
* Library tinyblocks-api: gestor publico de modulos/libraries e "cola" entre
|
||||
* plugins (.so), libraries registradas e o registro de tipos do runtime.
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - carregar/descarregar/recarregar plugins .so (dlopen + tb_library_register)
|
||||
* - adotar libraries embutidas (registradas estaticamente no runtime)
|
||||
* - hot reload por mtime
|
||||
* - introspeccao: enumerar modulos e tipos de bloco disponiveis
|
||||
*
|
||||
* NAO desenha UI: quem desenha e o editor.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_API_H
|
||||
#define TINYBLOCKS_API_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TB_API_MAX_MODULES 64
|
||||
#define TB_API_PATH_LEN 512
|
||||
|
||||
/* Um modulo/library conhecido pelo processo (embutido ou .so dinamico). */
|
||||
typedef struct TbApiModule {
|
||||
char name[64]; /* nome da library (desc->name) */
|
||||
char version[32];
|
||||
char path[TB_API_PATH_LEN]; /* .so de origem; "" se embutido */
|
||||
void* handle; /* dlopen; NULL se embutido */
|
||||
TinyBlocks_LibraryBlock* lib;
|
||||
const TinyBlocks_Category* category; /* pode ser NULL */
|
||||
unsigned block_count;
|
||||
time_t mtime; /* mtime do .so (hot reload) */
|
||||
} TbApiModule;
|
||||
|
||||
/* -- Ciclo de vida --- */
|
||||
TB_API bool tb_api_init(TinyBlocks* rt);
|
||||
TB_API void tb_api_shutdown(void);
|
||||
TB_API TinyBlocks* tb_api_runtime(void);
|
||||
TB_API const char* tb_api_last_error(void);
|
||||
|
||||
/* -- Registro de modulos --- */
|
||||
/* Adota as libraries ja registradas no runtime (embutidas). Retorna o total. */
|
||||
TB_API int tb_api_adopt_all(void);
|
||||
TB_API int tb_api_module_count(void);
|
||||
TB_API const TbApiModule* tb_api_module(int i);
|
||||
TB_API const TbApiModule* tb_api_module_find(const char* name);
|
||||
|
||||
/* -- Carregamento dinamico --- */
|
||||
/* Carrega um .so; retorna o indice (>=0) ou -1 (erro em tb_api_last_error). */
|
||||
TB_API int tb_api_load(const char* path);
|
||||
TB_API bool tb_api_unload(int idx);
|
||||
TB_API bool tb_api_reload(int idx);
|
||||
TB_API bool tb_api_is_loaded(const char* name);
|
||||
|
||||
/* Hot reload: true se algum .so mudou no disco desde o ultimo load. */
|
||||
TB_API bool tb_api_any_changed(void);
|
||||
/* Recarrega os .so que mudaram; retorna quantos. */
|
||||
TB_API int tb_api_poll_reload(void);
|
||||
|
||||
/* -- Introspeccao de tipos de bloco (API publica) --- */
|
||||
TB_API int tb_api_type_count(void);
|
||||
TB_API const TinyBlocks_TypeBlock* tb_api_type(int i);
|
||||
TB_API const TinyBlocks_TypeBlock* tb_api_type_find(const char* name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* TINYBLOCKS_API_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library api (gestor publico de modulos/libraries).
|
||||
[library]
|
||||
name = "tinyblocks-api"
|
||||
version = "1.0.0"
|
||||
category = "API"
|
||||
description = "Gestor publico de modulos, plugins e tipos de bloco"
|
||||
color = [96, 125, 139, 255]
|
||||
dark = [69, 90, 100, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_api.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-api.so"
|
||||
static = "build/libtinyblocks-api.a"
|
||||
depends = []
|
||||
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* TinyBlocks - tb_api.c
|
||||
* Implementacao do gestor publico de modulos/libraries (tinyblocks-api).
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef __linux__
|
||||
# include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_api.h"
|
||||
|
||||
typedef const TinyBlocks_LibraryDescriptor* (*GetLibFn)(void);
|
||||
|
||||
static TinyBlocks* s_rt = NULL;
|
||||
static TbApiModule s_mods[TB_API_MAX_MODULES];
|
||||
static int s_n = 0;
|
||||
static char s_err[256];
|
||||
|
||||
/* -- Helpers --- */
|
||||
static time_t file_mtime(const char* p) {
|
||||
struct stat st;
|
||||
return (stat(p, &st) == 0) ? st.st_mtime : 0;
|
||||
}
|
||||
|
||||
static void set_err(const char* fmt, const char* a) {
|
||||
snprintf(s_err, sizeof(s_err), fmt, a ? a : "");
|
||||
}
|
||||
|
||||
static int find_by_lib(const TinyBlocks_LibraryBlock* lib) {
|
||||
for (int i = 0; i < s_n; i++) if (s_mods[i].lib == lib) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int find_by_name(const char* name) {
|
||||
if (!name) return -1;
|
||||
for (int i = 0; i < s_n; i++) if (strcmp(s_mods[i].name, name) == 0) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -- Ciclo de vida --- */
|
||||
bool tb_api_init(TinyBlocks* rt) {
|
||||
s_rt = rt;
|
||||
s_n = 0;
|
||||
s_err[0] = 0;
|
||||
return rt != NULL;
|
||||
}
|
||||
|
||||
void tb_api_shutdown(void) {
|
||||
/* O runtime e o dono dos blocos; aqui so esquecemos os modulos. */
|
||||
s_n = 0;
|
||||
s_rt = NULL;
|
||||
}
|
||||
|
||||
TinyBlocks* tb_api_runtime(void) { return s_rt; }
|
||||
const char* tb_api_last_error(void) { return s_err; }
|
||||
|
||||
/* -- Registro de modulos --- */
|
||||
int tb_api_adopt_all(void) {
|
||||
if (!s_rt) return 0;
|
||||
for (uint32_t i = 0; i < tb_rt_library_count(s_rt); i++) {
|
||||
TinyBlocks_LibraryBlock* lib = tb_rt_library_at(s_rt, i);
|
||||
if (!lib || find_by_lib(lib) >= 0) continue;
|
||||
if (s_n >= TB_API_MAX_MODULES) break;
|
||||
TbApiModule* m = &s_mods[s_n];
|
||||
memset(m, 0, sizeof(*m));
|
||||
const char* nm = tb_library_name(lib);
|
||||
snprintf(m->name, sizeof(m->name), "%s", nm ? nm : "?");
|
||||
m->lib = lib;
|
||||
m->category = tb_library_category(lib);
|
||||
m->block_count = tb_library_block_count(lib);
|
||||
s_n++;
|
||||
}
|
||||
return s_n;
|
||||
}
|
||||
|
||||
int tb_api_module_count(void) { return s_n; }
|
||||
|
||||
const TbApiModule* tb_api_module(int i) {
|
||||
return (i >= 0 && i < s_n) ? &s_mods[i] : NULL;
|
||||
}
|
||||
|
||||
const TbApiModule* tb_api_module_find(const char* name) {
|
||||
int i = find_by_name(name);
|
||||
return i >= 0 ? &s_mods[i] : NULL;
|
||||
}
|
||||
|
||||
bool tb_api_is_loaded(const char* name) { return find_by_name(name) >= 0; }
|
||||
|
||||
/* -- Carregamento dinamico --- */
|
||||
int tb_api_load(const char* path) {
|
||||
if (!s_rt) { set_err("%s", "tb_api: runtime nao inicializado"); return -1; }
|
||||
if (!path || !path[0]) { set_err("%s", "tb_api: caminho vazio"); return -1; }
|
||||
if (s_n >= TB_API_MAX_MODULES) { set_err("%s", "tb_api: limite de modulos atingido"); return -1; }
|
||||
|
||||
#ifdef __linux__
|
||||
void* dl = dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
||||
if (!dl) { set_err("dlopen: %s", dlerror()); return -1; }
|
||||
|
||||
GetLibFn get_lib = (GetLibFn)(uintptr_t)dlsym(dl, TINYBLOCKS_LIBRARY_SYMBOL);
|
||||
if (!get_lib) {
|
||||
const char* base = strrchr(path, '/');
|
||||
base = base ? base + 1 : path;
|
||||
if (strncmp(base, "libtinyblocks-", 14) == 0)
|
||||
set_err("'%s' e dependencia = carregue a library (ex: python.so)", base);
|
||||
else
|
||||
set_err("'%s' nao exporta tinyblocks_get_library", base);
|
||||
dlclose(dl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* desc = get_lib();
|
||||
TinyBlocks_LibraryBlock* lib = tb_library_register(s_rt, desc);
|
||||
if (!lib) { set_err("tb_library_register falhou para %s", path); dlclose(dl); return -1; }
|
||||
|
||||
TbApiModule* m = &s_mods[s_n];
|
||||
memset(m, 0, sizeof(*m));
|
||||
snprintf(m->name, sizeof(m->name), "%s", desc->name ? desc->name : "?");
|
||||
snprintf(m->version, sizeof(m->version), "%s", desc->version ? desc->version : "");
|
||||
snprintf(m->path, sizeof(m->path), "%s", path);
|
||||
m->handle = dl;
|
||||
m->lib = lib;
|
||||
m->category = tb_library_category(lib);
|
||||
m->block_count = tb_library_block_count(lib);
|
||||
m->mtime = file_mtime(path);
|
||||
s_err[0] = 0;
|
||||
return s_n++;
|
||||
#else
|
||||
(void)path;
|
||||
set_err("%s", "tb_api: dlopen nao suportado nesta plataforma");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool tb_api_unload(int idx) {
|
||||
if (idx < 0 || idx >= s_n) return false;
|
||||
TbApiModule* m = &s_mods[idx];
|
||||
if (m->lib) tb_library_unload(s_rt, m->lib);
|
||||
#ifdef __linux__
|
||||
if (m->handle) dlclose(m->handle);
|
||||
#endif
|
||||
for (int i = idx; i < s_n - 1; i++) s_mods[i] = s_mods[i + 1];
|
||||
s_n--;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tb_api_reload(int idx) {
|
||||
if (idx < 0 || idx >= s_n) return false;
|
||||
if (!s_mods[idx].path[0]) return false; /* embutida nao recarrega */
|
||||
char path[TB_API_PATH_LEN];
|
||||
snprintf(path, sizeof(path), "%s", s_mods[idx].path);
|
||||
tb_api_unload(idx);
|
||||
return tb_api_load(path) >= 0;
|
||||
}
|
||||
|
||||
bool tb_api_any_changed(void) {
|
||||
for (int i = 0; i < s_n; i++) {
|
||||
TbApiModule* m = &s_mods[i];
|
||||
if (!m->path[0]) continue;
|
||||
time_t mt = file_mtime(m->path);
|
||||
if (mt && mt != m->mtime) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int tb_api_poll_reload(void) {
|
||||
int n = 0;
|
||||
for (int i = 0; i < s_n; i++) {
|
||||
TbApiModule* m = &s_mods[i];
|
||||
if (!m->path[0]) continue;
|
||||
time_t mt = file_mtime(m->path);
|
||||
if (!mt || mt == m->mtime) continue;
|
||||
char path[TB_API_PATH_LEN];
|
||||
snprintf(path, sizeof(path), "%s", m->path);
|
||||
tb_api_unload(i);
|
||||
if (tb_api_load(path) >= 0) n++;
|
||||
i = -1; /* a lista mudou; reinicia a varredura */
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* -- Introspeccao de tipos --- */
|
||||
int tb_api_type_count(void) { return (int)tb_rt_type_count(s_rt); }
|
||||
|
||||
const TinyBlocks_TypeBlock* tb_api_type(int i) {
|
||||
if (i < 0) return NULL;
|
||||
return tb_rt_type_at(s_rt, (uint32_t)i);
|
||||
}
|
||||
|
||||
const TinyBlocks_TypeBlock* tb_api_type_find(const char* name) {
|
||||
return s_rt ? tb_rt_find_type(s_rt, name) : NULL;
|
||||
}
|
||||
Reference in new issue
Block a user