Editor/core: resolve caminhos relativos pela pasta do projeto
Blocos que abrem arquivo (carregue o PNG, Image.Load, FS.*) passam a resolver caminhos relativos contra o diretorio-base do projeto, definido pelo editor ao abrir um .sb. Assim exemplos acham atlas.png etc. mesmo quando o editor roda de outro cwd (ex.: AppImage aberto pelo menu), sem mudar o cwd global (que quebrava "Ver codigo-fonte" e o harness). - tb_path_set_base / tb_path_resolve, na API publica (src/tinyblocks.c) - editor: project_set_dir no project_load (sem chdir) - tb_png, tb_fs e tb_imglib usam o resolvedor - MAX_USER_BLOCKS 16 -> 8192 (documentado em docs/editor.md) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e165e07e02
commit
85bd6f7cf4
9 files changed
+70
-5
No files matched your search
@@ -282,6 +282,10 @@ cinza. A sequência é desenhada **de baixo para cima**, para o encaixe de cada
|
||||
bloco cobrir o seguinte. Campos com nome de variável (`TB_SLOT_VARIABLE`)
|
||||
ficam na cor de Variáveis.
|
||||
|
||||
Um programa pode definir até `MAX_USER_BLOCKS` blocos próprios (8192, em
|
||||
`tb_editor_internal.h`). Os que passarem do limite não são registrados, então
|
||||
as chamadas ficam cinzas como um tipo desconhecido.
|
||||
|
||||
### Encaixes de bloco (`TB_SLOT_CODE`, `TB_SLOT_STACK`)
|
||||
|
||||
Um campo marcado como encaixe recebe o **código** do bloco encaixado, não
|
||||
|
||||
@@ -1034,6 +1034,13 @@ Fluxo de uso mínimo:
|
||||
| `bool tinyblocks_init (TinyBlocks** out_rt, const TinyBlocks_InitOptions* opts)` | Cria o runtime em `*out_rt`. `opts` NULL = alocador `malloc` e capacidades padrão. |
|
||||
| `void tinyblocks_deinit(TinyBlocks** rt)` | Destrói tudo em três fases (instâncias, libraries, órfãos), fecha os `dlopen` e zera `*rt`. |
|
||||
|
||||
### Caminhos de arquivo
|
||||
|
||||
| Declaração | Descrição |
|
||||
|---|---|
|
||||
| `void tb_path_set_base(const char* dir)` | O aplicativo (ex.: o editor) define o diretorio-base do projeto aberto; os blocos que abrem arquivos resolvem caminhos relativos contra ele, para que um caminho como "atlas.png" ache o arquivo na pasta do projeto mesmo quando o processo roda de outro diretorio (ex.: AppImage aberto pelo menu). |
|
||||
| `const char* tb_path_resolve(const char* path, char* out, size_t out_n)` | |
|
||||
|
||||
### Registro interno (usado pelas implementações de bloco)
|
||||
|
||||
| Declaração | Descrição |
|
||||
@@ -1735,7 +1742,7 @@ vem juntas com o editor e podem compartilhar este cabecalho.
|
||||
| `#define STAGE_BUF` = `16384` | Tamanho do texto da saída (console). |
|
||||
| `#define LIT_TEXT_CAP` = `512` | Texto de um literal. |
|
||||
| `#define MAX_HOT_TYPES` = `64` | Tipos lembrados no hot reload. |
|
||||
| `#define MAX_USER_BLOCKS` = `16` | Blocos do usuário. |
|
||||
| `#define MAX_USER_BLOCKS` = `8192` | Blocos do usuário. |
|
||||
| `#define MAX_UBLK_SLOTS` = `8` | Entradas/saídas de um bloco do usuário. |
|
||||
| `#define MAX_UBLK_PARTS` = `16` | Partes do protótipo. |
|
||||
|
||||
|
||||
@@ -92,6 +92,14 @@ typedef struct TinyBlocks_InitOptions {
|
||||
TB_API bool tinyblocks_init (TinyBlocks** out_rt, const TinyBlocks_InitOptions* opts);
|
||||
TB_API void tinyblocks_deinit(TinyBlocks** rt);
|
||||
|
||||
/* -- Caminhos de arquivo --- */
|
||||
/* O aplicativo (ex.: o editor) define o diretorio-base do projeto aberto;
|
||||
os blocos que abrem arquivos resolvem caminhos relativos contra ele, para
|
||||
que um caminho como "atlas.png" ache o arquivo na pasta do projeto mesmo
|
||||
quando o processo roda de outro diretorio (ex.: AppImage aberto pelo menu). */
|
||||
TB_API void tb_path_set_base(const char* dir);
|
||||
TB_API const char* tb_path_resolve(const char* path, char* out, size_t out_n);
|
||||
|
||||
/* -- Registro interno (usado pelas implementações de bloco) --- */
|
||||
TB_API bool tb_rt_register_block (TinyBlocks* rt, TinyBlocks_Block* block);
|
||||
TB_API void tb_rt_unregister_block(TinyBlocks* rt, TinyBlocks_Block* block);
|
||||
|
||||
@@ -203,7 +203,7 @@ enum { CV_CODE, CV_STAGE };
|
||||
#define STAGE_BUF 16384
|
||||
#define LIT_TEXT_CAP 512
|
||||
#define MAX_HOT_TYPES 64
|
||||
#define MAX_USER_BLOCKS 16
|
||||
#define MAX_USER_BLOCKS 8192
|
||||
#define MAX_UBLK_SLOTS 8
|
||||
#define MAX_UBLK_PARTS 16
|
||||
|
||||
|
||||
@@ -3639,7 +3639,21 @@ static bool has_ext(const char* path, const char* ext) {
|
||||
return n > e && !strcasecmp(path + n - e, ext);
|
||||
}
|
||||
|
||||
/* Define o diretorio-base do projeto aberto, para que caminhos relativos
|
||||
(imagens, FS.ReadText...) achem os arquivos do exemplo mesmo quando o
|
||||
editor roda de outro cwd (ex.: AppImage aberto pelo menu). Nao muda o cwd. */
|
||||
static void project_set_dir(const char* path) {
|
||||
char buf[1024];
|
||||
if (!path || !path[0] || strlen(path) >= sizeof(buf)) return;
|
||||
strcpy(buf, path);
|
||||
char* slash = strrchr(buf, '/');
|
||||
if (!slash) return;
|
||||
*slash = '\0';
|
||||
if (buf[0]) tb_path_set_base(buf);
|
||||
}
|
||||
|
||||
bool project_load(Ed* ed, const char* path) {
|
||||
project_set_dir(path);
|
||||
if (has_ext(path, ".txt") || has_ext(path, ".sb") || has_ext(path, ".scratchblocks"))
|
||||
return sb_import_file(ed, path); /* notacao de texto do forum do Scratch */
|
||||
char err[160] = "";
|
||||
|
||||
@@ -45,10 +45,11 @@ static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
|
||||
static void eval_open(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
const char* path = tb_block_input_text(self, rt, 0);
|
||||
char rpath[1100];
|
||||
int slot = fs_slot();
|
||||
int64_t handle = 0;
|
||||
if (slot >= 0 && path[0]) {
|
||||
FILE* f = fopen(path, "rb");
|
||||
FILE* f = fopen(tb_path_resolve(path, rpath, sizeof(rpath)), "rb");
|
||||
if (f) { s_files[slot] = f; handle = slot + 1; }
|
||||
}
|
||||
tb_block_set_output(self, 0, tb_value_integer(handle));
|
||||
@@ -80,9 +81,10 @@ static void eval_read(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
|
||||
static void eval_readtext(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
const char* path = tb_block_input_text(self, rt, 0);
|
||||
char rpath[1100];
|
||||
char* buf = NULL;
|
||||
if (path[0]) {
|
||||
FILE* f = fopen(path, "rb");
|
||||
FILE* f = fopen(tb_path_resolve(path, rpath, sizeof(rpath)), "rb");
|
||||
if (f) {
|
||||
fseek(f, 0, SEEK_END);
|
||||
long end = ftell(f);
|
||||
@@ -109,8 +111,9 @@ static void eval_write(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
static void eval_append(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
const char* path = tb_block_input_text(self, rt, 0);
|
||||
const char* text = tb_block_input_text(self, rt, 1);
|
||||
char rpath[1100];
|
||||
if (path[0]) {
|
||||
FILE* f = fopen(path, "ab");
|
||||
FILE* f = fopen(tb_path_resolve(path, rpath, sizeof(rpath)), "ab");
|
||||
if (f) { fwrite(text, 1, strlen(text), f); fclose(f); }
|
||||
}
|
||||
self->dirty = false;
|
||||
|
||||
@@ -141,6 +141,8 @@ static void eval_scale(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
}
|
||||
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
const char* path = tb_block_input_text(self, rt, 0);
|
||||
char rpath[1100];
|
||||
path = tb_path_resolve(path, rpath, sizeof(rpath));
|
||||
TinyBlocks_Image* img = tb_image_load(path);
|
||||
if (!img) { tinyblocks_set_error(rt, "nao carregou '%s' (formato?)", path); self->dirty = false; return; }
|
||||
/* A imagem carregada ganha nome (nome do arquivo) e numero no armazem,
|
||||
|
||||
@@ -21,6 +21,9 @@ const char* tb_png_last_error(void) { return s_err; }
|
||||
TinyBlocks_Image* tb_png_load(const char* path) {
|
||||
s_err[0] = 0;
|
||||
if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return NULL; }
|
||||
char rpath[1100];
|
||||
tb_path_resolve(path, rpath, sizeof(rpath));
|
||||
path = rpath;
|
||||
png_image pv;
|
||||
memset(&pv, 0, sizeof(pv));
|
||||
pv.version = PNG_IMAGE_VERSION;
|
||||
@@ -48,6 +51,8 @@ TinyBlocks_Image* tb_png_load(const char* path) {
|
||||
bool tb_png_save(const TinyBlocks_Image* img, const char* path) {
|
||||
s_err[0] = 0;
|
||||
if (!img || !path) { snprintf(s_err, sizeof(s_err), "imagem/caminho nulo"); return false; }
|
||||
char rpath[1100];
|
||||
path = tb_path_resolve(path, rpath, sizeof(rpath));
|
||||
int w = tb_image_width(img), h = tb_image_height(img);
|
||||
if (w <= 0 || h <= 0) { snprintf(s_err, sizeof(s_err), "imagem vazia"); return false; }
|
||||
size_t sz = (size_t)w * h * 4;
|
||||
|
||||
@@ -109,6 +109,28 @@ void tinyblocks_deinit(TinyBlocks** rt_ptr)
|
||||
*rt_ptr = NULL;
|
||||
}
|
||||
|
||||
/* -- Caminhos de arquivo --- */
|
||||
static char s_path_base[1024] = "";
|
||||
|
||||
void tb_path_set_base(const char* dir)
|
||||
{
|
||||
if (!dir) { s_path_base[0] = 0; return; }
|
||||
snprintf(s_path_base, sizeof(s_path_base), "%s", dir);
|
||||
}
|
||||
|
||||
const char* tb_path_resolve(const char* path, char* out, size_t out_n)
|
||||
{
|
||||
if (!path) path = "";
|
||||
/* absoluto, sem base, ou pedindo só o ponteiro: devolve como veio */
|
||||
if (path[0] == '/' || !s_path_base[0]) {
|
||||
if (out && out_n) snprintf(out, out_n, "%s", path);
|
||||
return out ? out : path;
|
||||
}
|
||||
if (!out || out_n == 0) return path;
|
||||
snprintf(out, out_n, "%s/%s", s_path_base, path);
|
||||
return out;
|
||||
}
|
||||
|
||||
/* -- Alocação --- */
|
||||
void* tb_alloc(TinyBlocks* rt, size_t size)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user